From 5e3d87858fdb3f3c754ed3d9939fd103569d41a8 Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Mon, 25 Mar 2024 15:04:34 -0600 Subject: [PATCH] Set compiler warnings on a per-target basis --- cmake/CompilerWarnings.cmake | 8 +++----- cmake/Macros.cmake | 2 +- examples/CMakeLists.txt | 6 ++---- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/cmake/CompilerWarnings.cmake b/cmake/CompilerWarnings.cmake index b542886..dfe8a02 100644 --- a/cmake/CompilerWarnings.cmake +++ b/cmake/CompilerWarnings.cmake @@ -2,8 +2,8 @@ # # https://github.com/lefticus/cppbestpractices/blob/master/02-Use_the_Tools_Available.md -# Helper function to enable compiler warnings for a specific set of files -function(set_file_warnings) +# Helper function to enable compiler warnings for a specific target +function(set_target_warnings target) option(WARNINGS_AS_ERRORS "Treat compiler warnings as errors" FALSE) set(MSVC_WARNINGS @@ -97,7 +97,5 @@ function(set_file_warnings) message(AUTHOR_WARNING "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler.") endif() - foreach(WARNING ${FILE_WARNINGS}) - set_property(SOURCE ${ARGV} APPEND_STRING PROPERTY COMPILE_FLAGS " ${WARNING}") - endforeach() + target_compile_options(${target} PRIVATE ${FILE_WARNINGS}) endfunction() diff --git a/cmake/Macros.cmake b/cmake/Macros.cmake index 68b27ca..c2f9489 100644 --- a/cmake/Macros.cmake +++ b/cmake/Macros.cmake @@ -17,7 +17,7 @@ macro(csfml_add_library target) target_include_directories(${target} PUBLIC ${PROJECT_SOURCE_DIR}/include) # add warnings - set_file_warnings(${THIS_SOURCES}) + set_target_warnings(${target}) # require C11 and C++17 target_compile_features(${target} PUBLIC c_std_11 cxx_std_17) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index b9068f6..4231596 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,5 +1,3 @@ -set(SRC example.c) - -add_executable(example ${SRC}) +add_executable(example example.c) target_link_libraries(example PRIVATE csfml-graphics csfml-audio) -set_file_warnings(${SRC}) +set_target_warnings(example)