project(pip) cmake_minimum_required(VERSION 2.6) include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}) set(PIP_BUILD 1) include(CheckFunctionExists) include(PIPMacros.cmake) # Options option(ICU "Unicode support" 1) option(STD_IOSTREAM "Building with std iostream operators support" 0) option(INTROSPECTION_CONTAINERS "Build with containers introspection" 0) option(INTROSPECTION_THREADS "Build with threads introspection" 0) option(LIB "System install" 1) option(DEBUG "Build with -g3" 0) # Basic macro(gather_src DIR CPP H H_P) set(CS) set(HS) set(PHS) file(GLOB CS "${DIR}/*.cpp") file(GLOB HS "${DIR}/*.h") file(GLOB PHS "${DIR}/*_p.h") list(REMOVE_ITEM HS "${PHS}") list(APPEND ${CPP} ${CS}) list(APPEND ${H} ${HS}) list(APPEND ${H_P} ${PHS}) endmacro() set(PIP_SRC_MAIN "src_main") set(PIP_SRC_CRYPT "src_crypt") set(PIP_SRC_COMPRESS "src_compress") set(PIP_SRC_USB "src_usb") set(PIP_SRC_FFTW "src_fftw") set(PIP_SRC_OPENCL "src_opencl") set(PIP_LIBS_TARGETS pip) set(LIBS_MAIN) set(LIBS_STATUS) #if(LIB) if(WIN32) if(MINGW) find_package(MinGW REQUIRED) list(APPEND CMAKE_LIBRARY_PATH ${MINGW_LIB}) endif() endif() #endif() if ("x${PIP_RC}" STREQUAL "x") set(PIP_CMG "${CMAKE_CURRENT_BINARY_DIR}/utils/code_model_generator/pip_cmg") set(PIP_RC "${CMAKE_CURRENT_BINARY_DIR}/utils/resources_compiler/pip_rc") endif() # Version file(READ "${PIP_SRC_MAIN}/piversion.h" VERSION_OFFSET LIMIT 4 OFFSET 3) file(READ "${PIP_SRC_MAIN}/piversion.h" VERSION_MAJOR LIMIT 1 OFFSET ${VERSION_OFFSET}) file(READ "${PIP_SRC_MAIN}/piversion.h" VERSION_OFFSET LIMIT 4 OFFSET 7) file(READ "${PIP_SRC_MAIN}/piversion.h" VERSION_MINOR LIMIT 1 OFFSET ${VERSION_OFFSET}) file(READ "${PIP_SRC_MAIN}/piversion.h" VERSION_OFFSET LIMIT 4 OFFSET 11) file(READ "${PIP_SRC_MAIN}/piversion.h" VERSION_REVISION LIMIT 1 OFFSET ${VERSION_OFFSET}) file(STRINGS "${PIP_SRC_MAIN}/piversion.h" VERSION_SUFFIX REGEX "\".*\"") string(REGEX MATCH "\".*\"" VERSION_SUFFIX ${VERSION_SUFFIX}) string(LENGTH ${VERSION_SUFFIX} SL) math(EXPR SL '${SL}-2') string(SUBSTRING ${VERSION_SUFFIX} 1 ${SL} VERSION_SUFFIX) string(LENGTH ${VERSION_MAJOR} SL) math(EXPR SL '${SL}-1') string(SUBSTRING ${VERSION_MAJOR} 0 ${SL} VERSION_MAJOR) string(LENGTH ${VERSION_MINOR} SL) math(EXPR SL '${SL}-1') string(SUBSTRING ${VERSION_MINOR} 0 ${SL} VERSION_MINOR) string(LENGTH ${VERSION_REVISION} SL) math(EXPR SL '${SL}-1') string(SUBSTRING ${VERSION_REVISION} 0 ${SL} VERSION_REVISION) set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_REVISION}") set(SOVERSION ${VERSION}) set(_PIP_VERSION_CHANGED 0) if ((NOT _PIP_FULL_VERSION) OR (NOT "x${_PIP_FULL_VERSION}" STREQUAL "x${VERSION}${VERSION_SUFFIX}")) set(_PIP_VERSION_CHANGED 1) set(_PIP_FULL_VERSION "${VERSION}${VERSION_SUFFIX}" CACHE STRING "pip_full_version" FORCE) endif() #message("${_PIP_VERSION_CHANGED}") message(STATUS "Building PIP version ${_PIP_FULL_VERSION}") if (_PIP_VERSION_CHANGED) file(WRITE "${PIP_SRC_MAIN}/pip_version_str.h" "// This file was generated by PIP CMake, don`t edit it!\n#define __PIP_VERSION_STR__ \"${_PIP_FULL_VERSION}\"\n") endif() # Compiler get_filename_component(C_COMPILER "${CMAKE_C_COMPILER}" NAME) #message("${C_COMPILER}") # Sources # Main lib set(PIP_FOLDERS "." "core" "containers" "thread" "system" "io" "console" "math" "code" "geo" "resources" "opencl" "crypt") include_directories("${PIP_SRC_MAIN}") foreach(F ${PIP_FOLDERS}) include_directories("${PIP_SRC_MAIN}/${F}") gather_src("${PIP_SRC_MAIN}/${F}" CPP_LIB_MAIN HDRS PHDRS) endforeach(F) # Crypt lib gather_src("${PIP_SRC_CRYPT}" CPP_LIB_CRYPT HDRS PHDRS) # Compress lib gather_src("${PIP_SRC_COMPRESS}" CPP_LIB_COMPRESS HDRS PHDRS) # USB lib gather_src("${PIP_SRC_USB}" CPP_LIB_USB HDRS PHDRS) # FFTW lib gather_src("${PIP_SRC_FFTW}" CPP_LIB_FFTW HDRS PHDRS) # OpenCL lib gather_src("${PIP_SRC_OPENCL}" CPP_LIB_OPENCL HDRS PHDRS) # 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 RT timers exists set(CMAKE_REQUIRED_INCLUDES time.h) set(CMAKE_REQUIRED_LIBRARIES ) if((NOT DEFINED ENV{QNX_HOST}) AND (NOT APPLE) AND (NOT WIN32) AND (NOT DEFINED ANDROID_PLATFORM)) list(APPEND LIBS_MAIN rt) set(CMAKE_REQUIRED_LIBRARIES rt) endif() CHECK_FUNCTION_EXISTS(timer_create PIP_TIMER_RT_0) CHECK_FUNCTION_EXISTS(timer_settime PIP_TIMER_RT_1) CHECK_FUNCTION_EXISTS(timer_delete PIP_TIMER_RT_2) # Check if build debug version if(DEBUG) add_definitions(-DPIP_DEBUG) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g3") message(STATUS "Building debug version") else() set(CMAKE_BUILD_TYPE "Release") message(STATUS "Building release version") endif() # Check if std::iostream operators support if(STD_IOSTREAM) add_definitions(-DPIP_STD_IOSTREAM) message(STATUS "Building with std iostream operators support") else() message(STATUS "Building without std iostream operators support") endif() # Check if STL containers is on (to enable use "-DSTL=" argument of cmake) if(STL) message(STATUS "Building with STL containers") add_definitions(-DPIP_CONTAINERS_STL) else() message(STATUS "Building with PIP containers") endif() # Check if ICU used for PIString and PIChar if(ICU) message(STATUS "Building with ICU") add_definitions(-DPIP_ICU) list(APPEND LIBS_MAIN icuuc) else() message(STATUS "Building without ICU, attention!") endif() # Check if PIP should be built with containers introspection if(INTROSPECTION_CONTAINERS) message(STATUS "Building with containers introspection") add_definitions(-DPIP_INTROSPECTION_CONTAINERS) else() message(STATUS "Building without containers introspection") endif() # Check if PIP should be built with threads introspection if(INTROSPECTION_THREADS) message(STATUS "Building with threads introspection") add_definitions(-DPIP_INTROSPECTION_THREADS) else() message(STATUS "Building without threads introspection") endif() # Check if RT timers exists if(PIP_TIMER_RT_0 AND PIP_TIMER_RT_1 AND PIP_TIMER_RT_2) add_definitions(-DPIP_TIMER_RT) message(STATUS "Building with timers: Thread, ThreadRT, Pool") else() message(STATUS "Building with timers: Thread, Pool") endif() # Add main library if(APPLE) add_definitions(-D_FILE_OFFSET_BITS=64 -D_LARGEFILE64_SOURCE) endif() if ((NOT DEFINED LIBPROJECT) AND (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() if(WIN32) if(${C_COMPILER} STREQUAL "cl.exe") else() list(APPEND LIBS_MAIN ws2_32 iphlpapi psapi) endif() else() list(APPEND LIBS_MAIN dl) if(DEFINED ENV{QNX_HOST}) list(APPEND LIBS_MAIN socket) else() if (NOT DEFINED ANDROID_PLATFORM) list(APPEND LIBS_MAIN pthread util) endif() endif() endif() foreach(LIB_ ${LIBS_MAIN}) find_library(${LIB_}_FOUND ${LIB_}) if(${LIB_}_FOUND) list(APPEND CMAKE_REQUIRED_LIBRARIES ${LIB_}) endif() endforeach() list(APPEND LIBS_STATUS ${LIBS_MAIN}) if(WIN32) list(APPEND CPP_LIB_MAIN "pip_resource_win.rc") add_definitions(-DPSAPI_VERSION=1) add_library(pip SHARED ${CPP_LIB_MAIN} ${HDRS} ${PHDRS}) if(${C_COMPILER} STREQUAL "cl.exe") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /O2 /Ob2 /Ot /W0") else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wall") #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") endif() else() set(${CMAKE_CXX_FLAGS} "${CMAKE_CXX_FLAGS} -O3 -fPIC") #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native") if(DEFINED ENV{QNX_HOST}) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ftemplate-depth-32") add_library(pip STATIC ${CPP_LIB_MAIN}) else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall") add_library(pip SHARED ${CPP_LIB_MAIN}) endif() endif() set(CMAKE_C_FLAGS "${CMAKE_CXX_FLAGS}") include(GenerateExportHeader) generate_export_header(pip) list(APPEND HDRS "${CMAKE_CURRENT_BINARY_DIR}/pip_export.h") target_link_libraries(pip ${LIBS_MAIN}) # Check if USB is supported find_library(usb_FOUND usb SHARED) if(usb_FOUND) message(STATUS "Building with USB support") add_definitions(-DPIP_USB) add_library(pip_usb SHARED ${CPP_LIB_USB}) target_link_libraries(pip_usb pip usb) list(APPEND LIBS_STATUS usb) list(APPEND PIP_LIBS_TARGETS pip_usb) else() message(STATUS "Building without USB support") endif() # Check if PIP support cryptographic encryption/decryption using sodium library find_library(sodium_FOUND sodium) if(sodium_FOUND) message(STATUS "Building with crypt support") add_definitions(-DPIP_CRYPT) add_library(pip_crypt SHARED ${CPP_LIB_CRYPT}) target_link_libraries(pip_crypt pip sodium) list(APPEND LIBS_STATUS sodium) list(APPEND PIP_LIBS_TARGETS pip_crypt) else() message(STATUS "Building without crypt support") endif() # Check if PIP support compress/decompress using zlib library find_library(zlib_FOUND NAMES z zlib) if(zlib_FOUND) message(STATUS "Building with zlib compress support") add_definitions(-DPIP_COMPRESS) add_library(pip_compress SHARED ${CPP_LIB_COMPRESS}) target_link_libraries(pip_compress pip ${zlib_FOUND}) list(APPEND LIBS_STATUS zlib) list(APPEND PIP_LIBS_TARGETS pip_compress) else() message(STATUS "Building without compress support") endif() # Check if PIP support fftw3 for PIFFT using in math module set(FFTW_LIB_NAME fftw3) set(FFTW_LIB_SUFFIXES "" "f" "l" "q") set(FFTW_LIB_SUFFIXES2 "" "-3") set(FFTW_LIBS) set(PIP_FFTW_FOUND) set(CMAKE_REQUIRED_INCLUDES fftw3.h) foreach(FFTW_S_ IN LISTS FFTW_LIB_SUFFIXES) set(FFTW_BREAK false) foreach(FFTW_S2_ IN LISTS FFTW_LIB_SUFFIXES2) if(NOT FFTW_BREAK) set(FFTW_CLN "${FFTW_LIB_NAME}${FFTW_S_}${FFTW_S2_}") set(FFTW_CLNT "${FFTW_LIB_NAME}${FFTW_S_}_threads${FFTW_S2_}") find_library(${FFTW_CLN}_FOUND ${FFTW_CLN}) find_library(${FFTW_CLNT}_FOUND ${FFTW_CLNT}) if(${FFTW_CLN}_FOUND) list(APPEND FFTW_LIBS "${FFTW_CLN}") #message(STATUS "PIFFTW found ${FFTW_CLN} = ${${FFTW_CLN}_FOUND}") set(${FFTW_CLN}_CTS "${FFTW_CLN}") if(${FFTW_CLNT}_FOUND) list(APPEND FFTW_LIBS "${FFTW_CLNT}") list(APPEND ${FFTW_CLN}_CTS "${FFTW_CLNT}") endif() set(CMAKE_REQUIRED_LIBRARIES ${${FFTW_CLN}_CTS}) CHECK_FUNCTION_EXISTS(fftw${FFTW_S_}_make_planner_thread_safe ${FFTW_CLN}_TSFE) add_definitions(-DPIP_FFTW${FFTW_S_}) set(PIP_FFTW_FOUND true) if(${FFTW_CLN}_TSFE) add_definitions(-DPIP_FFTW${FFTW_S_}_THREADSAFE) else() message(STATUS "Warning: PIFFTW${FFTW_S_}::preparePlan was not threadsafe") endif() #message(STATUS "${FFTW_CLN} -> ${${FFTW_CLN}_TSFE}") endif() endif() endforeach() endforeach() if(FFTW_LIBS) message(STATUS "Building with fftw3 support: ${FFTW_LIBS}") add_library(pip_fftw SHARED ${CPP_LIB_FFTW}) target_link_libraries(pip_fftw pip ${FFTW_LIBS}) list(APPEND LIBS_STATUS ${FFTW_LIBS}) list(APPEND PIP_LIBS_TARGETS pip_fftw) else() message(STATUS "Building without fftw3 support") endif() # Check if PIP support OpenCL find_package(OpenCL QUIET) if(OpenCL_FOUND) message(STATUS "Building with OpenCL support") if(APPLE) include_directories(${OpenCL_INCLUDE_DIRS}/Headers) else() include_directories(${OpenCL_INCLUDE_DIRS}) endif() add_definitions(-DPIP_OPENCL) pip_resources(CL_RES "src_opencl/resources.conf") add_library(pip_opencl SHARED ${CPP_LIB_OPENCL} ${CL_RES}) add_dependencies(pip_opencl pip_rc) target_link_libraries(pip_opencl pip OpenCL::OpenCL) list(APPEND LIBS_STATUS OpenCL) list(APPEND PIP_LIBS_TARGETS pip_opencl) set(OpenCL_FOUND ${OpenCL_LIBRARIES}) else() message(STATUS "Building without OpenCL support") endif() # Test program add_executable(pip_test "main.cpp") target_link_libraries(pip_test pip pip_crypt) # Install # Check if system or local install will be used (to system install use "-DLIB=" argument of cmake) if(LIB) if(WIN32) if(MINGW) find_package(MinGW REQUIRED) set(CMAKE_INSTALL_PREFIX ${MINGW_DIR}) install(FILES ${HDRS} DESTINATION ${MINGW_INCLUDE}/pip) install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION ${MINGW_LIB}) install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION ${MINGW_BIN}) find_library(STDLIB "stdc++-6") #message("${STDLIB}") if (STDLIB) file(COPY "${STDLIB}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/utils/code_model_generator") file(COPY "${STDLIB}" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/utils/resources_compiler") endif() else() #message("${CMAKE_CURRENT_BINARY_DIR}/pip_export.h") install(FILES ${CMAKE_CURRENT_BINARY_DIR}/pip_export.h DESTINATION include) endif() else() if(APPLE) set(CMAKE_INSTALL_PREFIX /usr/local) else() if (DEFINED ANDROID_PLATFORM) set(CMAKE_INSTALL_PREFIX ${ANDROID_SYSTEM_LIBRARY_PATH}/usr) else() set(CMAKE_INSTALL_PREFIX /usr) endif() endif() install(FILES ${HDRS} DESTINATION ${CMAKE_INSTALL_PREFIX}/include/pip) install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION ${CMAKE_INSTALL_PREFIX}/lib) endif() message(STATUS "Install ${PROJECT_NAME} to system \"${CMAKE_INSTALL_PREFIX}\"") # Precompiled header #add_custom_target(pip_pch ALL COMMAND ${CMAKE_CXX_COMPILER} -O2 -fPIC -g3 ${CMAKE_INSTALL_PREFIX}/include/pip/pip.h DEPENDS pip SOURCES ${HDRS}) #list(APPEND HDRS "pip.h.gch") file(GLOB CMAKES "*.cmake") install(FILES ${CMAKES} DESTINATION ${CMAKE_ROOT}/Modules) else() install(TARGETS ${PIP_LIBS_TARGETS} DESTINATION bin) install(FILES ${HDRS} DESTINATION include) message(STATUS "Install ${PROJECT_NAME} to local \"bin\"") endif() # Auxiliary add_subdirectory("${PIP_SRC_MAIN}/auxiliary/piterminal") # Utils add_subdirectory("utils/system_test") add_subdirectory("utils/remote_console") add_subdirectory("utils/code_model_generator") add_subdirectory("utils/resources_compiler") add_subdirectory("utils/udp_file_transfer") if(sodium_FOUND) add_subdirectory("utils/system_daemon") add_subdirectory("utils/crypt_tool") endif() # Libraries messages message(STATUS "Building modules: ${PIP_LIBS_TARGETS}") if(DEFINED LIBPROJECT) set(PIP_LIBS_TARGETS ${PIP_LIBS_TARGETS} PARENT_SCOPE) endif() foreach(LIB_ ${LIBS_STATUS}) message(STATUS "Library ${LIB_} -> " ${${LIB_}_FOUND}) if(NOT ${LIB_}_FOUND) message(WARNING "Library ${LIB_} not found, please install it") endif() endforeach() # # Build Documentation # find_package(Doxygen QUIET) if(Doxygen_FOUND) message(STATUS "Building with documentation via Doxygen") #set(DOXYFILE_IN ${CMAKE_CURRENT_SOURCE_DIR}/doc/Doxyfile.in) set(DOXYFILE ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile) #configure_file(${DOXYFILE_IN} ${DOXYFILE} @ONLY) add_custom_target(DOC COMMAND ${DOXYGEN_EXECUTABLE} ${DOXYFILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} COMMENT "Generating API documentation with Doxygen" VERBATIM) add_custom_command(TARGET DOC POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/source/doc/Documentation.html ${CMAKE_SOURCE_DIR}/doc ) install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/html DESTINATION share/doc) endif()