Files
kaiju/cmake/compilers.cmake

136 lines
5.0 KiB
CMake

#Handle compiler/defaults
#-------------
#Handle libraries (HDF5/OMP/MPI)
find_package(HDF5 REQUIRED COMPONENTS Fortran Fortran_HL)
set(CMAKE_REQUIRED_INCLUDES ${HDF5_Fortran_INCLUDE_DIRS})
set(CMAKE_REQUIRED_LIBRARIES ${HDF5_Fortran_LIBRARIES} ${HDF5_Fortran_HL_LIBRARIES})
#Use set compiler (below) or link_libraries but not both
set(CMAKE_Fortran_COMPILER ${HDF5_Fortran_COMPILER_EXECUTABLE})
find_package(OpenMP COMPONENTS Fortran)
set(CMAKE_REQUIRED_FLAGS ${OpenMP_Fortran_FLAGS})
set(CMAKE_REQUIRED_LIBRARIES ${OpenMP_Fortran_LIBRARIES})
if (ENABLE_MPI)
#mpi is a nightmare
#try to find a fortran 2008 specific wrapper first
set(MPI_Fortran_COMPILER mpif08)
find_package(MPI COMPONENTS Fortran QUIET)
if(NOT MPI_FOUND OR NOT MPI_Fortran_HAVE_F08_MODULE)
#just look for whatever
unset(MPI_Fortran_COMPILER)
find_package(MPI REQUIRED COMPONENTS Fortran)
else()
message("-- Found MPI")
endif()
if(NOT MPI_Fortran_HAVE_F08_MODULE)
message(FATAL_ERROR "MPI Library does not support F08 interface")
endif()
endif()
#-------------
#Set default build to release
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release")
endif()
#-------------
#Set base release options
set(CMAKE_DEFOPT "-O3") #Default optimization
#-------------
#Set minimum compiler versions
if(CMAKE_Fortran_COMPILER_ID MATCHES Intel)
if(CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 17.0)
message("Fortran compiler too old! What, were you gonna use punch cards?")
message(FATAL_ERROR "ifort > 17.0 required")
elseif( (CMAKE_Fortran_COMPILER_VERSION VERSION_GREATER_EQUAL 17.0) AND (CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 18.0) )
message(WARNING "Compiler has incomplete F2008 features, Git hash/compiler information won't be saved to H5 files")
add_compile_definitions(__INTEL_COMPILER_OLD)
elseif( (CMAKE_Fortran_COMPILER_VERSION VERSION_GREATER_EQUAL 20.0) )
message(WARNING "Setting default optimization to O2 to avoid certain Intel compiler bugs")
set(CMAKE_DEFOPT "-O2")
endif()
elseif(CMAKE_Fortran_COMPILER_ID MATCHES GNU)
if(CMAKE_Fortran_COMPILER_VERSION VERSION_LESS 8.0)
message("Fortran compiler too old! What, were you gonna use punch cards?")
message(FATAL_ERROR "gfortran > 8.0 required")
endif()
endif()
set(CMAKE_Fortran_FLAGS_DEBUG "-g")
set(CMAKE_Fortran_FLAGS_RELEASE "${CMAKE_DEFOPT}")
set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "${CMAKE_DEFOPT} -g")
#Do compiler specific options
if(CMAKE_Fortran_COMPILER_ID MATCHES Intel)
set(dialect "-free -implicitnone")
#Base
string(APPEND CMAKE_Fortran_FLAGS " -fPIC")
#Production
set(PROD "-align array64byte -align rec32byte -no-prec-div -fast-transcendentals")
#Debug
set(DEBUG "-g -traceback -check bounds -check uninit -debug all -gen-interfaces -warn interfaces -fp-stack-check")
set(PRODWITHDEBUGINFO "${CMAKE_DEFOPT} -g -traceback -debug all -align array64byte -align rec32byte -no-prec-div -fast-transcendentals")
#Now do OS-dep options
if (CMAKE_SYSTEM_NAME MATCHES Darwin)
string(APPEND CMAKE_Fortran_FLAGS " -Wl,-stack_size,0x40000000,-stack_addr,0xf0000000 -xHost")
else()
#If we're not doing Mac, then add IPO
string(APPEND PROD " -ipo")
string(APPEND PRODWITHDEBUGINFO " -ipo")
endif()
#Handle individual hosts
if (HOST MATCHES cheyenne)
string(APPEND PROD " -march=corei7 -axCORE-AVX2")
string(APPEND PRODWITHDEBUGINFO " -march=corei7 -axCORE-AVX2")
endif()
#Check Intel Fortran version
if(NOT ALLOW_INVALID_COMPILERS AND CMAKE_Fortran_COMPILER_VERSION VERSION_GREATER "19")
message(FATAL_ERROR "Intel Fortran compilers 19 or newer are not supported. Set the ALLOW_INVALID_COMPILERS variable to ON to force compilation at your own risk.")
endif()
elseif(CMAKE_Fortran_COMPILER_ID MATCHES GNU)
set(dialect "-ffree-form -ffree-line-length-none -fimplicit-none")
#Base
string(APPEND CMAKE_Fortran_FLAGS " -fPIC")
#Production
set(PROD "-ffast-math")
#Debug
set(DEBUG "-fbacktrace -g -Warray-temporaries -Wall -Wfatal-errors -finit-local-zero")
#Now do machine-dep options
if (CMAKE_SYSTEM_NAME MATCHES Darwin)
string(APPEND CMAKE_Fortran_FLAGS " -Wl,-stack_size,0x40000000,-stack_addr,0xf0000000")
endif()
endif()
string(APPEND CMAKE_Fortran_FLAGS " ${dialect}")
string(APPEND CMAKE_Fortran_FLAGS_DEBUG " ${DEBUG}")
string(APPEND CMAKE_Fortran_FLAGS_RELEASE " ${PROD}")
string(APPEND CMAKE_Fortran_FLAGS_RELWITHDEBINFO " ${PRODWITHDEBUGINFO}")
if(ENABLE_OMP)
string(APPEND CMAKE_Fortran_FLAGS " ${OpenMP_Fortran_FLAGS}")
endif()
if(ENABLE_MPI)
add_compile_options(${MPI_Fortran_COMPILE_OPTIONS})
add_definitions(${MPI_Fortran_COMPILE_DEFINITIONS})
include_directories(${MPI_Fortran_INCLUDE_DIRS})
link_directories(${MPI_Fortran_LIBRARIES})
if(MPI_Fortran_COMPILER MATCHES mpiifort)
#Using Intel MPI Library
string(APPEND CMAKE_Fortran_FLAGS " -mt_mpi")
endif()
set(CMAKE_Fortran_COMPILER ${MPI_Fortran_COMPILER})
# we changed compiler, link HDF5 libraries
link_libraries(${HDF5_Fortran_LIBRARIES} ${HDF5_Fortran_HL_LIBRARIES})
endif()