[clang] [flang] [flang][driver] Allow explicit specification of -lFortran_main (PR #78152)

Tom Eccles via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 15 05:08:59 PST 2024


https://github.com/tblah updated https://github.com/llvm/llvm-project/pull/78152

>From 5c7fa9c3ef911674e5b6888fcba4289834d04fda Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Mon, 15 Jan 2024 11:27:46 +0000
Subject: [PATCH 1/2] [flang][driver] Allow explicit specification of
 -lFortran_main

I can understand there might be differing opinions on whether this is
actually a bug. My thinking is that -lFortran_main should behave the
same as -lFortranRuntime.
---
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 8 ++++++++
 flang/test/Driver/linker-flags.f90         | 3 +++
 2 files changed, 11 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 385f66f3782bc1..82edb93d157d3f 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1200,6 +1200,14 @@ static void addFortranMain(const ToolChain &TC, const ArgList &Args,
   // TODO: Find an equivalent of `--whole-archive` for Darwin and AIX.
   if (!isWholeArchivePresent(Args) && !TC.getTriple().isMacOSX() &&
       !TC.getTriple().isOSAIX()) {
+    // Adding -lFortran_main with --whole-archive will create an error if the
+    // user specifies -lFortran_main explicitly. Remove the user's
+    // -lFortran_main arguments to avoid this (making sure -lFortran_main
+    // behaves the same as -lFortranRuntime)
+    llvm::erase_if(CmdArgs, [](const char *arg) {
+      return strcmp(arg, "-lFortran_main") == 0;
+    });
+
     CmdArgs.push_back("--whole-archive");
     CmdArgs.push_back("-lFortran_main");
     CmdArgs.push_back("--no-whole-archive");
diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index ea91946316cfaa..0d531cedff4bd2 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -12,6 +12,9 @@
 ! RUN: %flang -### --target=x86_64-unknown-haiku %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,HAIKU
 ! RUN: %flang -### --target=x86_64-windows-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MINGW
 
+! Verify that linking the runtime explicitly doesn't result in a multiple definitions of main error
+! RUN: %flang -lFortran_main -lFortranRuntime -lFortranDecimal %S/Inputs/hello.f90 -o %s.out
+
 ! NOTE: Clang's driver library, clangDriver, usually adds 'oldnames' on Windows,
 !       but it is not needed when compiling Fortran code and they might bring in
 !       additional dependencies. Make sure its not added.

>From 7eb03ef56d278bc800884dfa0f33ee9b0ebccdb4 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Mon, 15 Jan 2024 13:08:07 +0000
Subject: [PATCH 2/2] Don't run the test on Windows

Windows uses different linker logic, which is unchanged by and not
related to this patch.
---
 flang/test/Driver/linker-flags.f90 | 1 +
 1 file changed, 1 insertion(+)

diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index 0d531cedff4bd2..fec5abf192bf59 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -13,6 +13,7 @@
 ! RUN: %flang -### --target=x86_64-windows-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MINGW
 
 ! Verify that linking the runtime explicitly doesn't result in a multiple definitions of main error
+! UNSUPPORTED: system-windows
 ! RUN: %flang -lFortran_main -lFortranRuntime -lFortranDecimal %S/Inputs/hello.f90 -o %s.out
 
 ! NOTE: Clang's driver library, clangDriver, usually adds 'oldnames' on Windows,



More information about the cfe-commits mailing list