CMakeLists.txt 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #
  2. # This is a CMake makefile. CMake is a tool that helps you build C++ programs.
  3. # You can download CMake from http://www.cmake.org. This CMakeLists.txt file
  4. # you are reading builds dlib's example programs.
  5. #
  6. # To compile this program all you need to do is ask cmake. You would type
  7. # these commands from within the directory containing this CMakeLists.txt
  8. # file:
  9. # mkdir build
  10. # cd build
  11. # cmake ..
  12. # cmake --build . --config Release
  13. #
  14. # The cmake .. command looks in the parent folder for a file named
  15. # CMakeLists.txt, reads it, and sets up everything needed to build program.
  16. # Also, note that CMake can generate Visual Studio or XCode project files. So
  17. # if instead you had written:
  18. # cd build
  19. # cmake .. -G Xcode
  20. #
  21. # You would be able to open the resulting Xcode project and compile and edit
  22. # the example programs within the Xcode IDE. CMake can generate a lot of
  23. # different types of IDE projects. Run the cmake -h command to see a list of
  24. # arguments to -G to see what kinds of projects cmake can generate for you. It
  25. # probably includes your favorite IDE in the list.
  26. cmake_minimum_required (VERSION 3.8)
  27. project(RvcFramework)
  28. set(CMAKE_CXX_STANDARD 11)
  29. option(BUILD_TESTING "Build the project tests." ON)
  30. option(INSTALL_INCLUDE_FILES "Enable installation of include header file or third_party" OFF)
  31. set(BUILD_NUMBER 0)
  32. if ($ENV{BUILD_NUMBER})
  33. set(BUILD_NUMBER $ENV{BUILD_NUMBER})
  34. endif()
  35. set(RVC_COMPANY "CMB" CACHE STRING "China Merchants Bank")
  36. set(RVC_PRODUCT "VTM")
  37. set(RVC_AUTHOR "CCDG")
  38. set(RVC_COPYRIGHT "Copyright (C) 2020")
  39. set(RVC_DESCRIPTION "2020.01")
  40. set(RAW_VERSION_STRING "0.0.1-dev1")
  41. string(STRIP ${RAW_VERSION_STRING} RAW_VERSION_STRING)
  42. set(VERSION_REGEX "^.?([0-9]+)\\.([0-9]+)\\.([0-9]+)-?(.*)")
  43. string(REGEX REPLACE "${VERSION_REGEX}" "\\1" RVC_VERSION_MAJOR "${RAW_VERSION_STRING}")
  44. string(REGEX REPLACE "${VERSION_REGEX}" "\\2" RVC_VERSION_MINOR "${RAW_VERSION_STRING}")
  45. string(REGEX REPLACE "${VERSION_REGEX}" "\\3" RVC_VERSION_REVISION "${RAW_VERSION_STRING}")
  46. string(REGEX REPLACE "${VERSION_REGEX}" "\\4" RVC_VERSION_SUFFIX "${RAW_VERSION_STRING}")
  47. set(RVC_VERSION_BUILD "${BUILD_NUMBER}")
  48. set(RVC_API_VERSION "${RVC_VERSION_MAJOR}")
  49. set(RVC_VERSION "${RVC_VERSION_MAJOR}.${RVC_VERSION_MINOR}.${RVC_VERSION_REVISION}.${RVC_VERSION_BUILD}")
  50. set(RVC_FRAMEWORK_VERSION "${RVC_VERSION_MAJOR}.${RVC_VERSION_MINOR}.${RVC_VERSION_REVISION}")
  51. if (RVC_VERSION_SUFFIX)
  52. set(RVC_VERSION_FULL "${RVC_VERSION}-${RVC_VERSION_SUFFIX}")
  53. else()
  54. set(RVC_VERSION_FULL "${RVC_VERSION}")
  55. endif()
  56. message("RVC_VERSION=${RVC_VERSION_FULL}")
  57. configure_file("${PROJECT_SOURCE_DIR}/version.h.in" "${PROJECT_BINARY_DIR}/version.h")
  58. include_directories(${PROJECT_BINARY_DIR})
  59. # 设置生成文件的存放路径
  60. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
  61. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
  62. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
  63. if(BUILD_TESTING)
  64. include(CTest)
  65. message(STATUS "build the tests.")
  66. enable_testing()
  67. set(GTEST_ROOT "${CMAKE_SOURCE_DIR}/third_party/gtest/googletest/include")
  68. set(GMOCK_ROOT "${CMAKE_SOURCE_DIR}/third_party/gtest/googlemock/include")
  69. if(MSVC)
  70. set(TESTING_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
  71. else()
  72. set(TESTING_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/Testing")
  73. endif()
  74. endif()
  75. # specifies what build type (configuration) will be built in this build tree
  76. if(NOT CMAKE_BUILD_TYPE)
  77. set(CMAKE_BUILD_TYPE "Debug")
  78. endif()
  79. # Turn on solution folders (2.8.4+)
  80. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  81. # 在 Windows 下用 "/" 会报错
  82. STRING(REGEX REPLACE "^([A-Z]*.)([/\\].*)" "\\1" CURRENT_DISK "${CMAKE_CURRENT_SOURCE_DIR}")
  83. # message(STATUS "current disk: ${CURRENT_DISK}")
  84. set(OUTPUT_VERSION_PATH "${CURRENT_DISK}\\run\\version\\${RVC_VERSION}")
  85. # message(STATUS "current OUTPUT_VERSION_PATH: ${OUTPUT_VERSION_PATH}")
  86. # 如果使用绝对路径,会导致 CPack 因 CMAKE_ERROR_ON_ABSOLUTE_INSTALL_DESTINATION 失败
  87. # set(RVC_INCLUDE_PATH "${CMAKE_INSTALL_PREFIX}/include")
  88. # set(RVC_LIBRARY_PATH "${CMAKE_INSTALL_LIBDIR}")
  89. # set(RVC_RUNTIME_PATH "${CMAKE_INSTALL_BINDIR}")
  90. # set(RVC_INSTALL_PATH "${CMAKE_INSTALL_PREFIX}/bin")
  91. set(RVC_INCLUDE_PATH "include")
  92. set(RVC_LIBRARY_PATH "lib")
  93. set(RVC_RUNTIME_PATH "bin")
  94. set(RVC_MODULE_PATH "mod")
  95. set(RVC_CONFIG_PATH "cfg")
  96. set(RVC_VENDOR_PATH "dep")
  97. # set(RVC_MODULE_LIBRARY_PATH "${CMAKE_BINARY_DIR}/mod")
  98. macro(copy_output_on_build module_name output_subdir)
  99. set(target_name ${module_name})
  100. set(output_dir ${OUTPUT_VERSION_PATH}\\${output_subdir})
  101. if(NOT EXISTS ${output_dir} OR NOT IS_DIRECTORY ${output_dir})
  102. # nothing to do.
  103. endif()
  104. add_custom_command(TARGET ${target_name} PRE_BUILD
  105. COMMAND ${CMAKE_COMMAND}
  106. ARGS -E make_directory ${output_dir})
  107. add_custom_command(TARGET ${target_name} POST_BUILD
  108. COMMAND ${CMAKE_COMMAND} -E copy
  109. $<TARGET_FILE:${target_name}>
  110. ${OUTPUT_VERSION_PATH}/${output_subdir}
  111. )
  112. endmacro(copy_output_on_build)
  113. if (CMAKE_BUILD_TYPE STREQUAL Debug OR CMAKE_CONFIGURATION_TYPES STREQUAL Debug)
  114. message(STATUS "debug type")
  115. set(BUILD_TYPE_NAME "Debug")
  116. else()
  117. set(BUILD_TYPE_NAME "Release")
  118. endif()
  119. string(TOLOWER ${BUILD_TYPE_NAME} BUILD_TYPE_NAME_lower)
  120. if(${CMAKE_GENERATOR} MATCHES "Visual Studio*")
  121. if(NOT CMAKE_GENERATOR_PLATFORM)
  122. message(STATUS "set win32 platform default")
  123. set(CMAKE_GENERATOR_PLATFORM "Win32")
  124. else()
  125. message(STATUS ${CMAKE_GENERATOR_PLATFORM})
  126. endif()
  127. endif()
  128. set(CPACK_SYSTEM_NAME "${CMAKE_SYSTEM_NAME}-${CMAKE_GENERATOR_PLATFORM}-${BUILD_TYPE_NAME}")
  129. set(CPACK_TOPLEVEL_TAG "${CMAKE_SYSTEM_NAME}-${CMAKE_GENERATOR_PLATFORM}-${BUILD_TYPE_NAME}")
  130. if(WIN32)
  131. # dll rc version
  132. string(TIMESTAMP RC_VERSION_YEAR "%Y")
  133. set(RC_VERSION_AUTHOR ${RVC_AUTHOR})
  134. set(RC_VERSION_COMPANY ${RVC_COMPANY})
  135. set(RC_VERSION_PRODUCT ${RVC_PRODUCT})
  136. set(RC_VERSION_PATCH ${BUILD_NUMBER})
  137. set(RC_VERSION_DESCRIPTION "${RVC_VERSION_FULL} ${CPACK_SYSTEM_NAME}")
  138. endif(WIN32)
  139. if(BUILD_SHARED_LIBS)
  140. message(STATUS "build shared libs")
  141. endif(BUILD_SHARED_LIBS)
  142. include_directories(Common)
  143. add_subdirectory(third_party)
  144. add_subdirectory(libtoolkit)
  145. add_subdirectory(RvcComm)
  146. add_subdirectory(spbase)
  147. add_subdirectory(spshell)
  148. add_subdirectory(sphost)
  149. if(BUILD_TESTING)
  150. add_subdirectory(test)
  151. endif()
  152. # 安装
  153. string(REPLACE "\\" "/" OUTPUT_VERSION_PATH ${OUTPUT_VERSION_PATH})
  154. set(CMAKE_INSTALL_PREFIX "${OUTPUT_VERSION_PATH}")
  155. #set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install") # /${RVC_VERSION}
  156. set(CMAKE_INSTALL_LOCAL_ONLY ON)
  157. include(InstallRequiredSystemLibraries)
  158. # 打包
  159. set(CPACK_SOURCE_IGNORE_FILES "/\\\\.git/;/\\\\.gitignore;/CMakeCache.txt;/\\\\build;/\\\\out")
  160. string(TOLOWER ${CMAKE_PROJECT_NAME} CMAKE_PROJECT_NAME_lower)
  161. set(CPACK_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME_lower}-${RVC_VERSION_FULL}-${CPACK_SYSTEM_NAME}")
  162. set(CPACK_SOURCE_PACKAGE_FILE_NAME "${CMAKE_PROJECT_NAME_lower}-${RVC_VERSION_FULL}-${CPACK_SYSTEM_NAME}")
  163. # 设置软件包名
  164. set(CPACK_PACKAGE_NAME "RvcFramework")
  165. set(CPACK_PACKAGE_VENDOR "RVC-CCDG")
  166. # 设置软件包版本
  167. set(CPACK_PACKAGE_VERSION ${RVC_VERSION_FULL})
  168. set(CPACK_PACKAGE_VERSION_MAJOR ${RVC_VERSION_MAJOR})
  169. set(CPACK_PACKAGE_VERSION_MINOR ${RVC_VERSION_MINOR})
  170. set(CPACK_PACKAGE_VERSION_PATCH ${RVC_VERSION_REVISION})
  171. set(CPACK_COMPONENTS_ALL headers libraries)
  172. if(MSVC)
  173. if( NOT CPACK_GENERATOR)
  174. set(CPACK_GENERATOR "ZIP")
  175. message(STATUS "set zip package file type")
  176. endif()
  177. if(CPACK_GENERATOR MATCHES "NSIS")
  178. set(CPACK_PACKAGE_INSTALL_DIRECTORY "C:\\\\Run")
  179. endif()
  180. endif(MSVC)
  181. if(MSVC)
  182. if(MSVC_RUNTIME STREQUAL "dynamic")
  183. message(STATUS "dynamic format")
  184. # the INSTALL command is not called. The user can use the variable
  185. # CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS to use a custom install command and install them however they want.
  186. set(CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS_SKIP TRUE)
  187. include(InstallRequiredSystemLibraries)
  188. install(PROGRAMS ${CMAKE_INSTALL_SYSTEM_RUNTIME_LIBS}
  189. DESTINATION ${RVC_RUNTIME_PATH}
  190. COMPONENT libraries)
  191. else()
  192. include(InstallRequiredSystemLibraries)
  193. endif()
  194. endif()
  195. include(CPack)
  196. # 会直接复制文件夹
  197. if(INSTALL_INCLUDE_FILES)
  198. install(DIRECTORY Common DESTINATION ${RVC_INCLUDE_PATH} COMPONENT headers FILES_MATCHING PATTERN "*.h")
  199. install(FILES "${PROJECT_BINARY_DIR}/version.h" DESTINATION "${RVC_INCLUDE_PATH}/Common" COMPONENT headers)
  200. endif()
  201. install(DIRECTORY "${CMAKE_SOURCE_DIR}/addin/${RVC_RUNTIME_PATH}" DESTINATION "." COMPONENT libraries)
  202. install(DIRECTORY "${CMAKE_SOURCE_DIR}/addin/${RVC_CONFIG_PATH}" DESTINATION "." COMPONENT configs FILES_MATCHING PATTERN "*.ini")
  203. configure_file("${CMAKE_CURRENT_SOURCE_DIR}/addin/${RVC_CONFIG_PATH}/shell.ini.in" ${CMAKE_CURRENT_BINARY_DIR}/shell.ini @ONLY)
  204. install(FILES ${CMAKE_CURRENT_BINARY_DIR}/shell.ini DESTINATION ${RVC_CONFIG_PATH})
  205. # if necessary
  206. # install(DIRECTORY ${CMAKE_SOURCE_DIR}/addin/mod DESTINATION ${RVC_MODULE_PATH} COMPONENT modules)
  207. # install(DIRECTORY ${CMAKE_SOURCE_DIR}/addin/dep DESTINATION ${RVC_VENDOR_PATH} COMPONENT vendors)
  208. # install(PROGRAMS "")
  209. install(CODE "message(\"install message start.\")")
  210. install(CODE "EXECUTE_PROCESS(COMMAND -E remove_directory \"${CMAKE_INSTALL_PREFIX}/${RVC_INCLUDE_PATH}\")")
  211. install(CODE "message(\"install message done.\")")
  212. # no used now
  213. function(package_static_libs output_library)
  214. get_target_property(libtype ${output_library} TYPE)
  215. if(NOT libtype STREQUAL "STATIC_LIBRARY" OR NOT libtype STREQUAL "EXECUTABLE ")
  216. message(FATAL_ERROR "can only process static library or execuate program.")
  217. endif()
  218. get_target_property(libfile ${output_library} LOCATION)
  219. endfunction()