godotcpp.cmake 14 KB

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