From 432cdaff1e5c8775d0d5c533dd0abe5eac229bd7 Mon Sep 17 00:00:00 2001 From: Abdessattar Sassi <457645+abdes@users.noreply.github.com> Date: Wed, 21 Sep 2022 14:54:25 +0400 Subject: [PATCH] fix(build): #21 target option `WARNING` not propagated properly By default, for every target we build, a compiler option will be added to treat warnings as errors, unless the target is added with `WARNING` as an option. When that option is used, we now properly propagate it to set the corresponding compiler option to **NOT** treat warnings as errors. This is useful when 3rd party dependencies have include files that generate warnings. --- cmake/AsapTargets.cmake | 85 +++++++++++++++++++------------------- cmake/CompileOptions.cmake | 12 ++++-- cmake/TestTargets.cmake | 22 +++++----- 3 files changed, 63 insertions(+), 56 deletions(-) diff --git a/cmake/AsapTargets.cmake b/cmake/AsapTargets.cmake index 0b1e0a5..a96fd3c 100644 --- a/cmake/AsapTargets.cmake +++ b/cmake/AsapTargets.cmake @@ -110,14 +110,32 @@ endfunction() # Target creation helpers # ------------------------------------------------------------------------------ +macro(_add_common_compiler_options target warnings) + # Set some common compiler options, and forward the 'WARNING' option if it was + # provided + if(warnings) + asap_set_compile_options(${target} WARNING) + else() + asap_set_compile_options(${target}) + endif() +endmacro() + function(asap_add_library target) - set(argOption "") - set(argSingle "CONTRACTS") - set(argMulti "") - unset(x_CONTRACTS) + set(argOption EXCEPTIONS RTTI WARNING) + set(argSingle CONTRACTS) + set(argMulti) + cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN}) - swift_add_library("${target}" ${x_UNPARSED_ARGUMENTS}) + if(x_WARNING) + set(warning_flag "WARNING") + else() + set(warning_flag) + endif() + + # Contrarily to swift default, we enable exceptions and RTTI for all targets + swift_add_library("${target}" EXCEPTIONS RTTI ${warning_flag} + ${x_UNPARSED_ARGUMENTS}) # We can refer to this target either with its standalone target name or with a # project scoped name (::) which we will alias to the target @@ -127,8 +145,8 @@ function(asap_add_library target) if(NOT ${type} STREQUAL "INTERFACE_LIBRARY") # Set some common private compiler defines asap_set_compile_definitions(${target} CONTRACTS ${x_CONTRACTS}) - # Set some common compiler options - asap_set_compile_options(${target}) + # Set common compiler options + asap_set_compile_options(${target} ${warning_flag}) # Generate export headers for the library asap_generate_export_headers(${target} ${META_MODULE_NAME}) @@ -144,51 +162,34 @@ function(asap_add_library target) endfunction() function(asap_add_executable target) - set(argOption "") - set(argSingle "CONTRACTS") - set(argMulti "") - unset(x_CONTRACTS) + set(argOption EXCEPTIONS RTTI WARNING) + set(argSingle CONTRACTS) + set(argMulti) + cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN}) - swift_add_executable("${target}" ${x_UNPARSED_ARGUMENTS}) + if(x_WARNING) + set(warning_flag "WARNING") + else() + set(warning_flag) + endif() + + # Contrarily to swift default, we enable exceptions and RTTI for all targets + swift_add_executable("${target}" EXCEPTIONS RTTI ${warning_flag} + ${x_UNPARSED_ARGUMENTS}) # Set some common private compiler defines asap_set_compile_definitions(${target} CONTRACTS ${x_CONTRACTS}) - # Set some common compiler options - asap_set_compile_options(${target}) + # Set common compiler options + asap_set_compile_options(${target} ${warning_flag}) set_target_properties(${target} PROPERTIES FOLDER "Executables") endfunction() function(asap_add_tool target) - set(argOption "") - set(argSingle "CONTRACTS") - set(argMulti "") - unset(x_CONTRACTS) - cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN}) - - swift_add_tool("${target}" ${x_UNPARSED_ARGUMENTS}) - # Set some common private compiler defines - asap_set_compile_definitions(${target} CONTRACTS ${x_CONTRACTS}) - # Set some common compiler options - asap_set_compile_options(${target}) + asap_add_executable(${target} ${ARGN}) set_target_properties(${target} PROPERTIES FOLDER "Tools") endfunction() function(asap_add_tool_library target) - set(argOption "") - set(argSingle "CONTRACTS") - set(argMulti "") - unset(x_CONTRACTS) - cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN}) - - swift_add_tool_library("${target}" ${x_UNPARSED_ARGUMENTS}) - # Set some common private compiler defines - asap_set_compile_definitions(${target} CONTRACTS ${x_CONTRACTS}) - # Set some common compiler options - asap_set_compile_options(${target}) - set_target_properties( - ${target} - PROPERTIES FOLDER "Tool Libraries" - VERSION ${META_MODULE_VERSION} - SOVERSION ${META_MODULE_VERSION_MAJOR} - DEBUG_POSTFIX "d") + asap_add_library(${target} ${ARGN}) + set_target_properties(${target} PROPERTIES FOLDER "Tool Libraries") endfunction() diff --git a/cmake/CompileOptions.cmake b/cmake/CompileOptions.cmake index c29a760..06f92ec 100644 --- a/cmake/CompileOptions.cmake +++ b/cmake/CompileOptions.cmake @@ -16,8 +16,6 @@ # RTTI. By default, both are enabled. function(asap_set_compile_options) - swift_set_compile_options(EXCEPTIONS RTTI ${ARGV}) - if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") # using Clang swift_set_compile_options( @@ -46,13 +44,19 @@ function(asap_set_compile_options) endif() elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") # using Visual Studio C++ - set(argOption "WARNING" "NO_EXCEPTIONS" "EXCEPTIONS" "NO_RTTI" "RTTI") + set(argOption "WARNING") set(argSingle "") - set(argMulti "ADD" "REMOVE" "EXTRA_FLAGS") + set(argMulti "") + cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN}) + set(targets ${x_UNPARSED_ARGUMENTS}) + foreach(target ${targets}) target_compile_options(${target} PRIVATE /EHsc /MP /W4) + if(NOT x_WARNING) + target_compile_options(${target} PRIVATE /WX) + endif() endforeach() endif() diff --git a/cmake/TestTargets.cmake b/cmake/TestTargets.cmake index 8db2888..73214ef 100644 --- a/cmake/TestTargets.cmake +++ b/cmake/TestTargets.cmake @@ -7,16 +7,17 @@ include(common/TestTargets) macro(asap_add_test target) - set(argOption "") - set(argSingle "CONTRACTS") - set(argMulti "") - unset(x_CONTRACTS) + set(argOption) + set(argSingle CONTRACTS) + set(argMulti) + cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN}) - swift_add_test("${target}" ${x_UNPARSED_ARGUMENTS}) + # Contrarily to swift default, we enable exceptions and RTTI for all targets + swift_add_test("${target}" ${warning_flag} ${x_UNPARSED_ARGUMENTS}) # Set some common private compiler defines asap_set_compile_definitions(${target} CONTRACTS ${x_CONTRACTS}) - # Set some common compiler options + # Set common compiler options asap_set_compile_options(${target}) if(TARGET gtest AND BUILD_SHARED_LIBS) target_compile_definitions(${target} PRIVATE GTEST_LINKED_AS_SHARED_LIBRARY) @@ -37,12 +38,13 @@ macro(asap_add_test_runner target) endmacro() function(asap_add_test_library target) - set(argOption "") - set(argSingle "CONTRACTS") - set(argMulti "") - unset(x_CONTRACTS) + set(argOption) + set(argSingle CONTRACTS) + set(argMulti) + cmake_parse_arguments(x "${argOption}" "${argSingle}" "${argMulti}" ${ARGN}) + # Contrarily to swift default, we enable exceptions and RTTI for all targets swift_add_test_library("${target}" ${x_UNPARSED_ARGUMENTS}) # Set some common private compiler defines asap_set_compile_definitions(${target} CONTRACTS ${x_CONTRACTS})