CMakeLists.txt 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. cmake_minimum_required(VERSION 3.1)
  2. project(DUMA C CXX)
  3. # See README.md for help building and using DUMA
  4. set(CMAKE_CXX_STANDARD 98)
  5. set(CMAKE_CXX_STANDARD_REQUIRED off)
  6. if(POLICY CMP0063)
  7. cmake_policy(SET CMP0063 NEW)
  8. endif()
  9. if(POLICY CMP0065)
  10. cmake_policy(SET CMP0065 NEW)
  11. endif()
  12. if(POLICY CMP0083)
  13. cmake_policy(SET CMP0083 NEW)
  14. endif()
  15. include(CheckPIESupported OPTIONAL RESULT_VARIABLE CHECK_PIE_SUPPORTED)
  16. if(CHECK_PIE_SUPPORTED)
  17. check_pie_supported(LANGUAGES CXX)
  18. if(CMAKE_CXX_LINK_PIE_SUPPORTED)
  19. set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
  20. endif()
  21. endif()
  22. if(NOT CMAKE_BUILD_TYPE)
  23. set(CMAKE_BUILD_TYPE Debug)
  24. endif()
  25. option( DUMA_WITH_GLOBAL_MALLOC_FREE "export malloc/free" ON) # NOT DUMA_NO_GLOBAL_MALLOC_FREE
  26. option( DUMA_WITH_EXPLICIT_INIT "duma_init() is to be called from user" OFF) # DUMA_EXPLICIT_INIT
  27. option( DUMA_WITH_THREAD_SAFETY "build thread safe duma library" ON) # NOT DUMA_NO_THREAD_SAFETY
  28. option( DUMA_WITH_STRERROR "use strerror - leads to recusion on some platforms!" ON) # NOT DUMA_NO_STRERROR
  29. option( DUMA_WITH_OLD_NEW_MACRO "use NEW_ELEM() / NEW_ARRAY() macros - DEL_ELEM/DEL_ARRAY have to be used anyway" OFF) # DUMA_OLD_NEW_MACRO
  30. option( DUMA_SHARED_WITH_CPP_SUPPORT "have new/delete shared library" ON) # NOT DUMA_SO_NO_CPP_SUPPORT
  31. option( DUMA_SHARED_WITH_LEAK_DETECTION "shared library: have leak detection" ON) # NOT DUMA_SO_NO_LEAKDETECTION
  32. option( DUMA_STATIC_WITH_LEAK_DETECTION "static library: have leak detection" ON) # NOT DUMA_LIB_NO_LEAKDETECTION
  33. option( DUMA_SHARED_PREFER_ATEXIT "shared library: have atexit() (vs. compiler's destructor)" OFF) # DUMA_SO_PREFER_ATEXIT
  34. option( DUMA_STATIC_PREFER_ATEXIT "static library: have atexit() (vs. compiler's destructor)" OFF) # DUMA_LIB_PREFER_ATEXIT
  35. option( DUMA_SHARED_PREFER_GETENV "shared library: have getenv() (vs. char **environ)" OFF) # DUMA_SO_PREFER_GETENV
  36. option( DUMA_STATIC_PREFER_GETENV "static library: have getenv() (vs. char **environ)" OFF) # DUMA_LIB_PREFER_GETENV
  37. option( DUMA_SHARED_NO_HANG_MSG "shared library: suppress the extra messages around atexit()" OFF) # DUMA_SO_NO_HANG_MSG
  38. option( DUMA_STATIC_NO_HANG_MSG "static library: suppress the extra messages around atexit()" OFF) # DUMA_LIB_NO_HANG_MSG
  39. option(BUILD_TESTS "build and run tests" ON)
  40. option(BUILD_EXAMPLES "build examples" ON)
  41. # C90 requires the gcc extensions for function attributes like always_inline
  42. # C99 provides the function attributes: no gcc extensions required
  43. # set(CMAKE_C_STANDARD 99)
  44. # set(CMAKE_C_EXTENSIONS OFF)
  45. # set(CMAKE_CXX_STANDARD 98)
  46. # set(CMAKE_CXX_STANDARD_REQUIRED ON)
  47. # set(CMAKE_CXX_EXTENSIONS OFF)
  48. ##############################################################
  49. # howto create the header
  50. # https://stackoverflow.com/questions/36084785/building-a-tool-immediately-so-it-can-be-used-later-in-same-cmake-run?noredirect=1&lq=1
  51. if(EARLY_BUILD)
  52. # This is the nested build and we will only be asked to
  53. # build the createconf target (see (c) below)
  54. add_subdirectory(confdir)
  55. # End immediately, we don't want anything else in the nested build
  56. return()
  57. endif()
  58. # This is the main build, setup and execute the nested build
  59. # to ensure the createconf executable exists before continuing
  60. # (a) When cross compiling, we cannot re-use the same binary dir
  61. # because the host and target are different architectures
  62. if(CMAKE_CROSSCOMPILING)
  63. set(workdir "${CMAKE_BINARY_DIR}/host")
  64. execute_process(COMMAND ${CMAKE_COMMAND} -E make_directory "${workdir}")
  65. else()
  66. set(workdir "${CMAKE_BINARY_DIR}")
  67. endif()
  68. # (b) Nested CMake run. May need more -D... options than shown here.
  69. execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}"
  70. -DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}
  71. -DCMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}
  72. -DEARLY_BUILD=ON
  73. ${CMAKE_SOURCE_DIR}
  74. WORKING_DIRECTORY "${workdir}")
  75. # (c) Build just createconf in the nested build. Don't specify a --config
  76. # because we cannot know what config the developer will be using
  77. # at this point. For non-multi-config generators, we've already
  78. # specified CMAKE_BUILD_TYPE above in (b).
  79. execute_process(COMMAND ${CMAKE_COMMAND} --build . --target createconf
  80. WORKING_DIRECTORY "${workdir}")
  81. # (d) We want everything from confdir in our main build,
  82. # not just the createconf target
  83. #add_subdirectory(confdir)
  84. # (e) Run createconf on the sources to generate a CMakeLists.txt in the
  85. # ${CMAKE_BINARY_DIR}/foobar directory. Note that because we want
  86. # to support cross compiling, working out the location of the
  87. # executable is a bit more tricky. We cannot know whether the user
  88. # wants debug or release build types for multi-config generators
  89. # so we have to choose one. We cannot query the target properties
  90. # because they are only known at generate time, which is after here.
  91. # Best we can do is hardcode some basic logic.
  92. if(MSVC)
  93. set(createconfsuffix "Debug/createconf.exe")
  94. elseif(CMAKE_GENERATOR STREQUAL "Xcode")
  95. set(createconfsuffix "Debug/createconf")
  96. else()
  97. set(createconfsuffix "createconf")
  98. endif()
  99. set(createconf_EXECUTABLE "${workdir}/confdir/${createconfsuffix}")
  100. add_custom_command( OUTPUT "${CMAKE_SOURCE_DIR}/duma_config.h"
  101. COMMAND "${createconf_EXECUTABLE}"
  102. WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
  103. )
  104. # -outdir foobar ${CMAKE_SOURCE_DIR}/foo.my ${CMAKE_SOURCE_DIR}/bar.my)
  105. # (f) Now pull that generated CMakeLists.txt into the main build.
  106. # It will create a CMake library target called foobar.
  107. # add_subdirectory(${CMAKE_BINARY_DIR}/foobar ${CMAKE_BINARY_DIR}/foobar-build)
  108. # (g) Another target which links to the foobar library
  109. # and includes headers from there
  110. #add_executable(gumby gumby.cpp)
  111. #target_link_libraries(gumby PUBLIC foobar)
  112. #target_include_directories(gumby PUBLIC foobar)
  113. ##############################################################
  114. add_custom_command( OUTPUT "${CMAKE_SOURCE_DIR}/verinfo.h"
  115. COMMAND "${CMAKE_SOURCE_DIR}/make_git_source_version.sh"
  116. WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}"
  117. )
  118. set( DUMA_PUB_HDRS
  119. noduma.h
  120. duma.h
  121. dumapp.h
  122. "${CMAKE_SOURCE_DIR}/duma_config.h"
  123. duma_sem.h # not for public use .. but required from dumapp.h
  124. )
  125. set( DUMA_HDRS
  126. ${DUMA_PUB_HDRS}
  127. # createconf.h # required but we can't put dependency here
  128. verinfo.h
  129. duma_hlp.h
  130. paging.h
  131. print.h
  132. )
  133. set( DUMA_SRCS
  134. src/duma.c
  135. src/dumapp.cpp
  136. src/print.c
  137. src/sem_inc.c
  138. )
  139. add_library(DUMA_STATIC STATIC ${DUMA_SRCS} ${DUMA_HDRS})
  140. target_include_directories(DUMA_STATIC INTERFACE "${CMAKE_SOURCE_DIR}")
  141. target_include_directories(DUMA_STATIC PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
  142. if (NOT DUMA_WITH_GLOBAL_MALLOC_FREE)
  143. target_compile_definitions(DUMA_STATIC PRIVATE DUMA_NO_GLOBAL_MALLOC_FREE)
  144. endif()
  145. if (DUMA_WITH_EXPLICIT_INIT)
  146. target_compile_definitions(DUMA_STATIC PUBLIC DUMA_EXPLICIT_INIT)
  147. endif()
  148. if (NOT DUMA_WITH_THREAD_SAFETY)
  149. target_compile_definitions(DUMA_STATIC PRIVATE DUMA_NO_THREAD_SAFETY)
  150. else()
  151. target_link_libraries(DUMA_STATIC PUBLIC pthread)
  152. endif()
  153. if (NOT DUMA_WITH_STRERROR)
  154. target_compile_definitions(DUMA_STATIC PRIVATE DUMA_NO_STRERROR)
  155. endif()
  156. if (NOT DUMA_WITH_OLD_NEW_MACRO)
  157. target_compile_definitions(DUMA_STATIC PRIVATE DUMA_OLD_NEW_MACRO)
  158. endif()
  159. if (NOT DUMA_STATIC_WITH_LEAK_DETECTION)
  160. target_compile_definitions(DUMA_STATIC PRIVATE DUMA_LIB_NO_LEAKDETECTION)
  161. endif()
  162. if (DUMA_STATIC_PREFER_ATEXIT)
  163. target_compile_definitions(DUMA_STATIC PRIVATE DUMA_LIB_PREFER_ATEXIT)
  164. endif()
  165. if (DUMA_STATIC_PREFER_GETENV)
  166. target_compile_definitions(DUMA_STATIC PRIVATE DUMA_LIB_PREFER_GETENV)
  167. endif()
  168. if (DUMA_STATIC_NO_HANG_MSG)
  169. target_compile_definitions(DUMA_STATIC PRIVATE DUMA_LIB_NO_HANG_MSG)
  170. endif()
  171. add_library(DUMA_SHARED SHARED ${DUMA_SRCS} ${DUMA_HDRS})
  172. target_include_directories(DUMA_SHARED INTERFACE "${CMAKE_SOURCE_DIR}")
  173. target_include_directories(DUMA_SHARED PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
  174. if (NOT DUMA_SHARED_WITH_CPP_SUPPORT)
  175. target_compile_definitions(DUMA_SHARED PRIVATE DUMA_SO_NO_CPP_SUPPORT)
  176. endif()
  177. if (NOT DUMA_SHARED_WITH_LEAK_DETECTION)
  178. target_compile_definitions(DUMA_SHARED PRIVATE DUMA_SO_NO_LEAKDETECTION)
  179. endif()
  180. if (DUMA_SHARED_PREFER_ATEXIT)
  181. target_compile_definitions(DUMA_SHARED PRIVATE DUMA_SO_PREFER_ATEXIT)
  182. endif()
  183. if (DUMA_SHARED_PREFER_GETENV)
  184. target_compile_definitions(DUMA_SHARED PRIVATE DUMA_SO_PREFER_GETENV)
  185. endif()
  186. if (DUMA_SHARED_NO_HANG_MSG)
  187. target_compile_definitions(DUMA_SHARED PRIVATE DUMA_SO_NO_HANG_MSG)
  188. endif()
  189. if (WIN32 AND MINGW)
  190. message(STATUS "MINGW")
  191. else()
  192. message(STATUS "target link pthread to DUMA_SHARED")
  193. target_link_libraries( DUMA_SHARED PUBLIC pthread )
  194. endif()
  195. # produce same filenames - as GNUmakefile
  196. set_target_properties(DUMA_STATIC PROPERTIES OUTPUT_NAME "duma")
  197. set_target_properties(DUMA_SHARED PROPERTIES OUTPUT_NAME "duma")
  198. set_target_properties(DUMA_SHARED PROPERTIES VERSION 0.0.0 SOVERSION 0)
  199. # todo: install readme, man file, ..?
  200. install( TARGETS DUMA_STATIC DUMA_SHARED DESTINATION lib)
  201. install( FILES ${DUMA_PUB_HDRS} DESTINATION include)
  202. if (NOT WIN32)
  203. # determine filename
  204. get_target_property(DUMA_SO_VERSION DUMA_SHARED VERSION)
  205. set(DUMA_SO_FILENAME "${CMAKE_SHARED_LIBRARY_PREFIX}duma${CMAKE_SHARED_LIBRARY_SUFFIX}")
  206. # message(STATUS "CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}")
  207. # message(STATUS "DUMA_SO_FILENAME ${DUMA_SO_FILENAME}")
  208. configure_file(duma.sh.in ${CMAKE_CURRENT_BINARY_DIR}/duma.sh @ONLY)
  209. install( FILES ${CMAKE_CURRENT_BINARY_DIR}/duma.sh DESTINATION bin
  210. PERMISSIONS OWNER_EXECUTE OWNER_WRITE OWNER_READ
  211. GROUP_EXECUTE GROUP_READ
  212. WORLD_EXECUTE WORLD_READ
  213. )
  214. endif()
  215. add_custom_target(uninstall
  216. "${CMAKE_COMMAND}" -P "${CMAKE_SOURCE_DIR}/uninstall.cmake"
  217. )
  218. enable_testing()
  219. if (BUILD_TESTS)
  220. add_executable(dumatest_static tests/dumatest.c)
  221. target_link_libraries(dumatest_static DUMA_STATIC)
  222. add_executable(tstheap_static tests/tstheap.c)
  223. target_link_libraries(tstheap_static DUMA_STATIC)
  224. add_executable(thread-test_static tests/thread-test.c)
  225. target_link_libraries(thread-test_static DUMA_STATIC)
  226. add_executable(testmt_static tests/testmt.c)
  227. target_link_libraries(testmt_static DUMA_STATIC)
  228. add_executable(dumatestpp_static tests/dumatestpp.cpp)
  229. target_link_libraries(dumatestpp_static DUMA_STATIC)
  230. add_executable(testoperators_static tests/testoperators.cpp)
  231. target_link_libraries(testoperators_static DUMA_STATIC)
  232. add_executable(testmemlimit_static tests/experimental/testmemlimit.c)
  233. target_link_libraries(testmemlimit_static DUMA_STATIC)
  234. # shared variants
  235. add_executable(dumatest_shared tests/dumatest.c)
  236. target_link_libraries(dumatest_shared DUMA_SHARED)
  237. add_executable(tstheap_shared tests/tstheap.c)
  238. target_link_libraries(tstheap_shared DUMA_SHARED)
  239. add_executable(thread-test_shared tests/thread-test.c)
  240. target_link_libraries(thread-test_shared DUMA_SHARED)
  241. add_executable(testmt_shared tests/testmt.c)
  242. target_link_libraries(testmt_shared DUMA_SHARED)
  243. add_executable(dumatestpp_shared tests/dumatestpp.cpp)
  244. target_link_libraries(dumatestpp_shared DUMA_SHARED)
  245. add_executable(testoperators_shared tests/testoperators.cpp)
  246. target_link_libraries(testoperators_shared DUMA_SHARED)
  247. add_executable(testmemlimit_shared tests/experimental/testmemlimit.c)
  248. target_link_libraries(testmemlimit_shared DUMA_SHARED)
  249. # tests are executed with 'ctest' or 'cmake --build . --target test' - each from build directory
  250. add_test(NAME dumatest_static COMMAND "${CMAKE_CURRENT_BINARY_DIR}/dumatest_static")
  251. add_test(NAME tstheap_static COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tstheap_static" 3072)
  252. add_test(NAME thread-test_static COMMAND "${CMAKE_CURRENT_BINARY_DIR}/thread-test_static")
  253. # add_test(NAME testmt_static COMMAND "${CMAKE_CURRENT_BINARY_DIR}/testmt_static")
  254. add_test(NAME dumatestpp_static COMMAND "${CMAKE_CURRENT_BINARY_DIR}/dumatestpp_static")
  255. add_test(NAME testoperators_static COMMAND "${CMAKE_CURRENT_BINARY_DIR}/testoperators_static")
  256. add_test(NAME testmemlimit_static COMMAND "${CMAKE_CURRENT_BINARY_DIR}/testmemlimit_static")
  257. add_test(NAME dumatest_shared COMMAND "${CMAKE_CURRENT_BINARY_DIR}/dumatest_shared")
  258. add_test(NAME tstheap_shared COMMAND "${CMAKE_CURRENT_BINARY_DIR}/tstheap_shared" 3072)
  259. add_test(NAME thread-test_shared COMMAND "${CMAKE_CURRENT_BINARY_DIR}/thread-test_shared")
  260. # add_test(NAME testmt_shared COMMAND "${CMAKE_CURRENT_BINARY_DIR}/testmt_shared")
  261. add_test(NAME dumatestpp_shared COMMAND "${CMAKE_CURRENT_BINARY_DIR}/dumatestpp_shared")
  262. add_test(NAME testoperators_shared COMMAND "${CMAKE_CURRENT_BINARY_DIR}/testoperators_shared")
  263. add_test(NAME testmemlimit_shared COMMAND "${CMAKE_CURRENT_BINARY_DIR}/testmemlimit_shared")
  264. endif()
  265. if (BUILD_EXAMPLES)
  266. # no linked against duma
  267. add_executable(example1_pure examples/example1.cpp)
  268. add_executable(example2_pure examples/example2.cpp)
  269. # add_executable(example3_pure examples/example3.cpp) # uses duma.h
  270. # add_executable(example4_pure examples/example4.cpp) # uses duma.h
  271. # add_executable(example5_pure examples/example5.cpp) # uses dumapp.h
  272. add_executable(example6_pure examples/example6.cpp)
  273. add_executable(example7_pure examples/example7.cpp)
  274. # add_executable(example8_pure examples/example8.cpp) # uses dumapp.h
  275. # linked statically against duma
  276. add_executable(example1_static examples/example1.cpp)
  277. target_link_libraries(example1_static DUMA_STATIC)
  278. add_executable(example2_static examples/example2.cpp)
  279. target_link_libraries(example2_static DUMA_STATIC)
  280. add_executable(example3_static examples/example3.cpp)
  281. target_link_libraries(example3_static DUMA_STATIC)
  282. add_executable(example4_static examples/example4.cpp)
  283. target_link_libraries(example4_static DUMA_STATIC)
  284. add_executable(example5_static examples/example5.cpp)
  285. target_link_libraries(example5_static DUMA_STATIC)
  286. add_executable(example6_static examples/example6.cpp)
  287. target_link_libraries(example6_static DUMA_STATIC)
  288. add_executable(example7_static examples/example7.cpp)
  289. target_link_libraries(example7_static DUMA_STATIC)
  290. add_executable(example8_static examples/example8.cpp)
  291. target_link_libraries(example8_static DUMA_STATIC)
  292. # linked against shared duma-library (.so)
  293. add_executable(example1_shared examples/example1.cpp)
  294. target_link_libraries(example1_shared DUMA_SHARED)
  295. add_executable(example2_shared examples/example2.cpp)
  296. target_link_libraries(example2_shared DUMA_SHARED)
  297. add_executable(example3_shared examples/example3.cpp)
  298. target_link_libraries(example3_shared DUMA_SHARED)
  299. add_executable(example4_shared examples/example4.cpp)
  300. target_link_libraries(example4_shared DUMA_SHARED)
  301. add_executable(example5_shared examples/example5.cpp)
  302. target_link_libraries(example5_shared DUMA_SHARED)
  303. add_executable(example6_shared examples/example6.cpp)
  304. target_link_libraries(example6_shared DUMA_SHARED)
  305. add_executable(example7_shared examples/example7.cpp)
  306. target_link_libraries(example7_shared DUMA_SHARED)
  307. add_executable(example8_shared examples/example8.cpp)
  308. target_link_libraries(example8_shared DUMA_SHARED)
  309. endif()
  310. message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")