diff --git a/CMakeLists.txt b/CMakeLists.txt index 91ba7c49..68e7ca0e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,51 +1,57 @@ -# cmake arguments -# CMAKE_BUILD_TYPE: Compilation target (editor, template_debug, template_release) +# Main cmake arguments +# +# CMAKE_BUILD_TYPE Compilation target (Debug, Release, RelWithDebInfo, MinSizeRel) +# +# CMAKE_CONFIGURATION_TYPES Set to "Debug;Release;RelWithDebInfo;MinSizeRel" +# if not all needed configurations are created in multi-config generator +# (Setting it in script causes crash on Ninja Multi-Config) +# +# TARGET Godot build target (EDITOR, TEMPLATE_DEBUG, TEMPLATE_RELEASE) # # PLATFORM: Platform type (LINUX, MACOS, WINDOWS, ANDROID, IOS, WEB) # Auto-detected by default depending on current OS or chosen toolchain # # other global and chosen platform-specific options: # -# cmake -LH +# cmake -LH # -# Note: use -Bbuild_dir option to build in separate directories +# Note: use `-B ` option to build in separate directories # for different configurations # -# cmake -Bbuild && cmake --build build +# cmake -B build && cmake --build build # # Examples # # Builds default configuration: -# cmake . -# cmake --build . +# cmake . -Bbuild +# cmake --build build # # Builds template_release version # -# cmake -DCMAKE_BUILD_TYPE=template_release . -# cmake --build . +# cmake -DTARGET=TEMPLATE_RELEASE -Bbuild +# cmake --build build # -# Creates multi-config setup and builds 2 library versions (example for Linux) +# Creates multi-config setup and builds Release version (example for Linux) # -# cmake -G "Ninja Multi-Config" . -# cmake --build . --config editor -# cmake --build . --config template_release +# cmake -G "Ninja Multi-Config" -Bbuild +# cmake --build build --config Release # # Builds web version, using Emscripten toolchain # -# cmake --toolchain /usr/lib/emscripten/cmake/Modules/Platform/Emscripten.cmake . -# cmake --build . +# cmake --toolchain /usr/lib/emscripten/cmake/Modules/Platform/Emscripten.cmake -Bbuild +# cmake --build build # # # Builds an android armeabi-v7a debug version: # 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 . +# -DANDROID_TOOLCHAIN_NAME=arm-linux-androideabi-4.9 -DANDROID_TOOLCHAIN=clang -DANDROID_PLATFORM=21 -Bbuild +# cmake --build build # # -# Todo -# Test build for Windows, Mac and mingw. +# TODO: +# Test builds for MacOS/IOS -cmake_minimum_required(VERSION 3.24) +cmake_minimum_required(VERSION 3.12) project(godot-cpp LANGUAGES CXX) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/") @@ -108,16 +114,17 @@ target_compile_options(${PROJECT_NAME} PRIVATE target_link_options(${PROJECT_NAME} PRIVATE ${GODOT_LINK_FLAGS}) -target_compile_definitions(${PROJECT_NAME} PRIVATE ${GODOT_DEFINITIONS}) +target_compile_definitions(${PROJECT_NAME} PUBLIC ${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 "${PROJECT_NAME}${LIBRARY_SUFFIX}" ) - +if(${GODOT_CPP_WARNING_AS_ERROR}) + set_warning_as_error(${PROJECT_NAME}) +endif() diff --git a/cmake/GodotCompilerWarnings.cmake b/cmake/GodotCompilerWarnings.cmake index 5695e135..6b1c6a64 100644 --- a/cmake/GodotCompilerWarnings.cmake +++ b/cmake/GodotCompilerWarnings.cmake @@ -76,3 +76,20 @@ list(APPEND GODOT_COMPILE_WARNING_FLAGS -Wno-return-type > ) + +# Treat warnings as errors +function(set_warning_as_error TARGET_NAME) + message(STATUS "[${TARGET_NAME}] Treating warnings as errors") + if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24") + set_target_properties(${TARGET_NAME} + PROPERTIES + COMPILE_WARNING_AS_ERROR ON + ) + else() + target_compile_options(${TARGET_NAME} + PRIVATE + $<${compiler_is_msvc}:/WX> + $<$:-Werror> + ) + endif() +endfunction() diff --git a/cmake/android.cmake b/cmake/android.cmake index 1262e906..cf8ae87a 100644 --- a/cmake/android.cmake +++ b/cmake/android.cmake @@ -63,5 +63,3 @@ list(APPEND GODOT_LINK_FLAGS > ) -string(APPEND LIBRARY_SUFFIX ".${ARCH}") - diff --git a/cmake/godotcpp.cmake b/cmake/godotcpp.cmake index d3b4001f..d9c20a95 100644 --- a/cmake/godotcpp.cmake +++ b/cmake/godotcpp.cmake @@ -2,12 +2,14 @@ ### Options -set(CMAKE_CONFIGURATION_TYPES "template_debug;editor;template_release") +set(CONFIGS_WITH_DEBUG "Debug;RelWithDebInfo" CACHE STRING "Configurations that should have debug symbols (Modify if support for custom configurations is needed)") +# Default config if("${CMAKE_BUILD_TYPE}" STREQUAL "") - set(CMAKE_BUILD_TYPE template_debug) + set(CMAKE_BUILD_TYPE "Debug") endif() +set(TARGET "TEMPLATE_DEBUG" CACHE STRING "Target platform (EDITOR, TEMPLATE_DEBUG, TEMPLATE_RELEASE)") # Auto-detect platform if(CMAKE_SYSTEM_NAME STREQUAL "Linux") set(DEFAULT_PLATFORM "LINUX") @@ -40,7 +42,7 @@ set(GODOT_CPP_LIBRARY_TYPE "STATIC" CACHE STRING "[Experimental] Library type (S option(DEV_BUILD "Developer build with dev-only debugging code" OFF) -option(DEBUG_SYMBOLS "Build with debugging symbols" ON) +option(DEBUG_SYMBOLS "Force build with debugging symbols" OFF) option(USE_HOT_RELOAD "Enable the extra accounting required to support hot reload" ON) @@ -69,7 +71,8 @@ option(GENERATE_TEMPLATE_GET_NODE "Generate a template version of the Node class # Compiler warnings and compiler check generators include(GodotCompilerWarnings) -# Create the correct name (godot-cpp.platform.target.arch) +# Create the correct name (godot-cpp.platform.target) +# See more prefix appends in platform-specific configs if(${DEV_BUILD}) string(APPEND LIBRARY_SUFFIX ".dev") endif() @@ -81,13 +84,21 @@ endif() # Workaround of $ expanding to "" when default build set set(CONFIG "$>,${CMAKE_BUILD_TYPE},$>") -string(TOLOWER ".${PLATFORM}" platform) -string(PREPEND LIBRARY_SUFFIX "${platform}.${CONFIG}") +string(TOLOWER ".${PLATFORM}.${TARGET}" platform_target) +string(PREPEND LIBRARY_SUFFIX ${platform_target}) # Default optimization levels if OPTIMIZE=AUTO, for multi-config support -set(DEFAULT_OPTIMIZATION_DEV "$,$>") -set(DEFAULT_OPTIMIZATION_DEBUG_FEATURES "$,$,$,$>>") -set(DEFAULT_OPTIMIZATION "$<$>,STREQUAL:${OPTIMIZE},AUTO>>") +set(DEFAULT_OPTIMIZATION_DEBUG_FEATURES "$,$>") +set(DEFAULT_OPTIMIZATION "$") + +set(DEBUG_SYMBOLS_ENABLED "$,$>") + +# Clean default options +set(CMAKE_CXX_FLAGS "") +set(CMAKE_CXX_FLAGS_DEBUG "") +set(CMAKE_CXX_FLAGS_RELEASE "") +set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "") +set(CMAKE_CXX_FLAGS_MINSIZEREL "") list(APPEND GODOT_DEFINITIONS GDEXTENSION @@ -101,10 +112,10 @@ list(APPEND GODOT_DEFINITIONS $<$: REAL_T_IS_DOUBLE > - $<$>,$>: + $<$>,$>: HOT_RELOAD_ENABLE > - $<$: + $<$: TOOLS_ENABLED > @@ -115,7 +126,7 @@ list(APPEND GODOT_DEFINITIONS NDEBUG > - $<$>: + $<$>: DEBUG_ENABLED DEBUG_METHODS_ENABLED > @@ -123,28 +134,40 @@ list(APPEND GODOT_DEFINITIONS list(APPEND GODOT_CC_FLAGS $<${compiler_is_msvc}: - $<$: + $<${DEBUG_SYMBOLS_ENABLED}: /Zi /FS > - $<$,${DEFAULT_OPTIMIZATION}>: - /O2 + $<$: + $<$: + $<${DEFAULT_OPTIMIZATION}: + /O2 + > + $<${DEFAULT_OPTIMIZATION_DEBUG_FEATURES}: + /O2 + > + > + $<$: + /O1 + > + $<$: + /Od + > > - $<$,${DEFAULT_OPTIMIZATION_DEBUG_FEATURES}>: - /O2 - > - $<$: - /O1 - > - $<$,$,${DEFAULT_OPTIMIZATION_DEV}>: - /Od + $<$>: + $<$:/O2> + $<$:/O2> + $<$:/O1> + $<$:/Od> + $<$:/Od> > + > $<$: - $<$: + $<${DEBUG_SYMBOLS_ENABLED}: -gdwarf-4 - + $<$: -g3 > @@ -160,20 +183,28 @@ list(APPEND GODOT_CC_FLAGS -fvisibility=hidden > - $<$>: - -O3 + $<$: + $<$: + $<${DEFAULT_OPTIMIZATION}: + -O3 + > + $<${DEFAULT_OPTIMIZATION_DEBUG_FEATURES}: + -O2 + > + > + $<$: + -Os + > + $<$: + -Og + > > - $<$,${DEFAULT_OPTIMIZATION_DEBUG_FEATURES}>: - -O2 - > - $<$: - -Os - > - $<$: - -Og - > - $<$,${DEFAULT_OPTIMIZATION_DEV}>: - -O0 + $<$>: + $<$:-O3> + $<$:-O2> + $<$:-Os> + $<$:-Og> + $<$:-O0> > > ) @@ -186,26 +217,35 @@ list(APPEND GODOT_CXX_FLAGS > $<$: $<$: - -fno-exceptions + -fno-exceptions > > ) list(APPEND GODOT_LINK_FLAGS $<${compiler_is_msvc}: - $<$: + $<${DEBUG_SYMBOLS_ENABLED}: /DEBUG:FULL > - $<$: - /OPT:REF + $<$: + $<$: + $<${DEFAULT_OPTIMIZATION}: + /OPT:REF + > + $<${DEFAULT_OPTIMIZATION_DEBUG_FEATURES}: + /OPT:REF + /OPT:NOICF + > + > + $<$: + /OPT:REF + > > - $<$,${DEFAULT_OPTIMIZATION_DEBUG_FEATURES}>: - /OPT:REF - /OPT:NOICF - > - $<$: - /OPT:REF + $<$>: + $<$:/OPT:REF> + $<$:/OPT:REF /OPT:NOICF> + $<$:/OPT:REF> > > $<$: @@ -216,7 +256,7 @@ list(APPEND GODOT_LINK_FLAGS -fvisibility=hidden > - $<$>: + $<$: $<$: # SCons: not is_vanilla_clang(env) "-Wl,-S" "-Wl,-x" @@ -246,9 +286,15 @@ else() message(FATAL_ERROR "Platform not supported: ${PLATFORM}") endif() +string(APPEND LIBRARY_SUFFIX ".${ARCH}") + +if(${IOS_SIMULATOR}) + string(APPEND LIBRARY_SUFFIX ".simulator") +endif() + # Write all flags to file for cmake configuration debug -file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/flags.txt" +file(GENERATE OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/flags-${CONFIG}.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" + "C_FLAGS '${GODOT_CC_FLAGS}'\nCXX_FLAGS '${GODOT_CXX_FLAGS}'\nLINK_FLAGS '${GODOT_LINK_FLAGS}'\nCOMPILE_WARNING_FLAGS '${GODOT_COMPILE_WARNING_FLAGS}'\nDEFINITIONS '${GODOT_DEFINITIONS}'" TARGET ${PROJECT_NAME} ) diff --git a/cmake/ios.cmake b/cmake/ios.cmake index f863b951..41831420 100644 --- a/cmake/ios.cmake +++ b/cmake/ios.cmake @@ -60,9 +60,3 @@ list(APPEND GODOT_LINK_FLAGS > ) -string(APPEND LIBRARY_SUFFIX ".${ARCH}") - -if(${IOS_SIMULATOR}) - string(APPEND LIBRARY_SUFFIX ".simulator") -endif() - diff --git a/cmake/linux.cmake b/cmake/linux.cmake index 9266b3e6..48e9ff47 100644 --- a/cmake/linux.cmake +++ b/cmake/linux.cmake @@ -10,7 +10,7 @@ list(APPEND GODOT_DEFINITIONS ) list(APPEND GODOT_CC_FLAGS - # -fPIC is controlled by POSITION_INDEPENDENT_CODE property + # -fPIC is controlled by POSITION_INDEPENDENT_CODE property $<$: -m64 @@ -57,5 +57,3 @@ list(APPEND GODOT_COMPILE_WARNING_FLAGS -Wwrite-strings ) -string(APPEND LIBRARY_SUFFIX ".${ARCH}") - diff --git a/cmake/macos.cmake b/cmake/macos.cmake index 55b632ca..29c02719 100644 --- a/cmake/macos.cmake +++ b/cmake/macos.cmake @@ -49,5 +49,3 @@ list(APPEND GODOT_LINK_FLAGS > ) -string(APPEND LIBRARY_SUFFIX ".${ARCH}") - diff --git a/cmake/web.cmake b/cmake/web.cmake index 9426bae4..7148a952 100644 --- a/cmake/web.cmake +++ b/cmake/web.cmake @@ -23,3 +23,4 @@ list(APPEND GODOT_LINK_FLAGS -sUSE_PTHREADS=1 -sSIDE_MODULE=1 ) + diff --git a/cmake/windows.cmake b/cmake/windows.cmake index d7c9270c..05883460 100644 --- a/cmake/windows.cmake +++ b/cmake/windows.cmake @@ -18,7 +18,7 @@ list(APPEND GODOT_DEFINITIONS list(APPEND GODOT_CC_FLAGS $<${compiler_is_msvc}: /utf-8 - + $<$: /MT > @@ -49,5 +49,3 @@ list(APPEND GODOT_COMPILE_WARNING_FLAGS > ) -string(APPEND LIBRARY_SUFFIX ".${ARCH}") - diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 88176b55..5a184b47 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.24) +cmake_minimum_required(VERSION 3.6) project(gdexample) add_subdirectory( @@ -18,6 +18,11 @@ if(${PLATFORM} STREQUAL "WEB") PROPERTIES PREFIX "lib" SUFFIX ".wasm" + RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/project/bin" + RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/project/bin" + RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_CURRENT_SOURCE_DIR}/project/bin" + RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${CMAKE_CURRENT_SOURCE_DIR}/project/bin" + ) elseif(${PLATFORM} STREQUAL "MACOS") # TODO: create framework with cmake FRAMEWORK property @@ -50,10 +55,14 @@ target_link_options(${PROJECT_NAME} PRIVATE ${GODOT_LINK_FLAGS}) get_directory_property(LIBRARY_SUFFIX DIRECTORY ../ DEFINITION LIBRARY_SUFFIX) set_target_properties(${PROJECT_NAME} PROPERTIES - ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/project/bin" - LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/project/bin" - RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/project/bin" - + LIBRARY_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/project/bin" + LIBRARY_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/project/bin" + LIBRARY_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_CURRENT_SOURCE_DIR}/project/bin" + LIBRARY_OUTPUT_DIRECTORY_MINSIZEREL "${CMAKE_CURRENT_SOURCE_DIR}/project/bin" + OUTPUT_NAME "${PROJECT_NAME}${LIBRARY_SUFFIX}" ) +if(${GODOT_CPP_WARNING_AS_ERROR}) + set_warning_as_error(${PROJECT_NAME}) +endif()