From a94e8d47fdc400db04c8c1073391cf6502975722 Mon Sep 17 00:00:00 2001 From: galanca Date: Mon, 27 Feb 2023 15:55:21 -0700 Subject: [PATCH 1/2] Add SET_CXX_FLAGS macro. --- cmake/compiler-flags.cmake | 66 ++++++++++++++++++++++++++++++-------- src/kiva/CMakeLists.txt | 3 ++ test/unit/CMakeLists.txt | 3 ++ 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/cmake/compiler-flags.cmake b/cmake/compiler-flags.cmake index 23a6222d..413a974c 100644 --- a/cmake/compiler-flags.cmake +++ b/cmake/compiler-flags.cmake @@ -1,3 +1,5 @@ +# Interface library contains the flags the library will use for each compiler +# Interface library is used instead of CMAKE_CXX flags to prevent it from propagating it to undesired places add_library(kiva_common_interface INTERFACE) #================# @@ -5,19 +7,57 @@ add_library(kiva_common_interface INTERFACE) #================# target_compile_options(kiva_common_interface INTERFACE -$<$: - /W4 # Warning level (default is W3) - $<$: - /WX # Turn warnings into errors + $<$: + /W4 # Warning level (default is W3) + $<$: + /WX # Turn warnings into errors + > > -> -$<$,$,$>: - -Wall - -Wextra - -Wpedantic - $<$: - -Werror # Turn warnings into errors + $<$: + -Wall + -Wextra + -Wpedantic + $<$: + -Werror # Turn warnings into errors + > > -> - ) + +# This macro will encapsulate the CMAKE_CXX flags that should only be set for executables +macro(SET_CXX_FLAGS) + # Remove unwanted CMake defaults from global flags + if (MSVC) + # See https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/Platform/Windows-MSVC.cmake + set(CMAKE_CXX_FLAGS + /EHsc #*Specifies the model of exception handling (sc options). + /DWIN32 #*Windows Platform (regardless of architecture) + /D_WINDOWS #* + ) + set(CMAKE_CXX_FLAGS_RELEASE + /O2 #*Creates fast code (Og+Oi+Ot+Oy+Ob2+GF+Gy). + # /Ob2 #*Controls inline expansion (level 2). (part of O2) + /DNDEBUG #*Enables or disables compilation of assertions (CSE traditionally did not define this. See https://github.com/cse-sim/cse/issues/285) + ) + set(CMAKE_CXX_FLAGS_DEBUG + /Ob0 #*Controls inline expansion (level 0 -- disabled). + /Od #*Disables optimization. + /Zi #*Generates complete debugging information. + /RTC1 #*Enables run-time error checking. + ) + else () # GCC or Clang or AppleClang + # See https://gitlab.kitware.com/cmake/cmake/-/blob/master/Modules/Compiler/GNU.cmake + set(CMAKE_CXX_FLAGS "") + set(CMAKE_CXX_FLAGS_RELEASE + -O3 #*Maximum optimization (see https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Optimize-Options). + -DNDEBUG #*Enables or disables compilation of assertions (CSE traditionally did not define this. See https://github.com/cse-sim/cse/issues/285) + ) + set(CMAKE_CXX_FLAGS_DEBUG + -g #*Produce debugging information in the operating system’s native format. + ) + endif() + + # Convert lists to space-separated strings + list(JOIN CMAKE_CXX_FLAGS " " CMAKE_CXX_FLAGS) + list(JOIN CMAKE_CXX_FLAGS_RELEASE " " CMAKE_CXX_FLAGS_RELEASE) + list(JOIN CMAKE_CXX_FLAGS_DEBUG " " CMAKE_CXX_FLAGS_DEBUG) +endmacro() \ No newline at end of file diff --git a/src/kiva/CMakeLists.txt b/src/kiva/CMakeLists.txt index c407569e..f862d3c9 100644 --- a/src/kiva/CMakeLists.txt +++ b/src/kiva/CMakeLists.txt @@ -1,3 +1,6 @@ +# Set CMAKE_CXX flags for kiva executable +SET_CXX_FLAGS() + set(kiva_src Main.cpp Input.cpp Input.hpp diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index bdbd6d45..6b683905 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -1,3 +1,6 @@ +# Set CMAKE_CXX flags for kiva_test executable +SET_CXX_FLAGS() + set(kiva_test_src foundation.unit.cpp domain_test.cpp From 7f1a023e21859123c287d8b0258fc796137ef335 Mon Sep 17 00:00:00 2001 From: galanca Date: Tue, 28 Feb 2023 08:02:19 -0700 Subject: [PATCH 2/2] Add CMake runtime library. --- src/kiva/CMakeLists.txt | 2 ++ test/unit/CMakeLists.txt | 2 ++ 2 files changed, 4 insertions(+) diff --git a/src/kiva/CMakeLists.txt b/src/kiva/CMakeLists.txt index f862d3c9..a9b947e0 100644 --- a/src/kiva/CMakeLists.txt +++ b/src/kiva/CMakeLists.txt @@ -13,6 +13,8 @@ set(kiva_src Main.cpp set(CMAKE_INSTALL_RPATH "@executable_path") +# Sets the MT flag for Release or Debug config +set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") add_executable(kiva ${kiva_src}) target_link_libraries(kiva PRIVATE groundplot boost_program_options yaml-cpp kiva_common_interface) diff --git a/test/unit/CMakeLists.txt b/test/unit/CMakeLists.txt index 6b683905..ab5e0c79 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -7,6 +7,8 @@ set(kiva_test_src cell_test.cpp ) +# Sets the MT flag for Release or Debug config +set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") add_executable(kiva_tests ${kiva_test_src}) target_link_libraries(kiva_tests PRIVATE gtest libkiva kiva_common_interface)