Skip to content

Commit c2fd6e7

Browse files
committed
Make CMake's Fortran cmplr ver. extraction robust
1 parent a0cd2f7 commit c2fd6e7

File tree

1 file changed

+42
-23
lines changed

1 file changed

+42
-23
lines changed

CMakeLists.txt

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,48 @@ else()
4545
)
4646
endif()
4747

48-
if (NOT (CMAKE_VERSION VERSION_LESS 3.3.1))
49-
# Detect Fortran compiler version directly
48+
#-----------------------------------------------------------------
49+
# Set CMAKE_Fortran_COMPILER_VERSION if CMake doesn't do it for us
50+
#-----------------------------------------------------------------
51+
if ( NOT CMAKE_Fortran_COMPILER_VERSION )
52+
if ( CMAKE_VERSION VERSION_GREATER_EQUAL 3.3.1 )
53+
message( AUTHOR_WARNING
54+
"CMake ${CMAKE_VERSION} should know about Fortran compiler versions but is missing CMAKE_Fortran_COMPILER_VERSION variable."
55+
)
56+
endif()
57+
# No CMAKE_Fortran_COMPILER_VERSION set, build our own
58+
# Try extracting it directly from ISO_FORTRAN_ENV's compiler_version
59+
# Write program for introspection
60+
file( WRITE "${CMAKE_BINARY_DIR}/get_compiler_ver.f90"
61+
"program main
62+
use iso_fortran_env, only: compiler_version, output_unit
63+
write(output_unit,'(a)') compiler_version()
64+
end program"
65+
)
66+
try_run( PROG_RAN COMPILE_SUCCESS
67+
"${CMAKE_BINARY_DIR}" "${CMAKE_BINARY_DIR}/get_compiler_ver.f90"
68+
RUN_OUTPUT_VARIABLE VER_STRING
69+
)
70+
if ( COMPILE_SUCCESS )
71+
string( REGEX MATCH "[0-9]+\\.[0-9]+(\\.[0-9]+)?"
72+
DETECTED_VER "${VER_STRING}"
73+
)
74+
message( STATUS "Detected Fortran compiler as ${VER_STRING}" )
75+
message( STATUS "Extracted version number: ${DETECTED_VER}" )
76+
endif()
77+
if( ( NOT COMPILE_SUCCESS ) OR ( NOT DETECTED_VER ) )
78+
message( WARNING "Could not reliably detect Fortran compiler version. We'll infer it from
79+
the C compiler if it matches the Fortran compiler ID." )
80+
endif()
81+
if( "${CMAKE_C_COMPILER_ID}" MATCHES "${CMAKE_Fortran_COMPILER_ID}" )
82+
set( DETECTED_VER "${CMAKE_C_COMPILER_VERSION}" )
83+
else()
84+
message( FATAL_ERROR "Exhausted all possible means of detecting the Fortran compiler version, cannot proceed!" )
85+
endif()
86+
set( CMAKE_Fortran_COMPILER_VERSION "${DETECTED_VER}" )
87+
endif()
88+
89+
# We have populated CMAKE_Fortran_COMPILER_VERSION if it was missing
5090
if(gfortran_compiler AND (CMAKE_Fortran_COMPILER_VERSION VERSION_GREATER 5.0.0))
5191
set(opencoarrays_aware_compiler true)
5292
add_definitions(-DPREFIX_NAME=_gfortran_caf_)
@@ -65,27 +105,6 @@ if (NOT (CMAKE_VERSION VERSION_LESS 3.3.1))
65105
CACHE STRING "Flags used by the compiler during release builds with debug info" FORCE)
66106
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -O0")
67107
endif()
68-
else()
69-
# Use the C compiler version as a proxy for the Fortran compiler version (won't work with NAG)
70-
if(gfortran_compiler AND (CMAKE_C_COMPILER_VERSION VERSION_GREATER 5.0.0))
71-
set(opencoarrays_aware_compiler true)
72-
add_definitions(-DPREFIX_NAME=_gfortran_caf_)
73-
else()
74-
set(opencoarrays_aware_compiler false)
75-
add_definitions(-DPREFIX_NAME=_caf_extensions_)
76-
endif()
77-
if(gfortran_compiler AND (CMAKE_C_COMPILER_VERSION VERSION_LESS 5.4))
78-
# GCC patch to fix issue accepted for the 5.4 release
79-
# See https://github.com/sourceryinstitute/opencoarrays/issues/28 and
80-
# https://groups.google.com/forum/#!msg/opencoarrays/RZOwwYTqG80/46S9eL696dgJ
81-
message( STATUS "Disabling optimization flags due to GCC < 5.4 bug")
82-
set(CMAKE_Fortran_FLAGS_RELEASE -O0
83-
CACHE STRING "Flags used by the compiler during release builds." FORCE)
84-
set(CMAKE_Fortran_FLAGS_RELWITHDEBINFO "-g -DNDEBUG -O0"
85-
CACHE STRING "Flags used by the compiler during release builds with debug info" FORCE)
86-
set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -O0")
87-
endif()
88-
endif()
89108

90109
if(gfortran_compiler)
91110
set(OLD_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})

0 commit comments

Comments
 (0)