cmake_minimum_required(VERSION 3.0)
project(cd_utils)
if (NOT LIBPROJECT)
	find_package(PIP REQUIRED)
	option(LIB "System install" 1)
	option(DEBUG "Build with -g3" 0)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall")
	if (DEBUG)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3")
	endif()
endif()
if (MINGW)
	find_package(MinGW REQUIRED)
endif()
include_directories(${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${PIP_INCLUDES})
#file(GLOB HDRS_UTILS "*.h")
#file(GLOB CPPS_UTILS "*.cpp")
file(GLOB CPPS_UTILS "cdutils_*.cpp")
file(GLOB HDRS_UTILS "cdutils_*.h")
if (DEFINED ENV{QNX_HOST})
	add_library(${PROJECT_NAME} STATIC ${CPPS_UTILS} ${HDRS_UTILS})
else()
	add_library(${PROJECT_NAME} SHARED ${CPPS_UTILS} ${HDRS_UTILS})
endif()
target_link_libraries(${PROJECT_NAME} ${PIP_LIBRARY})

# # Apple crosscompiling rpath patch
# if (APPLE AND CMAKE_CROSSCOMPILING AND CMAKE_MACOSX_RPATH)
	# foreach(_RP ${CMAKE_INSTALL_RPATH})
		# add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
			# COMMAND "${CMAKE_INSTALL_NAME_TOOL}"
					# "-add_rpath" "${_RP}"
					# "$<TARGET_FILE_DIR:${PROJECT_NAME}>/$<TARGET_FILE_NAME:${PROJECT_NAME}>"
			# COMMENT "Add to ${PROJECT_NAME} rpath \"${_RP}\"")
	# endforeach()
# endif()

add_executable(cdutilstest "cdutilstest.cpp" "cdtest.h")
target_link_libraries(cdutilstest ${PIP_LIBRARY} ${PROJECT_NAME})
message(STATUS "Building ${PROJECT_NAME}")

if(NOT LIBPROJECT)
	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()
endif()

if(LIB)
	list(APPEND _ALL_TARGETS ${PROJECT_NAME})
	set(_ALL_TARGETS ${_ALL_TARGETS} PARENT_SCOPE)
	if(WIN32)
		install(FILES ${HDRS_UTILS} DESTINATION ${MINGW_INCLUDE})
		install(TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION ${MINGW_LIB})
		install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION ${MINGW_BIN})
	else()
		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()
	if(WIN32)
		install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin)
		install(TARGETS ${PROJECT_NAME} ARCHIVE DESTINATION lib)
	else()
		install(TARGETS ${PROJECT_NAME} DESTINATION lib)
	endif()
	install(FILES ${HDRS_UTILS} DESTINATION include)
	#message(STATUS "Install ${PROJECT_NAME} to local \"bin\"")
endif()
