godotcpp.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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("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("Using ${PROC_N} cores for multi-threaded compilation.")
  166. # TODO You can override it at configure time with ...." )
  167. else()
  168. message(
  169. "Using ${CMAKE_BUILD_PARALLEL_LEVEL} cores, You can override"
  170. " it at configure time by using -j <n> or --parallel <n> on the build"
  171. " command."
  172. )
  173. message(" eg. cmake --build . -j 7 ...")
  174. endif()
  175. #[[ GODOTCPP_SYMBOL_VISIBLITY
  176. To match the SCons options, the allowed values are "auto", "visible", and "hidden"
  177. This effects the compiler flag_ -fvisibility=[default|internal|hidden|protected]
  178. The corresponding target option CXX_VISIBILITY_PRESET accepts the compiler values.
  179. TODO: It is probably worth a pull request which changes both to use the compiler values
  180. .. _flag:https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fvisibility
  181. ]]
  182. if(${GODOTCPP_SYMBOL_VISIBILITY} STREQUAL "auto" OR ${GODOTCPP_SYMBOL_VISIBILITY} STREQUAL "visible")
  183. set(GODOTCPP_SYMBOL_VISIBILITY "default")
  184. endif()
  185. # Setup variable to optionally mark headers as SYSTEM
  186. set(GODOTCPP_SYSTEM_HEADERS_ATTRIBUTE "")
  187. if(GODOTCPP_SYSTEM_HEADERS)
  188. set(GODOTCPP_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
  189. endif()
  190. #[[ Configure Binding Variables ]]
  191. # Generate Binding Parameters (True|False)
  192. set(USE_TEMPLATE_GET_NODE "False")
  193. if(GODOTCPP_GENERATE_TEMPLATE_GET_NODE)
  194. set(USE_TEMPLATE_GET_NODE "True")
  195. endif()
  196. # Bits (32|64)
  197. math(EXPR BITS "${CMAKE_SIZEOF_VOID_P} * 8") # CMAKE_SIZEOF_VOID_P refers to target architecture.
  198. # API json File
  199. set(GODOTCPP_GDEXTENSION_API_FILE "${GODOTCPP_GDEXTENSION_DIR}/extension_api.json")
  200. if(GODOTCPP_CUSTOM_API_FILE) # User-defined override.
  201. set(GODOTCPP_GDEXTENSION_API_FILE "${GODOTCPP_CUSTOM_API_FILE}")
  202. endif()
  203. # Build Profile
  204. if(GODOTCPP_BUILD_PROFILE)
  205. message(STATUS "Using build profile to trim api file")
  206. message("\tBUILD_PROFILE = '${GODOTCPP_BUILD_PROFILE}'")
  207. message("\tAPI_SOURCE = '${GODOTCPP_GDEXTENSION_API_FILE}'")
  208. build_profile_generate_trimmed_api(
  209. "${GODOTCPP_BUILD_PROFILE}"
  210. "${GODOTCPP_GDEXTENSION_API_FILE}"
  211. "${CMAKE_CURRENT_BINARY_DIR}/extension_api.json"
  212. )
  213. set(GODOTCPP_GDEXTENSION_API_FILE "${CMAKE_CURRENT_BINARY_DIR}/extension_api.json")
  214. endif()
  215. message(STATUS "GODOTCPP_GDEXTENSION_API_FILE = '${GODOTCPP_GDEXTENSION_API_FILE}'")
  216. # generate the file list to use
  217. binding_generator_get_file_list( GENERATED_FILES_LIST
  218. "${GODOTCPP_GDEXTENSION_API_FILE}"
  219. "${CMAKE_CURRENT_BINARY_DIR}"
  220. )
  221. binding_generator_generate_bindings(
  222. "${GODOTCPP_GDEXTENSION_API_FILE}"
  223. "${USE_TEMPLATE_GET_NODE}"
  224. "${BITS}"
  225. "${GODOTCPP_PRECISION}"
  226. "${CMAKE_CURRENT_BINARY_DIR}"
  227. )
  228. ### Platform is derived from the toolchain target
  229. # See GeneratorExpressions PLATFORM_ID and CMAKE_SYSTEM_NAME
  230. string(
  231. CONCAT
  232. SYSTEM_NAME
  233. "$<$<PLATFORM_ID:Android>:android.${ANDROID_ABI}>"
  234. "$<$<PLATFORM_ID:iOS>:ios>"
  235. "$<$<PLATFORM_ID:Linux>:linux>"
  236. "$<$<PLATFORM_ID:Darwin>:macos>"
  237. "$<$<PLATFORM_ID:Emscripten>:web>"
  238. "$<$<PLATFORM_ID:Windows>:windows>"
  239. "$<$<PLATFORM_ID:Msys>:windows>"
  240. )
  241. # Process CPU architecture argument.
  242. godot_arch_name( ARCH_NAME )
  243. # Transform options into generator expressions
  244. set(HOT_RELOAD-UNSET "$<STREQUAL:${GODOTCPP_USE_HOT_RELOAD},>")
  245. set(DISABLE_EXCEPTIONS "$<BOOL:${GODOTCPP_DISABLE_EXCEPTIONS}>")
  246. set(THREADS_ENABLED "$<BOOL:${GODOTCPP_THREADS}>")
  247. # GODOTCPP_DEV_BUILD
  248. set(RELEASE_TYPES "Release;MinSizeRel")
  249. get_property(IS_MULTI_CONFIG GLOBAL PROPERTY GENERATOR_IS_MULTI_CONFIG)
  250. if(IS_MULTI_CONFIG)
  251. message(NOTICE "=> Default build type is Debug. For other build types add --config <type> to build command")
  252. elseif(GODOTCPP_DEV_BUILD AND CMAKE_BUILD_TYPE IN_LIST RELEASE_TYPES)
  253. message(
  254. WARNING
  255. "=> GODOTCPP_DEV_BUILD implies a Debug-like build but CMAKE_BUILD_TYPE is '${CMAKE_BUILD_TYPE}'"
  256. )
  257. endif()
  258. set(IS_DEV_BUILD "$<BOOL:${GODOTCPP_DEV_BUILD}>")
  259. ### Define our godot-cpp library targets
  260. # Generator Expressions that rely on the target
  261. set(DEBUG_FEATURES "$<NOT:$<STREQUAL:${GODOTCPP_TARGET},template_release>>")
  262. set(HOT_RELOAD "$<IF:${HOT_RELOAD-UNSET},${DEBUG_FEATURES},$<BOOL:${GODOTCPP_USE_HOT_RELOAD}>>")
  263. # Suffix
  264. string(
  265. CONCAT
  266. GODOTCPP_SUFFIX
  267. "$<1:.${SYSTEM_NAME}>"
  268. "$<1:.${GODOTCPP_TARGET}>"
  269. "$<${IS_DEV_BUILD}:.dev>"
  270. "$<$<STREQUAL:${GODOTCPP_PRECISION},double>:.double>"
  271. "$<1:.${ARCH_NAME}>"
  272. # TODO IOS_SIMULATOR
  273. "$<$<NOT:${THREADS_ENABLED}>:.nothreads>"
  274. )
  275. # the godot-cpp.* library targets
  276. add_library(godot-cpp STATIC)
  277. # Without adding this dependency to the binding generator, XCode will complain.
  278. add_dependencies(godot-cpp generate_bindings)
  279. # Added for backwards compatibility with prior cmake solution so that builds dont immediately break
  280. # from a missing target.
  281. add_library(godot::cpp ALIAS godot-cpp)
  282. file(GLOB_RECURSE GODOTCPP_SOURCES LIST_DIRECTORIES NO CONFIGURE_DEPENDS src/*.cpp)
  283. target_sources(godot-cpp PRIVATE ${GODOTCPP_SOURCES} ${GENERATED_FILES_LIST})
  284. target_include_directories(
  285. godot-cpp
  286. ${GODOTCPP_SYSTEM_HEADERS_ATTRIBUTE}
  287. PUBLIC include ${CMAKE_CURRENT_BINARY_DIR}/gen/include ${GODOTCPP_GDEXTENSION_DIR}
  288. )
  289. # gersemi: off
  290. set_target_properties(
  291. godot-cpp
  292. PROPERTIES
  293. CXX_STANDARD 17
  294. CXX_EXTENSIONS OFF
  295. CXX_VISIBILITY_PRESET ${GODOTCPP_SYMBOL_VISIBILITY}
  296. COMPILE_WARNING_AS_ERROR ${GODOTCPP_WARNING_AS_ERROR}
  297. POSITION_INDEPENDENT_CODE ON
  298. BUILD_RPATH_USE_ORIGIN ON
  299. PREFIX "lib"
  300. OUTPUT_NAME "${PROJECT_NAME}${GODOTCPP_SUFFIX}"
  301. ARCHIVE_OUTPUT_DIRECTORY "$<1:${CMAKE_BINARY_DIR}/bin>"
  302. # Things that are handy to know for dependent targets
  303. GODOTCPP_PLATFORM "${SYSTEM_NAME}"
  304. GODOTCPP_TARGET "${GODOTCPP_TARGET}"
  305. GODOTCPP_ARCH "${ARCH_NAME}"
  306. GODOTCPP_PRECISION "${GODOTCPP_PRECISION}"
  307. GODOTCPP_SUFFIX "${GODOTCPP_SUFFIX}"
  308. # Some IDE's respect this property to logically group targets
  309. FOLDER "godot-cpp"
  310. )
  311. # gersemi: on
  312. if(CMAKE_SYSTEM_NAME STREQUAL Android)
  313. android_generate()
  314. elseif(CMAKE_SYSTEM_NAME STREQUAL iOS)
  315. ios_generate()
  316. elseif(CMAKE_SYSTEM_NAME STREQUAL Linux)
  317. linux_generate()
  318. elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
  319. macos_generate()
  320. elseif(CMAKE_SYSTEM_NAME STREQUAL Emscripten)
  321. web_generate()
  322. elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)
  323. windows_generate()
  324. endif()
  325. endfunction()