CMake rewrite, reflecting default SCons variables

Vano 2023-12-31 18:17:28 +02:00
parent 0ddef6ed96
commit fe21845ca0
9 changed files with 650 additions and 178 deletions

View File

@ -1,140 +1,79 @@
# cmake arguments
# CMAKE_BUILD_TYPE: Compilation target (Debug or Release defaults to Debug)
# CMAKE_BUILD_TYPE: Compilation target (editor, template_debug, template_release)
#
# godot-cpp cmake arguments
# GODOT_GDEXTENSION_DIR: Path to the directory containing GDExtension interface header and API JSON file
# GODOT_CPP_SYSTEM_HEADERS Mark the header files as SYSTEM. This may be useful to supress warnings in projects including this one.
# GODOT_CPP_WARNING_AS_ERROR Treat any warnings as errors
# GODOT_CUSTOM_API_FILE: Path to a custom GDExtension API JSON file (takes precedence over `gdextension_dir`)
# FLOAT_PRECISION: Floating-point precision level ("single", "double")
# PLATFORM: Platform type (LINUX, MACOS, WINDOWS, ANDROID, IOS, WEB)
# Auto-detected by default depending on current OS or chosen toolchain
#
# Android cmake arguments
# CMAKE_TOOLCHAIN_FILE: The path to the android cmake toolchain ($ANDROID_NDK/build/cmake/android.toolchain.cmake)
# ANDROID_NDK: The path to the android ndk root folder
# ANDROID_TOOLCHAIN_NAME: The android toolchain (arm-linux-androideabi-4.9 or aarch64-linux-android-4.9 or x86-4.9 or x86_64-4.9)
# ANDROID_PLATFORM: The android platform version (android-23)
# More info here: https://godot.readthedocs.io/en/latest/development/compiling/compiling_for_android.html
# other global and chosen platform-specific options:
#
# cmake -LH
#
# Note: use -Bbuild_dir option to build in separate directories
# for different configurations
#
# cmake -Bbuild && cmake --build build
#
# Examples
#
# Builds a debug version:
# Builds default configuration:
# cmake .
# cmake --build .
#
# Builds a release version with clang
# CC=/usr/bin/clang CXX=/usr/bin/clang++ cmake -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" .
# Builds template_release version
#
# cmake -DCMAKE_BUILD_TYPE=template_release .
# cmake --build .
#
# Creates multi-config setup and builds 2 library versions (example for Linux)
#
# cmake -G "Ninja Multi-Config" .
# cmake --build . --config editor
# cmake --build . --config template_release
#
# Builds web version, using Emscripten toolchain
#
# cmake --toolchain /usr/lib/emscripten/cmake/Modules/Platform/Emscripten.cmake .
# cmake --build .
#
#
# Builds an android armeabi-v7a debug version:
# cmake -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_NDK=$ANDROID_NDK \
# -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9 -DANDROID_PLATFORM=android-23 -DCMAKE_BUILD_TYPE=Debug .
# cmake --toolchain $ANDROID_NDK/build/cmake/android.toolchain.cmake \
# -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9 -DANDROID_TOOLCHAIN=clang -DANDROID_PLATFORM=21 .
# cmake --build .
#
# Protip
# Generate the buildfiles in a sub directory to not clutter the root directory with build files:
# mkdir build && cd build && cmake -G "Unix Makefiles" .. && cmake --build .
#
# Todo
# Test build for Windows, Mac and mingw.
cmake_minimum_required(VERSION 3.12)
cmake_minimum_required(VERSION 3.24)
project(godot-cpp LANGUAGES CXX)
option(GENERATE_TEMPLATE_GET_NODE "Generate a template version of the Node class's get_node." ON)
option(GODOT_CPP_SYSTEM_HEADERS "Expose headers as SYSTEM." ON)
option(GODOT_CPP_WARNING_AS_ERROR "Treat warnings as errors" OFF)
# Add path to modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
# Set some helper variables for readability
set( compiler_is_clang "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>" )
set( compiler_is_gnu "$<CXX_COMPILER_ID:GNU>" )
set( compiler_is_msvc "$<CXX_COMPILER_ID:MSVC>" )
# Default build type is Debug in the SConstruct
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE Debug)
endif()
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()
# Input from user for GDExtension interface header and the API JSON file
set(GODOT_GDEXTENSION_DIR "gdextension" CACHE STRING "")
set(GODOT_CUSTOM_API_FILE "" CACHE STRING "")
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()
set(FLOAT_PRECISION "single" CACHE STRING "")
if ("${FLOAT_PRECISION}" STREQUAL "double")
add_definitions(-DREAL_T_IS_DOUBLE)
endif()
set(GODOT_COMPILE_FLAGS )
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
# using Visual Studio C++
set(GODOT_COMPILE_FLAGS "/utf-8") # /GF /MP
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MDd") # /Od /RTC1 /Zi
else()
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /MD /O2") # /Oy /GL /Gy
STRING(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
endif(CMAKE_BUILD_TYPE MATCHES Debug)
add_definitions(-DNOMINMAX)
else() # GCC/Clang
if(CMAKE_BUILD_TYPE MATCHES Debug)
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-omit-frame-pointer -O0 -g")
else()
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -O3")
endif(CMAKE_BUILD_TYPE MATCHES Debug)
endif()
# 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 ON "Force disabling exception handling code")
if (GODOT_DISABLE_EXCEPTIONS)
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -D_HAS_EXCEPTIONS=0")
else()
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-exceptions")
endif()
else()
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /EHsc")
endif()
endif()
# Handles all global and platform-specific variables
include(godotcpp)
# Generate source from the bindings file
find_package(Python3 3.4 REQUIRED) # pathlib should be present
execute_process(COMMAND "${Python3_EXECUTABLE}" "-c" "import binding_generator; binding_generator.print_file_list(\"${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
)
string(TOLOWER ${FLOAT_PRECISION} FLOAT_PRECISION_ARG)
if(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}\", \"${FLOAT_PRECISION}\", \"${CMAKE_CURRENT_BINARY_DIR}\")"
COMMAND "${Python3_EXECUTABLE}" "-c" "import binding_generator; binding_generator.generate_bindings(\"${GDEXTENSION_API_FILE}\", \"${GENERATE_BINDING_PARAMETERS}\", \"${BITS}\", \"${FLOAT_PRECISION_ARG}\", \"${CMAKE_CURRENT_BINARY_DIR}\")"
VERBATIM
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
MAIN_DEPENDENCY ${GODOT_GDEXTENSION_API_FILE}
MAIN_DEPENDENCY ${GDEXTENSION_API_FILE}
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/binding_generator.py
COMMENT "Generating bindings"
)
@ -144,73 +83,41 @@ file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.c**)
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS include/*.h**)
# Define our godot-cpp library
add_library(${PROJECT_NAME} STATIC
add_library(${PROJECT_NAME} ${GODOT_CPP_LIBRARY_TYPE}
${SOURCES}
${HEADERS}
${GENERATED_FILES_LIST}
)
add_library(godot::cpp ALIAS ${PROJECT_NAME})
include(GodotCompilerWarnings)
target_compile_features(${PROJECT_NAME}
PRIVATE
cxx_std_17
)
target_compile_definitions(${PROJECT_NAME} PUBLIC
$<$<CONFIG:Debug>:
DEBUG_ENABLED
DEBUG_METHODS_ENABLED
>
$<${compiler_is_msvc}:
TYPED_METHOD_BIND
>
)
target_link_options(${PROJECT_NAME} PRIVATE
$<$<NOT:${compiler_is_msvc}>:
-static-libgcc
-static-libstdc++
-Wl,-R,'$$ORIGIN'
>
)
# Optionally mark headers as SYSTEM
set(GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE "")
if (GODOT_CPP_SYSTEM_HEADERS)
set(GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
endif ()
target_include_directories(${PROJECT_NAME} ${GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE} PUBLIC
include
${CMAKE_CURRENT_BINARY_DIR}/gen/include
${GODOT_GDEXTENSION_DIR}
${GDEXTENSION_DIR}
)
# Add the compile flags
set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS ${GODOT_COMPILE_FLAGS})
target_compile_features(${PROJECT_NAME} PUBLIC
cxx_std_17
)
# Create the correct name (godot.os.build_type.system_bits)
string(TOLOWER "${CMAKE_SYSTEM_NAME}" SYSTEM_NAME)
string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE)
target_compile_options(${PROJECT_NAME} PRIVATE
${GODOT_CC_FLAGS}
${GODOT_CXX_FLAGS}
${GODOT_COMPILE_WARNING_FLAGS}
)
if(ANDROID)
# Added the android abi after system name
set(SYSTEM_NAME ${SYSTEM_NAME}.${ANDROID_ABI})
target_link_options(${PROJECT_NAME} PRIVATE ${GODOT_LINK_FLAGS})
# Android does not have the bits at the end if you look at the main godot repo build
set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}")
else()
set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}.${BITS}")
endif()
target_compile_definitions(${PROJECT_NAME} PRIVATE ${GODOT_DEFINITIONS})
set_target_properties(${PROJECT_NAME}
PROPERTIES
CXX_EXTENSIONS OFF
COMPILE_WARNING_AS_ERROR ${GODOT_CPP_WARNING_AS_ERROR}
POSITION_INDEPENDENT_CODE ON
ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin"
OUTPUT_NAME "${OUTPUT_NAME}"
OUTPUT_NAME "${PROJECT_NAME}${LIBRARY_SUFFIX}"
)

View File

@ -1,3 +1,8 @@
# Set some helper variables for readability
set(compiler_is_clang "$<OR:$<CXX_COMPILER_ID:AppleClang>,$<CXX_COMPILER_ID:Clang>>")
set(compiler_is_gnu "$<CXX_COMPILER_ID:GNU>")
set(compiler_is_msvc "$<CXX_COMPILER_ID:MSVC>")
# Add warnings based on compiler & version
# Set some helper variables for readability
set(compiler_less_than_v8 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,8>")
@ -7,7 +12,7 @@ set( compiler_less_than_v11 "$<VERSION_LESS:$<CXX_COMPILER_VERSION>,11>" )
set(compiler_greater_than_or_equal_v12 "$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,12>")
# These compiler options reflect what is in godot/SConstruct.
target_compile_options( ${PROJECT_NAME} PRIVATE
list(APPEND GODOT_COMPILE_WARNING_FLAGS
# MSVC only
$<${compiler_is_msvc}:
/W4
@ -71,24 +76,3 @@ target_compile_options( ${PROJECT_NAME} PRIVATE
-Wno-return-type
>
)
# Treat warnings as errors
function( set_warning_as_error )
message( STATUS "[${PROJECT_NAME}] Treating warnings as errors")
if ( CMAKE_VERSION VERSION_GREATER_EQUAL "3.24" )
set_target_properties( ${PROJECT_NAME}
PROPERTIES
COMPILE_WARNING_AS_ERROR ON
)
else()
target_compile_options( ${PROJECT_NAME}
PRIVATE
$<${compiler_is_msvc}:/WX>
$<$<OR:${compiler_is_clang},${compiler_is_gnu}>:-Werror>
)
endif()
endfunction()
if ( GODOT_CPP_WARNING_AS_ERROR )
set_warning_as_error()
endif()

67
cmake/android.cmake Normal file
View File

@ -0,0 +1,67 @@
# Used with android toolchain at $ANDROID_NDK/build/cmake/android.toolchain.cmake
set(ARCH "arm64" CACHE STRING "Target architecture (arm64, x86_64, arm32, x86_32, CUSTOM")
if(${ANDROID_PLATFORM} VERSION_LESS 21)
message(FATAL_ERROR "Minimum supported API level is 21 (-DANDROID_PLATFORM=21)")
endif()
# This must be kept in sync with the value in https://github.com/godotengine/godot/blob/master/platform/android/detect.py#L58.
set(ANDROID_NDK_VERSION_EXPECTED "23.2.8568313")
if(NOT "${ANDROID_NDK_REVISION}" STREQUAL "${ANDROID_NDK_VERSION_EXPECTED}")
message(FATAL_ERROR "Android NDK version '${ANDROID_NDK_VERSION_EXPECTED}' expected, got '${ANDROID_NDK_REVISION}'")
endif()
string(REGEX MATCH "32$|64$" DEFAULT_BITS "${ARCH}")
set(BITS "${DEFAULT_BITS}" CACHE STRING "Architecture bits. Needs to be set manually for custom architecture")
list(APPEND GODOT_DEFINITIONS
ANDROID_ENABLED
UNIX_ENABLED
)
list(APPEND GODOT_CC_FLAGS
$<$<STREQUAL:${ARCH},arm32>:
--target=armv7a-linux-androideabi${ANDROID_PLATFORM}
-march=armv7-a
-mfpu=neon
>
$<$<STREQUAL:${ARCH},arm64>:
--target=aarch64-linux-android${ANDROID_PLATFORM}
-march=armv8-a
>
$<$<STREQUAL:${ARCH},x86_32>:
--target=i686-linux-android${ANDROID_PLATFORM}
-march=i686
-mstackrealign
>
$<$<STREQUAL:${ARCH},x86_64>:
--target=x86_64-linux-android${ANDROID_PLATFORM}
-march=x86-64
>
)
list(APPEND GODOT_LINK_FLAGS
$<$<STREQUAL:${ARCH},arm32>:
--target=armv7a-linux-androideabi${ANDROID_PLATFORM}
-march=armv7-a
>
$<$<STREQUAL:${ARCH},arm64>:
--target=aarch64-linux-android${ANDROID_PLATFORM}
-march=armv8-a
>
$<$<STREQUAL:${ARCH},x86_32>:
--target=i686-linux-android${ANDROID_PLATFORM}
-march=i686
>
$<$<STREQUAL:${ARCH},x86_64>:
--target=x86_64-linux-android${ANDROID_PLATFORM}
-march=x86-64
>
)
string(APPEND LIBRARY_SUFFIX ".${ARCH}")

254
cmake/godotcpp.cmake Normal file
View File

@ -0,0 +1,254 @@
# This file contains variables needed by all platforms
### Options
set(CMAKE_CONFIGURATION_TYPES "template_debug;editor;template_release")
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
set(CMAKE_BUILD_TYPE template_debug)
endif()
# Auto-detect platform
if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(DEFAULT_PLATFORM "LINUX")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Windows")
set(DEFAULT_PLATFORM "WINDOWS")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
set(DEFAULT_PLATFORM "MACOS")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Emscripten") # Set by providing Emscripten toolchain
set(DEFAULT_PLATFORM "WEB")
elseif(CMAKE_SYSTEM_NAME STREQUAL "Android") # Set by providing Android toolchain
set(DEFAULT_PLATFORM "ANDROID")
else()
message(FATAL_ERROR "Could not auto-detect platform for \"${CMAKE_SYSTEM_NAME}\" automatically, please specify with -DPLATFORM=<platform>")
endif()
set(PLATFORM "${DEFAULT_PLATFORM}" CACHE STRING "[Auto-detected] Target platform (LINUX, MACOS, WINDOWS, ANDROID, IOS, WEB)")
set(GDEXTENSION_DIR "${CMAKE_CURRENT_SOURCE_DIR}/gdextension" CACHE FILEPATH "Path to a directory containing GDExtension interface header")
set(GDEXTENSION_API_FILE "${CMAKE_CURRENT_SOURCE_DIR}/gdextension/extension_api.json" CACHE FILEPATH "Path to GDExtension API JSON file")
set(FLOAT_PRECISION "SINGLE" CACHE STRING "Floating-point precision level (SINGLE, DOUBLE)")
set(OPTIMIZE "AUTO" CACHE STRING "The desired optimization flags (NONE, CUSTOM, DEBUG, SPEED, SPEED_TRACE, SIZE)")
set(SYMBOLS_VISIBILITY "AUTO" CACHE STRING "Symbols visibility on GNU platforms (AUTO, VISIBLE, HIDDEN)")
#TODO: custom .gdextension template file to add shared library as dependency
set(GODOT_CPP_LIBRARY_TYPE "STATIC" CACHE STRING "[Experimental] Library type (STATIC, SHARED)")
option(DEV_BUILD "Developer build with dev-only debugging code" OFF)
option(DEBUG_SYMBOLS "Build with debugging symbols" ON)
option(USE_HOT_RELOAD "Enable the extra accounting required to support hot reload" ON)
# 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)
# Optionally mark headers as SYSTEM
option(GODOT_CPP_SYSTEM_HEADERS "Mark the header files as SYSTEM. This may be useful to supress warnings in projects including this one" OFF)
set(GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE "")
if(GODOT_CPP_SYSTEM_HEADERS)
set(GODOT_CPP_SYSTEM_HEADERS_ATTRIBUTE SYSTEM)
endif()
# Enable by default when building godot-cpp only
set(DEFAULT_WARNING_AS_ERROR OFF)
if(${CMAKE_PROJECT_NAME} STREQUAL ${PROJECT_NAME})
set(DEFAULT_WARNING_AS_ERROR ON)
endif()
set(GODOT_CPP_WARNING_AS_ERROR ${DEFAULT_WARNING_AS_ERROR} CACHE BOOL "Treat warnings as errors")
option(GENERATE_TEMPLATE_GET_NODE "Generate a template version of the Node class's get_node" ON)
###
# Compiler warnings and compiler check generators
include(GodotCompilerWarnings)
# Create the correct name (godot-cpp.platform.target.arch)
if(${DEV_BUILD})
string(APPEND LIBRARY_SUFFIX ".dev")
endif()
if(${FLOAT_PRECISION} STREQUAL "DOUBLE")
string(APPEND LIBRARY_SUFFIX ".double")
endif()
# Workaround of $<CONFIG> expanding to "" when default build set
set(CONFIG "$<IF:$<STREQUAL:,$<CONFIG>>,${CMAKE_BUILD_TYPE},$<CONFIG>>")
string(TOLOWER ".${PLATFORM}" platform)
string(PREPEND LIBRARY_SUFFIX "${platform}.${CONFIG}")
# Default optimization levels if OPTIMIZE=AUTO, for multi-config support
set(DEFAULT_OPTIMIZATION_DEV "$<AND:$<STREQUAL:${OPTIMIZE},AUTO>,$<BOOL:${DEV_BUILD}>>")
set(DEFAULT_OPTIMIZATION_DEBUG_FEATURES "$<AND:$<NOT:${DEFAULT_OPTIMIZATION_DEV}>,$<STREQUAL:${OPTIMIZE},AUTO>,$<OR:$<STREQUAL:${CONFIG},editor>,$<STREQUAL:${CONFIG},template_debug>>>")
set(DEFAULT_OPTIMIZATION "$<$<AND:$<NOT:$<OR:${DEFAULT_OPTIMIZATION_DEV},${DEFAULT_OPTIMIZATION_DEBUG_FEATURES}>>,STREQUAL:${OPTIMIZE},AUTO>>")
list(APPEND GODOT_DEFINITIONS
GDEXTENSION
$<${compiler_is_msvc}:
$<$<BOOL:${GODOT_DISABLE_EXCEPTIONS}>:
_HAS_EXCEPTIONS=0
>
>
$<$<STREQUAL:${FLOAT_PRECISION},DOUBLE>:
REAL_T_IS_DOUBLE
>
$<$<AND:$<NOT:$<STREQUAL:${CONFIG},template_release>>,$<BOOL:${USE_HOT_RELOAD}>>:
HOT_RELOAD_ENABLE
>
$<$<STREQUAL:${CONFIG},editor>:
TOOLS_ENABLED
>
$<$<BOOL:${DEV_BUILD}>:
DEV_ENABLED
>
$<$<NOT:$<BOOL:${DEV_BUILD}>>:
NDEBUG
>
$<$<NOT:$<STREQUAL:${CONFIG},template_release>>:
DEBUG_ENABLED
DEBUG_METHODS_ENABLED
>
)
list(APPEND GODOT_CC_FLAGS
$<${compiler_is_msvc}:
$<$<BOOL:${DEBUG_SYMBOLS}>:
/Zi
/FS
>
$<$<OR:$<STREQUAL:${OPTIMIZE},SPEED>,${DEFAULT_OPTIMIZATION}>:
/O2
>
$<$<OR:$<STREQUAL:${OPTIMIZE},SPEED_TRACE>,${DEFAULT_OPTIMIZATION_DEBUG_FEATURES}>:
/O2
>
$<$<STREQUAL:${OPTIMIZE},SIZE>:
/O1
>
$<$<OR:$<STREQUAL:${OPTIMIZE},DEBUG>,$<STREQUAL:${OPTIMIZE},NONE>,${DEFAULT_OPTIMIZATION_DEV}>:
/Od
>
>
$<$<NOT:${compiler_is_msvc}>:
$<$<BOOL:${DEBUG_SYMBOLS}>:
-gdwarf-4
$<$<BOOL:${DEV_BUILD}>:
-g3
>
$<$<NOT:$<BOOL:${DEV_BUILD}>>:
-g2
>
>
$<$<STREQUAL:${SYMBOLS_VISIBILITY},VISIBLE>:
-fvisibility=default
>
$<$<STREQUAL:${SYMBOLS_VISIBILITY},HIDDEN>:
-fvisibility=hidden
>
$<$<OR:$<STREQUAL:${OPTIMIZE},SPEED>>:
-O3
>
$<$<OR:$<STREQUAL:${OPTIMIZE},SPEED_TRACE>,${DEFAULT_OPTIMIZATION_DEBUG_FEATURES}>:
-O2
>
$<$<STREQUAL:${OPTIMIZE},SIZE>:
-Os
>
$<$<STREQUAL:${OPTIMIZE},DEBUG>:
-Og
>
$<$<OR:$<STREQUAL:${OPTIMIZE},NONE>,${DEFAULT_OPTIMIZATION_DEV}>:
-O0
>
>
)
list(APPEND GODOT_CXX_FLAGS
$<${compiler_is_msvc}:
$<$<NOT:$<BOOL:${GODOT_DISABLE_EXCEPTIONS}>>:
/EHsc
>
>
$<$<NOT:${compiler_is_msvc}>:
$<$<BOOL:${GODOT_DISABLE_EXCEPTIONS}>:
-fno-exceptions
>
>
)
list(APPEND GODOT_LINK_FLAGS
$<${compiler_is_msvc}:
$<$<BOOL:${DEBUG_SYMBOLS}>:
/DEBUG:FULL
>
$<$<STREQUAL:${OPTIMIZE},SPEED>:
/OPT:REF
>
$<$<OR:$<STREQUAL:${OPTIMIZE},SPEED_TRACE>,${DEFAULT_OPTIMIZATION_DEBUG_FEATURES}>:
/OPT:REF
/OPT:NOICF
>
$<$<STREQUAL:${OPTIMIZE},SIZE>:
/OPT:REF
>
>
$<$<NOT:${compiler_is_msvc}>:
$<$<STREQUAL:${SYMBOLS_VISIBILITY},VISIBLE>:
-fvisibility=default
>
$<$<STREQUAL:${SYMBOLS_VISIBILITY},HIDDEN>:
-fvisibility=hidden
>
$<$<NOT:$<BOOL:${DEBUG_SYMBOLS}>>:
$<$<CXX_COMPILER_ID:AppleClang>: # SCons: not is_vanilla_clang(env)
"-Wl,-S"
"-Wl,-x"
"-Wl,-dead_strip"
>
$<$<NOT:$<CXX_COMPILER_ID:AppleClang>>:
"-s"
>
>
>
)
# Platform-specific options
if("${PLATFORM}" STREQUAL "LINUX")
include(linux)
elseif("${PLATFORM}" STREQUAL "MACOS")
include(macos)
elseif("${PLATFORM}" STREQUAL "WINDOWS")
include(windows)
elseif("${PLATFORM}" STREQUAL "ANDROID")
include(android)
elseif("${PLATFORM}" STREQUAL "IOS")
include(ios)
elseif("${PLATFORM}" STREQUAL "WEB")
include(web)
else()
message(FATAL_ERROR "Platform not supported: ${PLATFORM}")
endif()
# Write all flags to file for cmake configuration debug
file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/flags.txt"
CONTENT
"C_FLAGS '${GODOT_CC_FLAGS}'\nCXX_FLAGS '${GODOT_CXX_FLAGS}'\nLINK_FLAGS '${GODOT_LINK_FLAGS}'\nCOMPILE_WARNING_FLAGS '${GODOT_COMPILE_WARNING_FLAGS}'\n"
TARGET ${PROJECT_NAME}
)

68
cmake/ios.cmake Normal file
View File

@ -0,0 +1,68 @@
message(WARNING "IOS configuration is not tested and may not work.")
set(ARCH "universal" CACHE STRING "Target architecture (universal, arm64, x86_64, CUSTOM)")
set(IOS_MIN_VERSION "12.0" CACHE STRING "Target minimum iphoneos/iphonesimulator version")
option(IOS_SIMULATOR "Target iOS Simulator" OFF)
if (${ARCH} STREQUAL "universal")
set(DEFAULT_BITS 64)
else()
string(REGEX MATCH "32$|64$" DEFAULT_BITS "${ARCH}")
endif()
set(BITS "${DEFAULT_BITS}" CACHE STRING "Architecture bits. Needs to be set manually for custom architecture")
list(APPEND GODOT_DEFINITIONS
IOS_ENABLED
UNIX_ENABLED
)
list(APPEND GODOT_CC_FLAGS
$<$<STREQUAL:${CMAKE_SYSTEM_NAME},Darwin>:
-stdlib=libc++
>
$<$<BOOL:${IOS_SIMULATOR}>:
-mios-simulator-version-min=${IOS_MIN_VERSION}
>
$<$<NOT:$<BOOL:${IOS_SIMULATOR}>>:
-miphoneos-version-min=${IOS_MIN_VERSION}
>
$<$<STREQUAL:${ARCH},universal>:
$<$<BOOL:${IOS_SIMULATOR}>:
"SHELL:-arch x86_64"
"SHELL:-arch arm64"
>
$<$<NOT:$<BOOL:${IOS_SIMULATOR}>>:
"SHELL:-arch arm64"
>
>
$<$<NOT:$<STREQUAL:${ARCH},universal>>:
"-arch ${ARCH}"
>
)
list(APPEND GODOT_LINK_FLAGS
$<$<STREQUAL:${ARCH},universal>:
$<$<BOOL:${IOS_SIMULATOR}>:
"SHELL:-arch x86_64"
"SHELL:-arch arm64"
>
$<$<NOT:$<BOOL:${IOS_SIMULATOR}>>:
"SHELL:-arch arm64"
>
>
$<$<NOT:$<STREQUAL:${ARCH},universal>>:
"SHELL:-arch ${ARCH}"
>
)
string(APPEND LIBRARY_SUFFIX ".${ARCH}")
if(${IOS_SIMULATOR})
string(APPEND LIBRARY_SUFFIX ".simulator")
endif()

61
cmake/linux.cmake Normal file
View File

@ -0,0 +1,61 @@
set(ARCH "x86_64" CACHE STRING "Target architecture (x86_32, x86_64, arm64, rv64, CUSTOM)")
string(REGEX MATCH "32$|64$" DEFAULT_BITS "${ARCH}")
set(BITS "${DEFAULT_BITS}" CACHE STRING "Architecture bits. Needs to be set manually for custom architecture")
list(APPEND GODOT_DEFINITIONS
LINUX_ENABLED
UNIX_ENABLED
)
list(APPEND GODOT_CC_FLAGS
# -fPIC is controlled by POSITION_INDEPENDENT_CODE property
$<$<STREQUAL:${ARCH},x86_64>:
-m64
-march=x86-64
>
$<$<STREQUAL:${ARCH},x86_32>:
-m32
-march=i686
>
$<$<STREQUAL:${ARCH},arm64>:
-march=armv8-a
>
$<$<STREQUAL:${ARCH},rv64>:
-march=rv64gc
>
)
list(APPEND GODOT_CXX_FLAGS
$<$<AND:$<BOOL:${USE_HOT_RELOAD}>,${compiler_is_gnu}>:
-fno-gnu-unique
>
)
list(APPEND GODOT_LINK_FLAGS
-Wl,-R,'$$ORIGIN'
$<$<STREQUAL:${ARCH},x86_64>:
-m64
-march=x86-64
>
$<$<STREQUAL:${ARCH},x86_32>:
-m32
-march=i686
>
$<$<STREQUAL:${ARCH},arm64>:
-march=armv8-a
>
$<$<STREQUAL:${ARCH},rv64>:
-march=rv64gc
>
)
list(APPEND GODOT_COMPILE_WARNING_FLAGS
-Wwrite-strings
)
string(APPEND LIBRARY_SUFFIX ".${ARCH}")

53
cmake/macos.cmake Normal file
View File

@ -0,0 +1,53 @@
message(WARNING "MacOS configuration is not tested and may not work.")
set(ARCH "universal" CACHE STRING "Target architecture (universal, arm64, x86_64, CUSTOM)")
set(MACOS_DEPLOYMENT_TARGET "DEFAULT" CACHE STRING "")
if (${ARCH} STREQUAL "universal")
set(DEFAULT_BITS 64)
else()
string(REGEX MATCH "32$|64$" DEFAULT_BITS "${ARCH}")
endif()
set(BITS "${DEFAULT_BITS}" CACHE STRING "Architecture bits. Needs to be set manually for custom architecture")
list(APPEND GODOT_DEFINITIONS
MACOS_ENABLED
UNIX_ENABLED
)
list(APPEND GODOT_CC_FLAGS
$<$<STREQUAL:${ARCH},universal>:
"SHELL:-arch x86_64"
"SHELL:-arch arm64"
>
$<$<NOT:$<STREQUAL:${ARCH},universal>>:
"SHELL:-arch ${ARCH}"
>
$<$<NOT:$<STREQUAL:${MACOS_DEPLOYMENT_TARGET},DEFAULT>>:
-mmacosx-version-min=${MACOS_DEPLOYMENT_TARGET}
>
)
list(APPEND GODOT_LINK_FLAGS
-framework
Cocoa
-Wl,-undefined,dynamic_lookup
$<$<STREQUAL:${ARCH},universal>:
"SHELL:-arch x86_64"
"SHELL:-arch arm64"
>
$<$<NOT:$<STREQUAL:${ARCH},universal>>:
"SHELL:-arch ${ARCH}"
>
$<$<NOT:$<STREQUAL:${MACOS_DEPLOYMENT_TARGET},DEFAULT>>:
-mmacosx-version-min=${MACOS_DEPLOYMENT_TARGET}
>
)
string(APPEND LIBRARY_SUFFIX ".${ARCH}")

25
cmake/web.cmake Normal file
View File

@ -0,0 +1,25 @@
# Used with Emscripted toolchain at *toolchain_dir*/cmake/Modules/Platform/Emscripten.cmake
set(ARCH "wasm32" CACHE STRING "Target architecture (wasm32, CUSTOM)")
string(REGEX MATCH "32$|64$" DEFAULT_BITS "${ARCH}")
set(BITS "${DEFAULT_BITS}" CACHE STRING "Architecture bits. Needs to be set manually for custom architecture")
list(APPEND GODOT_DEFINITIONS
WEB_ENABLED
UNIX_ENABLED
)
list(APPEND GODOT_CC_FLAGS
-sUSE_PTHREADS=1
)
list(APPEND GODOT_CXX_FLAGS
-sSIDE_MODULE=1
)
list(APPEND GODOT_LINK_FLAGS
-sUSE_PTHREADS=1
-sSIDE_MODULE=1
)

53
cmake/windows.cmake Normal file
View File

@ -0,0 +1,53 @@
set(ARCH "x86_64" CACHE STRING "Target architecture (x86_32, x86_64, CUSTOM)")
option(USE_STATIC_CPP "Link MinGW/MSVC C++ runtime libraries statically" ON)
string(REGEX MATCH "32$|64$" DEFAULT_BITS "${ARCH}")
set(BITS "${DEFAULT_BITS}" CACHE STRING "Architecture bits. Needs to be set manually for custom architecture")
list(APPEND GODOT_DEFINITIONS
WINDOWS_ENABLED
$<${compiler_is_msvc}:
TYPED_METHOD_BIND
NOMINMAX
>
)
list(APPEND GODOT_CC_FLAGS
$<${compiler_is_msvc}:
/utf-8
$<$<BOOL:${USE_STATIC_CPP}>:
/MT
>
$<$<NOT:$<BOOL:${USE_STATIC_CPP}>>:
/MD
>
>
)
list(APPEND GODOT_LINK_FLAGS
$<${compiler_is_msvc}:
/WX
>
$<$<NOT:${compiler_is_msvc}>:
-Wl,--no-undefined
$<$<BOOL:${USE_STATIC_CPP}>:
-static
-static-libgcc
-static-libstdc++
>
>
)
list(APPEND GODOT_COMPILE_WARNING_FLAGS
$<$<NOT:${compiler_is_msvc}>:
-Wwrite-strings
>
)
string(APPEND LIBRARY_SUFFIX ".${ARCH}")