[flang-commits] [flang] [Flang] Fix more of CMake Fortran detection probes (PR #204438)
via flang-commits
flang-commits at lists.llvm.org
Wed Jun 17 13:52:13 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-driver
Author: Michael Kruse (Meinersbur)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/204438.diff
2 Files Affected:
- (modified) flang/test/Driver/fakeflang.F (+10-2)
- (modified) flang/tools/fakeflang/fakeflang.cpp (+11-3)
``````````diff
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 500174b612862..f1eb5bf0921d0 100644
--- a/flang/tools/fakeflang/fakeflang.cpp
+++ b/flang/tools/fakeflang/fakeflang.cpp
@@ -84,9 +84,17 @@ 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"});
``````````
</details>
https://github.com/llvm/llvm-project/pull/204438
More information about the flang-commits
mailing list