[flang] [clang] [flang][Driver] Let the linker fail on multiple definitions of main() (PR #73124)

Krzysztof Parzyszek via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 28 08:20:28 PST 2023


================
@@ -977,14 +977,63 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
   return true;
 }
 
-void tools::addFortranRuntimeLibs(const ToolChain &TC,
+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)
----------------
kparzysz wrote:

Thanks.  If could be somewhat simplified though.  You could use
```
Arg *A = Args.getLastArg(clang::driver::options::OPT_Wl_COMMA));
```
To get the last instance of `-Wl`, and then iterate over the values in reverse, using `StringRef`:
```
for (StringRef V : llvm::reverse(A->getValues()))
```

`StringRef` allows comparisons against string literals, e.g. `string_ref == "blah"`,


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


More information about the cfe-commits mailing list