AtomicCommon.cmake 9.5 KB

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