diff --git a/CMakeLists.txt b/CMakeLists.txt index 7188a591..6a369362 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,8 +116,6 @@ else () string(TOUPPER ${CMAKE_BUILD_TYPE} CONFIG_STR_UPPER) endif () -set(RAZ_BUILD_FLAGS "${CMAKE_CXX_FLAGS_${CONFIG_STR_UPPER}}") - option(ENABLE_DEBUG_INFO "Creates a debug target which prints useful values" OFF) if (ENABLE_DEBUG_INFO) @@ -156,7 +154,7 @@ if (ENABLE_DEBUG_INFO) ${CMAKE_COMMAND} -E echo " RAZ_CONFIG_SHORT: ${RAZ_CONFIG_SHORT}" && ${CMAKE_COMMAND} -E echo "" && - ${CMAKE_COMMAND} -E echo "--- Build flags: '${RAZ_BUILD_FLAGS}'" && + ${CMAKE_COMMAND} -E echo "--- Build flags: '${CMAKE_CXX_FLAGS_${CONFIG_STR_UPPER}}'" && ${CMAKE_COMMAND} -E echo "" ) endif () @@ -172,7 +170,7 @@ endif () ######################## include(CompilerFlags) -add_compiler_flags(RaZ PRIVATE) +add_compiler_flags(TARGET RaZ SCOPE PRIVATE) if (RAZ_COMPILER_GCC) # Enabling code coverage @@ -197,12 +195,12 @@ if (RAZ_COMPILER_GCC) endif () if (RAZ_PLATFORM_LINUX) - set(RAZ_LINKER_FLAGS ${RAZ_LINKER_FLAGS} pthread) + list(APPEND RAZ_LINKER_FLAGS pthread) endif () if (RAZ_USE_EMSCRIPTEN) target_link_options(RaZ PUBLIC "SHELL:-s USE_GLFW=3") - set(RAZ_LINKER_FLAGS ${RAZ_LINKER_FLAGS} glfw) + list(APPEND RAZ_LINKER_FLAGS glfw) endif () ###################### @@ -261,10 +259,10 @@ if (RAZ_PLATFORM_LINUX) -fsanitize=leak ) - set( + list( + APPEND RAZ_LINKER_FLAGS - ${RAZ_LINKER_FLAGS} asan ubsan ) @@ -370,7 +368,12 @@ endif () ######################### include(EmbedFiles) -embed_files("${PROJECT_SOURCE_DIR}/shaders/*.*" "${CMAKE_BINARY_DIR}/shaders" RaZ Shaders) +embed_files( + INPUT_PATTERN "${PROJECT_SOURCE_DIR}/shaders/*.*" + OUTPUT_FOLDER "${CMAKE_BINARY_DIR}/shaders" + MAIN_TARGET RaZ + EMBED_TARGET_SUFFIX Shaders +) ############### # RaZ - Build # diff --git a/cmake/CompilerFlags.cmake b/cmake/CompilerFlags.cmake index ea494096..45cb4b79 100644 --- a/cmake/CompilerFlags.cmake +++ b/cmake/CompilerFlags.cmake @@ -1,8 +1,24 @@ # Allows to define useful compiler flags (warnings, needed definitions, ...) -function(add_compiler_flags TARGET_NAME SCOPE) +function(add_compiler_flags) + set(options) + set(oneValueArgs TARGET SCOPE) + set(multiValueArgs) + cmake_parse_arguments( + PARSE_ARGV 0 + arg + "${options}" + "${oneValueArgs}" + "${multiValueArgs}" + ) + foreach (REQUIRED_ARG IN LISTS oneValueArgs) + if (NOT arg_${REQUIRED_ARG}) + message(FATAL_ERROR "Adding compiler flags requires a value for the '${REQUIRED_ARG}' argument") + endif () + endforeach () + # The definitions MUST be propagated to avoid warnings and/or errors in headers - set(DEFINITIONS_SCOPE ${SCOPE}) + set(DEFINITIONS_SCOPE ${arg_SCOPE}) if (DEFINITIONS_SCOPE STREQUAL "PRIVATE") set(DEFINITIONS_SCOPE "PUBLIC") endif () @@ -25,7 +41,8 @@ function(add_compiler_flags TARGET_NAME SCOPE) endif () if (COMPILER_GCC) - set( + list( + APPEND COMPILER_FLAGS -pedantic @@ -72,10 +89,10 @@ function(add_compiler_flags TARGET_NAME SCOPE) ) if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 5) - set( + list( + APPEND COMPILER_FLAGS - ${COMPILER_FLAGS} -fsized-deallocation -Warray-bounds=2 -Wformat-signedness @@ -84,20 +101,20 @@ function(add_compiler_flags TARGET_NAME SCOPE) endif () if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 6) - set( + list( + APPEND COMPILER_FLAGS - ${COMPILER_FLAGS} -Wduplicated-cond #-Wnull-dereference # Lua bindings (Sol) generate a lot of these ) endif () if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 7) - set( + list( + APPEND COMPILER_FLAGS - ${COMPILER_FLAGS} -Waligned-new -Walloca -Walloc-zero @@ -106,7 +123,8 @@ function(add_compiler_flags TARGET_NAME SCOPE) ) endif () elseif (COMPILER_CLANG) - set( + list( + APPEND COMPILER_FLAGS -Weverything @@ -133,20 +151,20 @@ function(add_compiler_flags TARGET_NAME SCOPE) ) if (COMPILER_CLANG_CL) - set( + list( + APPEND COMPILER_FLAGS - ${COMPILER_FLAGS} # Disabling warnings triggered in externals -Wno-language-extension-token -Wno-nonportable-system-include-path -Wno-zero-as-null-pointer-constant ) else () - set( + list( + APPEND COMPILER_FLAGS - ${COMPILER_FLAGS} # Other flags not recognized by clang-cl -pedantic -pedantic-errors @@ -154,24 +172,15 @@ function(add_compiler_flags TARGET_NAME SCOPE) endif () if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 5) - set( - COMPILER_FLAGS - - ${COMPILER_FLAGS} - -Wno-unused-template - ) + list(APPEND COMPILER_FLAGS -Wno-unused-template) endif () if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 16) - set( - COMPILER_FLAGS - - ${COMPILER_FLAGS} - -Wno-unsafe-buffer-usage - ) + list(APPEND COMPILER_FLAGS -Wno-unsafe-buffer-usage) endif () elseif (COMPILER_MSVC) - set( + list( + APPEND COMPILER_FLAGS /Wall @@ -219,24 +228,24 @@ function(add_compiler_flags TARGET_NAME SCOPE) ) # Automatically export all classes & functions - set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS TRUE) + set_property(TARGET ${arg_TARGET} PROPERTY WINDOWS_EXPORT_ALL_SYMBOLS ON) endif () if (COMPILER_MINGW) - set( + list( + APPEND COMPILER_FLAGS - ${COMPILER_FLAGS} -Wa,-mbig-obj # Allowing big object files -fuse-ld=lld # Using LLVM LLD as the linker ) endif () if (COMPILER_MSVC OR COMPILER_CLANG_CL) - set( + list( + APPEND COMPILER_FLAGS - ${COMPILER_FLAGS} /permissive- # Improving standard compliance /bigobj # Allowing object files to be bigger /EHsc # Enabling exceptions @@ -256,9 +265,10 @@ function(add_compiler_flags TARGET_NAME SCOPE) ) if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.13) - set(COMPILER_FLAGS + list( + APPEND + COMPILER_FLAGS - ${COMPILER_FLAGS} # Allowing external linkage for 'extern constexpr' variables # See: https://learn.microsoft.com/en-us/cpp/build/reference/zc-externconstexpr /Zc:externConstexpr @@ -266,9 +276,10 @@ function(add_compiler_flags TARGET_NAME SCOPE) endif () if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.14) - set(COMPILER_FLAGS + list( + APPEND + COMPILER_FLAGS - ${COMPILER_FLAGS} # Forcing the '__cplusplus' definition to be of the proper value # See: https://learn.microsoft.com/en-us/cpp/build/reference/zc-cplusplus /Zc:__cplusplus @@ -276,9 +287,10 @@ function(add_compiler_flags TARGET_NAME SCOPE) endif () if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.25) - set(COMPILER_FLAGS + list( + APPEND + COMPILER_FLAGS - ${COMPILER_FLAGS} # Forcing the preprocessor to be compliant with C++11 and above # See: https://learn.microsoft.com/en-us/cpp/build/reference/zc-preprocessor /Zc:preprocessor @@ -286,9 +298,10 @@ function(add_compiler_flags TARGET_NAME SCOPE) endif () if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.28) - set(COMPILER_FLAGS + list( + APPEND + COMPILER_FLAGS - ${COMPILER_FLAGS} # Forcing lambdas' parsing to be standard compliant # To be removed in C++20 (implied by /std:c++20) # See: https://learn.microsoft.com/en-us/cpp/build/reference/zc-lambda @@ -297,9 +310,10 @@ function(add_compiler_flags TARGET_NAME SCOPE) endif () if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.34) - set(COMPILER_FLAGS + list( + APPEND + COMPILER_FLAGS - ${COMPILER_FLAGS} # Forcing standard enumeration type deduction # See: https://learn.microsoft.com/en-us/cpp/build/reference/zc-enumtypes /Zc:enumTypes @@ -307,9 +321,10 @@ function(add_compiler_flags TARGET_NAME SCOPE) endif () if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 19.35) - set(COMPILER_FLAGS + list( + APPEND + COMPILER_FLAGS - ${COMPILER_FLAGS} # Forcing template parameters' names to not be reused (shadowed) # See: https://learn.microsoft.com/en-us/cpp/build/reference/zc-templatescope /Zc:templateScope @@ -324,7 +339,7 @@ function(add_compiler_flags TARGET_NAME SCOPE) if (WIN32 OR CYGWIN) target_compile_definitions( - ${TARGET_NAME} + ${arg_TARGET} ${DEFINITIONS_SCOPE} @@ -345,26 +360,26 @@ function(add_compiler_flags TARGET_NAME SCOPE) # See https://emscripten.org/docs/optimizing/Optimizing-Code.html#link-times for details on improving link times target_link_options( - ${TARGET_NAME} + ${arg_TARGET} - ${SCOPE} + ${arg_SCOPE} "SHELL:-s ERROR_ON_WASM_CHANGES_AFTER_LINK" # Produces a linking error if the linker requires modifying the generated WASM "SHELL:-s WASM_BIGINT" # Enabling BigInt support ) else () - set( + list( + APPEND COMPILER_FLAGS - ${COMPILER_FLAGS} -g #-fsanitize=address,undefined # Enable sanitizers ) target_link_options( - ${TARGET_NAME} + ${arg_TARGET} - ${SCOPE} + ${arg_SCOPE} #-fsanitize=address,undefined # Enable sanitizers "SHELL:-s ASSERTIONS=1" # Enable assertions @@ -376,12 +391,12 @@ function(add_compiler_flags TARGET_NAME SCOPE) ) endif () - set(COMPILER_FLAGS ${COMPILER_FLAGS} -O0) + list(APPEND COMPILER_FLAGS -O0) target_link_options( - ${TARGET_NAME} + ${arg_TARGET} - ${SCOPE} + ${arg_SCOPE} --emrun # Forward stdout & stderr to the launching console --cpuprofiler # Display a CPU profiler on the page @@ -389,26 +404,26 @@ function(add_compiler_flags TARGET_NAME SCOPE) #-gsource-map # Generate source map ) else () - target_compile_definitions(${TARGET_NAME} ${DEFINITIONS_SCOPE} NDEBUG) - set(COMPILER_FLAGS ${COMPILER_FLAGS} -O3) + target_compile_definitions(${arg_TARGET} ${DEFINITIONS_SCOPE} NDEBUG) + list(APPEND COMPILER_FLAGS -O3) endif () # Threading is available on Emscripten's side (see arguments below), but may not be on the browser's. This will need to be checked again in the future # See: https://emscripten.org/docs/porting/pthreads.html - set( + list( + APPEND COMPILER_FLAGS - ${COMPILER_FLAGS} -c # Emit object files (may be unrequired) #-pthread # Enabling pthread "SHELL:-s DISABLE_EXCEPTION_CATCHING=0" # Force catching exceptions ) target_link_options( - ${TARGET_NAME} + ${arg_TARGET} - ${SCOPE} + ${arg_SCOPE} #-pthread # Enabling pthread "SHELL:-s ALLOW_MEMORY_GROWTH=1" # Automatically reallocate memory if needed @@ -421,5 +436,5 @@ function(add_compiler_flags TARGET_NAME SCOPE) ) endif () - target_compile_options(${TARGET_NAME} ${SCOPE} ${COMPILER_FLAGS}) + target_compile_options(${arg_TARGET} ${arg_SCOPE} ${COMPILER_FLAGS}) endfunction() diff --git a/cmake/EmbedFiles.cmake b/cmake/EmbedFiles.cmake index 6703f77f..01a6077e 100644 --- a/cmake/EmbedFiles.cmake +++ b/cmake/EmbedFiles.cmake @@ -1,18 +1,34 @@ # Allows to create embeddable (#include-able) files directly in code -function(embed_files INPUT_PATTERN OUTPUT_FOLDER MAIN_TARGET_NAME EMBED_TARGET_SUFFIX) - set(EMBED_TARGET_NAME "${MAIN_TARGET_NAME}_Embed${EMBED_TARGET_SUFFIX}") +function(embed_files) + set(options) + set(oneValueArgs INPUT_PATTERN OUTPUT_FOLDER MAIN_TARGET EMBED_TARGET_SUFFIX) + set(multiValueArgs) + cmake_parse_arguments( + PARSE_ARGV 0 + arg + "${options}" + "${oneValueArgs}" + "${multiValueArgs}" + ) + foreach (REQUIRED_ARG IN LISTS oneValueArgs) + if (NOT arg_${REQUIRED_ARG}) + message(FATAL_ERROR "Embedding files requires a value for the '${REQUIRED_ARG}' argument") + endif () + endforeach () + + set(EMBED_TARGET_NAME "${arg_MAIN_TARGET}_Embed${arg_EMBED_TARGET_SUFFIX}") set(EMBED_SCRIPT "${CMAKE_BINARY_DIR}/${EMBED_TARGET_NAME}.cmake") # Emptying the embedding script file(WRITE "${EMBED_SCRIPT}" "") - file(GLOB EMBEDDABLE_FILES "${INPUT_PATTERN}") + file(GLOB EMBEDDABLE_FILES "${arg_INPUT_PATTERN}") # Creating a copy of each file to enclose its content with a raw string literal R"(...)" foreach (FILE_PATH ${EMBEDDABLE_FILES}) get_filename_component(FILE_NAME "${FILE_PATH}" NAME) - set(EMBED_FILE_PATH "${OUTPUT_FOLDER}/${FILE_NAME}.embed") + set(EMBED_FILE_PATH "${arg_OUTPUT_FOLDER}/${FILE_NAME}.embed") # CMake cannot execute a script from a string and requires an actual file to be executed # The embeddable file will be written only if the original has been modified @@ -35,8 +51,8 @@ function(embed_files INPUT_PATTERN OUTPUT_FOLDER MAIN_TARGET_NAME EMBED_TARGET_S VERBATIM ) - add_dependencies(${MAIN_TARGET_NAME} ${EMBED_TARGET_NAME}) + add_dependencies(${arg_MAIN_TARGET} ${EMBED_TARGET_NAME}) # Adding the embedded files directory to the include dirs, so that we can include them directly in code - target_include_directories(${MAIN_TARGET_NAME} PUBLIC "${OUTPUT_FOLDER}") + target_include_directories(${arg_MAIN_TARGET} PUBLIC "${arg_OUTPUT_FOLDER}") endfunction() diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 7de5780c..2543bd09 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -7,7 +7,7 @@ target_link_libraries(RaZ_Examples INTERFACE RaZ) target_sources(RaZ_Examples INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/DemoUtils.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/DemoUtils.hpp") include(CompilerFlags) -add_compiler_flags(RaZ_Examples INTERFACE) +add_compiler_flags(TARGET RaZ_Examples SCOPE INTERFACE) # Disabling some warnings, which are intendedly present if (RAZ_COMPILER_GCC) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 58acfb95..6e598492 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -16,7 +16,7 @@ enable_testing() ############################## include(CompilerFlags) -add_compiler_flags(RaZ_Tests PRIVATE) +add_compiler_flags(TARGET RaZ_Tests SCOPE PRIVATE) if (RAZ_COMPILER_CLANG) target_compile_options(