#!-------------------------------------------------------------------------------------------------! #! CP2K: A general program to perform molecular dynamics simulations ! #! Copyright 2000-2026 CP2K developers group ! #! ! #! SPDX-License-Identifier: GPL-2.0-or-later ! #!-------------------------------------------------------------------------------------------------! set(CP2K_C_COMPILER_LIST "GNU;Intel;IntelLLVM;NAG;Cray;PGI;NVHPC;Clang;AppleClang") set(CP2K_Fortran_COMPILER_LIST "GNU;Intel;IntelLLVM;NAG;Cray;PGI;NVHPC") if(NOT CMAKE_C_COMPILER_ID IN_LIST CP2K_C_COMPILER_LIST) message( WARNING "Unknown C compiler, trying without any additional (optimization) flags.\n" "You may have to specify flags for C11 support manually.\n Please open an\n" "issue at https://github.com/cp2k/cp2k/issues with the reported compiler \n" "name and the required flags.") message("-- CMAKE_C_COMPILER_ID: ${CMAKE_C_COMPILER_ID}\n" "-- CMAKE_C_COMPILER full path: ${CMAKE_C_COMPILER}\n") endif() if(NOT CMAKE_Fortran_COMPILER_ID IN_LIST CP2K_Fortran_COMPILER_LIST) message( WARNING "Unknown Fortran compiler, trying without any additional (optimization) flags.\n" "You will most likely have to specify extra options for free-form Fortran 2008\n" "for your compiler! Please open an issue at https://github.com/cp2k/cp2k/issues\n" "with the reported compiler name and the required flags.") message("-- CMAKE_Fortran_COMPILER_ID: ${CMAKE_Fortran_COMPILER_ID}\n" "-- CMAKE_Fortran_COMPILER full path: ${CMAKE_Fortran_COMPILER}\n") endif() # ================================ GNU Compilers =============================== # Baseline add_compile_options( "$<$:-std=f2008;-ffree-form;-fimplicit-none>" "$<$:-g;-fno-omit-frame-pointer;-fbacktrace>" "$<$:$<$,10>:-fallow-argument-mismatch>>" "$<$:-Wno-deprecated-declarations;-Wno-maybe-uninitialized;-Wuninitialized;-Wuse-without-only>" ) add_compile_options( "$<$:-g;-fno-omit-frame-pointer>" "$<$:-Wno-deprecated-declarations;-Wno-vla-parameter>" ) add_compile_options( "$<$:-g;-fno-omit-frame-pointer>" "$<$:-Wno-deprecated-declarations;-Wno-vla-parameter>" ) # -- Apple Silicon + GCC: -march=native expands internally to -march=apple-m1 # (invalid). Use -mcpu=native instead. set(_CP2K_GNU_NATIVE_TUNE "-march=native;-mtune=native") if(APPLE AND CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64" AND (CMAKE_Fortran_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "GNU")) set(_CP2K_GNU_NATIVE_TUNE "-mcpu=native") endif() if(APPLE) add_definitions(-D__MACOSX) endif() if(APPLE OR BSD) add_definitions(-D__NO_STATM_ACCESS) endif() # Release add_compile_options( "$<$,$>:-O3;${_CP2K_GNU_NATIVE_TUNE};-funroll-loops>" "$<$,$>:-O3;${_CP2K_GNU_NATIVE_TUNE};-funroll-loops>" "$<$,$>:-O3;${_CP2K_GNU_NATIVE_TUNE};-funroll-loops>" ) # Generic add_compile_options( "$<$,$>:-O3;-mtune=generic;-funroll-loops>" "$<$,$>:-O3;-mtune=generic;-funroll-loops>" "$<$,$>:-O3;-mtune=generic;-funroll-loops>" ) # Debug # TODO: Replace deprecated MASTER constructs with MASKED (only available since # GCC 12) and remove "-Wno-error=deprecated-openmp" once GCC 11 support is # dropped. add_compile_options( "$<$,$>:-O1;${_CP2K_GNU_NATIVE_TUNE}>" "$<$,$>:-O1;${_CP2K_GNU_NATIVE_TUNE}>" "$<$,$>:-O1;${_CP2K_GNU_NATIVE_TUNE};-Wall;-Wextra;-Werror>" "$<$,$,$,16>>:-Wno-error=deprecated-openmp>" ) # FIXME: GCC 16 diagnoses legacy character-length mismatches that were not # reported by earlier GCC versions. Restore "-Werror=character-truncation" after # auditing and resolving them. add_compile_options( "$<$,$>:-fsanitize=leak;-Werror=realloc-lhs>" "$<$,$>:-fcheck=all,no-array-temps;-finline-matmul-limit=0>" "$<$,$>:-ffpe-trap=invalid,zero,overflow>" "$<$,$>:-finit-derived;-finit-real=snan;-finit-integer=-42>" "$<$,$>:-Werror=aliasing;-Werror=ampersand;-Werror=c-binding-type>" "$<$,$>:-Werror=intrinsics-std;-Werror=intrinsic-shadow>" "$<$,$>:-Werror=line-truncation;-Werror=tabs;-Werror=target-lifetime>" "$<$,$>:-Werror=underflow;-Werror=unused-but-set-variable;-Werror=unused-variable>" "$<$,$>:-Werror=unused-dummy-argument;-Werror=unused-parameter>" "$<$,$>:-Werror=unused-label;-Werror=conversion;-Werror=zerotrip>" ) add_compile_definitions("$<$:__HAS_IEEE_EXCEPTIONS;__CHECK_DIAG>") add_link_options( "$<$,$>:-fsanitize=leak>" "$<$,$>:-fsanitize=leak>" "$<$,$>:-fsanitize=leak>") # Conventions add_compile_options( "$<$:-O1;${_CP2K_GNU_NATIVE_TUNE}>" "$<$,$>:-Wno-pedantic;-Wall;-Wextra;-Wsurprising>" "$<$,$>:-Warray-temporaries;-Wconversion-extra;-Wimplicit-interface>" "$<$,$>:-Wimplicit-procedure;-Wreal-q-constant;-Walign-commons>" "$<$,$>:-Wfunction-elimination;-Wrealloc-lhs;-Wcompare-reals;-Wzerotrip>" # Writes a lot of output. Make sure to use: # -DCMAKE_Fortran_COMPILER_LAUNCHER="redirect_gfortran_output.py" "$<$,$>:-fdump-fortran-original>" ) # Coverage add_compile_options( "$<$:-coverage;-fkeep-static-functions;-O1;${_CP2K_GNU_NATIVE_TUNE}>" ) add_compile_definitions("$<$:__NO_ABORT>") # Address Sanitizer add_compile_options( "$<$:-fsanitize=address;-no-pie;-O3;${_CP2K_GNU_NATIVE_TUNE};-funroll-loops>" ) add_link_options("$<$:-fsanitize=address>") # =========================== Intel oneAPI Compilers =========================== # Baseline add_compile_options( "$<$:-g;-traceback>" "$<$:-free;-stand;f08;-fpp;-qopenmp;-heap-arrays;-D__MAX_CONTR=4>" "$<$:-fp-model;consistent;-fpscomp;logicals>" "$<$:-free;-stand;f08;-fpp;-qopenmp;-D__MAX_CONTR=4>" "$<$:-fp-model;consistent;-fpscomp;logicals>" ) # Release add_compile_options( "$<$,$>:-O3;-xHOST;-D__HAS_IEEE_EXCEPTIONS>" "$<$,$>:-O2;-xHOST;-D__HAS_IEEE_EXCEPTIONS>" "$<$,$>:-O3;-xHOST>" "$<$,$>:-O3;-xHOST>") # Debug add_compile_options( "$<$,$>:-O0;-debug>" "$<$,$>:-O0;-debug>" "$<$,$>:-O0;-g>" "$<$,$>:-O0;-g>") # =============================== Other Compilers ============================== # Baseline add_compile_options( "$<$:-Mfreeform;-Mextend;-Mallocatable=03>" "$<$:-f2008;-free;-Warn=reallocation;-Warn=subnormal>" "$<$:-f;free;-M3105;-ME7212;-hnoacc;-M1234>" ) add_compile_options( "$<$:-hnoacc;-h;nomessage=1234>") # Release add_compile_options( "$<$,$>:-fast>" "$<$,$>:-O2;-G2>" "$<$,$>:-gline>") add_compile_options( "$<$,$>:-fast>" "$<$,$>:-O3;-g>" "$<$,$>:-O3>" "$<$,$>:-gline>" "$<$,$>:-O3;-funroll-loops>" "$<$,$>:-O3;-funroll-loops>" "$<$>:$<$,$>:-gline>>" ) # Debug add_compile_options( "$<$,$>:-g>" "$<$,$>:-G2>") add_compile_options( "$<$,$>:-fast>" "$<$,$>:-O2;-g>" "$<$,$>:-G2>" "$<$,$>:-g;-C>" "$<$,$>:-O1;-g;-fno-omit-frame-pointer>" "$<$,$>:-O0;-g>" "$<$,$>:-C=all>") # =================================== Tweaks =================================== # Workaround https://gitlab.kitware.com/cmake/cmake/-/issues/27231 if(TARGET MPI::MPI_Fortran) get_target_property(opts MPI::MPI_Fortran INTERFACE_COMPILE_OPTIONS) set_target_properties( MPI::MPI_Fortran PROPERTIES INTERFACE_COMPILE_OPTIONS "$<$:${opts}>") unset(opts) endif() if(CMAKE_C_COMPILER_ID STREQUAL "AppleClang") set(CMAKE_Fortran_MODOUT_FLAG "-ef") # override to get lower-case module file # names set(CMAKE_EXE_LINKER_FLAGS_COVERAGE "-lgcov") # Apple's Clang needs an extra endif() if(CMAKE_Fortran_COMPILER_ID STREQUAL "Cray") set(CMAKE_Fortran_MODOUT_FLAG "-ef") # override to get lower-case module file # names endif() if(CMAKE_C_COMPILER_ID STREQUAL "Cray" AND CMAKE_C_COMPILER_VERSION VERSION_LESS 9) # prevent deallocation failures due to tcmalloc's free with glibc's # aligned_alloc, see https://bugzilla.redhat.com/show_bug.cgi?id=1569391 add_compile_options("$<$:-h;system_alloc>" "$<$:-h;system_alloc>") # since the detection of the implicitly linked libraries occurs before we can # intervene, filter them out again list(FILTER CMAKE_C_IMPLICIT_LINK_LIBRARIES EXCLUDE REGEX "tcmalloc") list(FILTER CMAKE_Fortran_IMPLICIT_LINK_LIBRARIES EXCLUDE REGEX "tcmalloc") endif() # Suppress GFortran runtime warnings when LIBXS provides the wrapper if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU" AND CP2K_USE_LIBXS AND NOT APPLE) add_link_options("-Wl,--wrap=_gfortran_runtime_warning_at") endif()