ksutil.cmake 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # This function will do the check_library_exists call, and if successful add the definition
  2. # to the target inline.
  3. function(ksutil_target_check_library_exists_hdr TARGET SCOPE LIB API_NAME HEADER_NAME DEFINITION)
  4. check_library_exists(${LIB} ${API_NAME} ${HEADER_NAME} ${DEFINITION})
  5. if (${DEFINITION})
  6. target_compile_definitions(${TARGET} ${SCOPE} "-D${DEFINITION}=1")
  7. endif()
  8. endfunction()
  9. # This function will do the check_library_exists call, and if successful add the definition
  10. # to the target inline.
  11. function(ksutil_target_check_library_exists TARGET SCOPE LIB API_NAME DEFINITION)
  12. check_library_exists(${LIB} ${API_NAME} "" ${DEFINITION})
  13. if (${DEFINITION})
  14. target_compile_definitions(${TARGET} ${SCOPE} "-D${DEFINITION}=1")
  15. endif()
  16. endfunction()
  17. # This function will do the check_function_exists call, and if successful add the definition
  18. # to the target inline.
  19. function(ksutil_target_check_function_exists TARGET SCOPE API_NAME DEFINITION)
  20. check_function_exists(${API_NAME} ${DEFINITION})
  21. if (${DEFINITION})
  22. target_compile_definitions(${TARGET} ${SCOPE} "-D${DEFINITION}=1")
  23. endif()
  24. endfunction()
  25. # This function will do the check_include_file call, and if successful add the definition
  26. # to the target inline.
  27. function(ksutil_target_check_include_file TARGET SCOPE HEADER DEFINITION)
  28. check_include_file(${HEADER} ${DEFINITION})
  29. if (${DEFINITION})
  30. target_compile_definitions(${TARGET} ${SCOPE} "-D${DEFINITION}=1")
  31. endif()
  32. endfunction()
  33. # This macro helps with adding a unit test with the tap library. The unit test
  34. # name must also exist as a source file e.t. testcon will have testcon.c and tap.c.
  35. macro(ksutil_add_test name)
  36. add_executable("test${name}" "test${name}.c" tap.c)
  37. target_link_libraries("test${name}" ks2)
  38. target_include_directories("test${name}" PUBLIC ${CMAKE_CURRENT_LIST_DIR})
  39. add_test("test${name}" "test${name}")
  40. # When debugging on windows, the cwd will be the binary dir (where the config files are)
  41. set_target_properties("test${name}" PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
  42. endmacro()
  43. macro(ksutil_add_test_extra name extra_deps extra_libs)
  44. add_executable("test${name}" "test${name}.c" tap.c ${extra_deps})
  45. target_link_libraries("test${name}" ks2 ${extra_libs})
  46. target_include_directories("test${name}" PUBLIC ${CMAKE_CURRENT_LIST_DIR})
  47. add_test("test${name}" "test${name}")
  48. # When debugging on windows, the cwd will be the binary dir (where the config files are)
  49. set_target_properties("test${name}" PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
  50. endmacro()
  51. # Adds a submodule subdirectory to be included in cmake. Will automatically
  52. # init it if its empty, and can optionally checkout any tag/branch if specified.
  53. macro(ksutil_add_submodule_directory PATH)
  54. set(oneValueArgs TAG RENAME)
  55. cmake_parse_arguments(OPTS "${options}" "${oneValueArgs}" "" ${ARGN})
  56. set(__MOD_UPDATED 0)
  57. # See if the dir is empty or not
  58. file(GLOB RESULT ${PATH}/*)
  59. list(LENGTH RESULT RES_LEN)
  60. if(RES_LEN EQUAL 0)
  61. # Empty so update it
  62. message("Adding submodule dir ${CMAKE_CURRENT_LIST_DIR}/${PATH}")
  63. execute_process(COMMAND git submodule update --init ${PATH} WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR} RESULT_VARIABLE CLONE_RESULT)
  64. if (CLONE_RESULS GREATER 0)
  65. message("Failed to update submodule path ${PATH}" FATAL)
  66. endif()
  67. set(__MOD_UPDATED 1)
  68. endif()
  69. # And check out the right tag
  70. if (OPTS_TAG AND ${__MOD_UPDATED} EQUAL 1)
  71. message("Checking out tag ${OPTS_TAG} for submodule dir ${CMAKE_CURRENT_LIST_DIR}/${PATH}")
  72. execute_process(COMMAND git checkout ${OPTS_TAG} WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/${PATH} RESULT_VARIABLE CHECKOUT_RESULT)
  73. if (CLONE_RESULS GREATER 0)
  74. message("Failed to checkout ${OPTS_TAG} for submodule ${PATH}" FATAL)
  75. endif()
  76. endif()
  77. message("Adding subdirectory ${PATH}")
  78. add_subdirectory(${PATH})
  79. endmacro()
  80. # This sets up the core build config and platform definitions in the cmake global scope
  81. # used everywhere to setup basic setup for all signalwire native projects.
  82. macro(ksutil_setup_platform)
  83. if (APPLE)
  84. message("Platform is mac")
  85. set(KS_PLAT_MAC 1 CACHE INTERNAL "Platform definition" FORCE)
  86. add_compile_options("$<$<CONFIG:Release>:-O2>")
  87. add_compile_options("$<$<CONFIG:Release>:-g>")
  88. add_compile_options("$<$<CONFIG:Release>:-Wno-parentheses>")
  89. add_compile_options("$<$<CONFIG:Release>:-Wno-pointer-sign>")
  90. add_compile_options("$<$<CONFIG:Release>:-Wno-switch>")
  91. add_compile_options("$<$<CONFIG:Debug>:-O0>")
  92. add_compile_options("$<$<CONFIG:Debug>:-g>")
  93. add_compile_options("$<$<CONFIG:Debug>:-DKS_BUILD_DEBUG=1>")
  94. add_compile_options("$<$<CONFIG:Debug>:-Wno-parentheses>")
  95. add_compile_options("$<$<CONFIG:Debug>:-Wno-pointer-sign>")
  96. add_compile_options("$<$<CONFIG:Debug>:-Wno-switch>")
  97. set(CMAKE_POSITION_INDEPENDENT_CODE YES)
  98. add_definitions("-DKS_PLAT_MAC=1")
  99. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
  100. elseif (WIN32)
  101. message("Platform is windows")
  102. set(KS_PLAT_WIN 1 CACHE INTERNAL "Platform definition" FORCE)
  103. add_definitions("-D_WIN32_WINNT=0x0600")
  104. add_definitions("-D_WINSOCK_DEPRECATED_NO_WARNINGS=1")
  105. add_definitions("-DWIN32_LEAN_AND_MEAN=1")
  106. add_definitions("-DKS_PLAT_WIN=1")
  107. add_definitions("-DNOMAXMIN=1")
  108. add_definitions("-D_CRT_SECURE_NO_WARNINGS=1")
  109. add_definitions("/bigobj")
  110. add_compile_options("$<$<CONFIG:Debug>:-DKS_BUILD_DEBUG=1>")
  111. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
  112. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
  113. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
  114. else()
  115. message("Platform is linux")
  116. set(KS_PLAT_LIN 1 CACHE INTERNAL "Platform definition" FORCE)
  117. set(CMAKE_POSITION_INDEPENDENT_CODE YES)
  118. add_compile_options("$<$<CONFIG:Release>:-O2>")
  119. add_compile_options("$<$<CONFIG:Release>:-g>")
  120. add_compile_options("$<$<CONFIG:Debug>:-O0>")
  121. add_compile_options("$<$<CONFIG:Debug>:-g>")
  122. add_compile_options("$<$<CONFIG:Debug>:-DKS_BUILD_DEBUG=1>")
  123. add_compile_options("$<$<CONFIG:Sanitize>:-O0>")
  124. add_compile_options("$<$<CONFIG:Sanitize>:-g>")
  125. add_compile_options("$<$<CONFIG:Sanitize>:-fsanitize=address>")
  126. add_compile_options("$<$<CONFIG:Sanitize>:-DKS_BUILD_DEBUG=1>")
  127. add_definitions("-DKS_PLAT_LIN=1")
  128. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")
  129. # Select a default install prefix of /usr
  130. if (NOT CMAKE_INSTALL_PREFIX)
  131. set(CMAKE_INSTALL_PREFIX "/usr" CACHE INTERNAL "Prefix prepended to install directories" FORCE)
  132. set(CMAKE_PREFIX_PATH "/usr" CACHE INTERNAL "Prefix search path" FORCE)
  133. endif()
  134. endif()
  135. # Default to debug if not specified
  136. if (NOT CMAKE_BUILD_TYPE)
  137. set(CMAKE_BUILD_TYPE "Debug" CACHE INTERNAL "Build config setting" FORCE)
  138. endif()
  139. message("Build type: ${CMAKE_BUILD_TYPE} CXX Flags: ${CMAKE_CXX_FLAGS_SANITIZE}")
  140. message("Install prefix: ${CMAKE_INSTALL_PREFIX}")
  141. endmacro()