AtomicCommon.cmake 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #
  2. # Portions Copyright (c) 2008-2016 the Urho3D project.
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. # THE SOFTWARE.
  21. #
  22. include(CMakeParseArguments)
  23. # Source environment
  24. execute_process(COMMAND env OUTPUT_VARIABLE ENVIRONMENT)
  25. string(REGEX REPLACE "=[^\n]*\n?" ";" ENVIRONMENT "${ENVIRONMENT}")
  26. set(IMPORT_ATOMIC_VARIABLES_FROM_ENV LINUX APPLE IOS ANDROID WEB)
  27. foreach(key ${ENVIRONMENT})
  28. list (FIND IMPORT_ATOMIC_VARIABLES_FROM_ENV ${key} _index)
  29. if ("${key}" MATCHES "^(ATOMIC_|CMAKE_).+" OR ${_index} GREATER -1)
  30. if (NOT DEFINED ${key})
  31. set (${key} $ENV{${key}} CACHE STRING "" FORCE)
  32. endif ()
  33. endif ()
  34. endforeach()
  35. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DATOMIC_DEBUG")
  36. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DATOMIC_DEBUG")
  37. if (NOT DEFINED ATOMIC_64BIT)
  38. if (CMAKE_SIZEOF_VOID_P MATCHES 8)
  39. set(ATOMIC_PROJECT_ARCH "x86_64")
  40. set(ATOMIC_PROJECT_ARCH_SHORT "x64")
  41. set(ATOMIC_PROJECT_ARCH_BITS "64")
  42. set(ATOMIC_64BIT 1)
  43. else ()
  44. set(ATOMIC_PROJECT_ARCH "x86")
  45. set(ATOMIC_PROJECT_ARCH_SHORT "x86")
  46. set(ATOMIC_PROJECT_ARCH_BITS "32")
  47. set(ATOMIC_64BIT 0)
  48. endif ()
  49. endif ()
  50. # Macro for defining source files with optional arguments as follows:
  51. # GLOB_CPP_PATTERNS <list> - Use the provided globbing patterns for CPP_FILES instead of the default *.cpp
  52. # GLOB_H_PATTERNS <list> - Use the provided globbing patterns for H_FILES instead of the default *.h
  53. # EXCLUDE_PATTERNS <list> - Use the provided patterns for excluding matched source files
  54. # EXTRA_CPP_FILES <list> - Include the provided list of files into CPP_FILES result
  55. # EXTRA_H_FILES <list> - Include the provided list of files into H_FILES result
  56. # PCH <list> - Enable precompiled header support on the defined source files using the specified header file, the list is "<path/to/header> [C++|C]"
  57. # PARENT_SCOPE - Glob source files in current directory but set the result in parent-scope's variable ${DIR}_CPP_FILES and ${DIR}_H_FILES instead
  58. # RECURSE - Option to glob recursively
  59. # GROUP - Option to group source files based on its relative path to the corresponding parent directory (only works when PARENT_SCOPE option is not in use)
  60. macro(define_source_files)
  61. # Source files are defined by globbing source files in current source directory and also by including the extra source files if provided
  62. cmake_parse_arguments(ARG "PARENT_SCOPE;RECURSE;GROUP" "" "PCH;EXTRA_CPP_FILES;EXTRA_H_FILES;GLOB_CPP_PATTERNS;GLOB_H_PATTERNS;EXCLUDE_PATTERNS" ${ARGN})
  63. if (NOT ARG_GLOB_CPP_PATTERNS)
  64. set(ARG_GLOB_CPP_PATTERNS *.cpp) # Default glob pattern
  65. endif ()
  66. if (NOT ARG_GLOB_H_PATTERNS)
  67. set(ARG_GLOB_H_PATTERNS *.h)
  68. endif ()
  69. if (ARG_RECURSE)
  70. set(ARG_RECURSE _RECURSE)
  71. else ()
  72. unset(ARG_RECURSE)
  73. endif ()
  74. file(GLOB${ARG_RECURSE} CPP_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${ARG_GLOB_CPP_PATTERNS})
  75. file(GLOB${ARG_RECURSE} H_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${ARG_GLOB_H_PATTERNS})
  76. if (ARG_EXCLUDE_PATTERNS)
  77. set(CPP_FILES_WITH_SENTINEL ";${CPP_FILES};") # Stringify the lists
  78. set(H_FILES_WITH_SENTINEL ";${H_FILES};")
  79. foreach (PATTERN ${ARG_EXCLUDE_PATTERNS})
  80. foreach (LOOP RANGE 1)
  81. string(REGEX REPLACE ";${PATTERN};" ";;" CPP_FILES_WITH_SENTINEL "${CPP_FILES_WITH_SENTINEL}")
  82. string(REGEX REPLACE ";${PATTERN};" ";;" H_FILES_WITH_SENTINEL "${H_FILES_WITH_SENTINEL}")
  83. endforeach ()
  84. endforeach ()
  85. set(CPP_FILES ${CPP_FILES_WITH_SENTINEL}) # Convert strings back to lists, extra sentinels are harmless
  86. set(H_FILES ${H_FILES_WITH_SENTINEL})
  87. endif ()
  88. list(APPEND CPP_FILES ${ARG_EXTRA_CPP_FILES})
  89. list(APPEND H_FILES ${ARG_EXTRA_H_FILES})
  90. set(SOURCE_FILES ${CPP_FILES} ${H_FILES})
  91. # Optionally enable PCH
  92. if (ARG_PCH)
  93. enable_pch(${ARG_PCH})
  94. endif ()
  95. # Optionally accumulate source files at parent scope
  96. if (ARG_PARENT_SCOPE)
  97. get_filename_component(NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
  98. set(${NAME}_CPP_FILES ${CPP_FILES} PARENT_SCOPE)
  99. set(${NAME}_H_FILES ${H_FILES} PARENT_SCOPE)
  100. # Optionally put source files into further sub-group (only works when PARENT_SCOPE option is not in use)
  101. elseif (ARG_GROUP)
  102. foreach (CPP_FILE ${CPP_FILES})
  103. get_filename_component(PATH ${CPP_FILE} PATH)
  104. if (PATH)
  105. string(REPLACE / \\ PATH ${PATH})
  106. source_group("Source Files\\${PATH}" FILES ${CPP_FILE})
  107. endif ()
  108. endforeach ()
  109. foreach (H_FILE ${H_FILES})
  110. get_filename_component(PATH ${H_FILE} PATH)
  111. if (PATH)
  112. string(REPLACE / \\ PATH ${PATH})
  113. source_group("Header Files\\${PATH}" FILES ${H_FILE})
  114. endif ()
  115. endforeach ()
  116. endif ()
  117. endmacro()
  118. # Macro for setting up dependency lib for compilation and linking of a target
  119. macro(setup_target)
  120. # Include directories
  121. target_include_directories(${TARGET_NAME} SYSTEM BEFORE PUBLIC ${INCLUDE_DIRS})
  122. # Link libraries
  123. target_link_libraries(${TARGET_NAME} ${ABSOLUTE_PATH_LIBS} ${LIBS})
  124. # Enable PCH if requested
  125. if (${TARGET_NAME}_HEADER_PATHNAME)
  126. enable_pch(${${TARGET_NAME}_HEADER_PATHNAME})
  127. endif ()
  128. # Set additional linker dependencies (only work for Makefile-based generator according to CMake documentation)
  129. if (LINK_DEPENDS)
  130. string(REPLACE ";" "\;" LINK_DEPENDS "${LINK_DEPENDS}") # Stringify for string replacement
  131. list(APPEND TARGET_PROPERTIES LINK_DEPENDS "${LINK_DEPENDS}") # Stringify with semicolons already escaped
  132. unset(LINK_DEPENDS)
  133. endif ()
  134. if (TARGET_PROPERTIES)
  135. set_target_properties(${TARGET_NAME} PROPERTIES ${TARGET_PROPERTIES})
  136. unset(TARGET_PROPERTIES)
  137. endif ()
  138. endmacro()
  139. # Macro for checking the SOURCE_FILES variable is properly initialized
  140. macro(check_source_files)
  141. if (NOT SOURCE_FILES)
  142. message(FATAL_ERROR "Could not configure and generate the project file because no source files have been defined yet. "
  143. "You can define the source files explicitly by setting the SOURCE_FILES variable in your CMakeLists.txt; or "
  144. "by calling the define_source_files() macro which would by default glob all the C++ source files found in the same scope of "
  145. "CMakeLists.txt where the macro is being called and the macro would set the SOURCE_FILES variable automatically. "
  146. "If your source files are not located in the same directory as the CMakeLists.txt or your source files are "
  147. "more than just C++ language then you probably have to pass in extra arguments when calling the macro in order to make it works. "
  148. "See the define_source_files() macro definition in the CMake/Modules/Urho3D-CMake-common.cmake for more detail.")
  149. endif ()
  150. endmacro()
  151. # Macro for setting up a library target
  152. # Macro arguments:
  153. # NODEPS - setup library target without defining Urho3D dependency libraries (applicable for downstream projects)
  154. # STATIC/SHARED/MODULE/EXCLUDE_FROM_ALL - see CMake help on add_library() command
  155. # CMake variables:
  156. # SOURCE_FILES - list of source files
  157. # INCLUDE_DIRS - list of directories for include search path
  158. # LIBS - list of dependent libraries that are built internally in the project
  159. # ABSOLUTE_PATH_LIBS - list of dependent libraries that are external to the project
  160. # LINK_DEPENDS - list of additional files on which a target binary depends for linking (Makefile-based generator only)
  161. # TARGET_PROPERTIES - list of target properties
  162. macro(setup_library)
  163. cmake_parse_arguments(ARG NODEPS "" "" ${ARGN})
  164. check_source_files()
  165. add_library(${TARGET_NAME} ${ARG_UNPARSED_ARGUMENTS} ${SOURCE_FILES})
  166. get_target_property(LIB_TYPE ${TARGET_NAME} TYPE)
  167. setup_target()
  168. # Setup the compiler flags for building shared library
  169. if (LIB_TYPE STREQUAL SHARED_LIBRARY)
  170. # Hide the symbols that are not explicitly marked for export
  171. add_compiler_export_flags()
  172. endif ()
  173. endmacro()
  174. # Macro for setting up an executable target
  175. # Macro arguments:
  176. # PRIVATE - setup executable target without installing it
  177. # TOOL - setup a tool executable target
  178. # NODEPS - setup executable target without defining Urho3D dependency libraries
  179. # WIN32/MACOSX_BUNDLE/EXCLUDE_FROM_ALL - see CMake help on add_executable() command
  180. # CMake variables:
  181. # SOURCE_FILES - list of source files
  182. # INCLUDE_DIRS - list of directories for include search path
  183. # LIBS - list of dependent libraries that are built internally in the project
  184. # ABSOLUTE_PATH_LIBS - list of dependent libraries that are external to the project
  185. # LINK_DEPENDS - list of additional files on which a target binary depends for linking (Makefile-based generator only)
  186. # TARGET_PROPERTIES - list of target properties
  187. macro(setup_executable)
  188. cmake_parse_arguments(ARG "PRIVATE;TOOL;NODEPS" "" "" ${ARGN})
  189. check_source_files()
  190. add_executable(${TARGET_NAME} ${ARG_UNPARSED_ARGUMENTS} ${SOURCE_FILES})
  191. setup_target()
  192. if (ARG_TOOL)
  193. if (DEFINED ATOMIC_TOOL_DIR)
  194. set (TOOL_DIR ${ATOMIC_TOOL_DIR})
  195. else ()
  196. set (TOOL_DIR ${ATOMIC_SOURCE_DIR}/Artifacts/Build/${TARGET_NAME})
  197. endif ()
  198. set_property(TARGET ${TARGET_NAME} PROPERTY RUNTIME_OUTPUT_DIRECTORY ${TOOL_DIR})
  199. endif ()
  200. endmacro()
  201. # Macro for replacing substrings in every variable specified in the list.
  202. # Macro arguments:
  203. # substring - a value that is to be replaced.
  204. # replacement - a new value that will replace `substring`.
  205. # variable_list - a list of variables. If list is specified manually enclose it in quotes and separate items with semicolon.
  206. macro(replace_in_list substring replacement variable_list)
  207. foreach (single_variable ${variable_list})
  208. string(REPLACE "${substring}" "${replacement}" ${single_variable} "${${single_variable}}")
  209. endforeach ()
  210. endmacro()
  211. # Macro for setting msvc runtime flags globally.
  212. # Macro arguments:
  213. # runtime_flag - release build runtime flag, /MT or /MD. Debug flag will be deduced automatically by appending 'd'.
  214. macro(msvc_set_runtime runtime_flag)
  215. set(COMPILER_DEBUG_VARS "CMAKE_C_FLAGS_DEBUG;CMAKE_CXX_FLAGS_DEBUG")
  216. set(COMPILER_RELEASE_VARS "CMAKE_C_FLAGS_RELEASE;CMAKE_C_FLAGS_RELWITHDEBINFO;CMAKE_C_FLAGS_MINSIZEREL;CMAKE_CXX_FLAGS_RELEASE;CMAKE_CXX_FLAGS_RELWITHDEBINFO;CMAKE_CXX_FLAGS_MINSIZEREL")
  217. set(COMPILER_COMMON_VARS "CMAKE_C_FLAGS;CMAKE_CXX_FLAGS")
  218. set(COMPILER_RUNTIME_FLAGS "/MDd;/MD;/MTd;/MT")
  219. # Clear old runtime flags.
  220. foreach(flag ${COMPILER_RUNTIME_FLAGS})
  221. replace_in_list(${flag} "" "${COMPILER_DEBUG_VARS};${COMPILER_RELEASE_VARS};${COMPILER_COMMON_VARS}")
  222. endforeach()
  223. # Add release runtime flags.
  224. foreach(var ${COMPILER_RELEASE_VARS})
  225. set(${var} "${${var}} ${runtime_flag}")
  226. endforeach()
  227. # Add debug runtime flags.
  228. foreach(var ${COMPILER_DEBUG_VARS})
  229. set(${var} "${${var}} ${runtime_flag}d")
  230. endforeach()
  231. endmacro()
  232. # Macro deploys Qt dlls to folder where target executable is located.
  233. # Macro arguments:
  234. # target - name of target which links to Qt.
  235. macro(deploy_qt_dlls target)
  236. if (WIN32)
  237. if (TARGET ${target})
  238. # Find Qt dir
  239. foreach (dir ${CMAKE_PREFIX_PATH})
  240. if (EXISTS ${dir}/bin/windeployqt.exe)
  241. set(windeployqt "${dir}/bin/windeployqt.exe")
  242. break()
  243. endif ()
  244. endforeach ()
  245. if (windeployqt)
  246. add_custom_command(
  247. TARGET ${target}
  248. POST_BUILD
  249. COMMAND "${windeployqt}" --no-quick-import --no-translations --no-system-d3d-compiler --no-compiler-runtime --no-webkit2 --no-angle --no-opengl-sw $<TARGET_FILE_DIR:${target}>
  250. VERBATIM
  251. )
  252. endif ()
  253. endif ()
  254. endif ()
  255. endmacro()