Skip to content

Commit

Permalink
CMake: Target as configuration option
Browse files Browse the repository at this point in the history
Add GODOTCPP_TARGET configuration option
Remove loop to generate the godot-cpp.<target> CMake Targets

Rename test bindings target
Update documentation
  • Loading branch information
enetheru committed Mar 11, 2025
1 parent a3f8921 commit 98d0777
Show file tree
Hide file tree
Showing 11 changed files with 225 additions and 255 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ jobs:
run: |
mkdir cmake-build
cd cmake-build
cmake ../ -DGODOTCPP_ENABLE_TESTING=YES
cmake --build . --verbose -j $(nproc) -t godot-cpp.test.template_release --config Release
cmake ../ -DGODOTCPP_ENABLE_TESTING=YES -DGODOTCPP_TARGET=template_release
cmake --build . --verbose -j $(nproc) --target godot-cpp-test --config Release
windows-msvc-cmake:
name: 🏁 Build (Windows, MSVC, CMake)
Expand All @@ -218,5 +218,5 @@ jobs:
run: |
mkdir cmake-build
cd cmake-build
cmake ../ -DGODOTCPP_ENABLE_TESTING=YES
cmake --build . --verbose -t godot-cpp.test.template_release --config Release
cmake ../ -DGODOTCPP_ENABLE_TESTING=YES -DGODOTCPP_TARGET=template_release
cmake --build . --verbose --target godot-cpp-test --config Release
2 changes: 1 addition & 1 deletion cmake/android.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ endfunction()

#[===========================[ Target Generation ]===========================]
function(android_generate)
target_compile_definitions(${TARGET_NAME} PUBLIC ANDROID_ENABLED UNIX_ENABLED)
target_compile_definitions(godot-cpp PUBLIC ANDROID_ENABLED UNIX_ENABLED)

common_compiler_flags()
endfunction()
6 changes: 3 additions & 3 deletions cmake/common_compiler_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function(common_compiler_flags)
# gersemi: off
# These compiler options reflect what is in godot/SConstruct.
target_compile_options(
${TARGET_NAME}
godot-cpp
PUBLIC
# Disable exception handling. Godot doesn't use exceptions anywhere, and this
# saves around 20% of binary size and very significant build time.
Expand Down Expand Up @@ -146,7 +146,7 @@ function(common_compiler_flags)
)

target_compile_definitions(
${TARGET_NAME}
godot-cpp
PUBLIC
GDEXTENSION

Expand All @@ -165,7 +165,7 @@ function(common_compiler_flags)
)

target_link_options(
${TARGET_NAME}
godot-cpp
PUBLIC
$<${IS_MSVC}:
/WX # treat link warnings as errors.
Expand Down
148 changes: 70 additions & 78 deletions cmake/godotcpp.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ function(godotcpp_options)
#NOTE: arch is managed by using toolchain files.
# To create a universal build for macos, set CMAKE_OSX_ARCHITECTURES

set(GODOTCPP_TARGET
"template_debug"
CACHE STRING
"Which target to generate. valid values are: template_debug, template_release, and editor"
)
set_property(CACHE GODOTCPP_TARGET PROPERTY STRINGS "template_debug;template_release;editor")

# Input from user for GDExtension interface header and the API JSON file
set(GODOTCPP_GDEXTENSION_DIR
"gdextension"
Expand Down Expand Up @@ -305,94 +312,79 @@ function(godotcpp_generate)
set(IS_DEV_BUILD "$<BOOL:${GODOTCPP_DEV_BUILD}>")

### Define our godot-cpp library targets
foreach(TARGET_ALIAS template_debug template_release editor)
set(TARGET_NAME "godot-cpp.${TARGET_ALIAS}")

# Generator Expressions that rely on the target
set(DEBUG_FEATURES "$<NOT:$<STREQUAL:${TARGET_ALIAS},template_release>>")
set(HOT_RELOAD "$<IF:${HOT_RELOAD-UNSET},${DEBUG_FEATURES},$<BOOL:${GODOTCPP_USE_HOT_RELOAD}>>")

# Suffix
string(
CONCAT
GODOTCPP_SUFFIX
"$<1:.${SYSTEM_NAME}>"
"$<1:.${TARGET_ALIAS}>"
"$<${IS_DEV_BUILD}:.dev>"
"$<$<STREQUAL:${GODOTCPP_PRECISION},double>:.double>"
"$<1:.${ARCH_NAME}>"
# TODO IOS_SIMULATOR
"$<$<NOT:${THREADS_ENABLED}>:.nothreads>"
)

# People are compiling godot by itself.
set(EXCLUDE EXCLUDE_FROM_ALL)
if(GODOTCPP_IS_TOP_LEVEL)
if(TARGET_ALIAS STREQUAL template_debug)
set(EXCLUDE "")
endif()
endif()
# Generator Expressions that rely on the target
set(DEBUG_FEATURES "$<NOT:$<STREQUAL:${GODOTCPP_TARGET},template_release>>")
set(HOT_RELOAD "$<IF:${HOT_RELOAD-UNSET},${DEBUG_FEATURES},$<BOOL:${GODOTCPP_USE_HOT_RELOAD}>>")

# the godot-cpp.* library targets
add_library(${TARGET_NAME} STATIC ${EXCLUDE})
# Suffix
string(
CONCAT
GODOTCPP_SUFFIX
"$<1:.${SYSTEM_NAME}>"
"$<1:.${GODOTCPP_TARGET}>"
"$<${IS_DEV_BUILD}:.dev>"
"$<$<STREQUAL:${GODOTCPP_PRECISION},double>:.double>"
"$<1:.${ARCH_NAME}>"
# TODO IOS_SIMULATOR
"$<$<NOT:${THREADS_ENABLED}>:.nothreads>"
)

add_library(godot-cpp::${TARGET_ALIAS} ALIAS ${TARGET_NAME})
# the godot-cpp.* library targets
add_library(godot-cpp STATIC)

file(GLOB_RECURSE GODOTCPP_SOURCES LIST_DIRECTORIES NO CONFIGURE_DEPENDS src/*.cpp)
# Added for backwards compatibility with prior cmake solution so that builds dont immediately break
# from a missing target.
add_library(godot::cpp ALIAS godot-cpp)

target_sources(${TARGET_NAME} PRIVATE ${GODOTCPP_SOURCES} ${GENERATED_FILES_LIST})
file(GLOB_RECURSE GODOTCPP_SOURCES LIST_DIRECTORIES NO CONFIGURE_DEPENDS src/*.cpp)

target_include_directories(
${TARGET_NAME}
${GODOTCPP_SYSTEM_HEADERS_ATTRIBUTE}
PUBLIC include ${CMAKE_CURRENT_BINARY_DIR}/gen/include ${GODOTCPP_GDEXTENSION_DIR}
)
target_sources(godot-cpp PRIVATE ${GODOTCPP_SOURCES} ${GENERATED_FILES_LIST})

# gersemi: off
set_target_properties(
${TARGET_NAME}
PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
CXX_VISIBILITY_PRESET ${GODOTCPP_SYMBOL_VISIBILITY}
target_include_directories(
godot-cpp
${GODOTCPP_SYSTEM_HEADERS_ATTRIBUTE}
PUBLIC include ${CMAKE_CURRENT_BINARY_DIR}/gen/include ${GODOTCPP_GDEXTENSION_DIR}
)

COMPILE_WARNING_AS_ERROR ${GODOTCPP_WARNING_AS_ERROR}
POSITION_INDEPENDENT_CODE ON
BUILD_RPATH_USE_ORIGIN ON
# gersemi: off
set_target_properties(
godot-cpp
PROPERTIES
CXX_STANDARD 17
CXX_EXTENSIONS OFF
CXX_VISIBILITY_PRESET ${GODOTCPP_SYMBOL_VISIBILITY}

PREFIX "lib"
OUTPUT_NAME "${PROJECT_NAME}${GODOTCPP_SUFFIX}"
COMPILE_WARNING_AS_ERROR ${GODOTCPP_WARNING_AS_ERROR}
POSITION_INDEPENDENT_CODE ON
BUILD_RPATH_USE_ORIGIN ON

ARCHIVE_OUTPUT_DIRECTORY "$<1:${CMAKE_BINARY_DIR}/bin>"
PREFIX "lib"
OUTPUT_NAME "${PROJECT_NAME}${GODOTCPP_SUFFIX}"

# Things that are handy to know for dependent targets
GODOTCPP_PLATFORM "${SYSTEM_NAME}"
GODOTCPP_TARGET "${TARGET_ALIAS}"
GODOTCPP_ARCH "${ARCH_NAME}"
GODOTCPP_PRECISION "${GODOTCPP_PRECISION}"
GODOTCPP_SUFFIX "${GODOTCPP_SUFFIX}"
ARCHIVE_OUTPUT_DIRECTORY "$<1:${CMAKE_BINARY_DIR}/bin>"

# Some IDE's respect this property to logically group targets
FOLDER "godot-cpp"
)
# gersemi: on

if(CMAKE_SYSTEM_NAME STREQUAL Android)
android_generate()
elseif(CMAKE_SYSTEM_NAME STREQUAL iOS)
ios_generate()
elseif(CMAKE_SYSTEM_NAME STREQUAL Linux)
linux_generate()
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
macos_generate()
elseif(CMAKE_SYSTEM_NAME STREQUAL Emscripten)
web_generate()
elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)
windows_generate()
endif()
endforeach()
# Things that are handy to know for dependent targets
GODOTCPP_PLATFORM "${SYSTEM_NAME}"
GODOTCPP_TARGET "${GODOTCPP_TARGET}"
GODOTCPP_ARCH "${ARCH_NAME}"
GODOTCPP_PRECISION "${GODOTCPP_PRECISION}"
GODOTCPP_SUFFIX "${GODOTCPP_SUFFIX}"

# Added for backwards compatibility with prior cmake solution so that builds dont immediately break
# from a missing target.
add_library(godot::cpp ALIAS godot-cpp.template_debug)
# Some IDE's respect this property to logically group targets
FOLDER "godot-cpp"
)
# gersemi: on
if(CMAKE_SYSTEM_NAME STREQUAL Android)
android_generate()
elseif(CMAKE_SYSTEM_NAME STREQUAL iOS)
ios_generate()
elseif(CMAKE_SYSTEM_NAME STREQUAL Linux)
linux_generate()
elseif(CMAKE_SYSTEM_NAME STREQUAL Darwin)
macos_generate()
elseif(CMAKE_SYSTEM_NAME STREQUAL Emscripten)
web_generate()
elseif(CMAKE_SYSTEM_NAME STREQUAL Windows)
windows_generate()
endif()
endfunction()
2 changes: 1 addition & 1 deletion cmake/ios.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ endfunction()

#[===========================[ Target Generation ]===========================]
function(ios_generate)
target_compile_definitions(${TARGET_NAME} PUBLIC IOS_ENABLED UNIX_ENABLED)
target_compile_definitions(godot-cpp PUBLIC IOS_ENABLED UNIX_ENABLED)

common_compiler_flags()
endfunction()
2 changes: 1 addition & 1 deletion cmake/linux.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ endfunction()

#[===========================[ Target Generation ]===========================]
function(linux_generate)
target_compile_definitions(${TARGET_NAME} PUBLIC LINUX_ENABLED UNIX_ENABLED)
target_compile_definitions(godot-cpp PUBLIC LINUX_ENABLED UNIX_ENABLED)

common_compiler_flags()
endfunction()
6 changes: 3 additions & 3 deletions cmake/macos.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ endfunction()

#[===========================[ Target Generation ]===========================]
function(macos_generate)
target_compile_definitions(${TARGET_NAME} PUBLIC MACOS_ENABLED UNIX_ENABLED)
target_compile_definitions(godot-cpp PUBLIC MACOS_ENABLED UNIX_ENABLED)

target_link_options(${TARGET_NAME} PUBLIC -Wl,-undefined,dynamic_lookup)
target_link_options(godot-cpp PUBLIC -Wl,-undefined,dynamic_lookup)

target_link_libraries(${TARGET_NAME} INTERFACE ${COCOA_LIBRARY})
target_link_libraries(godot-cpp INTERFACE ${COCOA_LIBRARY})

common_compiler_flags()
endfunction()
6 changes: 3 additions & 3 deletions cmake/web.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ endfunction()

#[===========================[ Target Generation ]===========================]
function(web_generate)
target_compile_definitions(${TARGET_NAME} PUBLIC WEB_ENABLED UNIX_ENABLED)
target_compile_definitions(godot-cpp PUBLIC WEB_ENABLED UNIX_ENABLED)

target_compile_options(
${TARGET_NAME}
godot-cpp
PUBLIC #
-sSIDE_MODULE
-sSUPPORT_LONGJMP=wasm
Expand All @@ -28,7 +28,7 @@ function(web_generate)
)

target_link_options(
${TARGET_NAME}
godot-cpp
INTERFACE #
-sWASM_BIGINT
-sSUPPORT_LONGJMP=wasm
Expand Down
6 changes: 3 additions & 3 deletions cmake/windows.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,16 @@ endfunction()
function(windows_generate)
set(STATIC_CPP "$<BOOL:${GODOTCPP_USE_STATIC_CPP}>")

set_target_properties(${TARGET_NAME} PROPERTIES PDB_OUTPUT_DIRECTORY "$<1:${CMAKE_SOURCE_DIR}/bin>")
set_target_properties(godot-cpp PROPERTIES PDB_OUTPUT_DIRECTORY "$<1:${CMAKE_SOURCE_DIR}/bin>")

target_compile_definitions(
${TARGET_NAME}
godot-cpp
PUBLIC WINDOWS_ENABLED $<${IS_MSVC}: TYPED_METHOD_BIND NOMINMAX >
)

# gersemi: off
target_link_options(
${TARGET_NAME}
godot-cpp
PUBLIC
$<${NOT_MSVC}:
-Wl,--no-undefined
Expand Down
Loading

0 comments on commit 98d0777

Please sign in to comment.