Created
June 13, 2022 20:00
-
-
Save scivision/a0eb5aecb265b5932da059864d491a9e to your computer and use it in GitHub Desktop.
CMake compiler hinting--not normally needed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this must be include() before CMakeLists.txt project() | |
# --- Help CMake find matching compilers | |
function(find_fortran) | |
if(NOT DEFINED FC AND DEFINED ENV{FC}) | |
set(FC $ENV{FC}) | |
endif() | |
if(FC) | |
get_filename_component(_dir ${FC} DIRECTORY) | |
endif() | |
# determine if the user is intending to use Intel oneAPI or default Gfortran | |
# Need to check because MKLROOT may be defined for | |
# use of oneMKL with Gfortran on Linux. | |
if(DEFINED ENV{MKLROOT} OR FC MATCHES "(ifort|ifx)") | |
find_program(FC | |
NAMES ifx ifort | |
NAMES_PER_DIR | |
HINTS ${_dir}) | |
endif() | |
find_program(FC | |
NAMES gfortran gfortran-12 gfortran-11 gfortran-10 | |
NAMES_PER_DIR | |
HINTS ${_dir}) | |
if(FC) | |
set(ENV{FC} ${FC}) | |
# ENV{FC} is how project() picks up our hint | |
endif() | |
endfunction(find_fortran) | |
function(find_c) | |
if(NOT DEFINED CC) | |
if(DEFINED ENV{CC}) | |
set(CC $ENV{CC}) | |
elseif(NOT DEFINED FC AND DEFINED ENV{FC}) | |
set(FC $ENV{FC}) | |
endif() | |
endif() | |
if(NOT DEFINED CC) | |
# Apple has "/usr/bin/gcc" which is really clang | |
if(DEFINED FC) | |
get_filename_component(_dir ${FC} DIRECTORY) | |
endif() | |
# use same compiler for C and Fortran | |
if(FC MATCHES "(ifort|ifx)") | |
if(APPLE) | |
set(_cc icc) | |
else() | |
set(_cc icx) | |
endif() | |
elseif(FC MATCHES "gfortran") | |
# get same GCC version as Gfortran | |
execute_process(COMMAND ${FC} -dumpversion | |
OUTPUT_VARIABLE _v | |
RESULT_VARIABLE _err) | |
if(NOT _err EQUAL 0) | |
return() | |
endif() | |
string(REGEX MATCH "^([0-9]+)" _v ${_v}) | |
if(_v) | |
set(_cc gcc-${_v}) | |
else() | |
set(_cc gcc-12 gcc-11 gcc-10 gcc) # generic last to avoid AppleClang | |
endif() | |
endif() | |
endif() | |
if(NOT _cc) | |
return() | |
endif() | |
find_program(CC | |
NAMES ${_cc} | |
NAMES_PER_DIR | |
HINTS ${_dir} | |
NO_SYSTEM_ENVIRONMENT_PATH NO_CMAKE_SYSTEM_PATH) | |
if(CC) | |
set(ENV{CC} ${CC}) | |
# ENV{CC} is how project() picks up our hint | |
endif() | |
endfunction(find_c) | |
function(find_c_fortran) | |
find_fortran() | |
find_c() | |
endfunction(find_c_fortran) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment