godotcpp.cmake 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. function( godotcpp_options )
  2. #TODO platform
  3. #TODO target
  4. # Input from user for GDExtension interface header and the API JSON file
  5. set(GODOT_GDEXTENSION_DIR "gdextension" CACHE PATH
  6. "Path to a custom directory containing GDExtension interface header and API JSON file ( /path/to/gdextension_dir )" )
  7. set(GODOT_CUSTOM_API_FILE "" CACHE FILEPATH
  8. "Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`) ( /path/to/custom_api_file )")
  9. #TODO generate_bindings
  10. option(GODOT_GENERATE_TEMPLATE_GET_NODE
  11. "Generate a template version of the Node class's get_node. (ON|OFF)" ON)
  12. #TODO build_library
  13. set(GODOT_PRECISION "single" CACHE STRING
  14. "Set the floating-point precision level (single|double)")
  15. #TODO arch
  16. #TODO threads
  17. #TODO compiledb
  18. #TODO compiledb_file
  19. #TODO build_profile aka cmake preset
  20. set(GODOT_USE_HOT_RELOAD "" CACHE BOOL
  21. "Enable the extra accounting required to support hot reload. (ON|OFF)")
  22. option(GODOT_DISABLE_EXCEPTIONS "Force disabling exception handling code (ON|OFF)" ON )
  23. set( GODOT_SYMBOL_VISIBILITY "hidden" CACHE STRING
  24. "Symbols visibility on GNU platforms. Use 'auto' to apply the default value. (auto|visible|hidden)")
  25. set_property( CACHE GODOT_SYMBOL_VISIBILITY PROPERTY STRINGS "auto;visible;hidden" )
  26. #TODO optimize
  27. #TODO debug_symbols
  28. #TODO dev_build
  29. # FIXME These options are not present in SCons, and perhaps should be added there.
  30. option(GODOT_SYSTEM_HEADERS "Expose headers as SYSTEM." ON)
  31. option(GODOT_WARNING_AS_ERROR "Treat warnings as errors" OFF)
  32. # Run options commands on the following to populate cache for all platforms.
  33. # This type of thing is typically done conditionally
  34. # But as scons shows all options so shall we.
  35. #TODO ios_options()
  36. #TODO linux_options()
  37. #TODO macos_options()
  38. #TODO web_options()
  39. #TODO windows_options()
  40. endfunction()
  41. function( godotcpp_generate )
  42. # Set some helper variables for readability
  43. set( compiler_is_clang "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" )
  44. set( compiler_is_gnu "$<CXX_COMPILER_ID:GNU>" )
  45. set( compiler_is_msvc "$<CXX_COMPILER_ID:MSVC>" )
  46. # CXX_VISIBILITY_PRESET supported values are: default, hidden, protected, and internal
  47. # which is inline with the gcc -fvisibility=
  48. # https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
  49. # To match the scons options we need to change the text to match the -fvisibility flag
  50. # it is probably worth another PR which changes both to use the flag options
  51. if( ${GODOT_SYMBOL_VISIBILITY} STREQUAL "auto" OR ${GODOT_SYMBOL_VISIBILITY} STREQUAL "visible" )
  52. set( GODOT_SYMBOL_VISIBILITY "default" )
  53. endif ()
  54. # Default build type is Debug in the SConstruct
  55. if("${CMAKE_BUILD_TYPE}" STREQUAL "")
  56. set(CMAKE_BUILD_TYPE Debug)
  57. endif()
  58. # Hot reload is enabled by default in Debug-builds
  59. if( GODOT_USE_HOT_RELOAD STREQUAL "" AND NOT CMAKE_BUILD_TYPE STREQUAL "Release")
  60. set(GODOT_USE_HOT_RELOAD ON)
  61. endif()
  62. if(NOT DEFINED BITS)
  63. set(BITS 32)
  64. if(CMAKE_SIZEOF_VOID_P EQUAL 8)
  65. set(BITS 64)
  66. endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
  67. endif()
  68. set(GODOT_GDEXTENSION_API_FILE "${GODOT_GDEXTENSION_DIR}/extension_api.json")
  69. if (NOT "${GODOT_CUSTOM_API_FILE}" STREQUAL "") # User-defined override.
  70. set(GODOT_GDEXTENSION_API_FILE "${GODOT_CUSTOM_API_FILE}")
  71. endif()
  72. if ("${GODOT_PRECISION}" STREQUAL "double")
  73. add_definitions(-DREAL_T_IS_DOUBLE)
  74. endif()
  75. set( GODOT_COMPILE_FLAGS )
  76. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  77. # using Visual Studio C++
  78. set(GODOT_COMPILE_FLAGS "/utf-8") # /GF /MP
  79. if(CMAKE_BUILD_TYPE MATCHES Debug)
  80. set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MDd") # /Od /RTC1 /Zi
  81. else()
  82. set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MD /O2") # /Oy /GL /Gy
  83. endif(CMAKE_BUILD_TYPE MATCHES Debug)
  84. add_definitions(-DNOMINMAX)
  85. else() # GCC/Clang
  86. if(CMAKE_BUILD_TYPE MATCHES Debug)
  87. set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-omit-frame-pointer -O0 -g")
  88. else()
  89. set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -O3")
  90. endif(CMAKE_BUILD_TYPE MATCHES Debug)
  91. endif()
  92. # Disable exception handling. Godot doesn't use exceptions anywhere, and this
  93. # saves around 20% of binary size and very significant build time (GH-80513).
  94. if (GODOT_DISABLE_EXCEPTIONS)
  95. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  96. set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -D_HAS_EXCEPTIONS=0")
  97. else()
  98. set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-exceptions")
  99. endif()
  100. else()
  101. if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
  102. set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /EHsc")
  103. endif()
  104. endif()
  105. # Generate source from the bindings file
  106. find_package(Python3 3.4 REQUIRED) # pathlib should be present
  107. if(GODOT_GENERATE_TEMPLATE_GET_NODE)
  108. set(GENERATE_BINDING_PARAMETERS "True")
  109. else()
  110. set(GENERATE_BINDING_PARAMETERS "False")
  111. endif()
  112. execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import binding_generator; binding_generator.print_file_list(\"${GODOT_GDEXTENSION_API_FILE}\", \"${CMAKE_CURRENT_BINARY_DIR}\", headers=True, sources=True)"
  113. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  114. OUTPUT_VARIABLE GENERATED_FILES_LIST
  115. OUTPUT_STRIP_TRAILING_WHITESPACE
  116. )
  117. add_custom_command(OUTPUT ${GENERATED_FILES_LIST}
  118. COMMAND "${Python3_EXECUTABLE}" "-c" "import binding_generator; binding_generator.generate_bindings(\"${GODOT_GDEXTENSION_API_FILE}\", \"${GENERATE_BINDING_PARAMETERS}\", \"${BITS}\", \"${GODOT_PRECISION}\", \"${CMAKE_CURRENT_BINARY_DIR}\")"
  119. VERBATIM
  120. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  121. MAIN_DEPENDENCY ${GODOT_GDEXTENSION_API_FILE}
  122. DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/binding_generator.py
  123. COMMENT "Generating bindings"
  124. )
  125. # Get Sources
  126. # As this cmake file was added using 'include(godotcpp)' from the root CMakeLists.txt,
  127. # the ${CMAKE_CURRENT_SOURCE_DIR} is still the root dir.
  128. file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.c**)
  129. file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS include/*.h**)
  130. # Define our godot-cpp library
  131. add_library(${PROJECT_NAME} STATIC
  132. ${SOURCES}
  133. ${HEADERS}
  134. ${GENERATED_FILES_LIST}
  135. )
  136. add_library(godot::cpp ALIAS ${PROJECT_NAME})
  137. include(${PROJECT_SOURCE_DIR}/cmake/common_compiler_flags.cmake)
  138. target_compile_features(${PROJECT_NAME}
  139. PRIVATE
  140. cxx_std_17
  141. )
  142. if(GODOT_USE_HOT_RELOAD)
  143. target_compile_definitions(${PROJECT_NAME} PUBLIC HOT_RELOAD_ENABLED)
  144. target_compile_options(${PROJECT_NAME} PUBLIC $<${compiler_is_gnu}:-fno-gnu-unique>)
  145. endif()
  146. target_compile_definitions(${PROJECT_NAME} PUBLIC
  147. $<$<CONFIG:Debug>:
  148. DEBUG_ENABLED
  149. DEBUG_METHODS_ENABLED
  150. >
  151. $<${compiler_is_msvc}:
  152. TYPED_METHOD_BIND
  153. >
  154. )
  155. target_link_options(${PROJECT_NAME} PRIVATE
  156. $<$<NOT:${compiler_is_msvc}>:
  157. -static-libgcc
  158. -static-libstdc++
  159. -Wl,-R,'$$ORIGIN'
  160. >
  161. )
  162. # Optionally mark headers as SYSTEM
  163. set(GODOT_SYSTEM_HEADERS_ATTRIBUTE "")
  164. if (GODOT_SYSTEM_HEADERS)
  165. set(GODOT_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
  166. endif ()
  167. target_include_directories(${PROJECT_NAME} ${GODOT_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
  168. include
  169. ${CMAKE_CURRENT_BINARY_DIR}/gen/include
  170. ${GODOT_GDEXTENSION_DIR}
  171. )
  172. # Add the compile flags
  173. set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS ${GODOT_COMPILE_FLAGS})
  174. # Create the correct name (godot.os.build_type.system_bits)
  175. string(TOLOWER "${CMAKE_SYSTEM_NAME}" SYSTEM_NAME)
  176. string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE)
  177. if(ANDROID)
  178. # Added the android abi after system name
  179. set(SYSTEM_NAME ${SYSTEM_NAME}.${ANDROID_ABI})
  180. # Android does not have the bits at the end if you look at the main godot repo build
  181. set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}")
  182. else()
  183. set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}.${BITS}")
  184. endif()
  185. set_target_properties(${PROJECT_NAME}
  186. PROPERTIES
  187. CXX_EXTENSIONS OFF
  188. POSITION_INDEPENDENT_CODE ON
  189. CXX_VISIBILITY_PRESET ${GODOT_SYMBOL_VISIBILITY}
  190. ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
  191. LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
  192. RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
  193. OUTPUT_NAME "${OUTPUT_NAME}"
  194. )
  195. endfunction()