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

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 15 03:55:26 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Tom Eccles (tblah)

<details>
<summary>Changes</summary>

Currently, `flang-new -lFortran_main` will fail on multiple definitions of `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`.

---
Full diff: https://github.com/llvm/llvm-project/pull/78152.diff


2 Files Affected:

- (modified) clang/lib/Driver/ToolChains/CommonArgs.cpp (+8) 
- (modified) flang/test/Driver/linker-flags.f90 (+3) 


``````````diff
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.

``````````

</details>


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


More information about the cfe-commits mailing list