utils.cmake 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #############################################################
  2. function(get_resources_source_list target var)
  3. get_property(RESOURCE_LIST GLOBAL PROPERTY _${target}_RESOURCE_LIST)
  4. foreach(RF ${RESOURCE_LIST})
  5. string(REPLACE "|" ";" PARTS "${RF}")
  6. list(GET PARTS 0 SOURCE_FILE)
  7. list(APPEND _SF ${SOURCE_FILE})
  8. endforeach()
  9. set(${var} ${_SF} PARENT_SCOPE)
  10. endfunction()
  11. #############################################################
  12. function(copy_resources target)
  13. if(XCODE)
  14. return()
  15. endif()
  16. get_property(RESOURCE_LIST GLOBAL PROPERTY _${target}_RESOURCE_LIST)
  17. # we read the LOCATION from the target instead of using a generator
  18. # here since add_custom_command doesn't support generator expresessions
  19. # in the output field, and this is still cleaner than hardcoding the path
  20. # of the output binary.
  21. #
  22. get_property(TARGET_LOC TARGET ${target} PROPERTY LOCATION)
  23. get_filename_component(TARGET_DIR ${TARGET_LOC} DIRECTORY)
  24. if(APPLE)
  25. set(TARGET_LOC ${TARGET_DIR}/..)
  26. else()
  27. set(TARGET_LOC ${TARGET_DIR})
  28. endif()
  29. if(RESOURCE_LIST)
  30. foreach(RF ${RESOURCE_LIST})
  31. string(REPLACE "|" ";" PARTS "${RF}")
  32. list(GET PARTS 0 SOURCE_FILE)
  33. list(GET PARTS 1 _TARGET_FILE)
  34. set(TARGET_FILE ${TARGET_LOC}/${_TARGET_FILE})
  35. add_custom_command(OUTPUT ${TARGET_FILE}
  36. COMMAND ${CMAKE_COMMAND} -E copy "${SOURCE_FILE}" "${TARGET_FILE}"
  37. DEPENDS ${SOURCE_FILE}
  38. COMMENT "CopyResource (${target}): ${TARGET_FILE}")
  39. list(APPEND RESOURCES ${TARGET_FILE})
  40. endforeach()
  41. add_custom_target(${target}_CopyResources DEPENDS ${RESOURCES})
  42. add_dependencies(${target} ${target}_CopyResources)
  43. endif(RESOURCE_LIST)
  44. endfunction()
  45. #############################################################
  46. function(add_resources)
  47. set(args1 TARGET)
  48. set(args2 SOURCES DEST EXCLUDE)
  49. cmake_parse_arguments(BD "" "${args1}" "${args2}" ${ARGN})
  50. foreach(_BDFILE ${BD_SOURCES})
  51. if(IS_DIRECTORY ${_BDFILE})
  52. file(GLOB _DIRCONTENTS ${_BDFILE}/*)
  53. foreach(_BDDFILE ${_DIRCONTENTS})
  54. get_filename_component(_BDFILE_NAME ${_BDDFILE} NAME)
  55. set(PROCESS_FILE 1)
  56. foreach(EX_FILE ${BD_EXCLUDE})
  57. string(REGEX MATCH ${EX_FILE} DID_MATCH ${_BDDFILE})
  58. if(NOT "${DID_MATCH}" STREQUAL "")
  59. set(PROCESS_FILE 0)
  60. endif(NOT "${DID_MATCH}" STREQUAL "")
  61. endforeach(EX_FILE ${BD_EXCLUDE})
  62. if(PROCESS_FILE STREQUAL "1")
  63. if(IS_DIRECTORY ${_BDDFILE})
  64. set(DEST ${BD_DEST}/${_BDFILE_NAME})
  65. else()
  66. set(DEST ${BD_DEST})
  67. endif()
  68. add_resources(SOURCES ${_BDDFILE} DEST ${DEST} EXCLUDE ${BD_EXCLUDE} TARGET ${BD_TARGET})
  69. endif()
  70. endforeach()
  71. else()
  72. get_filename_component(_BDFILE_NAME ${_BDFILE} NAME)
  73. set_property(GLOBAL APPEND PROPERTY _${BD_TARGET}_RESOURCE_LIST "${_BDFILE}|${BD_DEST}/${_BDFILE_NAME}")
  74. if(XCODE)
  75. set_source_files_properties(${_BDFILE} PROPERTIES MACOSX_PACKAGE_LOCATION ${BD_DEST})
  76. endif()
  77. endif()
  78. endforeach()
  79. endfunction()
  80. #############################################################
  81. macro(find_all_sources DIRECTORY VARIABLE)
  82. aux_source_directory(${DIRECTORY} ${VARIABLE})
  83. file(GLOB headers ${DIRECTORY}/*h)
  84. list(APPEND ${VARIABLE} ${headers})
  85. endmacro()
  86. #############################################################
  87. # function to collect all the sources from sub-directories
  88. # into a single list
  89. function(add_sources)
  90. get_property(is_defined GLOBAL PROPERTY SRCS_LIST DEFINED)
  91. if(NOT is_defined)
  92. define_property(GLOBAL PROPERTY SRCS_LIST
  93. BRIEF_DOCS "List of source files"
  94. FULL_DOCS "List of source files to be compiled in one library")
  95. endif()
  96. # make absolute paths
  97. set(SRCS)
  98. foreach(s IN LISTS ARGN)
  99. if(NOT IS_ABSOLUTE "${s}")
  100. get_filename_component(s "${s}" ABSOLUTE)
  101. endif()
  102. list(APPEND SRCS "${s}")
  103. endforeach()
  104. string(REPLACE ${CMAKE_SOURCE_DIR}/src/ "" SUBDIR ${CMAKE_CURRENT_SOURCE_DIR})
  105. string(TOLOWER ${SUBDIR} SUBDIR)
  106. string(REPLACE "/" "\\\\" LIBNAME ${SUBDIR})
  107. source_group(${LIBNAME} FILES ${SRCS})
  108. # add it to the global list.
  109. set_property(GLOBAL APPEND PROPERTY SRCS_LIST ${SRCS})
  110. endfunction(add_sources)
  111. ## ---------------------------------------------------------------------
  112. ##
  113. ## Copyright (C) 2012 - 2014 by the deal.II authors
  114. ##
  115. ## This file is part of the deal.II library.
  116. ##
  117. ## The deal.II library is free software; you can use it, redistribute
  118. ## it, and/or modify it under the terms of the GNU Lesser General
  119. ## Public License as published by the Free Software Foundation; either
  120. ## version 2.1 of the License, or (at your option) any later version.
  121. ## The full text of the license can be found in the file LICENSE at
  122. ## the top level of the deal.II distribution.
  123. ##
  124. ## ---------------------------------------------------------------------
  125. #
  126. # Tests whether the cxx compiler understands a flag.
  127. # If so, add it to 'variable'.
  128. #
  129. # Usage:
  130. # ENABLE_IF_SUPPORTED(variable flag)
  131. #
  132. include(CheckCXXCompilerFlag)
  133. MACRO(ENABLE_IF_SUPPORTED _variable _flag)
  134. #
  135. # Clang is too conservative when reporting unsupported compiler flags.
  136. # Therefore, we promote all warnings for an unsupported compiler flag to
  137. # actual errors with the -Werror switch:
  138. #
  139. IF(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  140. SET(_werror_string "-Werror ")
  141. ELSE()
  142. SET(_werror_string "")
  143. ENDIF()
  144. STRING(STRIP "${_flag}" _flag_stripped)
  145. SET(_flag_stripped_orig "${_flag_stripped}")
  146. #
  147. # Gcc does not emit a warning if testing -Wno-... flags which leads to
  148. # false positive detection. Unfortunately it later warns that an unknown
  149. # warning option is used if another warning is emitted in the same
  150. # compilation unit.
  151. # Therefore we invert the test for -Wno-... flags:
  152. #
  153. IF(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  154. STRING(REPLACE "-Wno-" "-W" _flag_stripped "${_flag_stripped}")
  155. ENDIF()
  156. IF(NOT "${_flag_stripped}" STREQUAL "")
  157. STRING(REGEX REPLACE "^-" "" _flag_name "${_flag_stripped}")
  158. STRING(REPLACE "," "" _flag_name "${_flag_name}")
  159. STRING(REPLACE "-" "_" _flag_name "${_flag_name}")
  160. STRING(REPLACE "+" "_" _flag_name "${_flag_name}")
  161. CHECK_CXX_COMPILER_FLAG(
  162. "${_werror_string}${_flag_stripped}"
  163. HAVE_FLAG_${_flag_name}
  164. )
  165. IF(HAVE_FLAG_${_flag_name})
  166. SET(${_variable} "${${_variable}} ${_flag_stripped_orig}")
  167. STRING(STRIP "${${_variable}}" ${_variable})
  168. ENDIF()
  169. ENDIF()
  170. ENDMACRO()
  171. #
  172. # Tests whether it is possible to compile and link a dummy program with a
  173. # given flag.
  174. # If so, add it to variable.
  175. #
  176. # Usage:
  177. # ENABLE_IF_LINKS(variable flag)
  178. #
  179. MACRO(ENABLE_IF_LINKS _variable _flag)
  180. STRING(STRIP "${_flag}" _flag_stripped)
  181. IF(NOT "${_flag_stripped}" STREQUAL "")
  182. STRING(REGEX REPLACE "^-" "" _flag_name "${_flag_stripped}")
  183. STRING(REPLACE "," "" _flag_name "${_flag_name}")
  184. STRING(REPLACE "-" "_" _flag_name "${_flag_name}")
  185. STRING(REPLACE "+" "_" _flag_name "${_flag_name}")
  186. SET(_backup ${CMAKE_REQUIRED_LIBRARIES})
  187. LIST(APPEND CMAKE_REQUIRED_LIBRARIES "${_flag_stripped}")
  188. CHECK_CXX_COMPILER_FLAG(
  189. ""
  190. HAVE_FLAG_${_flag_name}
  191. )
  192. SET(CMAKE_REQUIRED_LIBRARIES ${_backup})
  193. IF(HAVE_FLAG_${_flag_name})
  194. SET(${_variable} "${${_variable}} ${_flag_stripped}")
  195. STRING(STRIP "${${_variable}}" ${_variable})
  196. ENDIF()
  197. ENDIF()
  198. ENDMACRO()
  199. #############################################################
  200. function(std_target_properties target)
  201. set_target_properties(${target} PROPERTIES CXX_STANDARD 14 CXX_STANDARD_REQUIRED ON)
  202. endfunction()
  203. #############################################################
  204. function(safe_download URL)
  205. set(ARGS FILENAME SHA1)
  206. cmake_parse_arguments(SD "SHOW_PROGRESS" "${ARGS}" "" ${ARGN})
  207. if(NOT DEFINED SD_FILENAME)
  208. get_filename_component(SD_FILENAME ${CU_URL} NAME)
  209. endif(NOT DEFINED SD_FILENAME)
  210. if(EXISTS "${SD_FILENAME}")
  211. file(SHA1 "${SD_FILENAME}" CURRENT_SHA1)
  212. endif()
  213. if(NOT DEFINED SD_SHA1)
  214. set(SD_SHA1 ${CURRENT_SHA1})
  215. endif()
  216. if(SD_SHOW_PROGRESS)
  217. set(_SHOW_PROGRESS "SHOW_PROGRESS")
  218. endif()
  219. if(NOT CURRENT_SHA1 STREQUAL SD_SHA1)
  220. message(STATUS "Downloading ${URL} to ${SD_FILENAME}...")
  221. file(
  222. DOWNLOAD ${URL} ${SD_FILENAME}
  223. STATUS SD_STATUS
  224. ${_SHOW_PROGRESS}
  225. )
  226. list(GET SD_STATUS 0 SD_SUCCESS)
  227. if(NOT SD_SUCCESS EQUAL 0)
  228. list(GET SD_STATUS 1 SD_ERROR)
  229. file(REMOVE ${SD_FILENAME})
  230. if("${SD_ERROR}" STREQUAL "\"Unsupported protocol\"")
  231. message(FATAL_ERROR "Download failed and your cmake probably don't support SSL! Beware that cmake downloaded from cmake.org doesn't support SSL on all platforms, make sure to build it yourself.")
  232. endif()
  233. message(FATAL_ERROR "Failed to download: ${URL}: ${SD_ERROR}")
  234. endif()
  235. file(SHA1 "${SD_FILENAME}" NEW_SHA1)
  236. if(DEFINED SD_SHA1)
  237. if(NOT SD_SHA1 STREQUAL NEW_SHA1)
  238. file(REMOVE ${SD_FILENAME})
  239. message(FATAL_ERROR "Failed to verify SHA1 on ${SD_FILENAME}, expected '${SD_SHA1}' got '${NEW_SHA1}'")
  240. endif()
  241. endif()
  242. endif()
  243. endfunction()