cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0011 NEW) # don`t affect includer policies
cmake_policy(SET CMP0017 NEW) # need include() with .cmake
cmake_policy(SET CMP0020 NEW) # Automatically link Qt executables to qtmain target on Windows
if (POLICY CMP0053)
	cmake_policy(SET CMP0053 NEW) # simpler variable expansion and escape sequence evaluation rules
endif()
project(libs)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/pip/cmake" "${CMAKE_CURRENT_SOURCE_DIR}/qad/cmake")
include(SDKMacros)
include(DeployMacros)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(LIBPROJECT 1)
if(NOT DEFINED BUILD_NUMBER)
	set(BUILD_NUMBER 9999)
endif()
set(_COMPANY  SHS)
set(_DOMAIN   org.SHS)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/pip)
if(MINGW)
	find_package(MinGW REQUIRED)
else()
	if(APPLE)
		set(CMAKE_INSTALL_RPATH "@executable_path/../Frameworks;@executable_path/lib;@loader_path/../lib")
		set(CMAKE_MACOSX_RPATH TRUE)
		if(CMAKE_CROSSCOMPILING)
			set(CMAKE_INSTALL_NAME_DIR "@rpath")
		else()
			include_directories(/usr/local/include)
			link_directories(/usr/local/lib)
		endif()
	else()
		set(CMAKE_INSTALL_RPATH "\$ORIGIN/lib")
	endif()
endif()
if (DEFINED ANDROID_PLATFORM)
	include_directories(${ANDROID_SYSTEM_LIBRARY_PATH}/usr/include)
	#set(TARGET_INSTALL_PATH "${ANDROID_SYSTEM_LIBRARY_PATH}/usr/")
	#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()
set(INSTALL_PREFIX "")
if(CMAKE_CROSSCOMPILING)
	if (DEFINED ANDROID_PLATFORM)
		set(INSTALL_PREFIX "${CMAKE_FIND_ROOT_PATH}/")
	else()
		set(INSTALL_PREFIX "${CMAKE_STAGING_PREFIX}")
	endif()
endif()
option(LIB "System install" 1)
option(QGLVIEW "Build QGLview library and utils" 0)
option(QGLENGINE "Build QGLENGINE library and utils" 0)
option(UTILS "Build various utils" 1)
option(DESIGNER_PLUGINS "Build qt designer plugins" 1)
if (CMAKE_BUILD_TYPE MATCHES Debug)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3 -Wall")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g3 -Wall")
else()
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3 -Wall")
endif()
set(CMAKE_CXX_STANDARD 11)

set(PIP_LIBRARY pip)
set(PIP_FOLDERS "." "core" "containers" "thread" "system" "io_devices" "io_utils" "console" "math" "code" "geo" "resources" "opencl" "crypt" "introspection" "concurrent" "cloud")
set(PIP_INCLUDES)

if(LIB)
	if(WIN32)
		if(MINGW)
			set(CMAKE_INSTALL_PREFIX ${MINGW_DIR})
		endif()
	else()
		if (DEFINED ANDROID_PLATFORM)
			set(CMAKE_INSTALL_PREFIX ${ANDROID_SYSTEM_LIBRARY_PATH}/usr)
		else()
			if(CMAKE_CROSSCOMPILING)
				set(CMAKE_INSTALL_PREFIX ${CMAKE_STAGING_PREFIX})
			else()
				set(CMAKE_INSTALL_PREFIX ${INSTALL_PREFIX}/usr/local)
			endif()
		endif()
	endif()
	message(STATUS "Install ${PROJECT_NAME} to system \"${CMAKE_INSTALL_PREFIX}\"")
else()
	message(STATUS "Install ${PROJECT_NAME} to local \"bin\", \"lib\" and \"include\"")
endif()

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

set(QAD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/qad)

include(QtWraps)
find_qt(Core QUIET)
set(QtVersions)
set(SomeQtFound 0)
foreach(_v ${_QT_VERSIONS_})
	option(Qt${_v} "Build for Qt${_v}" 1)
	if (Qt${_v})
		#message("Qt${_v} -> ${LOCAL_FOUND${_v}}")
		if (LOCAL_FOUND${_v})
			list(APPEND QtVersions Qt${_v})
			set(SomeQtFound 1)
		endif()
	endif()
endforeach()

if(UTILS)
	message(STATUS "Building with utils")
else()
	message(STATUS "Building only libraries")
endif()

add_subdirectory(pip)
set(_DIRS)
if (SomeQtFound)
	message(STATUS "Building Qt-derived targets for ${QtVersions}")
	add_subdirectory(qad)
	set(_DIRS piqt piqt_utils qcd_utils)
	if (Qt5)
		if (QGLVIEW)
			list(APPEND _DIRS qglview)
		endif()
		if (QGLENGINE)
			list(APPEND _DIRS qglengine)
		endif()
	endif()
	foreach(_D ${_DIRS})
		list(APPEND QT_MULTILIB_LIST ${_D})
	endforeach(_D)
	include_directories(piqt)
else()
	message(STATUS "None of Qt found, skip Qt-derived targets")
endif()
include_directories(cd_utils)
list(INSERT _DIRS 0 cd_utils)
foreach(_D ${_DIRS})
	add_subdirectory(${_D})
endforeach(_D)

if(WIN32)
	foreach(PIP_LT ${PIP_LIBS_TARGETS})
		if (SomeQtFound)
			qt_install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pip/lib${PIP_LT}.dll" DESTINATION QtBin)
		endif()
	endforeach()
endif()
