AtomicCommon.cmake 11 KB

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