2018-12-02 20:07:26 +00:00
|
|
|
# cmake arguments
|
|
|
|
# CMAKE_BUILD_TYPE: Compilation target (Debug or Release defaults to Debug)
|
2021-08-18 14:03:52 +00:00
|
|
|
#
|
2018-12-02 20:07:26 +00:00
|
|
|
# godot-cpp cmake arguments
|
2023-01-19 13:01:50 +00:00
|
|
|
# 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")
|
2021-08-18 14:03:52 +00:00
|
|
|
#
|
2018-12-02 20:07:26 +00:00
|
|
|
# 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
|
2021-08-18 14:03:52 +00:00
|
|
|
#
|
2018-12-02 20:07:26 +00:00
|
|
|
# Examples
|
2021-08-18 14:03:52 +00:00
|
|
|
#
|
2018-12-02 20:07:26 +00:00
|
|
|
# Builds a debug version:
|
|
|
|
# cmake .
|
|
|
|
# cmake --build .
|
2021-08-18 14:03:52 +00:00
|
|
|
#
|
2018-12-02 20:07:26 +00:00
|
|
|
# Builds a release version with clang
|
|
|
|
# CC=/usr/bin/clang CXX=/usr/bin/clang++ cmake -DCMAKE_BUILD_TYPE=Release -G "Unix Makefiles" .
|
|
|
|
# cmake --build .
|
2021-08-18 14:03:52 +00:00
|
|
|
#
|
2018-12-02 20:07:26 +00:00
|
|
|
# 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 --build .
|
2021-08-18 14:03:52 +00:00
|
|
|
#
|
2018-12-02 20:07:26 +00:00
|
|
|
# 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 .
|
2021-08-18 14:03:52 +00:00
|
|
|
#
|
2018-12-02 20:07:26 +00:00
|
|
|
# Todo
|
|
|
|
# Test build for Windows, Mac and mingw.
|
|
|
|
|
2022-12-03 21:43:00 +00:00
|
|
|
cmake_minimum_required(VERSION 3.12)
|
2023-05-06 06:55:41 +00:00
|
|
|
project(godot-cpp LANGUAGES CXX)
|
2018-12-02 20:07:26 +00:00
|
|
|
|
2020-09-20 10:18:42 +00:00
|
|
|
option(GENERATE_TEMPLATE_GET_NODE "Generate a template version of the Node class's get_node." ON)
|
2023-02-02 17:58:03 +00:00
|
|
|
option(GODOT_CPP_SYSTEM_HEADERS "Expose headers as SYSTEM." ON)
|
2023-01-19 13:01:50 +00:00
|
|
|
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/" )
|
|
|
|
|
2022-12-23 17:38:35 +00:00
|
|
|
# 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>" )
|
|
|
|
|
2018-12-02 20:07:26 +00:00
|
|
|
# Default build type is Debug in the SConstruct
|
2022-02-05 20:34:24 +00:00
|
|
|
if("${CMAKE_BUILD_TYPE}" STREQUAL "")
|
2018-12-02 20:07:26 +00:00
|
|
|
set(CMAKE_BUILD_TYPE Debug)
|
|
|
|
endif()
|
|
|
|
|
2022-02-05 20:34:24 +00:00
|
|
|
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()
|
2022-03-20 16:19:27 +00:00
|
|
|
|
2022-12-13 23:40:17 +00:00
|
|
|
# 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()
|
2018-12-02 20:07:26 +00:00
|
|
|
|
2023-08-16 09:47:19 +00:00
|
|
|
set(FLOAT_PRECISION "single" CACHE STRING "")
|
|
|
|
if ("${FLOAT_PRECISION}" STREQUAL "double")
|
|
|
|
add_definitions(-DREAL_T_IS_DOUBLE)
|
|
|
|
endif()
|
|
|
|
|
2018-12-02 20:07:26 +00:00
|
|
|
set(GODOT_COMPILE_FLAGS )
|
|
|
|
|
|
|
|
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
|
|
|
|
# using Visual Studio C++
|
2023-08-16 09:47:19 +00:00
|
|
|
set(GODOT_COMPILE_FLAGS "/utf-8") # /GF /MP
|
2018-12-02 20:07:26 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2022-09-11 11:12:44 +00:00
|
|
|
add_definitions(-DNOMINMAX)
|
2022-07-18 10:58:50 +00:00
|
|
|
else() # GCC/Clang
|
2018-12-02 20:07:26 +00:00
|
|
|
if(CMAKE_BUILD_TYPE MATCHES Debug)
|
2023-10-26 06:57:13 +00:00
|
|
|
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-omit-frame-pointer -O0 -g")
|
2018-12-02 20:07:26 +00:00
|
|
|
else()
|
|
|
|
set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -O3")
|
|
|
|
endif(CMAKE_BUILD_TYPE MATCHES Debug)
|
|
|
|
endif()
|
|
|
|
|
2023-08-16 09:47:19 +00:00
|
|
|
# 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()
|
|
|
|
|
2018-12-02 20:07:26 +00:00
|
|
|
# Generate source from the bindings file
|
2021-12-29 14:02:38 +00:00
|
|
|
find_package(Python3 3.4 REQUIRED) # pathlib should be present
|
2020-09-20 10:18:42 +00:00
|
|
|
if(GENERATE_TEMPLATE_GET_NODE)
|
|
|
|
set(GENERATE_BINDING_PARAMETERS "True")
|
|
|
|
else()
|
|
|
|
set(GENERATE_BINDING_PARAMETERS "False")
|
|
|
|
endif()
|
|
|
|
|
2022-12-13 23:40:17 +00:00
|
|
|
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)"
|
2019-04-11 09:56:42 +00:00
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
2021-08-28 22:15:12 +00:00
|
|
|
OUTPUT_VARIABLE GENERATED_FILES_LIST
|
2023-11-11 12:59:33 +00:00
|
|
|
OUTPUT_STRIP_TRAILING_WHITESPACE
|
2020-02-02 19:47:45 +00:00
|
|
|
)
|
|
|
|
|
2021-08-28 22:15:12 +00:00
|
|
|
add_custom_command(OUTPUT ${GENERATED_FILES_LIST}
|
2023-01-09 10:03:07 +00:00
|
|
|
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}\")"
|
2020-02-02 19:47:45 +00:00
|
|
|
VERBATIM
|
|
|
|
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
|
2022-12-13 23:40:17 +00:00
|
|
|
MAIN_DEPENDENCY ${GODOT_GDEXTENSION_API_FILE}
|
2020-02-02 19:47:45 +00:00
|
|
|
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/binding_generator.py
|
2021-09-01 21:43:07 +00:00
|
|
|
COMMENT "Generating bindings"
|
2020-02-02 19:47:45 +00:00
|
|
|
)
|
2018-12-02 20:07:26 +00:00
|
|
|
|
|
|
|
# Get Sources
|
2022-12-03 21:43:00 +00:00
|
|
|
file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS src/*.c**)
|
|
|
|
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS include/*.h**)
|
2018-12-02 20:07:26 +00:00
|
|
|
|
|
|
|
# Define our godot-cpp library
|
2022-12-23 17:38:35 +00:00
|
|
|
add_library(${PROJECT_NAME} STATIC
|
2020-02-02 19:47:45 +00:00
|
|
|
${SOURCES}
|
|
|
|
${HEADERS}
|
2021-08-28 22:15:12 +00:00
|
|
|
${GENERATED_FILES_LIST}
|
2020-02-02 19:47:45 +00:00
|
|
|
)
|
2021-09-01 22:54:47 +00:00
|
|
|
add_library(godot::cpp ALIAS ${PROJECT_NAME})
|
2022-12-03 21:43:00 +00:00
|
|
|
|
2023-01-19 13:01:50 +00:00
|
|
|
include(GodotCompilerWarnings)
|
|
|
|
|
2022-12-03 21:43:00 +00:00
|
|
|
target_compile_features(${PROJECT_NAME}
|
|
|
|
PRIVATE
|
|
|
|
cxx_std_17
|
|
|
|
)
|
|
|
|
|
2022-07-18 10:58:50 +00:00
|
|
|
target_compile_definitions(${PROJECT_NAME} PUBLIC
|
2023-01-19 13:01:50 +00:00
|
|
|
$<$<CONFIG:Debug>:
|
|
|
|
DEBUG_ENABLED
|
|
|
|
DEBUG_METHODS_ENABLED
|
|
|
|
>
|
2023-12-05 16:36:54 +00:00
|
|
|
$<$<AND:$<BOOL:${WIN32}>,$<NOT:${compiler_is_gnu}>>:
|
2022-12-23 17:38:35 +00:00
|
|
|
TYPED_METHOD_BIND
|
|
|
|
>
|
2021-10-03 14:30:37 +00:00
|
|
|
)
|
2021-11-11 11:46:03 +00:00
|
|
|
|
2022-12-23 17:38:35 +00:00
|
|
|
target_link_options(${PROJECT_NAME} PRIVATE
|
|
|
|
$<$<NOT:${compiler_is_msvc}>:
|
|
|
|
-static-libgcc
|
|
|
|
-static-libstdc++
|
|
|
|
-Wl,-R,'$$ORIGIN'
|
|
|
|
>
|
|
|
|
)
|
2021-11-11 11:46:03 +00:00
|
|
|
|
2023-01-19 02:12:50 +00:00
|
|
|
# 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
|
2018-12-02 20:07:26 +00:00
|
|
|
include
|
2021-08-18 14:03:52 +00:00
|
|
|
${CMAKE_CURRENT_BINARY_DIR}/gen/include
|
2022-12-13 23:40:17 +00:00
|
|
|
${GODOT_GDEXTENSION_DIR}
|
2018-12-02 20:07:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Add the compile flags
|
|
|
|
set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS ${GODOT_COMPILE_FLAGS})
|
|
|
|
|
|
|
|
# Create the correct name (godot.os.build_type.system_bits)
|
2021-05-20 12:47:20 +00:00
|
|
|
string(TOLOWER "${CMAKE_SYSTEM_NAME}" SYSTEM_NAME)
|
|
|
|
string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE)
|
2018-12-02 20:07:26 +00:00
|
|
|
|
|
|
|
if(ANDROID)
|
|
|
|
# Added the android abi after system name
|
|
|
|
set(SYSTEM_NAME ${SYSTEM_NAME}.${ANDROID_ABI})
|
2022-12-03 21:43:00 +00:00
|
|
|
|
2018-12-02 20:07:26 +00:00
|
|
|
# Android does not have the bits at the end if you look at the main godot repo build
|
2022-12-03 21:43:00 +00:00
|
|
|
set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}")
|
2018-12-02 20:07:26 +00:00
|
|
|
else()
|
2022-12-03 21:43:00 +00:00
|
|
|
set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}.${BITS}")
|
2018-12-02 20:07:26 +00:00
|
|
|
endif()
|
2022-12-03 21:43:00 +00:00
|
|
|
|
|
|
|
set_target_properties(${PROJECT_NAME}
|
|
|
|
PROPERTIES
|
|
|
|
CXX_EXTENSIONS OFF
|
2022-12-23 17:38:35 +00:00
|
|
|
POSITION_INDEPENDENT_CODE ON
|
2022-12-03 21:43:00 +00:00
|
|
|
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}"
|
|
|
|
)
|