AtomicCommon.cmake 9.3 KB

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