[flang-commits] [flang] [clang] [flang][driver][RFC] Add -fno-fortran-main (link time) option to remove Fortran_main from link line (PR #74139)

Michael Klemm via flang-commits flang-commits at lists.llvm.org
Fri Dec 1 13:14:05 PST 2023


https://github.com/mjklemm updated https://github.com/llvm/llvm-project/pull/74139

>From 2e41335a7de3d2efa88eacee659172a3b9525e45 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Fri, 1 Dec 2023 21:41:44 +0100
Subject: [PATCH 1/4] Add -fno-fortran-main driver option

---
 clang/include/clang/Driver/Options.td | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 19d04e82aed4d68..aa26344f67b3132 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -6345,6 +6345,10 @@ def J : JoinedOrSeparate<["-"], "J">,
   Group<gfortran_Group>,
   Alias<module_dir>;
 
+def no_fortran_main : Flag<["-"], "fno-fortran-main">,
+  Visibility<[FlangOption]>, Group<f_Group>,
+  HelpText<"Don't link in Fortran main">;
+
 //===----------------------------------------------------------------------===//
 // FC1 Options
 //===----------------------------------------------------------------------===//

>From 44b684ae43a6da37bb56c5b699628c6807257ad9 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Fri, 1 Dec 2023 21:42:09 +0100
Subject: [PATCH 2/4] Skip linking Fortran_main.a if -fno-fortran-main is
 present

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 46 ++++++++++++----------
 1 file changed, 25 insertions(+), 21 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 0ae8e2dce32e94a..2899f07cb7490ca 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1116,33 +1116,37 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
   return true;
 }
 
+// TODO: add -fno-fortran-main option and check it in this function.
 void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
                                   llvm::opt::ArgStringList &CmdArgs) {
   // These are handled earlier on Windows by telling the frontend driver to add
   // the correct libraries to link against as dependents in the object file.
   if (!TC.getTriple().isKnownWindowsMSVCEnvironment()) {
-    // The --whole-archive option needs to be part of the link line to
-    // make sure that the main() function from Fortran_main.a is pulled
-    // in by the linker.  Determine if --whole-archive is active when
-    // flang will try to link Fortran_main.a.  If it is, don't add the
-    // --whole-archive flag to the link line.  If it's not, add a proper
-    // --whole-archive/--no-whole-archive bracket to the link line.
-    bool WholeArchiveActive = false;
-    for (auto *Arg : Args.filtered(options::OPT_Wl_COMMA))
-      if (Arg)
-        for (StringRef ArgValue : Arg->getValues()) {
-          if (ArgValue == "--whole-archive")
-            WholeArchiveActive = true;
-          if (ArgValue == "--no-whole-archive")
-            WholeArchiveActive = false;
-        }
-
-    if (!WholeArchiveActive)
-      CmdArgs.push_back("--whole-archive");
-    CmdArgs.push_back("-lFortran_main");
-    if (!WholeArchiveActive)
-      CmdArgs.push_back("--no-whole-archive");
+    // if -fno-fortran-main has been passed, skip linking Fortran_main.a
+    bool DontLinkFortranMain = Args.getLastArg(options::OPT_no_fortran_main) != nullptr;
+    if (!DontLinkFortranMain) {
+      // The --whole-archive option needs to be part of the link line to
+      // make sure that the main() function from Fortran_main.a is pulled
+      // in by the linker.  Determine if --whole-archive is active when
+      // flang will try to link Fortran_main.a.  If it is, don't add the
+      // --whole-archive flag to the link line.  If it's not, add a proper
+      // --whole-archive/--no-whole-archive bracket to the link line.
+      bool WholeArchiveActive = false;
+      for (auto *Arg : Args.filtered(options::OPT_Wl_COMMA))
+        if (Arg)
+          for (StringRef ArgValue : Arg->getValues()) {
+            if (ArgValue == "--whole-archive")
+              WholeArchiveActive = true;
+            if (ArgValue == "--no-whole-archive")
+              WholeArchiveActive = false;
+          }
 
+      if (!WholeArchiveActive)
+        CmdArgs.push_back("--whole-archive");
+      CmdArgs.push_back("-lFortran_main");
+      if (!WholeArchiveActive)
+        CmdArgs.push_back("--no-whole-archive");
+    }
     // Perform regular linkage of the remaining runtime libraries.
     CmdArgs.push_back("-lFortranRuntime");
     CmdArgs.push_back("-lFortranDecimal");

>From e1a472fa914ea9405be6589e89fbe8201448600f Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Fri, 1 Dec 2023 15:10:29 -0600
Subject: [PATCH 3/4] Cleanup and simplify the code a bit

---
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 2899f07cb7490ca..f0e3df2a63ae989 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1116,14 +1116,13 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
   return true;
 }
 
-// TODO: add -fno-fortran-main option and check it in this function.
 void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
                                   llvm::opt::ArgStringList &CmdArgs) {
   // These are handled earlier on Windows by telling the frontend driver to add
   // the correct libraries to link against as dependents in the object file.
   if (!TC.getTriple().isKnownWindowsMSVCEnvironment()) {
     // if -fno-fortran-main has been passed, skip linking Fortran_main.a
-    bool DontLinkFortranMain = Args.getLastArg(options::OPT_no_fortran_main) != nullptr;
+    bool DontLinkFortranMain = Args.hasArg(options::OPT_no_fortran_main);
     if (!DontLinkFortranMain) {
       // The --whole-archive option needs to be part of the link line to
       // make sure that the main() function from Fortran_main.a is pulled

>From fd6cc81bd2ac594b23c6e8f96ad81e18fb1213d2 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Fri, 1 Dec 2023 22:13:25 +0100
Subject: [PATCH 4/4] Update test to reflect positive outcome for
 -fno-fortran-main

---
 flang/test/Driver/no_duplicate_main.f90 | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/flang/test/Driver/no_duplicate_main.f90 b/flang/test/Driver/no_duplicate_main.f90
index 4e33f4f2aeba3f7..12d5e46247bad27 100644
--- a/flang/test/Driver/no_duplicate_main.f90
+++ b/flang/test/Driver/no_duplicate_main.f90
@@ -4,6 +4,8 @@
 ! RUN: %flang -o %t -c %s
 ! RUN: not %flang -o %t.exe %t %t.c-object 2>&1
 
+! RUN: %flang -fno-fortran-main -o %t.exe %t %t.c-object 2>&1
+
 ! TODO: potentially add further checks to ensure that proper
 !       linker error messages are detected and checked via
 !       FileCheck.



More information about the flang-commits mailing list