-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flang][driver] Allow explicit specification of -lFortran_main #78152
Conversation
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.
@llvm/pr-subscribers-flang-driver @llvm/pr-subscribers-clang-driver Author: Tom Eccles (tblah) ChangesCurrently, I can understand there might be differing opinions on whether this is actually a bug. My thinking is that Full diff: https://github.com/llvm/llvm-project/pull/78152.diff 2 Files Affected:
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.
|
@llvm/pr-subscribers-clang Author: Tom Eccles (tblah) ChangesCurrently, I can understand there might be differing opinions on whether this is actually a bug. My thinking is that Full diff: https://github.com/llvm/llvm-project/pull/78152.diff 2 Files Affected:
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.
|
Windows uses different linker logic, which is unchanged by and not related to this patch.
I am not too fond of this. The current design has taken quite some effort and a few discussions to land in its current form. Also, Flang has gained proper documentation for this: This change is effectively undoing an important part of that. In this case:
the right thing to do would be to check the documentation, which should explain: "don't do this, because of xyz". In general, I see Having said all that, I'm not going to block this. Howerver, I would like to see that there's consensus before this lands. |
I agree that the documentation says not to do this, but existing common build configurations are doing it anyway. For example, building netcdf-parallel (https://github.com/Parallel-NetCDF/PnetCDF) using spack (https://spack.io/). netcdf-parallel uses autotools. I haven't tried another autotools project to see if it has the same problem. Of course the long term solution will be to fix autotools (or whatever the culprit is), but I worry it could take a long time for such patches to filter out to distros. In the meantime, I think it is a bad user experience to fail builds with a cryptic linker error. |
How would |
Good question. This won't work with this patch but currently should work. I'll have to fix this. Do you agree that |
I'm kind of split brain on this. While I do see the issue, I'm not sure if this should be considered a bug or a user error. One thing that came to my mind though is (remotely related to this) to have a command line flag that reports the proper libraries needed to successfully link a Fortran program using a different linker driver (e.g., clang). Similar to what
So, this would look like this at the moment:
|
In my opinion, we should hide 'libFortran_main' as much as possible since it is really an implementation detail. I can see that the proposed behavior will give users less surprise. I am slightly in favor of this behavior. In a long run, I still think that we need to get rid of |
I'd agree to that! The way gfortran does it, seems the best approach. When there's a program unit in the code, gfortran also add a I could opt-in to make this a learning project for me, but I'd need some hand holding to get it done. Volunteers? :-) |
+2
I'm happy to help, but I think we should discuss this in a community call first |
This works because I only remove |
I don't know what is the right way to handle the case that users have conflicting flags specified. This behavior is to only remove the implicit |
This comment made me realise what might be the source of confusion/friction. With
My suggestion:
The final point is probably key. Flang might change how "main" is defined in the future and we should discourage anyone linking |
@banach-space I tentatively support this. But I am concerned that disallowing |
That sounds like a good idea for now. Then, we can buy a bit of time for the RFC and to make decision how we want to deal with the |
These are very good points, +1 to all of the above! |
Intended to warn users of the 19.x release not to do this. A better solution should be found for the 20.x release. See discussion in llvm#78152. Unfortunately there is no warning on Windows currently. I am rushing to get this landed before 19.x branches.
Intended to warn users of the 18.x release not to do this. A better solution should be found for the 19.x release. See discussion in #78152. Unfortunately there is no warning on Windows currently. I am rushing to get this landed before 18.x branches.
Should this PR be closed at some point? It seems to be obsolete with the work that has been done in the recent past to improve the situation. |
Currently,
flang-new -lFortran_main
will fail on multiple definitions ofmain
.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
.