From 1bc521416f12dd319b23d29f4296be7ebc7f42a2 Mon Sep 17 00:00:00 2001
From: Brad King <brad.king@kitware.com>
Date: Thu, 27 Feb 2020 12:02:08 -0500
Subject: CheckFortranSource*: Avoid passing -DVAR to linker

The test project is compiled with a `-DVAR` compiler flag where `VAR` is
the result variable.  Tell `try_compile` to add the flag through
`add_definitions` instead of `CMAKE_Fortran_FLAGS` so that it is not
used for linking.  Otherwise some Fortran compilers (e.g. XL 15) do not
like the flag when used to drive linking.
---
 Modules/CheckFortranSourceCompiles.cmake |  6 ++--
 Modules/CheckFortranSourceRuns.cmake     |  6 ++--
 Tests/FortranOnly/CMakeLists.txt         | 50 +++++++++++++++-----------------
 3 files changed, 27 insertions(+), 35 deletions(-)

diff --git a/Modules/CheckFortranSourceCompiles.cmake b/Modules/CheckFortranSourceCompiles.cmake
index f0fde8d..3354bfb 100644
--- a/Modules/CheckFortranSourceCompiles.cmake
+++ b/Modules/CheckFortranSourceCompiles.cmake
@@ -103,8 +103,6 @@ macro(CHECK_Fortran_SOURCE_COMPILES SOURCE VAR)
     if(NOT _SRC_EXT)
       set(_SRC_EXT F)
     endif()
-    set(MACRO_CHECK_FUNCTION_DEFINITIONS
-      "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
     if(CMAKE_REQUIRED_LINK_OPTIONS)
       set(CHECK_Fortran_SOURCE_COMPILES_ADD_LINK_OPTIONS
         LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
@@ -132,10 +130,10 @@ macro(CHECK_Fortran_SOURCE_COMPILES SOURCE VAR)
     try_compile(${VAR}
       ${CMAKE_BINARY_DIR}
       ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.${_SRC_EXT}
-      COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
+      COMPILE_DEFINITIONS -D${VAR} ${CMAKE_REQUIRED_DEFINITIONS}
       ${CHECK_Fortran_SOURCE_COMPILES_ADD_LINK_OPTIONS}
       ${CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES}
-      CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
+      CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}
       "${CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES}"
       OUTPUT_VARIABLE OUTPUT)
 
diff --git a/Modules/CheckFortranSourceRuns.cmake b/Modules/CheckFortranSourceRuns.cmake
index a3e5d5d..f858b84 100644
--- a/Modules/CheckFortranSourceRuns.cmake
+++ b/Modules/CheckFortranSourceRuns.cmake
@@ -98,8 +98,6 @@ macro(CHECK_Fortran_SOURCE_RUNS SOURCE VAR)
     if(NOT _SRC_EXT)
       set(_SRC_EXT F90)
     endif()
-    set(MACRO_CHECK_FUNCTION_DEFINITIONS
-      "-D${VAR} ${CMAKE_REQUIRED_FLAGS}")
     if(CMAKE_REQUIRED_LINK_OPTIONS)
       set(CHECK_Fortran_SOURCE_COMPILES_ADD_LINK_OPTIONS
         LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
@@ -127,10 +125,10 @@ macro(CHECK_Fortran_SOURCE_RUNS SOURCE VAR)
     try_run(${VAR}_EXITCODE ${VAR}_COMPILED
       ${CMAKE_BINARY_DIR}
       ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.${_SRC_EXT}
-      COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
+      COMPILE_DEFINITIONS -D${VAR} ${CMAKE_REQUIRED_DEFINITIONS}
       ${CHECK_Fortran_SOURCE_COMPILES_ADD_LINK_OPTIONS}
       ${CHECK_Fortran_SOURCE_COMPILES_ADD_LIBRARIES}
-      CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_FUNCTION_DEFINITIONS}
+      CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${CMAKE_REQUIRED_FLAGS}
       -DCMAKE_SKIP_RPATH:BOOL=${CMAKE_SKIP_RPATH}
       "${CHECK_Fortran_SOURCE_COMPILES_ADD_INCLUDES}"
       COMPILE_OUTPUT_VARIABLE OUTPUT
diff --git a/Tests/FortranOnly/CMakeLists.txt b/Tests/FortranOnly/CMakeLists.txt
index d945375..4327c2f 100644
--- a/Tests/FortranOnly/CMakeLists.txt
+++ b/Tests/FortranOnly/CMakeLists.txt
@@ -51,40 +51,36 @@ add_custom_target(checksayhello ALL
   )
 add_dependencies(checksayhello sayhello)
 
-# Exclude this test on IBM XL for now because the check strangely
-# fails with 'ld: 0706-029 Use a number with the -H flag'.
-if(NOT CMAKE_Fortran_COMPILER_ID STREQUAL XL)
-  set(err_log ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeError.log)
-  file(REMOVE "${err_log}")
-  include(CheckFortranSourceCompiles)
-  unset(HAVE_PRINT CACHE)
-  CHECK_Fortran_SOURCE_COMPILES([[
+set(err_log ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeError.log)
+file(REMOVE "${err_log}")
+include(CheckFortranSourceCompiles)
+unset(HAVE_PRINT CACHE)
+CHECK_Fortran_SOURCE_COMPILES([[
       PROGRAM TEST_HAVE_PRINT
         PRINT *, 'Hello'
       END
 ]] HAVE_PRINT)
-  if(NOT HAVE_PRINT)
-    if(EXISTS "${err_log}")
-      file(READ "${err_log}" err)
-    endif()
-    string(REPLACE "\n" "\n  " err "  ${err}")
-    message(SEND_ERROR "CHECK_Fortran_SOURCE_COMPILES for HAVE_PRINT failed:\n"
-      "${err}")
+if(NOT HAVE_PRINT)
+  if(EXISTS "${err_log}")
+    file(READ "${err_log}" err)
   endif()
+  string(REPLACE "\n" "\n  " err "  ${err}")
+  message(SEND_ERROR "CHECK_Fortran_SOURCE_COMPILES for HAVE_PRINT failed:\n"
+    "${err}")
+endif()
 
-  unset(Fortran_BOGUS_FLAG CACHE)
-  include(CheckFortranCompilerFlag)
-  CHECK_Fortran_COMPILER_FLAG(-_this_is_not_a_flag_ Fortran_BOGUS_FLAG)
-  if (Fortran_BOGUS_FLAG)
-    message(SEND_ERROR "CHECK_Fortran_COMPILER_FLAG() succeeded, but should have failed")
-  endif()
+unset(Fortran_BOGUS_FLAG CACHE)
+include(CheckFortranCompilerFlag)
+CHECK_Fortran_COMPILER_FLAG(-_this_is_not_a_flag_ Fortran_BOGUS_FLAG)
+if (Fortran_BOGUS_FLAG)
+  message(SEND_ERROR "CHECK_Fortran_COMPILER_FLAG() succeeded, but should have failed")
+endif()
 
-  unset(Fortran_RUN_FLAG CACHE)
-  include(CheckFortranSourceRuns)
-  check_fortran_source_runs("program a; end program" Fortran_RUN_FLAG SRC_EXT F90)
-  if(NOT Fortran_RUN_FLAG)
-    message(SEND_ERROR "CHECK_Fortran_SOURCE_RUNS() failed")
-  endif()
+unset(Fortran_RUN_FLAG CACHE)
+include(CheckFortranSourceRuns)
+check_fortran_source_runs("program a; end program" Fortran_RUN_FLAG SRC_EXT F90)
+if(NOT Fortran_RUN_FLAG)
+  message(SEND_ERROR "CHECK_Fortran_SOURCE_RUNS() failed")
 endif()
 
 # Test generation of preprocessed sources.
-- 
cgit v0.12