Blob Blame History Raw
From 537d964fab8b3f524eb3622c53f22279ea14fd62 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Esser?= <besser82@fedoraproject.org>
Date: Wed, 19 Jan 2022 17:26:14 +0100
Subject: [PATCH] FortranCInterface: Fix compatibility with GCC gfortran 12 LTO
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Since version 12.0 the GCC Fortran compiler has implemented "WG5/N1942",
which causes, if link-time opmization is enabled, obfuscation of hard-coded
string values in the compiler objects and its resulting ELF-binaries.

This causes the CMake-internal detection of the mangling scheme for the
naming of subroutines to fail.  Thus we must ensure to have any link-time
optimization features to be disabled on the executable file we perform the
detection on.

Fixes: #23123

Signed-off-by: Björn Esser <besser82@fedoraproject.org>
---
 Modules/FortranCInterface/CMakeLists.txt | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/Modules/FortranCInterface/CMakeLists.txt b/Modules/FortranCInterface/CMakeLists.txt
index 13e4498ad9..f21b92fc54 100644
--- a/Modules/FortranCInterface/CMakeLists.txt
+++ b/Modules/FortranCInterface/CMakeLists.txt
@@ -5,6 +5,15 @@ cmake_minimum_required(VERSION ${CMAKE_VERSION})
 project(FortranCInterface C Fortran)
 include(${FortranCInterface_BINARY_DIR}/Input.cmake OPTIONAL)
 
+# If IPO is enabled here, GCC gfortran >= 12.0 will obfuscate
+# the strings of the return values in the compiled executable,
+# which we use to regex match against later.
+if(CMAKE_Fortran_COMPILER_ID STREQUAL "GNU" AND
+  CMAKE_Fortran_COMPILER_VERSION VERSION_GREATER_EQUAL 12)
+  string(APPEND CMAKE_C_FLAGS " -fno-lto")
+  string(APPEND CMAKE_Fortran_FLAGS " -fno-lto")
+endif()
+
 # Check if the C compiler supports '$' in identifiers.
 include(CheckCSourceCompiles)
 check_c_source_compiles("
-- 
2.34.1