From cc25ef616b2bc0d0dcc29999717626be5bb49a31 Mon Sep 17 00:00:00 2001 From: Samuel Nicholas Date: Mon, 23 Sep 2024 12:07:03 +0930 Subject: [PATCH] Modernise Existing CMakeLists.txt == godot-cpp == Moved find package for python to root CMakeLists.txt Silenced warning for unused CMAKE_C_COMPILER when specified in toolchain Changed godot-cpp compile feature cxx_std_17 to PUBLIC to propagate to consumers Changed the MSVC warning flags to PUBLIC to propagate to consumers Turn on WINDOWS_ENABLED based on WIN32, not IS_MSVC New cmake/sources.cmake to collect all the pre-existing source files, because globing is evil. Made all godot-cpp sources PRIVATE Exposed headers as INTERFACE using target_include_directories for consumers ### Generator Expressions Renamed generator expression helper variables to ease readability Moved all flags to generator expressions Moved generator expression helpers to common_compiler_flags.cmake Refactor SYSTEM_NAME and BUILD_TYPE to use generator expressions Refactor HOT_RELOAD to use generator expressions CMAKE_BUILD_TYPE is no longer depended upon, eliminating config errors for multi-config builds like Visual Studio, and Ninja-MultiConfig Now that we are specifying the config at build time, the hack to re-write CMAKE_CXX_FLAGS(_DEBUG) flags is no longer needed. Remove CMAKE_BUILD_TYPE from msvc CI target as it is a Multi-Config generator and ignores it ### test as a target Update test/CmakeLists.txt to use target_link_libraries to pull in required things from godot-cpp target Add EXCLUDE_FROM_ALL to godot-cpp-test so that it isn't built as part of the 'all' target == godot-cpp-test == updated ci to build the godot-cpp-test target from the root directory using cmake Adding the LANGUAGES CXX to the test project maintains the existing behavior of not checking for C compiler during configure. Removed majority of the cmake code as it was duplicating things that propagate as transient dependency from godot-cpp --- .github/workflows/ci.yml | 17 ++- CMakeLists.txt | 17 +-- cmake/common_compiler_flags.cmake | 137 +++++++++++++++--------- cmake/godotcpp.cmake | 118 ++++----------------- cmake/sources.cmake | 96 +++++++++++++++++ test/CMakeLists.txt | 166 ++++++++---------------------- 6 files changed, 267 insertions(+), 284 deletions(-) create mode 100644 cmake/sources.cmake diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 76abdd570a..0c5e83ef8f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -221,12 +221,11 @@ jobs: - name: Build godot-cpp run: | cmake -DCMAKE_BUILD_TYPE=Release . - make -j $(nproc) VERBOSE=1 + cmake --build . --verbose -j $(nproc) -t godot-cpp - name: Build test GDExtension library run: | - cd test && cmake -DCMAKE_BUILD_TYPE=Release -DGODOT_HEADERS_PATH="../godot-headers" -DCPP_BINDINGS_PATH=".." . - make -j $(nproc) VERBOSE=1 + cmake --build . --verbose -j $(nproc) -t godot-cpp-test linux-cmake-ninja: name: 🐧 Build (Linux, GCC, CMake Ninja) @@ -245,12 +244,11 @@ jobs: - name: Build godot-cpp run: | cmake -DCMAKE_BUILD_TYPE=Release -GNinja . - cmake --build . -j $(nproc) --verbose + cmake --build . --verbose -j $(nproc) -t godot-cpp - name: Build test GDExtension library run: | - cd test && cmake -DCMAKE_BUILD_TYPE=Release -DGODOT_HEADERS_PATH="../godot-headers" -DCPP_BINDINGS_PATH=".." -GNinja . - cmake --build . -j $(nproc) --verbose + cmake --build . --verbose -j $(nproc) -t godot-cpp-test windows-msvc-cmake: name: 🏁 Build (Windows, MSVC, CMake) @@ -263,10 +261,9 @@ jobs: - name: Build godot-cpp run: | - cmake -DCMAKE_BUILD_TYPE=Release -G"Visual Studio 16 2019" . - cmake --build . --verbose + cmake -G"Visual Studio 16 2019" . + cmake --build . --verbose -t godot-cpp - name: Build test GDExtension library run: | - cd test && cmake -DCMAKE_BUILD_TYPE=Release -DGODOT_HEADERS_PATH="../godot-headers" -DCPP_BINDINGS_PATH=".." -G"Visual Studio 16 2019" . - cmake --build . --verbose + cmake --build . --verbose -t godot-cpp-test diff --git a/CMakeLists.txt b/CMakeLists.txt index ff77368ba6..f968081f78 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,16 +1,14 @@ cmake_minimum_required(VERSION 3.13) project(godot-cpp LANGUAGES CXX) -# Configure CMake -# https://discourse.cmake.org/t/how-do-i-remove-compile-options-from-target/5965 -# https://stackoverflow.com/questions/74426638/how-to-remove-rtc1-from-specific-target-or-file-in-cmake -if(${CMAKE_CXX_COMPILER_ID} STREQUAL MSVC) - if(NOT CMAKE_BUILD_TYPE MATCHES Debug) - STRING(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") - string(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG}) - endif () +# Get Python +find_package(Python3 3.4 REQUIRED) # pathlib should be present + +if( CMAKE_C_COMPILER) + #Silence warning from unused CMAKE_C_COMPILER from toolchain endif () +# Main Library include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake ) # I know this doesn't look like a typical CMakeLists.txt, but as we are @@ -22,3 +20,6 @@ include( ${PROJECT_SOURCE_DIR}/cmake/godotcpp.cmake ) godotcpp_options() godotcpp_generate() + +# Test Example +add_subdirectory( test ) diff --git a/cmake/common_compiler_flags.cmake b/cmake/common_compiler_flags.cmake index 94556415b1..9e14d8ccdd 100644 --- a/cmake/common_compiler_flags.cmake +++ b/cmake/common_compiler_flags.cmake @@ -1,15 +1,33 @@ -# Add warnings based on compiler & version -# Set some helper variables for readability -set( compiler_less_than_v8 "$,8>" ) -set( compiler_greater_than_or_equal_v9 "$,9>" ) -set( compiler_greater_than_or_equal_v11 "$,11>" ) -set( compiler_less_than_v11 "$,11>" ) -set( compiler_greater_than_or_equal_v12 "$,12>" ) +#Generator Expression Helpers +set( IS_CLANG "$,$>" ) +set( IS_GNU "$" ) +set( IS_MSVC "$" ) + +set( GNU_LT_V8 "$,8>" ) +set( GNU_GE_V9 "$,9>" ) +set( GNU_GT_V11 "$,11>" ) +set( GNU_LT_V11 "$,11>" ) +set( GNU_GE_V12 "$,12>" ) + +set( WARNING_AS_ERROR "$") + +set( NOT_RELEASE "$>") + +set( HOT_RELOAD-UNSET "$") +set( HOT_RELOAD "$>" ) + +set( DISABLE_EXCEPTIONS "$") + +target_compile_features(${PROJECT_NAME} + PUBLIC + cxx_std_17 +) # These compiler options reflect what is in godot/SConstruct. -target_compile_options( ${PROJECT_NAME} PRIVATE +target_compile_options( ${PROJECT_NAME} +PUBLIC # MSVC only - $<${compiler_is_msvc}: + $<${IS_MSVC}: /W4 # Disable warnings which we don't plan to fix. @@ -23,26 +41,30 @@ target_compile_options( ${PROJECT_NAME} PRIVATE /wd4514 # C4514 (unreferenced inline function has been removed) /wd4714 # C4714 (function marked as __forceinline not inlined) /wd4820 # C4820 (padding added after construct) + $<${WARNING_AS_ERROR}:/WX> > +PRIVATE # Clang and GNU common options - $<$: + $<$: -Wall -Wctor-dtor-privacy -Wextra -Wno-unused-parameter -Wnon-virtual-dtor -Wwrite-strings + + $<${WARNING_AS_ERROR}:-Werror> > # Clang only - $<${compiler_is_clang}: + $<${IS_CLANG}: -Wimplicit-fallthrough -Wno-ordered-compare-function-pointers > # GNU only - $<${compiler_is_gnu}: + $<${IS_GNU}: -Walloc-zero -Wduplicated-branches -Wduplicated-cond @@ -50,45 +72,66 @@ target_compile_options( ${PROJECT_NAME} PRIVATE -Wplacement-new=1 -Wshadow-local -Wstringop-overflow=4 - > - $<$: + # Bogus warning fixed in 8+. - -Wno-strict-overflow - > - $<$: - -Wattribute-alias=2 - > - $<$: + $<${GNU_LT_V8}:-Wno-strict-overflow> + + $<${GNU_GE_V9}:-Wattribute-alias=2> + # Broke on MethodBind templates before GCC 11. - -Wlogical-op - > - $<$: + $<${GNU_GT_V11}:-Wlogical-op> + # Regression in GCC 9/10, spams so much in our variadic templates that we need to outright disable it. - -Wno-type-limits - > - $<$: + $<${GNU_LT_V11}:-Wno-type-limits> + # False positives in our error macros, see GH-58747. - -Wno-return-type + $<${GNU_GE_V12}:-Wno-return-type> + > +PUBLIC + $<${IS_MSVC}: + /utf-8 + $,/MDd,/MD /O2> + $<$:/EHsc> + > + + $<$: + $<${DISABLE_EXCEPTIONS}:-fno-exceptions> + + $,-fno-omit-frame-pointer -O0 -g,-O3> > + $<${IS_GNU}:$<${HOT_RELOAD}:-fno-gnu-unique>> +) + +target_compile_definitions(${PROJECT_NAME} + PUBLIC + GDEXTENSION + + $<$:WINDOWS_ENABLED> + + $<${IS_MSVC}: + TYPED_METHOD_BIND + NOMINMAX + $<${DISABLE_EXCEPTIONS}:_HAS_EXCEPTIONS=0> + > + + $,DEBUG_ENABLED DEBUG_METHODS_ENABLED,NDEBUG> + + $<${HOT_RELOAD}:HOT_RELOAD_ENABLED> + + $<$:REAL_T_IS_DOUBLE> ) -# 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> - $<$:-Werror> - ) - endif() -endfunction() - -if ( GODOT_WARNING_AS_ERROR ) - set_warning_as_error() -endif() +target_link_options(${PROJECT_NAME} PRIVATE + $<$: + -static-libgcc + -static-libstdc++ + > + # reading up on RPATH this is only relevant for unix based systems + # https://stackoverflow.com/questions/107888/is-there-a-windows-msvc-equivalent-to-the-rpath-linker-flag + # Is probably worth putting it behind another guard in the future. + # It appears that gcc silently ignores it on windows + # It's also probably unnecessary to be explicitly stated as CMake has a target property for it. + $<${IS_GNU}: + -Wl,-R,'$$ORIGIN' + > +) diff --git a/cmake/godotcpp.cmake b/cmake/godotcpp.cmake index a5c6677964..047da926b2 100644 --- a/cmake/godotcpp.cmake +++ b/cmake/godotcpp.cmake @@ -28,6 +28,8 @@ function( godotcpp_options ) 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 @@ -54,11 +56,7 @@ endfunction() function( godotcpp_generate ) - # Set some helper variables for readability - set( compiler_is_clang "$,$>" ) - set( compiler_is_gnu "$" ) - set( compiler_is_msvc "$" ) - + ### Configure variables # CXX_VISIBILITY_PRESET supported values are: default, hidden, protected, and internal # which is inline with the gcc -fvisibility= # https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html @@ -68,16 +66,7 @@ function( godotcpp_generate ) set( GODOT_SYMBOL_VISIBILITY "default" ) endif () - # Default build type is Debug in the SConstruct - if("${CMAKE_BUILD_TYPE}" STREQUAL "") - set(CMAKE_BUILD_TYPE Debug) - endif() - - # Hot reload is enabled by default in Debug-builds - if( GODOT_USE_HOT_RELOAD STREQUAL "" AND NOT CMAKE_BUILD_TYPE STREQUAL "Release") - set(GODOT_USE_HOT_RELOAD ON) - endif() - + ### Generate Bindings if(NOT DEFINED BITS) set(BITS 32) if(CMAKE_SIZEOF_VOID_P EQUAL 8) @@ -91,47 +80,7 @@ function( godotcpp_generate ) set(GODOT_GDEXTENSION_API_FILE "${GODOT_CUSTOM_API_FILE}") endif() - if ("${GODOT_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 - 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). - 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() - # Generate source from the bindings file - find_package(Python3 3.4 REQUIRED) # pathlib should be present if(GODOT_GENERATE_TEMPLATE_GET_NODE) set(GENERATE_BINDING_PARAMETERS "True") else() @@ -153,48 +102,23 @@ function( godotcpp_generate ) COMMENT "Generating bindings" ) - # Get Sources - # As this cmake file was added using 'include(godotcpp)' from the root CMakeLists.txt, - # the ${CMAKE_CURRENT_SOURCE_DIR} is still the root dir. - 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 - ${SOURCES} - ${HEADERS} - ${GENERATED_FILES_LIST} - ) + ### Define our godot-cpp library + add_library(${PROJECT_NAME} STATIC ) add_library(godot::cpp ALIAS ${PROJECT_NAME}) - include(${PROJECT_SOURCE_DIR}/cmake/common_compiler_flags.cmake) - - target_compile_features(${PROJECT_NAME} + # Get Sources + include( cmake/sources.cmake ) + target_sources( ${PROJECT_NAME} PRIVATE - cxx_std_17 - ) - - if(GODOT_USE_HOT_RELOAD) - target_compile_definitions(${PROJECT_NAME} PUBLIC HOT_RELOAD_ENABLED) - target_compile_options(${PROJECT_NAME} PUBLIC $<${compiler_is_gnu}:-fno-gnu-unique>) - endif() - - target_compile_definitions(${PROJECT_NAME} PUBLIC - $<$: - DEBUG_ENABLED - DEBUG_METHODS_ENABLED - > - $<${compiler_is_msvc}: - TYPED_METHOD_BIND - > + ${GODOTCPP_HEADERS} + ${GODOTCPP_SOURCES} + ${GENERATED_FILES_LIST} ) - target_link_options(${PROJECT_NAME} PRIVATE - $<$: - -static-libgcc - -static-libstdc++ - -Wl,-R,'$$ORIGIN' - > + #This exposes the include directory to consumers + target_include_directories( ${PROJECT_NAME} + INTERFACE + ${PROJECT_SOURCE_DIR}/include/godot_cpp ) # Optionally mark headers as SYSTEM @@ -209,12 +133,11 @@ function( godotcpp_generate ) ${GODOT_GDEXTENSION_DIR} ) - # Add the compile flags - set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS ${GODOT_COMPILE_FLAGS}) + include(${PROJECT_SOURCE_DIR}/cmake/common_compiler_flags.cmake) - # Create the correct name (godot.os.build_type.system_bits) - string(TOLOWER "${CMAKE_SYSTEM_NAME}" SYSTEM_NAME) - string(TOLOWER "${CMAKE_BUILD_TYPE}" BUILD_TYPE) + ### Create the correct name (godot.os.build_type.system_bits) + set( SYSTEM_NAME "$" ) + set( BUILD_TYPE "$>" ) if(ANDROID) # Added the android abi after system name @@ -226,6 +149,7 @@ function( godotcpp_generate ) set(OUTPUT_NAME "godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}.${BITS}") endif() + set_target_properties(${PROJECT_NAME} PROPERTIES CXX_EXTENSIONS OFF diff --git a/cmake/sources.cmake b/cmake/sources.cmake new file mode 100644 index 0000000000..939f17f437 --- /dev/null +++ b/cmake/sources.cmake @@ -0,0 +1,96 @@ +set( GODOTCPP_SOURCES + src/godot.cpp + src/classes/editor_plugin_registration.cpp + src/classes/low_level.cpp + src/classes/wrapped.cpp + src/core/class_db.cpp + src/core/error_macros.cpp + src/core/memory.cpp + src/core/method_bind.cpp + src/core/object.cpp + src/variant/aabb.cpp + src/variant/basis.cpp + src/variant/callable_custom.cpp + src/variant/callable_method_pointer.cpp + src/variant/char_string.cpp + src/variant/color.cpp + src/variant/packed_arrays.cpp + src/variant/plane.cpp + src/variant/projection.cpp + src/variant/quaternion.cpp + src/variant/rect2.cpp + src/variant/rect2i.cpp + src/variant/transform2d.cpp + src/variant/transform3d.cpp + src/variant/variant.cpp + src/variant/vector2.cpp + src/variant/vector2i.cpp + src/variant/vector3.cpp + src/variant/vector3i.cpp + src/variant/vector4.cpp + src/variant/vector4i.cpp +) +set( GODOTCPP_HEADERS + include/godot_cpp/godot.hpp + include/godot_cpp/classes/editor_plugin_registration.hpp + include/godot_cpp/classes/ref.hpp + include/godot_cpp/classes/wrapped.hpp + include/godot_cpp/core/binder_common.hpp + include/godot_cpp/core/builtin_ptrcall.hpp + include/godot_cpp/core/class_db.hpp + include/godot_cpp/core/defs.hpp + include/godot_cpp/core/engine_ptrcall.hpp + include/godot_cpp/core/error_macros.hpp + include/godot_cpp/core/math.hpp + include/godot_cpp/core/memory.hpp + include/godot_cpp/core/method_bind.hpp + include/godot_cpp/core/method_ptrcall.hpp + include/godot_cpp/core/mutex_lock.hpp + include/godot_cpp/core/object.hpp + include/godot_cpp/core/object_id.hpp + include/godot_cpp/core/property_info.hpp + include/godot_cpp/core/type_info.hpp + include/godot_cpp/templates/cowdata.hpp + include/godot_cpp/templates/hash_map.hpp + include/godot_cpp/templates/hash_set.hpp + include/godot_cpp/templates/hashfuncs.hpp + include/godot_cpp/templates/list.hpp + include/godot_cpp/templates/local_vector.hpp + include/godot_cpp/templates/pair.hpp + include/godot_cpp/templates/rb_map.hpp + include/godot_cpp/templates/rb_set.hpp + include/godot_cpp/templates/rid_owner.hpp + include/godot_cpp/templates/safe_refcount.hpp + include/godot_cpp/templates/search_array.hpp + include/godot_cpp/templates/self_list.hpp + include/godot_cpp/templates/sort_array.hpp + include/godot_cpp/templates/spin_lock.hpp + include/godot_cpp/templates/thread_work_pool.hpp + include/godot_cpp/templates/vector.hpp + include/godot_cpp/templates/vmap.hpp + include/godot_cpp/templates/vset.hpp + include/godot_cpp/variant/aabb.hpp + include/godot_cpp/variant/array_helpers.hpp + include/godot_cpp/variant/basis.hpp + include/godot_cpp/variant/callable_custom.hpp + include/godot_cpp/variant/callable_method_pointer.hpp + include/godot_cpp/variant/char_string.hpp + include/godot_cpp/variant/char_utils.hpp + include/godot_cpp/variant/color.hpp + include/godot_cpp/variant/color_names.inc.hpp + include/godot_cpp/variant/plane.hpp + include/godot_cpp/variant/projection.hpp + include/godot_cpp/variant/quaternion.hpp + include/godot_cpp/variant/rect2.hpp + include/godot_cpp/variant/rect2i.hpp + include/godot_cpp/variant/transform2d.hpp + include/godot_cpp/variant/transform3d.hpp + include/godot_cpp/variant/typed_array.hpp + include/godot_cpp/variant/variant.hpp + include/godot_cpp/variant/vector2.hpp + include/godot_cpp/variant/vector2i.hpp + include/godot_cpp/variant/vector3.hpp + include/godot_cpp/variant/vector3i.hpp + include/godot_cpp/variant/vector4.hpp + include/godot_cpp/variant/vector4i.hpp +) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 42ea6e0869..613fa28120 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,8 +1,9 @@ cmake_minimum_required(VERSION 3.13) -project(godot-cpp-test) +project(godot-cpp-test LANGUAGES CXX) -set(GODOT_GDEXTENSION_DIR ../gdextension/ CACHE STRING "Path to GDExtension interface header directory") -set(CPP_BINDINGS_PATH ../ CACHE STRING "Path to C++ bindings") +if( NOT CMAKE_BUILD_TYPE ) + set(CMAKE_BUILD_TYPE Release) +endif() if(CMAKE_SYSTEM_NAME STREQUAL "Linux") set(TARGET_PATH x11) @@ -14,130 +15,51 @@ else() message(FATAL_ERROR "Not implemented support for ${CMAKE_SYSTEM_NAME}") endif() -# Change the output directory to the bin directory -set(BUILD_PATH ${CMAKE_SOURCE_DIR}/bin/${TARGET_PATH}) -set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${BUILD_PATH}") -set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${BUILD_PATH}") -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${BUILD_PATH}") -SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${BUILD_PATH}") -SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${BUILD_PATH}") -SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_DEBUG "${BUILD_PATH}") -SET(CMAKE_LIBRARY_OUTPUT_DIRECTORY_RELEASE "${BUILD_PATH}") -SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_DEBUG "${BUILD_PATH}") -SET(CMAKE_ARCHIVE_OUTPUT_DIRECTORY_RELEASE "${BUILD_PATH}") - -# Set the c++ standard to c++17 -set(CMAKE_CXX_STANDARD 17) -set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_EXTENSIONS OFF) - -set(GODOT_COMPILE_FLAGS ) -set(GODOT_LINKER_FLAGS ) - -if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") - # using Visual Studio C++ - set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /WX") # /GF /MP - set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /DTYPED_METHOD_BIND") - - 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) - - # Disable conversion warning, truncation, unreferenced var, signed mismatch - set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} /wd4244 /wd4305 /wd4101 /wd4018 /wd4267") - - add_definitions(-DNOMINMAX) - - # Unkomment for warning level 4 - #if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") - # string(REGEX REPLACE "/W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") - #endif() +set( IS_CLANG "$,$>" ) +set( IS_GNU "$" ) +set( IS_MSVC "$" ) -else() - - set(GODOT_LINKER_FLAGS "-static-libgcc -static-libstdc++ -Wl,-R,'$$ORIGIN'") - - set(GODOT_COMPILE_FLAGS "-fPIC -g -Wwrite-strings") - - if(CMAKE_BUILD_TYPE MATCHES Debug) - set(GODOT_COMPILE_FLAGS "${GODOT_COMPILE_FLAGS} -fno-omit-frame-pointer -O0") - 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() - -# Get Sources -file(GLOB_RECURSE SOURCES src/*.c**) -file(GLOB_RECURSE HEADERS include/*.h**) # Define our godot-cpp library -add_library(${PROJECT_NAME} SHARED ${SOURCES} ${HEADERS}) - -target_include_directories(${PROJECT_NAME} SYSTEM - PRIVATE - ${CPP_BINDINGS_PATH}/include - ${CPP_BINDINGS_PATH}/gen/include - ${GODOT_GDEXTENSION_DIR} +add_library(${PROJECT_NAME} SHARED EXCLUDE_FROM_ALL ) + +target_sources(${PROJECT_NAME} + PRIVATE + src/example.cpp + src/example.h + src/register_types.cpp + src/register_types.h + src/tests.h ) -# Create the correct name (godot.os.build_type.system_bits) -# Synchronized with godot-cpp's CMakeLists.txt - -set(BITS 32) -if(CMAKE_SIZEOF_VOID_P EQUAL 8) - set(BITS 64) -endif(CMAKE_SIZEOF_VOID_P EQUAL 8) - -if(CMAKE_BUILD_TYPE MATCHES Debug) - set(GODOT_CPP_BUILD_TYPE Debug) -else() - set(GODOT_CPP_BUILD_TYPE Release) -endif() +target_link_libraries(${PROJECT_NAME} + PRIVATE + godot::cpp ) -string(TOLOWER ${CMAKE_SYSTEM_NAME} SYSTEM_NAME) -string(TOLOWER ${GODOT_CPP_BUILD_TYPE} BUILD_TYPE) - -if(ANDROID) - # Added the android abi after system name - set(SYSTEM_NAME ${SYSTEM_NAME}.${ANDROID_ABI}) -endif() - -if(CMAKE_VERSION VERSION_GREATER "3.13") - target_link_directories(${PROJECT_NAME} - PRIVATE - ${CPP_BINDINGS_PATH}/bin/ - ) - - target_link_libraries(${PROJECT_NAME} - godot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}$<$>:.${BITS}> - ) -else() - target_link_libraries(${PROJECT_NAME} - ${CPP_BINDINGS_PATH}/bin/libgodot-cpp.${SYSTEM_NAME}.${BUILD_TYPE}$<$>:.${BITS}>.a - ) -endif() +target_compile_options(${PROJECT_NAME} + PRIVATE + $<$: + -fPIC + -Wwrite-strings + > +) -# Add the compile flags -set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS ${GODOT_COMPILE_FLAGS}) -set_property(TARGET ${PROJECT_NAME} APPEND_STRING PROPERTY LINK_FLAGS ${GODOT_LINKER_FLAGS}) +target_link_options( ${PROJECT_NAME} + PRIVATE + $<$: + -static-libgcc + -static-libstdc++ + > + $<${IS_GNU}: # see comments in the cmake/common_compiler_flags.cmake + -Wl,-R,'$$ORIGIN' + > +) -set_property(TARGET ${PROJECT_NAME} PROPERTY OUTPUT_NAME "gdexample") +set_target_properties( ${PROJECT_NAME} + PROPERTIES + OUTPUT_NAME "gdexample" + POSITION_INDEPENDENT_CODE ON + BUILD_RPATH_USE_ORIGIN ON + LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/${TARGET_PATH} + RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin/${TARGET_PATH} +)