godot-cpp/cmake/godotcpp.cmake

297 lines
11 KiB
CMake

#[=======================================================================[.rst:
godotcpp.cmake
--------------
Because these files are included into the top level CMakelists.txt before the
project directive, it means that
* ``CMAKE_CURRENT_SOURCE_DIR`` is the location of godot-cpp's CMakeLists.txt
* ``CMAKE_SOURCE_DIR`` is the location where any prior ``project(...)``
directive was
]=======================================================================]
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/common_compiler_flags.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/android.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/ios.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/linux.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/macos.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/web.cmake)
include( ${CMAKE_CURRENT_SOURCE_DIR}/cmake/windows.cmake)
#Silence warning from unused CMAKE_C_COMPILER from toolchain
if( CMAKE_C_COMPILER )
endif ()
include(ProcessorCount)
ProcessorCount(PROC_MAX)
message( "Auto-detected ${PROC_MAX} CPU cores available for build parallelism." )
# List of known platforms
set( PLATFORM_LIST linux macos windows android ios web )
# List of known architectures
set( ARCH_LIST universal x86_32 x86_64 arm32 arm64 rv64 ppc32 ppc64 wasm32 )
# Function to map processors to known architectures
function( godot_arch_map ALIAS PROC )
string( TOLOWER "${PROC}" PROC )
if( "${PROC}" IN_LIST ARCH_LIST )
set( ${ALIAS} "${PROC}" PARENT_SCOPE)
return()
endif()
set( x86_64 "w64;amd64" )
set( arm32 "armv7" )
set( arm64 "armv8;arm64v8;aarch64" )
set( rv64 "rv;riscv;riscv64" )
set( ppc32 "ppcle;ppc" )
set( ppc64 "ppc64le" )
if( PROC IN_LIST x86_64 )
set(${ALIAS} "x86_64" PARENT_SCOPE )
elseif( PROC IN_LIST arm32 )
set(${ALIAS} "arm32" PARENT_SCOPE )
elseif( PROC IN_LIST arm64 )
set(${ALIAS} "arm64" PARENT_SCOPE )
elseif( PROC IN_LIST rv64 )
set(${ALIAS} "rv64" PARENT_SCOPE )
elseif( PROC IN_LIST ppc32 )
set(${ALIAS} "ppc32" PARENT_SCOPE )
elseif( PROC IN_LIST ppc64 )
set(${ALIAS} "ppc64" PARENT_SCOPE )
else()
set(${ALIAS} "unknown" PARENT_SCOPE )
endif ()
endfunction()
# Function to define all the options.
function( godotcpp_options )
#NOTE: platform is managed using toolchain files.
# Input from user for GDExtension interface header and the API JSON file
set(GODOT_GDEXTENSION_DIR "gdextension" CACHE PATH
"Path to a custom directory containing GDExtension interface header and API JSON file ( /path/to/gdextension_dir )" )
set(GODOT_CUSTOM_API_FILE "" CACHE FILEPATH
"Path to a custom GDExtension API JSON file (takes precedence over `GODOT_GDEXTENSION_DIR`) ( /path/to/custom_api_file )")
#TODO generate_bindings
option(GODOT_GENERATE_TEMPLATE_GET_NODE
"Generate a template version of the Node class's get_node. (ON|OFF)" ON)
#TODO build_library
set(GODOT_PRECISION "single" CACHE STRING
"Set the floating-point precision level (single|double)")
# The arch is typically set by the toolchain
# however for Apple multi-arch setting it here will override.
set( GODOT_ARCH "" CACHE STRING "Target CPU Architecture")
set_property( CACHE GODOT_ARCH PROPERTY STRINGS ${ARCH_LIST} )
#TODO threads
#TODO compiledb
#TODO compiledb_file
#NOTE: build_profile's equivalent in cmake is CMakePresets.json
set(GODOT_USE_HOT_RELOAD "" CACHE BOOL
"Enable the extra accounting required to support hot reload. (ON|OFF)")
# Disable exception handling. Godot doesn't use exceptions anywhere, and this
# saves around 20% of binary size and very significant build time (GH-80513).
option(GODOT_DISABLE_EXCEPTIONS "Force disabling exception handling code (ON|OFF)" ON )
set( GODOT_SYMBOL_VISIBILITY "hidden" CACHE STRING
"Symbols visibility on GNU platforms. Use 'auto' to apply the default value. (auto|visible|hidden)")
set_property( CACHE GODOT_SYMBOL_VISIBILITY PROPERTY STRINGS "auto;visible;hidden" )
#TODO optimize
#TODO debug_symbols
option( GODOT_DEBUG_SYMBOLS "" OFF )
option( GODOT_DEV_BUILD "Developer build with dev-only debugging code (DEV_ENABLED)" OFF )
# FIXME These options are not present in SCons, and perhaps should be added there.
option( GODOT_SYSTEM_HEADERS "Expose headers as SYSTEM." OFF )
option( GODOT_WARNING_AS_ERROR "Treat warnings as errors" OFF )
# Run options commands on the following to populate cache for all
# platforms. This type of thing is typically done conditionally But as
# scons shows all options so shall we.
android_options()
ios_options()
linux_options()
macos_options()
web_options()
windows_options()
endfunction()
# Function to configure and generate the targets
function( godotcpp_generate )
#[[ Multi-Threaded MSVC Compilation
When using the MSVC compiler the build command -j <n> only specifies
parallel jobs or targets, and not multi-threaded compilation To speed up
compile times on msvc, the /MP <n> flag can be set. But we need to set it
at configure time.
MSVC is true when the compiler is some version of Microsoft Visual C++ or
another compiler simulating the Visual C++ cl command-line syntax. ]]
if( MSVC )
math( EXPR PROC_N "(${PROC_MAX}-1) | (${X}-2)>>31 & 1" )
message( "Using ${PROC_N} cores for multi-threaded compilation.")
# TODO You can override it at configure time with ...." )
else ()
message( "Using ${CMAKE_BUILD_PARALLEL_LEVEL} cores, You can override"
" it at configure time by using -j <n> or --parallel <n> on the build"
" command.")
message( " eg. cmake --build . -j 7 ...")
endif ()
#[[ GODOT_SYMBOL_VISIBLITY
To match the SCons options, the allowed values are "auto", "visible", and "hidden"
This effects the compiler flag -fvisibility=[default|internal|hidden|protected]
The corresponding CMake option CXX_VISIBILITY_PRESET accepts the compiler values.
TODO: It is probably worth a pull request which changes both to use the compiler values
https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html#index-fvisibility
This performs the necessary conversion
]]
if( ${GODOT_SYMBOL_VISIBILITY} STREQUAL "auto" OR ${GODOT_SYMBOL_VISIBILITY} STREQUAL "visible" )
set( GODOT_SYMBOL_VISIBILITY "default" )
endif ()
# Setup variable to optionally mark headers as SYSTEM
set(GODOT_SYSTEM_HEADERS_ATTRIBUTE "")
if (GODOT_SYSTEM_HEADERS)
set(GODOT_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
endif ()
#[[ Generate Bindings ]]
if(NOT DEFINED BITS)
set(BITS 32)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(BITS 64)
endif(CMAKE_SIZEOF_VOID_P EQUAL 8)
endif()
set(GODOT_GDEXTENSION_API_FILE "${GODOT_GDEXTENSION_DIR}/extension_api.json")
if (NOT "${GODOT_CUSTOM_API_FILE}" STREQUAL "") # User-defined override.
set(GODOT_GDEXTENSION_API_FILE "${GODOT_CUSTOM_API_FILE}")
endif()
# Code Generation option
if(GODOT_GENERATE_TEMPLATE_GET_NODE)
set(GENERATE_BINDING_PARAMETERS "True")
else()
set(GENERATE_BINDING_PARAMETERS "False")
endif()
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)"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GENERATED_FILES_LIST
OUTPUT_STRIP_TRAILING_WHITESPACE
)
add_custom_command(OUTPUT ${GENERATED_FILES_LIST}
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}')"
VERBATIM
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
MAIN_DEPENDENCY ${GODOT_GDEXTENSION_API_FILE}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/binding_generator.py
COMMENT "Generating bindings"
)
### Platform is derived from the toolchain target
# See GeneratorExpressions PLATFORM_ID and CMAKE_SYSTEM_NAME
set( SYSTEM_NAME
$<$<PLATFORM_ID:Android>:android.${ANDROID_ABI}>
$<$<PLATFORM_ID:iOS>:ios>
$<$<PLATFORM_ID:Linux>:linux>
$<$<PLATFORM_ID:Darwin>:macos>
$<$<PLATFORM_ID:Emscripten>:web>
$<$<PLATFORM_ID:Windows>:windows>
$<$<PLATFORM_ID:Msys>:windows>
)
string(REPLACE ";" "" SYSTEM_NAME "${SYSTEM_NAME}")
### Use the arch from the toolchain if it isn't set manually
if( GODOT_ARCH )
set(SYSTEM_ARCH ${GODOT_ARCH})
else()
godot_arch_map( SYSTEM_ARCH ${CMAKE_SYSTEM_PROCESSOR} )
endif()
### Define our godot-cpp library targets
foreach ( TARGET_NAME template_debug template_release editor )
# Useful genex snippits used in subsequent genex's
set( IS_RELEASE "$<STREQUAL:${TARGET_NAME},template_release>")
set( IS_DEV "$<BOOL:${GODOT_DEV_BUILD}>")
set( DEBUG_FEATURES "$<OR:$<STREQUAL:${TARGET_NAME},template_debug>,$<STREQUAL:${TARGET_NAME},editor>>" )
set( HOT_RELOAD "$<IF:${HOT_RELOAD-UNSET},$<NOT:${IS_RELEASE}>,$<BOOL:${GODOT_USE_HOT_RELOAD}>>" )
# the godot-cpp.* library targets
add_library( ${TARGET_NAME} STATIC ${EXCLUDE} )
add_library( godot-cpp::${TARGET_NAME} ALIAS ${TARGET_NAME} )
file( GLOB_RECURSE GODOTCPP_SOURCES LIST_DIRECTORIES NO CONFIGURE_DEPENDS src/*.cpp )
target_sources( ${TARGET_NAME}
PRIVATE
${GODOTCPP_SOURCES}
${GENERATED_FILES_LIST}
)
target_include_directories( ${TARGET_NAME} ${GODOT_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
include
${CMAKE_CURRENT_BINARY_DIR}/gen/include
${GODOT_GDEXTENSION_DIR}
)
set_target_properties( ${TARGET_NAME}
PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
CXX_VISIBILITY_PRESET ${GODOT_SYMBOL_VISIBILITY}
COMPILE_WARNING_AS_ERROR ${GODOT_WARNING_AS_ERROR}
POSITION_INDEPENDENT_CODE ON
BUILD_RPATH_USE_ORIGIN ON
PREFIX lib
OUTPUT_NAME "${PROJECT_NAME}.${SYSTEM_NAME}.${TARGET_NAME}.${SYSTEM_ARCH}"
ARCHIVE_OUTPUT_DIRECTORY "$<1:${CMAKE_BINARY_DIR}/bin>"
# Things that are handy to know for dependent targets
GODOT_PLATFORM "${SYSTEM_NAME}"
GODOT_TARGET "${TARGET_NAME}"
GODOT_ARCH "${SYSTEM_ARCH}"
)
if( CMAKE_SYSTEM_NAME STREQUAL Android )
android_generate( ${TARGET_NAME} )
elseif ( CMAKE_SYSTEM_NAME STREQUAL iOS )
ios_generate( ${TARGET_NAME} )
elseif ( CMAKE_SYSTEM_NAME STREQUAL Linux )
linux_generate( ${TARGET_NAME} )
elseif ( CMAKE_SYSTEM_NAME STREQUAL Darwin )
macos_generate( ${TARGET_NAME} )
elseif ( CMAKE_SYSTEM_NAME STREQUAL Emscripten )
web_generate( ${TARGET_NAME} )
elseif ( CMAKE_SYSTEM_NAME STREQUAL Windows )
windows_generate( ${TARGET_NAME} )
endif ()
endforeach ()
endfunction()