cmake_minimum_required(VERSION 2.6)
if (POLICY CMP0011)
	cmake_policy(SET CMP0011 OLD)
endif()
if (POLICY CMP0017)
	cmake_policy(SET CMP0017 OLD)
endif()
if (POLICY CMP0053)
	cmake_policy(SET CMP0053 OLD)
endif()
if(POLICY CMP0020)
	cmake_policy(SET CMP0020 OLD)
endif()
project(libs)
set(LIBPROJECT 1)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/pip/")
if(MINGW)
	find_package(MinGW REQUIRED)
endif()
if (DEFINED ANDROID_PLATFORM)
	include_directories(${ANDROID_SYSTEM_LIBRARY_PATH}/usr/include)
	#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${ANDROID_NDK}/sysroot/usr/include")
	#message("${ANDROID_SYSTEM_LIBRARY_PATH}/usr/include")
	#message("${ANDROID_NDK}/sysroot/usr/include")
endif()
option(LIB "System install" 1)
option(DEBUG "Build with -g3" 0)
option(QGLVIEW "Build QGLview library and utils" 0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
#set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native")
if (DEBUG)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g3")
endif ()

set(PIP_LIBRARY pip)
set(PIP_FOLDERS "." "core" "containers" "thread" "system" "io" "console" "math" "code" "geo")
set(PIP_INCLUDES)

if (LIB)
	if(WIN32)
		set(CMAKE_INSTALL_PREFIX ${MINGW_DIR})
	else()
		if(APPLE)
			set(CMAKE_INSTALL_PREFIX /usr/local)
		else()
			set(CMAKE_INSTALL_PREFIX /usr)
		endif()
	endif()
	message(STATUS "Install ${PROJECT_NAME} to system \"${CMAKE_INSTALL_PREFIX}\"")
else()
	message(STATUS "Install ${PROJECT_NAME} to local \"bin\"")
endif()

foreach(F ${PIP_FOLDERS})
	list(APPEND PIP_INCLUDES "${CMAKE_CURRENT_SOURCE_DIR}/pip/src_main/${F}")
endforeach(F)
#message(${PIP_INCLUDES})
if(DEFINED ANDROID_PLATFORM)
	set(PIP_CMG "pip_cmg")
else()
	set(PIP_CMG "${CMAKE_CURRENT_BINARY_DIR}/pip/utils/code_model_generator/pip_cmg")
endif()
if(WIN32)
	set(PIP_DLL_DIR "${CMAKE_CURRENT_BINARY_DIR}/pip")
endif()

set(QAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/qad)

include(qad/QtWraps.cmake)
set(QtVersions)
foreach(_v ${_QT_VERSIONS_})
	option(Qt${_v} "Build for Qt${_v}" 1)
	if (Qt${_v})
		#message("Qt${_v}")
		list(APPEND QtVersions Qt${_v})
	endif()
endforeach()

add_subdirectory(pip)
message(STATUS "Building Qt-derived targets for ${QtVersions}")
add_subdirectory(qad)
set(_DIRS piqt piqt_utils qcd_utils)
if (QGLVIEW)
	list(APPEND _DIRS qglview) 
endif()
foreach(_D ${_DIRS})
	list(APPEND QT_MULTILIB_LIST ${D})
endforeach(_D)
list(INSERT _DIRS 0 cd_utils)
foreach(_D ${_DIRS})
	include_directories(${_D})
	add_subdirectory(${_D})
endforeach(_D)

if(WIN32)
	foreach(PIP_LT ${PIP_LIBS_TARGETS})
		qt_install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pip/lib${PIP_LT}.dll" DESTINATION QtBin)
	endforeach()
	set(CMAKES "FindMinGW.cmake")
	install(FILES ${CMAKES} DESTINATION ${CMAKE_ROOT}/Modules)
endif()
