godotcpp.cmake 14 KB

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