godotcpp.cmake 14 KB

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