Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SANITIZE CMake option to check for memory leaks and undefined behavior #24

Merged
merged 7 commits into from
Nov 18, 2022
26 changes: 13 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,42 +55,42 @@ set(GEOSChem_DETECTED_FORTRAN_COMPILER_VERSION ${CMAKE_Fortran_COMPILER_VERSION}
)

set(GEOSChem_Fortran_FLAGS_Intel
-cpp -w -auto -noalign "SHELL:-convert big_endian" "SHELL:-fp-model source"
-g -cpp -w -auto -noalign "SHELL:-convert big_endian" "SHELL:-fp-model source"
lizziel marked this conversation as resolved.
Show resolved Hide resolved
-mcmodel=medium -shared-intel -traceback -DLINUX_IFORT
CACHE STRING "GEOSChem compiler flags for all build types with Intel compilers"
)
set(GEOSChem_Fortran_FLAGS_RELEASE_Intel
-O2
CACHE STRING "GEOSChem compiler flags for build type release with Intel compilers"
CACHE STRING "GEOSChem compiler flags for build type Release with Intel compilers"
)
set(GEOSChem_Fortran_FLAGS_RELWITHDEBINFO_Intel
-O2
CACHE STRING "GEOSChem compiler flags for build type relwithdebinfo with Intel compilers"
CACHE STRING "GEOSChem compiler flags for build type RelWithdDebInfo with Intel compilers"
)
set(GEOSChem_Fortran_FLAGS_DEBUG_Intel
-g -O0 "SHELL:-check arg_temp_created" "SHELL:-debug all" -fpe0 -ftrapuv -check,bounds
CACHE STRING "GEOSChem compiler flags for build type debug with Intel compilers"
-O0 "SHELL:-check arg_temp_created" "SHELL:-debug all" -fpe0 -ftrapuv -check,bounds
lizziel marked this conversation as resolved.
Show resolved Hide resolved
CACHE STRING "GEOSChem compiler flags for build type Debug with Intel compilers"
)

set(GEOSChem_Fortran_FLAGS_GNU
-cpp -w -std=legacy -fautomatic -fno-align-commons
-g -cpp -w -std=legacy -fautomatic -fno-align-commons
-fconvert=big-endian -fno-range-check -mcmodel=medium
-fbacktrace -g -DLINUX_GFORTRAN -ffree-line-length-none
CACHE STRING "GEOSChem compiler flags for all build types with GNU compilers"
)
set(GEOSChem_Fortran_FLAGS_RELEASE_GNU
-O3 -funroll-loops
CACHE STRING "GEOSChem compiler flags for build type release with GNU compilers"
CACHE STRING "GEOSChem compiler flags for build type Release with GNU compilers"
)
set(GEOSChem_Fortran_FLAGS_RELWITHDEBINFO_GNU
-O3 -funroll-loops
CACHE STRING "GEOSChem compiler flags for build type relwithdebinfo with GNU compilers"
CACHE STRING "GEOSChem compiler flags for build type RelWithDebInfo with GNU compilers"
)
set(GEOSChem_Fortran_FLAGS_DEBUG_GNU
-g -gdwarf-2 -gstrict-dwarf -O0 -Wall -Wextra -Wconversion -Warray-temporaries
-O0 -Wall -Wextra -Wconversion -Warray-temporaries
-fcheck=array-temps -ffpe-trap=invalid,zero,overflow -finit-real=snan
-fcheck=bounds -fcheck=pointer
CACHE STRING "GEOSChem compiler flags for build type debug with GNU compilers"
CACHE STRING "GEOSChem compiler flags for build type Debug with GNU compilers"
)

set(GEOSChem_SUPPORTED_COMPILER_IDS "Intel" "GNU")
Expand Down Expand Up @@ -221,9 +221,9 @@ if(NOT GC_EXTERNAL_CONFIG)
set(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT FALSE)
gc_pretty_print(VARIABLE RUNDIR)

# Configure for GCClassic
include(GC-ConfigureClassic)
configureGCClassic()
# Configure for GCClassic
include(GC-ConfigureClassic)
configureGCClassic()

endif()

Expand Down
34 changes: 34 additions & 0 deletions CMakeScripts/GC-ConfigureClassic.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,39 @@ function(configureGCClassic)
INTERFACE $<$<BOOL:${USE_REAL8}>:USE_REAL8>
)

#-------------------------------------------------------------------------
# Add code sanitization options (GNU Fortran only)
# We need to add these options to both compiler & linker.
#-------------------------------------------------------------------------
set(SANITIZE OFF CACHE BOOL
"Switch to turn on code sanitation (i.e. identify memory leaks and similar conditions)"
)
gc_pretty_print(VARIABLE SANITIZE IS_BOOLEAN)
if(${SANITIZE})
if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU")
target_compile_options(GEOSChemBuildProperties
INTERFACE "-fsanitize=address"
)
target_link_libraries(GEOSChemBuildProperties
INTERFACE "-fsanitize=address"
)
target_compile_options(GEOSChemBuildProperties
INTERFACE "-fsanitize=leak"
)
target_link_libraries(GEOSChemBuildProperties
INTERFACE "-fsanitize=leak"
)
target_compile_options(GEOSChemBuildProperties
INTERFACE "-fsanitize=undefined"
)
target_link_libraries(GEOSChemBuildProperties
INTERFACE "-fsanitize=undefined"
)
else()
message( FATAL_ERROR "The SANITIZE option is only defined for GNU Fortran.")
endif()
endif()

#-------------------------------------------------------------------------
# Always set MODEL_CLASSIC when building GEOS-Chem Classic
#-------------------------------------------------------------------------
Expand Down Expand Up @@ -180,6 +213,7 @@ function(configureGCClassic)
set(RRTMG ${RRTMG} PARENT_SCOPE)
set(GTMM ${GTMM} PARENT_SCOPE)
set(LUO_WETDEP ${LUO_WETDEP} PARENT_SCOPE)
set(SANITIZE ${SANITIZE} PARENT_SCOPE)

#-------------------------------------------------------------------------
# Export information about Git status
Expand Down