Skip to content

Commit 5c2f898

Browse files
committed
Skip linking Fortran_main.a if -fno-fortran-main is present
1 parent e0784bd commit 5c2f898

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,33 +1116,37 @@ bool tools::addOpenMPRuntime(ArgStringList &CmdArgs, const ToolChain &TC,
11161116
return true;
11171117
}
11181118

1119+
// TODO: add -fno-fortran-main option and check it in this function.
11191120
void tools::addFortranRuntimeLibs(const ToolChain &TC, const ArgList &Args,
11201121
llvm::opt::ArgStringList &CmdArgs) {
11211122
// These are handled earlier on Windows by telling the frontend driver to add
11221123
// the correct libraries to link against as dependents in the object file.
11231124
if (!TC.getTriple().isKnownWindowsMSVCEnvironment()) {
1124-
// The --whole-archive option needs to be part of the link line to
1125-
// make sure that the main() function from Fortran_main.a is pulled
1126-
// in by the linker. Determine if --whole-archive is active when
1127-
// flang will try to link Fortran_main.a. If it is, don't add the
1128-
// --whole-archive flag to the link line. If it's not, add a proper
1129-
// --whole-archive/--no-whole-archive bracket to the link line.
1130-
bool WholeArchiveActive = false;
1131-
for (auto *Arg : Args.filtered(options::OPT_Wl_COMMA))
1132-
if (Arg)
1133-
for (StringRef ArgValue : Arg->getValues()) {
1134-
if (ArgValue == "--whole-archive")
1135-
WholeArchiveActive = true;
1136-
if (ArgValue == "--no-whole-archive")
1137-
WholeArchiveActive = false;
1138-
}
1139-
1140-
if (!WholeArchiveActive)
1141-
CmdArgs.push_back("--whole-archive");
1142-
CmdArgs.push_back("-lFortran_main");
1143-
if (!WholeArchiveActive)
1144-
CmdArgs.push_back("--no-whole-archive");
1125+
// if -fno-fortran-main has been passed, skip linking Fortran_main.a
1126+
bool DontLinkFortranMain = Args.getLastArg(options::OPT_no_fortran_main) != nullptr;
1127+
if (!DontLinkFortranMain) {
1128+
// The --whole-archive option needs to be part of the link line to
1129+
// make sure that the main() function from Fortran_main.a is pulled
1130+
// in by the linker. Determine if --whole-archive is active when
1131+
// flang will try to link Fortran_main.a. If it is, don't add the
1132+
// --whole-archive flag to the link line. If it's not, add a proper
1133+
// --whole-archive/--no-whole-archive bracket to the link line.
1134+
bool WholeArchiveActive = false;
1135+
for (auto *Arg : Args.filtered(options::OPT_Wl_COMMA))
1136+
if (Arg)
1137+
for (StringRef ArgValue : Arg->getValues()) {
1138+
if (ArgValue == "--whole-archive")
1139+
WholeArchiveActive = true;
1140+
if (ArgValue == "--no-whole-archive")
1141+
WholeArchiveActive = false;
1142+
}
11451143

1144+
if (!WholeArchiveActive)
1145+
CmdArgs.push_back("--whole-archive");
1146+
CmdArgs.push_back("-lFortran_main");
1147+
if (!WholeArchiveActive)
1148+
CmdArgs.push_back("--no-whole-archive");
1149+
}
11461150
// Perform regular linkage of the remaining runtime libraries.
11471151
CmdArgs.push_back("-lFortranRuntime");
11481152
CmdArgs.push_back("-lFortranDecimal");

0 commit comments

Comments
 (0)