Skip to content

[flang][driver] Allow explicit specification of -lFortran_main #78152

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1200,8 +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, [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;
}
Expand Down
9 changes: 9 additions & 0 deletions flang/test/Driver/linker-flags.f90
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
! 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
! 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.
Expand Down Expand Up @@ -53,3 +60,5 @@
! MSVC-LABEL: link
! MSVC-SAME: /subsystem:console
! MSVC-SAME: "[[object_file]]"

! MANUAL_MAIN: -lFortran_main