Skip to content

Commit

Permalink
CompilerFlags modernization
Browse files Browse the repository at this point in the history
 * uses project_warnings and project_options interface projects
 * now we no longer force our warning flags on third_party projects
  • Loading branch information
lefticus committed Dec 11, 2020
1 parent d1a7af0 commit 9abbe80
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 105 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
add_library(project_options INTERFACE)
target_compile_features(project_options INTERFACE cxx_std_17)

add_library(project_warnings INTERFACE)

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads)
target_link_libraries(project_options INTERFACE Threads::Threads)
Expand Down
166 changes: 75 additions & 91 deletions cmake/CompilerFlags.cmake
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
# Compiler-agnostic compiler flags first
add_cxx_definitions("-DOBJEXXFCL_ALIGN=64") # Align ObjexxFCL arrays to 64B
add_cxx_debug_definitions("-DOBJEXXFCL_ARRAY_INIT_DEBUG") # Initialize ObjexxFCL arrays to aid debugging
target_compile_definitions(project_options INTERFACE "-DOBJEXXFCL_ALIGN=64") # Align ObjexxFCL arrays to 64B
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"-DOBJEXXFCL_ARRAY_INIT_DEBUG">) # Initialize ObjexxFCL arrays to aid debugging

if(NOT OPENGL_FOUND)
add_definitions("-DEP_NO_OPENGL")
target_compile_definitions(project_options INTERFACE "-DEP_NO_OPENGL")
endif()

# Make sure expat is compiled as a static library
add_definitions("-DXML_STATIC")
target_compile_definitions(project_options INTERFACE "-DXML_STATIC")

set(CMAKE_CXX_STANDARD 17)

if(APPLE)
# Force no auto ptr
# TODO remove this after kiva/boost is updated to a version that supports
# C++17
add_definitions("-DBOOST_NO_AUTO_PTR")
target_compile_definitions(project_options INTERFACE "-DBOOST_NO_AUTO_PTR")
endif()

if(MSVC AND NOT ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")) # Visual C++ (VS 2013)

# COMPILER FLAGS
add_compile_options("/nologo")
add_compile_options("/EHsc")
add_compile_options("/MP") # Enables multi-processor compilation of source within a single project
string(REGEX REPLACE "/W3" "/W1" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}"
)# Increase to /W2 then /W3 as more serious warnings are addressed (using regex to avoid VC override warnings)
target_compile_options(project_options INTERFACE "/nologo")
target_compile_options(project_options INTERFACE "/EHsc")
target_compile_options(project_options INTERFACE "/MP") # Enables multi-processor compilation of source within a single project
# string(REGEX REPLACE "/W3" "/W1" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}"
# )# Increase to /W2 then /W3 as more serious warnings are addressed (using regex to avoid VC override warnings)

# Disabled Warnings: Enable some of these as more serious warnings are addressed
# 4068 Unknown pragma
Expand All @@ -36,7 +36,7 @@ if(MSVC AND NOT ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")) # Visual C++ (VS
# 4355 Passing this pointer in class initializer (object is incomplete so bases/members can only use this in limited ways)
# 4996 Deprecated functions (/D_SCL_SECURE_NO_WARNINGS /D_CRT_SECURE_NO_WARNINGS /D_CRT_NONSTDC_NO_WARNINGS)
# 4503 The decorated name was longer than the compiler limit (4096), and was truncated.
add_compile_options(
target_compile_options(project_warnings INTERFACE
/wd4068
/wd4101
/wd4102
Expand All @@ -46,64 +46,60 @@ if(MSVC AND NOT ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")) # Visual C++ (VS
/wd4996
/wd4503) # Disables warning messages listed above

add_definitions(/DNOMINMAX) # Avoid build errors due to STL/Windows min-max conflicts
add_definitions(/DWIN32_LEAN_AND_MEAN) # Excludes rarely used services and headers from compilation
target_compile_definitions(project_options INTERFACE /DNOMINMAX) # Avoid build errors due to STL/Windows min-max conflicts
target_compile_definitions(project_options INTERFACE /DWIN32_LEAN_AND_MEAN) # Excludes rarely used services and headers from compilation
# ADD_CXX_DEFINITIONS("-d2SSAOptimizer-") # this disables this optimizer which has known major issues

# ADDITIONAL RELEASE-MODE-SPECIFIC FLAGS
add_cxx_release_definitions("/GS-") # Disable buffer overrun checks for performance in release mode
target_compile_options(project_options INTERFACE $<$<CONFIG:Release>:"/GS-">) # Disable buffer overrun checks for performance in release mode

# ADDITIONAL DEBUG-MODE-SPECIFIC FLAGS
add_cxx_debug_definitions("/Ob0") # Disable inlining
add_cxx_debug_definitions("/RTCsu") # Runtime checks
add_cxx_debug_definitions("/fp:strict") # Floating point model
add_cxx_debug_definitions("/DMSVC_DEBUG") # Triggers code in main.cc to catch floating point NaNs
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"/Ob0">) # Disable inlining
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"/RTCsu">) # Runtime checks
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"/fp:strict">) # Floating point model
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"/DMSVC_DEBUG">) # Triggers code in main.cc to catch floating point NaNs

elseif(
CMAKE_COMPILER_IS_GNUCXX
OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang"
OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") # g++/Clang
option(ENABLE_COVERAGE "Enable Coverage Reporting in GCC" FALSE)
if(ENABLE_COVERAGE)
add_definitions("--coverage -O0")
set(LINKER_FLAGS "${LINKER_FLAGS} --coverage")
target_compile_definitions(project_options INTERFACE --coverage -O0 -g)
target_link_libraries(project_options INTERFACE --coverage)
endif()

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${LINKER_FLAGS}")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${LINKER_FLAGS}")
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${LINKER_FLAGS}")

# COMPILER FLAGS
add_cxx_definitions("-pipe") # Faster compiler processing
add_cxx_definitions("-Wpedantic") # Turn on warnings about constructs/situations that may be non-portable or outside of the standard
add_cxx_definitions("-Wall -Wextra") # Turn on warnings
add_cxx_definitions("-Wno-unknown-pragmas")
target_compile_options(project_options INTERFACE "-pipe") # Faster compiler processing
target_compile_options(project_warnings INTERFACE "-Wpedantic") # Turn on warnings about constructs/situations that may be non-portable or outside of the standard
target_compile_options(project_warnings INTERFACE -Wall -Wextra) # Turn on warnings
target_compile_options(project_warnings INTERFACE "-Wno-unknown-pragmas")
if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 9.0)
add_cxx_definitions("-Wno-deprecated-copy")
target_compile_options(project_warnings INTERFACE "-Wno-deprecated-copy")
endif()
add_cxx_definitions("-Wno-attributes") # Don't warn on attributes Clang doesn't know
add_cxx_definitions("-Wno-delete-non-virtual-dtor")
add_cxx_definitions("-Wno-missing-braces")
target_compile_options(project_warnings INTERFACE "-Wno-attributes") # Don't warn on attributes Clang doesn't know
target_compile_options(project_warnings INTERFACE "-Wno-delete-non-virtual-dtor")
target_compile_options(project_warnings INTERFACE "-Wno-missing-braces")
if(CMAKE_COMPILER_IS_GNUCXX) # g++
add_cxx_definitions("-Wno-unused-but-set-parameter -Wno-unused-but-set-variable"
target_compile_options(project_warnings INTERFACE "-Wno-unused-but-set-parameter" "-Wno-unused-but-set-variable"
)# Suppress unused-but-set warnings until more serious ones are addressed
add_cxx_definitions("-Wno-maybe-uninitialized")
add_cxx_definitions("-Wno-aggressive-loop-optimizations")
target_compile_options(project_warnings INTERFACE "-Wno-maybe-uninitialized")
target_compile_options(project_warnings INTERFACE "-Wno-aggressive-loop-optimizations")
elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
add_cxx_definitions("-Wno-vexing-parse")
add_cxx_definitions("-Wno-invalid-source-encoding")
target_compile_options(project_warnings INTERFACE "-Wno-vexing-parse")
target_compile_options(project_warnings INTERFACE "-Wno-invalid-source-encoding")
endif()

# ADDITIONAL GCC-SPECIFIC FLAGS
if(CMAKE_COMPILER_IS_GNUCXX) # g++
add_cxx_debug_definitions("-ffloat-store") # Improve debug run solution stability
add_cxx_debug_definitions("-fsignaling-nans") # Disable optimizations that may have concealed NaN behavior
add_cxx_debug_definitions("-D_GLIBCXX_DEBUG") # Standard container debug mode (bounds checking, ...)
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"-ffloat-store">) # Improve debug run solution stability
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"-fsignaling-nans">) # Disable optimizations that may have concealed NaN behavior
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"-D_GLIBCXX_DEBUG">) # Standard container debug mode (bounds checking, ...>)
# ADD_CXX_RELEASE_DEFINITIONS("-finline-limit=2000") # More aggressive inlining This is causing unit test failures on Ubuntu 14.04
endif()

add_cxx_debug_definitions("-ggdb") # Produces debugging information specifically for gdb
add_cxx_release_definitions("-fno-stack-protector")
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"-ggdb">) # Produces debugging information specifically for gdb
target_compile_options(project_options INTERFACE $<$<CONFIG:Release>:-fno-stack-protector>)
# ADD_CXX_RELEASE_DEFINITIONS("-Ofast") # -Ofast (or -ffast-math) needed to auto-vectorize floating point loops

elseif(WIN32 AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
Expand All @@ -122,12 +118,12 @@ elseif(WIN32 AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# 11075 Inlining inhibited

# COMPILER FLAGS
add_cxx_definitions("/nologo") # Skip banner text
add_cxx_definitions("/Qcxx-features") # Enables standard C++ features without disabling Microsoft extensions
add_cxx_definitions("/Wall") # Enable "all" warnings
add_cxx_definitions("/Qdiag-disable:161,177,488,809,869,1786,2259,3280,10382,11074,11075") # Disable warnings listed above
add_cxx_definitions("/DNOMINMAX") # Avoid build errors due to STL/Windows min-max conflicts
add_cxx_definitions("/DWIN32_LEAN_AND_MEAN") # Excludes rarely used services and headers from compilation
target_compile_options(project_options INTERFACE "/nologo") # Skip banner text
target_compile_options(project_options INTERFACE "/Qcxx-features") # Enables standard C++ features without disabling Microsoft extensions
target_compile_options(project_options INTERFACE "/Wall") # Enable "all" warnings
target_compile_options(project_options INTERFACE "/Qdiag-disable:161,177,488,809,869,1786,2259,3280,10382,11074,11075") # Disable warnings listed above
target_compile_definitions(project_options INTERFACE "/DNOMINMAX") # Avoid build errors due to STL/Windows min-max conflicts
target_compile_definitions(project_options INTERFACE "/DWIN32_LEAN_AND_MEAN") # Excludes rarely used services and headers from compilation

# Optimization options that had no significant benefit for EnergyPlus
# /Qipo instead of /Qip
Expand All @@ -136,23 +132,21 @@ elseif(WIN32 AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# /Qunroll-aggressive

# ADDITIONAL RELEASE-MODE-SPECIFIC FLAGS
add_cxx_release_definitions("/O3") # Agressive optimization
add_cxx_release_definitions("/Qprec-div-") # Faster division
add_cxx_release_definitions("/Qansi-alias") # Better optimization via strict aliasing rules
add_cxx_release_definitions("/Qip") # Inter-procedural optimnization within a single file
add_cxx_release_definitions("/Qinline-factor:225") # Aggressive inlining
target_compile_options(project_options INTERFACE $<$<CONFIG:Release>:"/O3">) # Agressive optimization
target_compile_options(project_options INTERFACE $<$<CONFIG:Release>:"/Qprec-div-">) # Faster division
target_compile_options(project_options INTERFACE $<$<CONFIG:Release>:"/Qansi-alias">) # Better optimization via strict aliasing rules
target_compile_options(project_options INTERFACE $<$<CONFIG:Release>:"/Qip">) # Inter-procedural optimnization within a single file
target_compile_options(project_options INTERFACE $<$<CONFIG:Release>:"/Qinline-factor:225">) # Aggressive inlining
# ADD_CXX_RELEASE_DEFINITIONS("/fp:fast=2") # Aggressive optimizations on floating-point data

# ADDITIONAL DEBUG-MODE-SPECIFIC FLAGS
add_cxx_debug_definitions("/fp:source") # Use source-specified floating point precision
add_cxx_debug_definitions("/Qtrapuv") # Initialize local variables to unusual values to help detect use uninitialized
add_cxx_debug_definitions("/check:stack,uninit"
)# Enables runtime checking of the stack (buffer over and underruns; pointer verification) and uninitialized variables
add_cxx_debug_definitions("/Gs0") # Enable stack checking for all functions
add_cxx_debug_definitions("/GS") # Buffer overrun detection
add_cxx_debug_definitions("/Qfp-stack-check"
)# Tells the compiler to generate extra code after every function call to ensure fp stack is as expected
add_cxx_debug_definitions("/traceback") # Enables traceback on error
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"/fp:source">) # Use source-specified floating point precision
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"/Qtrapuv">) # Initialize local variables to unusual values to help detect use uninitialized
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"/check:stack,uninit")# Enables runtime checking of the stack (buffer over and underruns; pointer verification>) and uninitialized variables
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"/Gs0">) # Enable stack checking for all functions
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"/GS">) # Buffer overrun detection
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"/Qfp-stack-check">)# Tells the compiler to generate extra code after every function call to ensure fp stack is as expected
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"/traceback">) # Enables traceback on error

elseif(UNIX AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")

Expand All @@ -170,12 +164,8 @@ elseif(UNIX AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# 11075 Inlining inhibited

# COMPILER FLAGS
add_cxx_definitions("-Wall") # Enable "all" warnings
add_cxx_definitions("-diag-disable:161,177,488,809,869,1786,2259,3280,10382,11074,11075") # Disable warnings listed above

if(NOT APPLE)
add_cxx_definitions(-pthread)
endif()
target_compile_options(project_warnings INTERFACE "-Wall") # Enable "all" warnings
target_compile_options(project_warnings INTERFACE "-diag-disable:161,177,488,809,869,1786,2259,3280,10382,11074,11075") # Disable warnings listed above

# Optimization options that had no significant benefit for EnergyPlus
# -ipo instead of -ip
Expand All @@ -184,22 +174,21 @@ elseif(UNIX AND "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
# -unroll-aggressive

# ADDITIONAL RELEASE-MODE-SPECIFIC FLAGS
add_cxx_release_definitions("-O3") # Agressive optimization
target_compile_options(project_options INTERFACE $<$<CONFIG:Release>:"-O3">) # Agressive optimization
# ADD_CXX_RELEASE_DEFINITIONS("-Ofast") # More aggressive optimizations (instead of -O3) (enables -no-prec-div and -fp-model fast=2)
add_cxx_release_definitions("-no-prec-div") # Faster division (enabled by -Ofast)
add_cxx_release_definitions("-ansi-alias") # Enables more aggressive optimizations on floating-point data
add_cxx_release_definitions("-ip") # Enables inter-procedural optimnization within a single file
add_cxx_release_definitions("-inline-factor=225") # Enables more aggressive inlining
target_compile_options(project_options INTERFACE $<$<CONFIG:Release>:"-no-prec-div") # Faster division (enabled by -Ofast>)
target_compile_options(project_options INTERFACE $<$<CONFIG:Release>:"-ansi-alias">) # Enables more aggressive optimizations on floating-point data
target_compile_options(project_options INTERFACE $<$<CONFIG:Release>:"-ip">) # Enables inter-procedural optimnization within a single file
target_compile_options(project_options INTERFACE $<$<CONFIG:Release>:"-inline-factor=225">) # Enables more aggressive inlining

# ADDITIONAL DEBUG-MODE-SPECIFIC FLAGS
add_cxx_debug_definitions("-strict-ansi") # Strict language conformance: Performance impact so limit to debug build
add_cxx_debug_definitions("-fp-model source") # Use source-specified floating point precision
add_cxx_debug_definitions("-ftrapuv") # Initialize local variables to unusual values to help detect use uninitialized
add_cxx_debug_definitions("-check=stack,uninit"
)# Enables runtime checking of the stack (buffer over and underruns; pointer verification) and uninitialized variables
add_cxx_debug_definitions("-fstack-security-check") # Buffer overrun detection
add_cxx_debug_definitions("-fp-stack-check") # Check the floating point stack after every function call
add_cxx_debug_definitions("-traceback") # Enables traceback on error
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"-strict-ansi">) # Strict language conformance: Performance impact so limit to debug build
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"-fp-model source">) # Use source-specified floating point precision
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"-ftrapuv">) # Initialize local variables to unusual values to help detect use uninitialized
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"-check=stack,uninit")# Enables runtime checking of the stack (buffer over and underruns; pointer verification>) and uninitialized variables
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"-fstack-security-check">) # Buffer overrun detection
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"-fp-stack-check">) # Check the floating point stack after every function call
target_compile_options(project_options INTERFACE $<$<CONFIG:Debug>:"-traceback">) # Enables traceback on error

endif() # COMPILER TYPE

Expand All @@ -208,16 +197,11 @@ endif() # COMPILER TYPE

# We use "add_compile_options" instead of just appending to CXX_FLAGS
# That way it'll work for pretty much everything including Fortran stuff
macro(AddFlagIfSupported flag test)
macro(add_flag_if_supported flag test)
check_cxx_compiler_flag(${flag} ${test})
if(${${test}})
message(STATUS "Adding ${flag}")
# On Mac with Ninja (kitware binary for fortran support) and brew gfortran, I get build errors due to this flag.
if(("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang") AND BUILD_FORTRAN)
add_compile_options($<$<NOT:$<COMPILE_LANGUAGE:Fortran>>:${flag}>)
else()
add_compile_options("${flag}")
endif()
target_compile_options(project_options INTERFACE "${flag}")
else()
message(STATUS "Flag ${flag} isn't supported")
endif()
Expand All @@ -227,17 +211,17 @@ if("Ninja" STREQUAL ${CMAKE_GENERATOR})
include(CheckCXXCompilerFlag)
# Clang
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
addflagifsupported(-fcolor-diagnostics COMPILER_SUPPORTS_fdiagnostics_color)
add_flag_if_supported(-fcolor-diagnostics COMPILER_SUPPORTS_fdiagnostics_color)
endif()

# g++
if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
addflagifsupported(-fdiagnostics-color=always COMPILER_SUPPORTS_fdiagnostics_color)
add_flag_if_supported(-fdiagnostics-color=always COMPILER_SUPPORTS_fdiagnostics_color)

# On some older gcc, it doesn't say that it's supported, but it works anyways
if(NOT COMPILER_SUPPORTS_fdiagnostics_color)
message(STATUS "Forcing -fdiagnostics-color=always")
add_compile_options(-fdiagnostics-color=always)
target_compile_options(project_options INTERFACE -fdiagnostics-color=always)
endif()

endif()
Expand Down
Loading

5 comments on commit 9abbe80

@nrel-bot-3
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmake_modernize (lefticus) - x86_64-MacOS-10.15-clang-11.0.0: Build Failed

Build Badge Test Badge

@nrel-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmake_modernize (lefticus) - Win64-Windows-10-VisualStudio-16: Build Failed

Build Badge Test Badge

@nrel-bot-2b
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmake_modernize (lefticus) - x86_64-Linux-Ubuntu-18.04-gcc-7.5: Build Failed

Build Badge Test Badge

@nrel-bot-2b
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmake_modernize (lefticus) - x86_64-Linux-Ubuntu-18.04-gcc-7.5-UnitTestsCoverage-Debug: Build Failed

Build Badge Test Badge Coverage Badge

@nrel-bot-2b
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmake_modernize (lefticus) - x86_64-Linux-Ubuntu-18.04-gcc-7.5-IntegrationCoverage-Debug: Build Failed

Build Badge Test Badge Coverage Badge

Please sign in to comment.