AtomicCommon.cmake 9.4 KB

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