cmake_minimum_required(VERSION 3.0)
project(kx_utils)
if (POLICY CMP0017)
	cmake_policy(SET CMP0017 NEW)
endif()
if (NOT LIBPROJECT)
	find_package(PIP REQUIRED)
endif ()
if (MINGW)
	find_package(MinGW REQUIRED)
endif()
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${PIP_INCLUDES})
set(KX_PULT_NAME "kx_pult")
option(KX_PULT "Build ${KX_PULT_NAME}" 1)
option(LIB "System install" 1)
option(DEBUG "Build with -g3" 0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall")
if (DEBUG)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3")
endif()
set(CPPS_UTILS "kx_coeffs.cpp" "piprotocol.cpp")
set(HDRS_UTILS "kx_coeffs.h" "kx_protocol_x.h" "kx_protocol_c.h" "piprotocol.h")
if (DEFINED ENV{QNX_HOST})
	add_library(${PROJECT_NAME} STATIC ${CPPS_UTILS})
else()
	add_library(${PROJECT_NAME} SHARED ${CPPS_UTILS})
endif()
target_link_libraries(${PROJECT_NAME} ${PIP_LIBRARY})
message(STATUS "Building ${PROJECT_NAME}")
if (NOT DEFINED ENV{QNX_HOST})
	if (KX_PULT)
		find_package(Qt4 REQUIRED)
		find_package(QAD REQUIRED)
		find_package(OpenGL REQUIRED)
		include_directories(${QT_INCLUDES} ${QAD_INCLUDES})
		set(CPPS "kx_pult.cpp" "kx_pult.h" "kx_pult.ui" "main_kx_pult.cpp")
		set(MOCS "kx_pult.h")
		file(GLOB UIS "*.ui")
		file(GLOB RES "*.qrc")
		qt4_wrap_cpp(CMOCS ${MOCS} OPTIONS -nw)
		qt4_wrap_ui(CUIS ${UIS})
		qt4_add_resources(RESS ${RES})
		add_executable(${KX_PULT_NAME} WIN32 ${CPPS} ${CMOCS} ${CUIS} ${RESS} ${MOCS})
		set(LIBS ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} ${QT_QTOPENGL_LIBRARY} ${OPENGL_LIBRARIES} ${PIP_LIBRARY} qad_graphic4 qad_utils4 qad_widgets4)
		target_link_libraries(${KX_PULT_NAME} ${LIBS} ${PROJECT_NAME})
	endif()
endif()
if (LIB)
	list(APPEND _ALL_TARGETS ${PROJECT_NAME})
	set(_ALL_TARGETS ${_ALL_TARGETS} PARENT_SCOPE)
	if (WIN32)
		set(CMAKE_INSTALL_PREFIX ${MINGW_DIR})
		install(FILES ${HDRS_UTILS} DESTINATION ${MINGW_INCLUDE})
		install(TARGETS ${PROJECT_NAME} DESTINATION ${MINGW_LIB})
		install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${MINGW_BIN})
	else()
		if (DEFINED ANDROID_PLATFORM)
			set(CMAKE_INSTALL_PREFIX ${ANDROID_SYSTEM_LIBRARY_PATH}/usr)
		else()
			set(CMAKE_INSTALL_PREFIX /usr/local)
		endif()
		install(FILES ${HDRS_UTILS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include)
		install(TARGETS ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
	endif()
	message(STATUS "Install ${PROJECT_NAME} to system \"${CMAKE_INSTALL_PREFIX}\"")
else()
	install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
	install(TARGETS ${PROJECT_NAME} DESTINATION lib)
	install(FILES ${HDRS_UTILS} DESTINATION include)
	message(STATUS "Install ${PROJECT_NAME} to local \"bin\", \"lib\" and \"include\"")
endif()
if (NOT DEFINED ENV{QNX_HOST})
	if (KX_PULT)
		install(TARGETS ${KX_PULT_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
	endif()
endif()
