project(pip)
cmake_minimum_required(VERSION 2.6)
include_directories(${CMAKE_CURRENT_SOURCE_DIR} .)
include(CheckFunctionExists)
set(VERSION "0.0400")
set(SOVERSION ${VERSION})
set(CMAKE_BUILD_TYPE "Release")
set(LIBS)
file(GLOB HDRS "pi*.h")
file(GLOB CPPS "pi*.cpp")


# Check Bessel functions
set(CMAKE_REQUIRED_INCLUDES math.h)
set(CMAKE_REQUIRED_LIBRARIES m)
CHECK_FUNCTION_EXISTS(j0 PIP_MATH_J0)
CHECK_FUNCTION_EXISTS(j1 PIP_MATH_J1)
CHECK_FUNCTION_EXISTS(jn PIP_MATH_JN)
CHECK_FUNCTION_EXISTS(y0 PIP_MATH_Y0)
CHECK_FUNCTION_EXISTS(y1 PIP_MATH_Y1)
CHECK_FUNCTION_EXISTS(yn PIP_MATH_YN)
if (PIP_MATH_J0)
	add_definitions("-DPIP_MATH_J0")
endif ()
if (PIP_MATH_J1)
	add_definitions("-DPIP_MATH_J1")
endif ()
if (PIP_MATH_JN)
	add_definitions("-DPIP_MATH_JN")
endif ()
if (PIP_MATH_Y0)
	add_definitions("-DPIP_MATH_Y0")
endif ()
if (PIP_MATH_Y1)
	add_definitions("-DPIP_MATH_Y1")
endif ()
if (PIP_MATH_YN)
	add_definitions("-DPIP_MATH_YN")
endif ()


# Check if USB is on (to enable use "-DUSB=" argument of cmake)
if (DEFINED USB)
	message(STATUS "Building with USB support")
	unset(USB)
	add_definitions("-DPIP_USB")
	list(APPEND LIBS usb)
else ()
	message(STATUS "Building without USB support")
endif ()


# Check if STL containers is on (to enable use "-DPIP_CONTAINERS_STL=" argument of cmake)
if (DEFINED PIP_CONTAINERS_STL)
	message(STATUS "Building with STL containers")
	unset(PIP_CONTAINERS_STL)
	add_definitions("-DPIP_CONTAINERS_STL")
else ()
	message(STATUS "Building with PIP containers")
endif ()

# Check if system or local install will be used (to system install use "-DLIB=" argument of cmake)
if (DEFINED LIB)
	message(STATUS "Install to system")
else ()
	message(STATUS "Install to local \"bin\"")
endif ()


if (${WIN32})
	list(APPEND LIBS ws2_32 Iphlpapi)
	execute_process(COMMAND "make_rc_win.bat" WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
	list(APPEND CPPS "pip_resource_win.o")
	add_library(pip SHARED ${CPPS})
	if (${CMAKE_C_COMPILER} STREQUAL "cl")
		include(GenerateExportHeader)
		generate_export_header(pip)
		add_definitions("/O2 /Ob2 /Ot")
	else ()
		add_definitions("-O2")
	endif ()
else ()
	add_definitions("-O2")
	if (DEFINED ENV{QNX_HOST})
		list(APPEND LIBS socket)
		add_definitions("-ftemplate-depth-32")
		add_library(pip STATIC ${CPPS})
	else ()
		list(APPEND LIBS pthread)
		if (NOT APPLE)
			list(APPEND LIBS rt)
		endif()
		add_definitions("-Wall")
		add_definitions("-g3")
		add_library(pip SHARED ${CPPS})
	endif ()
endif ()
target_link_libraries(pip ${LIBS})
#install(TARGETS pip DESTINATION bin)


# Test program
#find_package(Qt4 REQUIRED)
#include_directories(${QT_INCLUDES})
add_executable(pip_test "main.cpp")
#target_link_libraries(pip_test pip ${QT_QTCORE_LIBRARY})
target_link_libraries(pip_test pip)


add_subdirectory(system_test)
add_subdirectory(remote_console)
add_subdirectory(code_model_generator)

if (DEFINED LIB)
	unset(LIB)
	if (${WIN32})
		get_filename_component(MGWDIR ${CMAKE_C_COMPILER} PATH)
		#get_filename_component(QTDIR ${QT_QMAKE_EXECUTABLE} PATH)
		set(CMAKE_INSTALL_PREFIX)
		#install(TARGETS pip DESTINATION ${QTDIR})
		install(TARGETS pip DESTINATION ${MGWDIR}/../lib)
		install(FILES ${HDRS} DESTINATION ${MGWDIR}/../include/pip)
	else ()
		set(CMAKE_INSTALL_PREFIX /usr)
		install(TARGETS pip DESTINATION lib)
		install(FILES ${HDRS} DESTINATION include/pip)
	endif ()
	install(FILES "FindPIP.cmake" DESTINATION ${CMAKE_ROOT}/Modules)
else ()
	install(TARGETS pip DESTINATION bin)
endif ()
