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

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Tue Jan 16 10:00:46 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/4] [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 385f66f3782bc1a..82edb93d157d3f1 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 ea91946316cfaa6..0d531cedff4bd2c 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/4] 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 0d531cedff4bd2c..fec5abf192bf593 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,

>From 711c1b20600251bdef823501382d1b2da45f742c Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Tue, 16 Jan 2024 17:50:09 +0000
Subject: [PATCH 3/4] Use strncmp

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 82edb93d157d3f1..a46e53a16437d0c 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1200,16 +1200,17 @@ 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()) {
+    const char *LinkFlag = "-lFortran_main";
     // 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;
+    llvm::erase_if(CmdArgs, [LinkFlag](const char *arg) {
+      return strncmp(arg, LinkFlag, strlen(LinkFlag)) == 0;
     });
 
     CmdArgs.push_back("--whole-archive");
-    CmdArgs.push_back("-lFortran_main");
+    CmdArgs.push_back(LinkFlag);
     CmdArgs.push_back("--no-whole-archive");
     return;
   }

>From a1e97384bdb165ad0fe747d6d98fe0ed243e35f3 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Tue, 16 Jan 2024 17:58:18 +0000
Subject: [PATCH 4/4] Verify that -fno-fortran-main -lFortran_main works

---
 flang/test/Driver/linker-flags.f90 | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index fec5abf192bf593..0b1aeaae49b9e2d 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -16,6 +16,9 @@
 ! UNSUPPORTED: system-windows
 ! RUN: %flang -lFortran_main -lFortranRuntime -lFortranDecimal %S/Inputs/hello.f90 -o %s.out
 
+! Verify that -fno-fortran-main -lFortran_main does link Fortran_main
+! RUN: %flang -### -fno-fortran-main -lFortran_main --target=aarch64-unknown-linux-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MANUAL_MAIN
+
 ! 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.
@@ -57,3 +60,5 @@
 ! MSVC-LABEL: link
 ! MSVC-SAME: /subsystem:console
 ! MSVC-SAME: "[[object_file]]"
+
+! MANUAL_MAIN: -lFortran_main



More information about the flang-commits mailing list