godotcpp.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #[=======================================================================[.rst:
  2. godotcpp.cmake
  3. --------------
  4. As godot-cpp is a C++ project, there are no C files, and detection of a C
  5. compiler is unnecessary. When CMake performs the configure process, if a
  6. C compiler is specified, like in a toolchain, or from an IDE, then it will
  7. print a warning stating that the CMAKE_C_COMPILER compiler is unused.
  8. This if statement simply silences that warning.
  9. ]=======================================================================]
  10. if( CMAKE_C_COMPILER )
  11. endif ()
  12. #[=======================================================================[.rst:
  13. Include Platform Files
  14. ----------------------
  15. Because these files are included into the top level CMakelists.txt before the
  16. project directive, it means that
  17. * ``CMAKE_CURRENT_SOURCE_DIR`` is the location of godot-cpp's CMakeLists.txt
  18. * ``CMAKE_SOURCE_DIR`` is the location where any prior ``project(...)``
  19. directive was
  20. ]=======================================================================]
  21. include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/GodotCPPModule.cmake)
  22. include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/common_compiler_flags.cmake)
  23. include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/android.cmake)
  24. include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ios.cmake)
  25. include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/linux.cmake)
  26. include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/macos.cmake)
  27. include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/web.cmake)
  28. include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/windows.cmake)
  29. # Detect number of processors
  30. include(ProcessorCount)
  31. ProcessorCount(PROC_MAX)
  32. message( "Auto-detected ${PROC_MAX} CPU cores available for build parallelism." )
  33. # List of known platforms
  34. set( PLATFORM_LIST linux macos windows android ios web )
  35. # List of known architectures
  36. set( ARCH_LIST x86_32 x86_64 arm32 arm64 rv64 ppc32 ppc64 wasm32 )
  37. # Function to map processors to known architectures
  38. function( godot_arch_name OUTVAR )
  39. # Special case for macos universal builds that target both x86_64 and arm64
  40. if( DEFINED CMAKE_OSX_ARCHITECTURES)
  41. if( "x86_64" IN_LIST CMAKE_OSX_ARCHITECTURES AND "arm64" IN_LIST CMAKE_OSX_ARCHITECTURES)
  42. set(${OUTVAR} "universal" PARENT_SCOPE )
  43. return()
  44. endif()
  45. endif()
  46. # Direct match early out.
  47. string( TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" ARCH )
  48. if( ARCH IN_LIST ARCH_LIST )
  49. set( ${OUTVAR} "${ARCH}" PARENT_SCOPE)
  50. return()
  51. endif()
  52. # Known aliases
  53. set( x86_64 "w64;amd64;x86-64" )
  54. set( arm32 "armv7;armv7-a" )
  55. set( arm64 "armv8;arm64v8;aarch64;armv8-a" )
  56. set( rv64 "rv;riscv;riscv64" )
  57. set( ppc32 "ppcle;ppc" )
  58. set( ppc64 "ppc64le" )
  59. if( ARCH IN_LIST x86_64 )
  60. set(${OUTVAR} "x86_64" PARENT_SCOPE )
  61. elseif( ARCH IN_LIST arm32 )
  62. set(${OUTVAR} "arm32" PARENT_SCOPE )
  63. elseif( ARCH IN_LIST arm64 )
  64. set(${OUTVAR} "arm64" PARENT_SCOPE )
  65. elseif( ARCH IN_LIST rv64 )
  66. set(${OUTVAR} "rv64" PARENT_SCOPE )
  67. elseif( ARCH IN_LIST ppc32 )
  68. set(${OUTVAR} "ppc32" PARENT_SCOPE )
  69. elseif( ARCH IN_LIST ppc64 )
  70. set(${OUTVAR} "ppc64" PARENT_SCOPE )
  71. elseif( ARCH MATCHES "86")
  72. # Catches x86, i386, i486, i586, i686, etc.
  73. set(${OUTVAR} "x86_32" PARENT_SCOPE )
  74. else()
  75. # Default value is whatever the processor is.
  76. set(${OUTVAR} ${CMAKE_SYSTEM_PROCESSOR} PARENT_SCOPE )
  77. endif ()
  78. endfunction()
  79. # Function to define all the options.
  80. function( godotcpp_options )
  81. #NOTE: platform is managed using toolchain files.
  82. #NOTE: arch is managed by using toolchain files.
  83. # Except for macos universal, which can be set by GODOTCPP_MACOS_UNIVERSAL=YES
  84. # Input from user for GDExtension interface header and the API JSON file
  85. set( GODOTCPP_GDEXTENSION_DIR "gdextension" CACHE PATH
  86. "Path to a custom directory containing GDExtension interface header and API JSON file ( /path/to/gdextension_dir )" )
  87. set( GODOTCPP_CUSTOM_API_FILE "" CACHE FILEPATH
  88. "Path to a custom GDExtension API JSON file (takes precedence over `GODOTCPP_GDEXTENSION_DIR`) ( /path/to/custom_api_file )")
  89. #TODO generate_bindings
  90. option( GODOTCPP_GENERATE_TEMPLATE_GET_NODE
  91. "Generate a template version of the Node class's get_node. (ON|OFF)" ON)
  92. #TODO build_library
  93. set( GODOTCPP_PRECISION "single" CACHE STRING
  94. "Set the floating-point precision level (single|double)")
  95. set( GODOTCPP_THREADS ON CACHE BOOL "Enable threading support" )
  96. #TODO compiledb
  97. #TODO compiledb_file
  98. set( GODOTCPP_BUILD_PROFILE "" CACHE PATH
  99. "Path to a file containing a feature build profile" )
  100. set( GODOTCPP_USE_HOT_RELOAD "" CACHE BOOL
  101. "Enable the extra accounting required to support hot reload. (ON|OFF)")
  102. # Disable exception handling. Godot doesn't use exceptions anywhere, and this
  103. # saves around 20% of binary size and very significant build time (GH-80513).
  104. option( GODOTCPP_DISABLE_EXCEPTIONS "Force disabling exception handling code (ON|OFF)" ON )
  105. set( GODOTCPP_SYMBOL_VISIBILITY "hidden" CACHE STRING
  106. "Symbols visibility on GNU platforms. Use 'auto' to apply the default value. (auto|visible|hidden)")
  107. set_property( CACHE GODOTCPP_SYMBOL_VISIBILITY PROPERTY STRINGS "auto;visible;hidden" )
  108. #TODO optimize
  109. option( GODOTCPP_DEV_BUILD "Developer build with dev-only debugging code (DEV_ENABLED)" OFF )
  110. #[[ debug_symbols
  111. Debug symbols are enabled by using the Debug or RelWithDebInfo build configurations.
  112. Single Config Generator is set at configure time
  113. cmake ../ -DCMAKE_BUILD_TYPE=Debug
  114. Multi-Config Generator is set at build time
  115. cmake --build . --config Debug
  116. ]]
  117. # FIXME These options are not present in SCons, and perhaps should be added there.
  118. option( GODOTCPP_SYSTEM_HEADERS "Expose headers as SYSTEM." OFF )
  119. option( GODOTCPP_WARNING_AS_ERROR "Treat warnings as errors" OFF )
  120. # Enable Testing
  121. option( GODOTCPP_ENABLE_TESTING "Enable the godot-cpp.test.<target> integration testing targets" OFF )
  122. #[[ Target Platform Options ]]
  123. android_options()
  124. ios_options()
  125. linux_options()
  126. macos_options()
  127. web_options()
  128. windows_options()
  129. endfunction()
  130. # Function to configure and generate the targets
  131. function( godotcpp_generate )
  132. #[[ Multi-Threaded MSVC Compilation
  133. When using the MSVC compiler the build command -j <n> only specifies
  134. parallel jobs or targets, and not multi-threaded compilation To speed up
  135. compile times on msvc, the /MP <n> flag can be set. But we need to set it
  136. at configure time.
  137. MSVC is true when the compiler is some version of Microsoft Visual C++ or
  138. another compiler simulating the Visual C++ cl command-line syntax. ]]
  139. if( MSVC )
  140. math( EXPR PROC_N "(${PROC_MAX}-1) | (${X}-2)>>31 & 1" )
  141. message( "Using ${PROC_N} cores for multi-threaded compilation.")
  142. # TODO You can override it at configure time with ...." )
  143. else ()
  144. message( "Using ${CMAKE_BUILD_PARALLEL_LEVEL} cores, You can override"
  145. " it at configure time by using -j <n> or --parallel <n> on the build"
  146. " command.")
  147. message( " eg. cmake --build . -j 7 ...")
  148. endif ()
  149. #[[ GODOTCPP_SYMBOL_VISIBLITY
  150. To match the SCons options, the allowed values are "auto", "visible", and "hidden"
  151. This effects the compiler flag_ -fvisibility=[default|internal|hidden|protected]
  152. The corresponding target option CXX_VISIBILITY_PRESET accepts the compiler values.
  153. TODO: It is probably worth a pull request which changes both to use the compiler values
  154. .. _flag:https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fvisibility
  155. ]]
  156. if( ${GODOTCPP_SYMBOL_VISIBILITY} STREQUAL "auto" OR ${GODOTCPP_SYMBOL_VISIBILITY} STREQUAL "visible" )
  157. set( GODOTCPP_SYMBOL_VISIBILITY "default" )
  158. endif ()
  159. # Setup variable to optionally mark headers as SYSTEM
  160. set( GODOTCPP_SYSTEM_HEADERS_ATTRIBUTE "")
  161. if( GODOTCPP_SYSTEM_HEADERS)
  162. set( GODOTCPP_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
  163. endif ()
  164. #[[ Configure Binding Variables ]]
  165. # Generate Binding Parameters (True|False)
  166. set( USE_TEMPLATE_GET_NODE "False" )
  167. if( GODOTCPP_GENERATE_TEMPLATE_GET_NODE )
  168. set( USE_TEMPLATE_GET_NODE "True" )
  169. endif()
  170. # Bits (32|64)
  171. math( EXPR BITS "${CMAKE_SIZEOF_VOID_P} * 8" ) # CMAKE_SIZEOF_VOID_P refers to target architecture.
  172. # API json File
  173. set( GODOTCPP_GDEXTENSION_API_FILE "${GODOTCPP_GDEXTENSION_DIR}/extension_api.json")
  174. if( GODOTCPP_CUSTOM_API_FILE ) # User-defined override.
  175. set( GODOTCPP_GDEXTENSION_API_FILE "${GODOTCPP_CUSTOM_API_FILE}")
  176. endif()
  177. # Build Profile
  178. if( GODOTCPP_BUILD_PROFILE )
  179. message( STATUS "Using build profile to trim api file")
  180. message( "\tBUILD_PROFILE = '${GODOTCPP_BUILD_PROFILE}'")
  181. message( "\tAPI_SOURCE = '${GODOTCPP_GDEXTENSION_API_FILE}'")
  182. build_profile_generate_trimmed_api(
  183. "${GODOTCPP_BUILD_PROFILE}"
  184. "${GODOTCPP_GDEXTENSION_API_FILE}"
  185. "${CMAKE_CURRENT_BINARY_DIR}/extension_api.json" )
  186. set( GODOTCPP_GDEXTENSION_API_FILE "${CMAKE_CURRENT_BINARY_DIR}/extension_api.json" )
  187. endif()
  188. message( STATUS "GODOTCPP_GDEXTENSION_API_FILE = '${GODOTCPP_GDEXTENSION_API_FILE}'")
  189. # generate the file list to use
  190. binding_generator_get_file_list( GENERATED_FILES_LIST
  191. "${GODOTCPP_GDEXTENSION_API_FILE}"
  192. "${CMAKE_CURRENT_BINARY_DIR}" )
  193. binding_generator_generate_bindings(
  194. "${GODOTCPP_GDEXTENSION_API_FILE}"
  195. "${USE_TEMPLATE_GET_NODE}"
  196. "${BITS}"
  197. "${GODOTCPP_PRECISION}"
  198. "${CMAKE_CURRENT_BINARY_DIR}" )
  199. add_custom_target( godot-cpp.generate_bindings DEPENDS ${GENERATED_FILES_LIST} )
  200. set_target_properties( godot-cpp.generate_bindings PROPERTIES FOLDER "godot-cpp" )
  201. ### Platform is derived from the toolchain target
  202. # See GeneratorExpressions PLATFORM_ID and CMAKE_SYSTEM_NAME
  203. string( CONCAT SYSTEM_NAME
  204. "$<$<PLATFORM_ID:Android>:android.${ANDROID_ABI}>"
  205. "$<$<PLATFORM_ID:iOS>:ios>"
  206. "$<$<PLATFORM_ID:Linux>:linux>"
  207. "$<$<PLATFORM_ID:Darwin>:macos>"
  208. "$<$<PLATFORM_ID:Emscripten>:web>"
  209. "$<$<PLATFORM_ID:Windows>:windows>"
  210. "$<$<PLATFORM_ID:Msys>:windows>"
  211. )
  212. # Process CPU architecture argument.
  213. godot_arch_name( ARCH_NAME )
  214. # Transform options into generator expressions
  215. set( HOT_RELOAD-UNSET "$<STREQUAL:${GODOTCPP_USE_HOT_RELOAD},>")
  216. set( DISABLE_EXCEPTIONS "$<BOOL:${GODOTCPP_DISABLE_EXCEPTIONS}>")
  217. set( THREADS_ENABLED "$<BOOL:${GODOTCPP_THREADS}>" )
  218. # GODOTCPP_DEV_BUILD
  219. set( RELEASE_TYPES "Release;MinSizeRel")
  220. get_property( IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG )
  221. if( IS_MULTI_CONFIG )
  222. message( NOTICE "=> Default build type is Debug. For other build types add --config <type> to build command")
  223. elseif( GODOTCPP_DEV_BUILD AND CMAKE_BUILD_TYPE IN_LIST RELEASE_TYPES )
  224. message( WARNING "=> GODOTCPP_DEV_BUILD implies a Debug-like build but CMAKE_BUILD_TYPE is '${CMAKE_BUILD_TYPE}'")
  225. endif ()
  226. set( IS_DEV_BUILD "$<BOOL:${GODOTCPP_DEV_BUILD}>")
  227. ### Define our godot-cpp library targets
  228. foreach ( TARGET_ALIAS template_debug template_release editor )
  229. set( TARGET_NAME "godot-cpp.${TARGET_ALIAS}" )
  230. # Generator Expressions that rely on the target
  231. set( DEBUG_FEATURES "$<NOT:$<STREQUAL:${TARGET_ALIAS},template_release>>" )
  232. set( HOT_RELOAD "$<IF:${HOT_RELOAD-UNSET},${DEBUG_FEATURES},$<BOOL:${GODOTCPP_USE_HOT_RELOAD}>>" )
  233. # Suffix
  234. string( CONCAT GODOTCPP_SUFFIX
  235. "$<1:.${SYSTEM_NAME}>"
  236. "$<1:.${TARGET_ALIAS}>"
  237. "$<${IS_DEV_BUILD}:.dev>"
  238. "$<$<STREQUAL:${GODOTCPP_PRECISION},double>:.double>"
  239. "$<1:.${ARCH_NAME}>"
  240. # TODO IOS_SIMULATOR
  241. "$<$<NOT:${THREADS_ENABLED}>:.nothreads>"
  242. )
  243. # the godot-cpp.* library targets
  244. add_library( ${TARGET_NAME} STATIC EXCLUDE_FROM_ALL )
  245. add_library( godot-cpp::${TARGET_ALIAS} ALIAS ${TARGET_NAME} )
  246. file( GLOB_RECURSE GODOTCPP_SOURCES LIST_DIRECTORIES NO CONFIGURE_DEPENDS src/*.cpp )
  247. target_sources( ${TARGET_NAME}
  248. PRIVATE
  249. ${GODOTCPP_SOURCES}
  250. ${GENERATED_FILES_LIST}
  251. )
  252. target_include_directories( ${TARGET_NAME} ${GODOTCPP_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
  253. include
  254. ${CMAKE_CURRENT_BINARY_DIR}/gen/include
  255. ${GODOTCPP_GDEXTENSION_DIR}
  256. )
  257. set_target_properties( ${TARGET_NAME}
  258. PROPERTIES
  259. CXX_STANDARD 17
  260. CXX_EXTENSIONS OFF
  261. CXX_VISIBILITY_PRESET ${GODOTCPP_SYMBOL_VISIBILITY}
  262. COMPILE_WARNING_AS_ERROR ${GODOTCPP_WARNING_AS_ERROR}
  263. POSITION_INDEPENDENT_CODE ON
  264. BUILD_RPATH_USE_ORIGIN ON
  265. PREFIX "lib"
  266. OUTPUT_NAME "${PROJECT_NAME}${GODOTCPP_SUFFIX}"
  267. ARCHIVE_OUTPUT_DIRECTORY "$<1:${CMAKE_BINARY_DIR}/bin>"
  268. # Things that are handy to know for dependent targets
  269. GODOTCPP_PLATFORM "${SYSTEM_NAME}"
  270. GODOTCPP_TARGET "${TARGET_ALIAS}"
  271. GODOTCPP_ARCH "${ARCH_NAME}"
  272. GODOTCPP_PRECISION "${GODOTCPP_PRECISION}"
  273. GODOTCPP_SUFFIX "${GODOTCPP_SUFFIX}"
  274. # Some IDE's respect this property to logically group targets
  275. FOLDER "godot-cpp"
  276. )
  277. if( CMAKE_SYSTEM_NAME STREQUAL Android )
  278. android_generate()
  279. elseif ( CMAKE_SYSTEM_NAME STREQUAL iOS )
  280. ios_generate()
  281. elseif ( CMAKE_SYSTEM_NAME STREQUAL Linux )
  282. linux_generate()
  283. elseif ( CMAKE_SYSTEM_NAME STREQUAL Darwin )
  284. macos_generate()
  285. elseif ( CMAKE_SYSTEM_NAME STREQUAL Emscripten )
  286. web_generate()
  287. elseif ( CMAKE_SYSTEM_NAME STREQUAL Windows )
  288. windows_generate()
  289. endif ()
  290. endforeach ()
  291. # Added for backwards compatibility with prior cmake solution so that builds dont immediately break
  292. # from a missing target.
  293. add_library( godot::cpp ALIAS godot-cpp.template_debug )
  294. endfunction()