AtomicCommon.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DATOMIC_DEBUG")
  24. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DATOMIC_DEBUG")
  25. if (CMAKE_SIZEOF_VOID_P MATCHES 8)
  26. set(ATOMIC_PROJECT_ARCH "x86_64")
  27. set(ATOMIC_PROJECT_ARCH_SHORT "x64")
  28. set(ATOMIC_PROJECT_ARCH_BITS "64")
  29. set(ATOMIC_64BIT 1)
  30. else ()
  31. set(ATOMIC_PROJECT_ARCH "x86")
  32. set(ATOMIC_PROJECT_ARCH_SHORT "x86")
  33. set(ATOMIC_PROJECT_ARCH_BITS "32")
  34. set(ATOMIC_64BIT 0)
  35. endif ()
  36. # Macro for defining source files with optional arguments as follows:
  37. # GLOB_CPP_PATTERNS <list> - Use the provided globbing patterns for CPP_FILES instead of the default *.cpp
  38. # GLOB_H_PATTERNS <list> - Use the provided globbing patterns for H_FILES instead of the default *.h
  39. # EXCLUDE_PATTERNS <list> - Use the provided patterns for excluding matched source files
  40. # EXTRA_CPP_FILES <list> - Include the provided list of files into CPP_FILES result
  41. # EXTRA_H_FILES <list> - Include the provided list of files into H_FILES result
  42. # PCH <list> - Enable precompiled header support on the defined source files using the specified header file, the list is "<path/to/header> [C++|C]"
  43. # 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
  44. # RECURSE - Option to glob recursively
  45. # 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)
  46. macro(define_source_files)
  47. # Source files are defined by globbing source files in current source directory and also by including the extra source files if provided
  48. cmake_parse_arguments(ARG "PARENT_SCOPE;RECURSE;GROUP" "" "PCH;EXTRA_CPP_FILES;EXTRA_H_FILES;GLOB_CPP_PATTERNS;GLOB_H_PATTERNS;EXCLUDE_PATTERNS" ${ARGN})
  49. if (NOT ARG_GLOB_CPP_PATTERNS)
  50. set(ARG_GLOB_CPP_PATTERNS *.cpp) # Default glob pattern
  51. endif ()
  52. if (NOT ARG_GLOB_H_PATTERNS)
  53. set(ARG_GLOB_H_PATTERNS *.h)
  54. endif ()
  55. if (ARG_RECURSE)
  56. set(ARG_RECURSE _RECURSE)
  57. else ()
  58. unset(ARG_RECURSE)
  59. endif ()
  60. file(GLOB${ARG_RECURSE} CPP_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${ARG_GLOB_CPP_PATTERNS})
  61. file(GLOB${ARG_RECURSE} H_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${ARG_GLOB_H_PATTERNS})
  62. if (ARG_EXCLUDE_PATTERNS)
  63. set(CPP_FILES_WITH_SENTINEL ";${CPP_FILES};") # Stringify the lists
  64. set(H_FILES_WITH_SENTINEL ";${H_FILES};")
  65. foreach (PATTERN ${ARG_EXCLUDE_PATTERNS})
  66. foreach (LOOP RANGE 1)
  67. string(REGEX REPLACE ";${PATTERN};" ";;" CPP_FILES_WITH_SENTINEL "${CPP_FILES_WITH_SENTINEL}")
  68. string(REGEX REPLACE ";${PATTERN};" ";;" H_FILES_WITH_SENTINEL "${H_FILES_WITH_SENTINEL}")
  69. endforeach ()
  70. endforeach ()
  71. set(CPP_FILES ${CPP_FILES_WITH_SENTINEL}) # Convert strings back to lists, extra sentinels are harmless
  72. set(H_FILES ${H_FILES_WITH_SENTINEL})
  73. endif ()
  74. list(APPEND CPP_FILES ${ARG_EXTRA_CPP_FILES})
  75. list(APPEND H_FILES ${ARG_EXTRA_H_FILES})
  76. set(SOURCE_FILES ${CPP_FILES} ${H_FILES})
  77. # Optionally enable PCH
  78. if (ARG_PCH)
  79. enable_pch(${ARG_PCH})
  80. endif ()
  81. # Optionally accumulate source files at parent scope
  82. if (ARG_PARENT_SCOPE)
  83. get_filename_component(NAME ${CMAKE_CURRENT_SOURCE_DIR} NAME)
  84. set(${NAME}_CPP_FILES ${CPP_FILES} PARENT_SCOPE)
  85. set(${NAME}_H_FILES ${H_FILES} PARENT_SCOPE)
  86. # Optionally put source files into further sub-group (only works when PARENT_SCOPE option is not in use)
  87. elseif (ARG_GROUP)
  88. foreach (CPP_FILE ${CPP_FILES})
  89. get_filename_component(PATH ${CPP_FILE} PATH)
  90. if (PATH)
  91. string(REPLACE / \\ PATH ${PATH})
  92. source_group("Source Files\\${PATH}" FILES ${CPP_FILE})
  93. endif ()
  94. endforeach ()
  95. foreach (H_FILE ${H_FILES})
  96. get_filename_component(PATH ${H_FILE} PATH)
  97. if (PATH)
  98. string(REPLACE / \\ PATH ${PATH})
  99. source_group("Header Files\\${PATH}" FILES ${H_FILE})
  100. endif ()
  101. endforeach ()
  102. endif ()
  103. endmacro()
  104. # Macro for setting up dependency lib for compilation and linking of a target
  105. macro(setup_target)
  106. # Include directories
  107. target_include_directories(${TARGET_NAME} SYSTEM BEFORE PUBLIC ${INCLUDE_DIRS})
  108. # Link libraries
  109. target_link_libraries(${TARGET_NAME} ${ABSOLUTE_PATH_LIBS} ${LIBS})
  110. # Enable PCH if requested
  111. if (${TARGET_NAME}_HEADER_PATHNAME)
  112. enable_pch(${${TARGET_NAME}_HEADER_PATHNAME})
  113. endif ()
  114. # Set additional linker dependencies (only work for Makefile-based generator according to CMake documentation)
  115. if (LINK_DEPENDS)
  116. string(REPLACE ";" "\;" LINK_DEPENDS "${LINK_DEPENDS}") # Stringify for string replacement
  117. list(APPEND TARGET_PROPERTIES LINK_DEPENDS "${LINK_DEPENDS}") # Stringify with semicolons already escaped
  118. unset(LINK_DEPENDS)
  119. endif ()
  120. if (TARGET_PROPERTIES)
  121. set_target_properties(${TARGET_NAME} PROPERTIES ${TARGET_PROPERTIES})
  122. unset(TARGET_PROPERTIES)
  123. endif ()
  124. endmacro()
  125. # Macro for checking the SOURCE_FILES variable is properly initialized
  126. macro(check_source_files)
  127. if (NOT SOURCE_FILES)
  128. message(FATAL_ERROR "Could not configure and generate the project file because no source files have been defined yet. "
  129. "You can define the source files explicitly by setting the SOURCE_FILES variable in your CMakeLists.txt; or "
  130. "by calling the define_source_files() macro which would by default glob all the C++ source files found in the same scope of "
  131. "CMakeLists.txt where the macro is being called and the macro would set the SOURCE_FILES variable automatically. "
  132. "If your source files are not located in the same directory as the CMakeLists.txt or your source files are "
  133. "more than just C++ language then you probably have to pass in extra arguments when calling the macro in order to make it works. "
  134. "See the define_source_files() macro definition in the CMake/Modules/Urho3D-CMake-common.cmake for more detail.")
  135. endif ()
  136. endmacro()
  137. # Macro for setting up a library target
  138. # Macro arguments:
  139. # NODEPS - setup library target without defining Urho3D dependency libraries (applicable for downstream projects)
  140. # STATIC/SHARED/MODULE/EXCLUDE_FROM_ALL - see CMake help on add_library() command
  141. # CMake variables:
  142. # SOURCE_FILES - list of source files
  143. # INCLUDE_DIRS - list of directories for include search path
  144. # LIBS - list of dependent libraries that are built internally in the project
  145. # ABSOLUTE_PATH_LIBS - list of dependent libraries that are external to the project
  146. # LINK_DEPENDS - list of additional files on which a target binary depends for linking (Makefile-based generator only)
  147. # TARGET_PROPERTIES - list of target properties
  148. macro(setup_library)
  149. cmake_parse_arguments(ARG NODEPS "" "" ${ARGN})
  150. check_source_files()
  151. add_library(${TARGET_NAME} ${ARG_UNPARSED_ARGUMENTS} ${SOURCE_FILES})
  152. get_target_property(LIB_TYPE ${TARGET_NAME} TYPE)
  153. setup_target()
  154. # Setup the compiler flags for building shared library
  155. if (LIB_TYPE STREQUAL SHARED_LIBRARY)
  156. # Hide the symbols that are not explicitly marked for export
  157. add_compiler_export_flags()
  158. endif ()
  159. endmacro()
  160. # Macro for setting up an executable target
  161. # Macro arguments:
  162. # PRIVATE - setup executable target without installing it
  163. # TOOL - setup a tool executable target
  164. # NODEPS - setup executable target without defining Urho3D dependency libraries
  165. # WIN32/MACOSX_BUNDLE/EXCLUDE_FROM_ALL - see CMake help on add_executable() command
  166. # CMake variables:
  167. # SOURCE_FILES - list of source files
  168. # INCLUDE_DIRS - list of directories for include search path
  169. # LIBS - list of dependent libraries that are built internally in the project
  170. # ABSOLUTE_PATH_LIBS - list of dependent libraries that are external to the project
  171. # LINK_DEPENDS - list of additional files on which a target binary depends for linking (Makefile-based generator only)
  172. # TARGET_PROPERTIES - list of target properties
  173. macro(setup_executable)
  174. cmake_parse_arguments(ARG "PRIVATE;TOOL;NODEPS" "" "" ${ARGN})
  175. check_source_files()
  176. add_executable(${TARGET_NAME} ${ARG_UNPARSED_ARGUMENTS} ${SOURCE_FILES})
  177. setup_target()
  178. endmacro()
  179. # Macro for replacing substrings in every variable specified in the list.
  180. # Macro arguments:
  181. # substring - a value that is to be replaced.
  182. # replacement - a new value that will replace `substring`.
  183. # variable_list - a list of variables. If list is specified manually enclose it in quotes and separate items with semicolon.
  184. macro(replace_in_list substring replacement variable_list)
  185. foreach (single_variable ${variable_list})
  186. string(REPLACE "${substring}" "${replacement}" ${single_variable} "${${single_variable}}")
  187. endforeach ()
  188. endmacro()
  189. # Macro for setting msvc runtime flags globally.
  190. # Macro arguments:
  191. # runtime_flag - release build runtime flag, /MT or /MD. Debug flag will be deduced automatically by appending 'd'.
  192. macro(msvc_set_runtime runtime_flag)
  193. set(COMPILER_DEBUG_VARS "CMAKE_C_FLAGS_DEBUG;CMAKE_CXX_FLAGS_DEBUG")
  194. 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")
  195. set(COMPILER_COMMON_VARS "CMAKE_C_FLAGS;CMAKE_CXX_FLAGS")
  196. set(COMPILER_RUNTIME_FLAGS "/MDd;/MD;/MTd;/MT")
  197. # Clear old runtime flags.
  198. foreach(flag ${COMPILER_RUNTIME_FLAGS})
  199. replace_in_list(${flag} "" "${COMPILER_DEBUG_VARS};${COMPILER_RELEASE_VARS};${COMPILER_COMMON_VARS}")
  200. endforeach()
  201. # Add release runtime flags.
  202. foreach(var ${COMPILER_RELEASE_VARS})
  203. set(${var} "${${var}} ${runtime_flag}")
  204. endforeach()
  205. # Add debug runtime flags.
  206. foreach(var ${COMPILER_DEBUG_VARS})
  207. set(${var} "${${var}} ${runtime_flag}d")
  208. endforeach()
  209. endmacro()