[flang-commits] [flang] [Flang] Fix more of CMake Fortran detection probes (PR #204438)
Michael Kruse via flang-commits
flang-commits at lists.llvm.org
Wed Jun 17 13:51:18 PDT 2026
https://github.com/Meinersbur created https://github.com/llvm/llvm-project/pull/204438
Tested with CMake 3.28 and 4.3, supporting the following variants that CMake may use to introspect the Fortran compiler:
1. `__GNUC__` may be tested before `__flang__` which results in the compiler to be identified as "GNU"
2. Invoke with `-E -cpp` to explicitly use the preprocessor
Should fix the [flang-aarch64-rel-assert](https://lab.llvm.org/buildbot/#/builders/29) buildbot
>From da08d8e8cee6f8f0419c5fdbce93ffbb8c3de885 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Wed, 17 Jun 2026 22:39:24 +0200
Subject: [PATCH 1/2] Fix CMake detection
---
flang/tools/fakeflang/fakeflang.cpp | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/flang/tools/fakeflang/fakeflang.cpp b/flang/tools/fakeflang/fakeflang.cpp
index 500174b612862..635691be60981 100644
--- a/flang/tools/fakeflang/fakeflang.cpp
+++ b/flang/tools/fakeflang/fakeflang.cpp
@@ -84,9 +84,16 @@ int main(int argc, const char **argv) {
Args.append({ClangExe, "-E", "-P", "-D__flang__=1",
"-D__flang_major__=" FLANG_VERSION_MAJOR_STRING,
"-D__flang_minor__=" FLANG_VERSION_MINOR_STRING,
- "-D__flang_patchlevel__=" FLANG_VERSION_PATCHLEVEL_STRING, "-x", "c"});
- for (int I = 1; I < argc; ++I)
- Args.push_back(argv[I]);
+ "-D__flang_patchlevel__=" FLANG_VERSION_PATCHLEVEL_STRING,
+ "-U__GNUC__", "-x", "c"});
+ for (int I = 1; I < argc; ++I) {
+ llvm::StringRef arg = argv[I];
+
+ // Sometime CMake tries to invoke the preprocessor
+ if (arg == "-cpp" || arg == "-E") continue ;
+
+ Args.push_back(arg);
+ }
if (!hasDashO)
Args.append({"-o", "a.out"});
>From 47a3925439b4201bfdce2cd7178834c7f9d64bd9 Mon Sep 17 00:00:00 2001
From: Michael Kruse <llvm-project at meinersbur.de>
Date: Wed, 17 Jun 2026 22:44:46 +0200
Subject: [PATCH 2/2] Fix fakeflang
---
flang/test/Driver/fakeflang.F | 12 ++++++++++--
flang/tools/fakeflang/fakeflang.cpp | 7 ++++---
2 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/flang/test/Driver/fakeflang.F b/flang/test/Driver/fakeflang.F
index bd4ed88409bd7..526f535769049 100644
--- a/flang/test/Driver/fakeflang.F
+++ b/flang/test/Driver/fakeflang.F
@@ -4,11 +4,15 @@
! RUN: mkdir -p %t
! RUN: cd %t
-! RUN: fakeflang -v -c --target=x86_64-unknown-linux-gnu %s
+! RUN: fakeflang -v -c -cpp -E --target=x86_64-unknown-linux-gnu %s
! RUN: FileCheck %s --input-file=a.out --check-prefixes=CHECK,GNU
! RUN: rm a.out
-! RUN: fakeflang -v -c --target=x86_64-pc-windows-msvc %s
+! RUN: fakeflang -v -c -cpp -E --target=aarch64-linux-gnu %s
+! RUN: FileCheck %s --input-file=a.out --check-prefixes=CHECK,GNU
+! RUN: rm a.out
+
+! RUN: fakeflang -v -c -cpp -E --target=x86_64-pc-windows-msvc %s
! RUN: FileCheck %s --input-file=a.out --check-prefixes=CHECK,MSVC --match-full-lines
! RUN: rm a.out
@@ -16,6 +20,10 @@
! CHECK: CMAKE_Fortran_COMPILER_ID=1
CMAKE_Fortran_COMPILER_ID=__flang__
+! __GNUC__ is not defined
+! CHECK: __GNUC__
+__GNUC__
+
! CHECK: CMAKE_Fortran_COMPILER_VERSION={{[0-9]+}} . {{[0-9]+}} . {{[0-9]+}}
CMAKE_Fortran_COMPILER_VERSION=__flang_major__.__flang_minor__.__flang_patchlevel__
diff --git a/flang/tools/fakeflang/fakeflang.cpp b/flang/tools/fakeflang/fakeflang.cpp
index 635691be60981..f1eb5bf0921d0 100644
--- a/flang/tools/fakeflang/fakeflang.cpp
+++ b/flang/tools/fakeflang/fakeflang.cpp
@@ -84,13 +84,14 @@ int main(int argc, const char **argv) {
Args.append({ClangExe, "-E", "-P", "-D__flang__=1",
"-D__flang_major__=" FLANG_VERSION_MAJOR_STRING,
"-D__flang_minor__=" FLANG_VERSION_MINOR_STRING,
- "-D__flang_patchlevel__=" FLANG_VERSION_PATCHLEVEL_STRING,
- "-U__GNUC__", "-x", "c"});
+ "-D__flang_patchlevel__=" FLANG_VERSION_PATCHLEVEL_STRING, "-U__GNUC__",
+ "-x", "c"});
for (int I = 1; I < argc; ++I) {
llvm::StringRef arg = argv[I];
// Sometime CMake tries to invoke the preprocessor
- if (arg == "-cpp" || arg == "-E") continue ;
+ if (arg == "-cpp" || arg == "-E")
+ continue;
Args.push_back(arg);
}
More information about the flang-commits
mailing list