You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
7.5 KiB

3 months ago
  1. # File: macros.cmake
  2. # Authors: Igor N. Bongartz
  3. # Erstellt: 2015-06-11
  4. # Version: 2015-06-11
  5. #
  6. # This file contains several macros which are used in this project. Notice that several are copied straight from web ressources.
  7. function(set_version major minor)
  8. execute_process(
  9. COMMAND git describe
  10. WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
  11. OUTPUT_VARIABLE GIT_VERSION
  12. OUTPUT_STRIP_TRAILING_WHITESPACE
  13. )
  14. set(patch "")
  15. if (GIT_VERSION MATCHES "([0-9]+)\.([0-9]+)(-?.*)")
  16. set(major ${CMAKE_MATCH_1})
  17. set(minor ${CMAKE_MATCH_2})
  18. if (CMAKE_MATCH_3 MATCHES "-([0-9]+)-(g[0-9a-f]+)")
  19. set(patch_full "${CMAKE_MATCH_1}-${CMAKE_MATCH_2}")
  20. set(patch "${CMAKE_MATCH_1}")
  21. endif()
  22. else()
  23. message(STATUS "Could not parse version from git, using default ${major}.${minor}")
  24. endif()
  25. set(PROJECT_VERSION_MAJOR ${major} PARENT_SCOPE)
  26. set(PROJECT_VERSION_MINOR ${minor} PARENT_SCOPE)
  27. set(PROJECT_VERSION_PATCH ${patch} PARENT_SCOPE)
  28. if(patch)
  29. set(PROJECT_VERSION_FULL "${major}.${minor}.${patch_full}" PARENT_SCOPE)
  30. set(PROJECT_VERSION "${major}.${minor}.${patch}" PARENT_SCOPE)
  31. else()
  32. set(PROJECT_VERSION_FULL "${major}.${minor}" PARENT_SCOPE)
  33. set(PROJECT_VERSION "${major}.${minor}" PARENT_SCOPE)
  34. endif()
  35. endfunction(set_version)
  36. function(add_imported_library_interface name include)
  37. add_library(${name} INTERFACE IMPORTED)
  38. set_target_properties(${name} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${include}")
  39. endfunction(add_imported_library_interface)
  40. function(add_imported_library name type lib include)
  41. # Workaround from https://cmake.org/Bug/view.php?id=15052
  42. file(MAKE_DIRECTORY "${include}")
  43. if("${lib}" STREQUAL "")
  44. if("${type}" STREQUAL "SHARED")
  45. add_library(${name} INTERFACE IMPORTED)
  46. set_target_properties(${name} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${include}")
  47. endif()
  48. else()
  49. set(lib_list "${lib}")
  50. list(LENGTH lib_list NumFiles)
  51. if(NumFiles GREATER 1)
  52. add_library(${name}_${type} INTERFACE IMPORTED GLOBAL)
  53. set(shortnames "${ARGN}")
  54. set(libs "")
  55. math(EXPR range "${NumFiles}-1")
  56. foreach(index RANGE ${range})
  57. list(GET lib_list ${index} l)
  58. list(GET shortnames ${index} shortname)
  59. add_imported_library("${name}_${shortname}" ${type} ${l} ${include})
  60. list(APPEND libs "${name}_${shortname}_${type}")
  61. # only from cmake 3.3 https://github.com/ceph/ceph/pull/7128
  62. #add_dependencies(${name}_${type} ${name}_${shortname}_${type})
  63. endforeach()
  64. set_target_properties(${name}_${type} PROPERTIES INTERFACE_LINK_LIBRARIES "${libs}")
  65. set_target_properties(${name}_${type} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${include}")
  66. else()
  67. add_library(${name}_${type} ${type} IMPORTED GLOBAL)
  68. set_target_properties(${name}_${type} PROPERTIES IMPORTED_LOCATION "${lib}")
  69. set_target_properties(${name}_${type} PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${include}")
  70. if(WIN32)
  71. string(REPLACE "dll" "lib" IMPLIB ${lib})
  72. set_target_properties(${name}_${type} PROPERTIES IMPORTED_IMPLIB "${IMPLIB}")
  73. endif()
  74. endif()
  75. endif()
  76. endfunction(add_imported_library)
  77. function(export_option name)
  78. list(APPEND EXPORTED_OPTIONS "${name}")
  79. set(EXPORTED_OPTIONS "${EXPORTED_OPTIONS}" PARENT_SCOPE)
  80. endfunction(export_option)
  81. function(_export_target_add_exec output TARGET)
  82. set(${output} "${${output}}
  83. add_executable(${TARGET} IMPORTED)" PARENT_SCOPE)
  84. endfunction(_export_target_add_exec)
  85. function(_export_target_add_lib output TARGET TYPE)
  86. set(${output} "${${output}}
  87. add_library(${TARGET} ${TYPE} IMPORTED)" PARENT_SCOPE)
  88. endfunction(_export_target_add_lib)
  89. function(_export_target_start output TARGET)
  90. set(${output} "${${output}}
  91. if(NOT TARGET ${TARGET})" PARENT_SCOPE)
  92. endfunction(_export_target_start)
  93. function(_export_target_end output)
  94. set(${output} "${${output}}
  95. endif()" PARENT_SCOPE)
  96. endfunction(_export_target_end)
  97. macro(_export_target_set_prop output TARGET REQUIRED PROPERTY)
  98. get_target_property(VALUE ${TARGET} ${PROPERTY})
  99. set(extra_args ${ARGN})
  100. list(GET extra_args 0 NEWPROPERTY)
  101. if(NOT NEWPROPERTY)
  102. set(NEWPROPERTY ${PROPERTY})
  103. endif()
  104. if(VALUE)
  105. set(${output} "${${output}}
  106. set_target_properties(${TARGET} PROPERTIES ${NEWPROPERTY} \"${VALUE}\")")
  107. elseif(${REQUIRED})
  108. message(STATUS "Did not find ${PROPERTY} of ${TARGET} for export.")
  109. endif()
  110. endmacro(_export_target_set_prop)
  111. macro(export_target output target)
  112. if(TARGET ${target})
  113. get_target_property(TYPE ${target} TYPE)
  114. _export_target_start(${output} ${target})
  115. if(TYPE STREQUAL "EXECUTABLE")
  116. _export_target_add_exec(${output} ${target})
  117. _export_target_set_prop(${output} ${target} TRUE IMPORTED_LOCATION)
  118. elseif(TYPE STREQUAL "SHARED_LIBRARY")
  119. _export_target_add_lib(${output} ${target} SHARED)
  120. _export_target_set_prop(${output} ${target} TRUE IMPORTED_LOCATION)
  121. _export_target_set_prop(${output} ${target} FALSE INTERFACE_INCLUDE_DIRECTORIES)
  122. _export_target_set_prop(${output} ${target} FALSE LINK_INTERFACE_LIBRARIES)
  123. elseif(TYPE STREQUAL "STATIC_LIBRARY")
  124. _export_target_add_lib(${output} ${target} STATIC)
  125. _export_target_set_prop(${output} ${target} TRUE IMPORTED_LOCATION)
  126. _export_target_set_prop(${output} ${target} FALSE INTERFACE_INCLUDE_DIRECTORIES)
  127. _export_target_set_prop(${output} ${target} FALSE LINK_INTERFACE_LIBRARIES IMPORTED_LINK_INTERFACE_LIBRARIES)
  128. elseif(TYPE STREQUAL "INTERFACE_LIBRARY")
  129. _export_target_add_lib(${output} ${target} INTERFACE)
  130. _export_target_set_prop(${output} ${target} TRUE INTERFACE_INCLUDE_DIRECTORIES)
  131. _export_target_set_prop(${output} ${target} FALSE INTERFACE_LINK_LIBRARIES)
  132. else()
  133. message(STATUS "Unknown type ${TYPE}")
  134. endif()
  135. if(NOT "${ARGN}" STREQUAL "")
  136. set(${output} "${${output}}
  137. set_target_properties(${target} PROPERTIES IMPORTED_LINK_INTERFACE_LIBRARIES \"${ARGN}\")")
  138. endif()
  139. _export_target_end(${output})
  140. endif()
  141. endmacro(export_target)
  142. macro(export_target_recursive output target RECURSE_ON)
  143. export_target(${output} ${target})
  144. get_target_property(deps ${target} ${RECURSE_ON})
  145. foreach(d ${deps})
  146. export_target(${output} ${d})
  147. endforeach()
  148. endmacro(export_target_recursive)
  149. macro(load_library group name version)
  150. string(TOUPPER ${name} LIBNAME)
  151. set(CMAKE_FIND_LIBRARY_SUFFIXES "${DYNAMIC_EXT};${STATIC_EXT}")
  152. set(Boost_USE_STATIC_LIBS OFF)
  153. find_package(${name} ${version} ${ARGN} QUIET)
  154. if(${name}_FOUND OR ${LIBNAME}_FOUND)
  155. if (${name}_FOUND)
  156. set(LIBNAME ${name})
  157. endif()
  158. if(LIBNAME MATCHES "Boost")
  159. add_imported_library(${LIBNAME} SHARED "${${LIBNAME}_LIBRARIES}" "${${LIBNAME}_INCLUDE_DIR}" "${BOOST_COMPONENTS}")
  160. else()
  161. add_imported_library(${LIBNAME} SHARED "${${LIBNAME}_LIBRARY}" "${${LIBNAME}_INCLUDE_DIR}")
  162. endif()
  163. unset(${LIBNAME}_FOUND CACHE)
  164. unset(${LIBNAME}_INCLUDE_DIR CACHE)
  165. unset(${LIBNAME}_LIBRARY CACHE)
  166. set(CMAKE_FIND_LIBRARY_SUFFIXES "${STATIC_EXT};${DYNAMIC_EXT}")
  167. set(Boost_USE_STATIC_LIBS ON)
  168. if (ARGN)
  169. list(REMOVE_ITEM ARGN "REQUIRED")
  170. endif()
  171. find_package(${name} ${version} ${ARGN} QUIET)
  172. if(${LIBNAME}_FOUND)
  173. if(LIBNAME MATCHES "Boost")
  174. add_imported_library(${LIBNAME} STATIC "${${LIBNAME}_LIBRARIES}" "${${LIBNAME}_INCLUDE_DIR}" "${BOOST_COMPONENTS}")
  175. else()
  176. add_imported_library(${LIBNAME} STATIC "${${LIBNAME}_LIBRARY}" "${${LIBNAME}_INCLUDE_DIR}")
  177. endif()
  178. endif()
  179. unset(${LIBNAME}_LIBRARY CACHE)
  180. endif()
  181. endmacro(load_library)
  182. macro(get_include_dir var name)
  183. get_target_property(INCLUDE_DIR ${name} INTERFACE_INCLUDE_DIRECTORIES)
  184. set(${var} ${${var}}:${INCLUDE_DIR})
  185. endmacro(get_include_dir)