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..a9b947e0 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 @@ -10,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 bdbd6d45..ab5e0c79 100644 --- a/test/unit/CMakeLists.txt +++ b/test/unit/CMakeLists.txt @@ -1,9 +1,14 @@ +# Set CMAKE_CXX flags for kiva_test executable +SET_CXX_FLAGS() + set(kiva_test_src foundation.unit.cpp domain_test.cpp 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)