Skip to content

Commit 1c0958b

Browse files
authored
[flang][Driver] Support -nostdlib and -nodefaultlibs (#108868)
This partially addresses some requests in #89888
1 parent 98cf656 commit 1c0958b

File tree

13 files changed

+56
-13
lines changed

13 files changed

+56
-13
lines changed

clang/include/clang/Driver/Options.td

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5572,7 +5572,8 @@ def : Flag<["-"], "nocudalib">, Alias<nogpulib>;
55725572
def gpulibc : Flag<["-"], "gpulibc">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>,
55735573
HelpText<"Link the LLVM C Library for GPUs">;
55745574
def nogpulibc : Flag<["-"], "nogpulibc">, Visibility<[ClangOption, CC1Option, FlangOption, FC1Option]>;
5575-
def nodefaultlibs : Flag<["-"], "nodefaultlibs">;
5575+
def nodefaultlibs : Flag<["-"], "nodefaultlibs">,
5576+
Visibility<[ClangOption, FlangOption, CLOption, DXCOption]>;
55765577
def nodriverkitlib : Flag<["-"], "nodriverkitlib">;
55775578
def nofixprebinding : Flag<["-"], "nofixprebinding">;
55785579
def nolibc : Flag<["-"], "nolibc">;
@@ -5592,7 +5593,9 @@ def nostdincxx : Flag<["-"], "nostdinc++">, Visibility<[ClangOption, CC1Option]>
55925593
Group<IncludePath_Group>,
55935594
HelpText<"Disable standard #include directories for the C++ standard library">,
55945595
MarshallingInfoNegativeFlag<HeaderSearchOpts<"UseStandardCXXIncludes">>;
5595-
def nostdlib : Flag<["-"], "nostdlib">, Group<Link_Group>;
5596+
def nostdlib : Flag<["-"], "nostdlib">,
5597+
Visibility<[ClangOption, CLOption, FlangOption, DXCOption]>,
5598+
Group<Link_Group>;
55965599
def nostdlibxx : Flag<["-"], "nostdlib++">;
55975600
def object : Flag<["-"], "object">;
55985601
def o : JoinedOrSeparate<["-"], "o">,

clang/lib/Driver/ToolChains/AIX.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ void aix::Linker::ConstructJob(Compilation &C, const JobAction &JA,
330330
}
331331
}
332332

333-
if (D.IsFlangMode()) {
333+
if (D.IsFlangMode() &&
334+
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
334335
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
335336
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
336337
CmdArgs.push_back("-lm");

clang/lib/Driver/ToolChains/Darwin.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,8 @@ void darwin::Linker::ConstructJob(Compilation &C, const JobAction &JA,
713713

714714
// Additional linker set-up and flags for Fortran. This is required in order
715715
// to generate executables.
716-
if (getToolChain().getDriver().IsFlangMode()) {
716+
if (getToolChain().getDriver().IsFlangMode() &&
717+
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
717718
addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
718719
addFortranRuntimeLibs(getToolChain(), Args, CmdArgs);
719720
}

clang/lib/Driver/ToolChains/DragonFly.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ void dragonfly::Linker::ConstructJob(Compilation &C, const JobAction &JA,
151151
// to generate executables. As Fortran runtime depends on the C runtime,
152152
// these dependencies need to be listed before the C runtime below (i.e.
153153
// AddRunTimeLibs).
154-
if (D.IsFlangMode()) {
154+
if (D.IsFlangMode() &&
155+
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
155156
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
156157
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
157158
CmdArgs.push_back("-lm");

clang/lib/Driver/ToolChains/FreeBSD.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
313313
// to generate executables. As Fortran runtime depends on the C runtime,
314314
// these dependencies need to be listed before the C runtime below (i.e.
315315
// AddRunTimeLibs).
316-
if (D.IsFlangMode()) {
316+
if (D.IsFlangMode() &&
317+
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
317318
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
318319
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
319320
if (Profiling)

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,8 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
572572
// to generate executables. As Fortran runtime depends on the C runtime,
573573
// these dependencies need to be listed before the C runtime below (i.e.
574574
// AddRunTimeLibs).
575-
if (D.IsFlangMode()) {
575+
if (D.IsFlangMode() &&
576+
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
576577
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
577578
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
578579
CmdArgs.push_back("-lm");

clang/lib/Driver/ToolChains/Haiku.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,8 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
119119
// to generate executables. As Fortran runtime depends on the C runtime,
120120
// these dependencies need to be listed before the C runtime below (i.e.
121121
// AddRunTimeLibs).
122-
if (D.IsFlangMode()) {
122+
if (D.IsFlangMode() &&
123+
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
123124
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
124125
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
125126
}

clang/lib/Driver/ToolChains/MSVC.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,8 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
138138
for (const auto &LibPath : Args.getAllArgValues(options::OPT_L))
139139
CmdArgs.push_back(Args.MakeArgString("-libpath:" + LibPath));
140140

141-
if (C.getDriver().IsFlangMode()) {
141+
if (C.getDriver().IsFlangMode() &&
142+
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
142143
addFortranRuntimeLibraryPath(TC, Args, CmdArgs);
143144
addFortranRuntimeLibs(TC, Args, CmdArgs);
144145

clang/lib/Driver/ToolChains/MinGW.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ void tools::MinGW::Linker::ConstructJob(Compilation &C, const JobAction &JA,
251251
D.getLTOMode() == LTOK_Thin);
252252
}
253253

254-
if (C.getDriver().IsFlangMode()) {
254+
if (C.getDriver().IsFlangMode() &&
255+
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
255256
addFortranRuntimeLibraryPath(TC, Args, CmdArgs);
256257
addFortranRuntimeLibs(TC, Args, CmdArgs);
257258
}

clang/lib/Driver/ToolChains/NetBSD.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,8 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
326326
// to generate executables. As Fortran runtime depends on the C runtime,
327327
// these dependencies need to be listed before the C runtime below (i.e.
328328
// AddRunTimeLibs).
329-
if (D.IsFlangMode()) {
329+
if (D.IsFlangMode() &&
330+
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
330331
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
331332
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
332333
CmdArgs.push_back("-lm");

clang/lib/Driver/ToolChains/OpenBSD.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
239239
// to generate executables. As Fortran runtime depends on the C runtime,
240240
// these dependencies need to be listed before the C runtime below (i.e.
241241
// AddRunTimeLibs).
242-
if (D.IsFlangMode()) {
242+
if (D.IsFlangMode() &&
243+
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
243244
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
244245
addFortranRuntimeLibs(ToolChain, Args, CmdArgs);
245246
if (Profiling)

clang/lib/Driver/ToolChains/Solaris.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,8 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
223223
// Additional linker set-up and flags for Fortran. This is required in order
224224
// to generate executables. As Fortran runtime depends on the C runtime,
225225
// these dependencies need to be listed before the C runtime below.
226-
if (D.IsFlangMode()) {
226+
if (D.IsFlangMode() &&
227+
!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
227228
addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
228229
addFortranRuntimeLibs(getToolChain(), Args, CmdArgs);
229230
CmdArgs.push_back("-lm");

flang/test/Driver/nostdlib.f90

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
! Check that the libraries do not appear when using -nostdlib and -nodefaultlibs
2+
3+
! RUN: %flang -### -nostdlib --target=ppc64le-linux-gnu %s 2>&1 | FileCheck %s
4+
! RUN: %flang -### -nostdlib --target=aarch64-apple-darwin %s 2>&1 | FileCheck %s
5+
! RUN: %flang -### -nostdlib --target=sparc-sun-solaris2.11 %s 2>&1 | FileCheck %s
6+
! RUN: %flang -### -nostdlib --target=x86_64-unknown-freebsd %s 2>&1 | FileCheck %s
7+
! RUN: %flang -### -nostdlib --target=x86_64-unknown-netbsd %s 2>&1 | FileCheck %s
8+
! RUN: %flang -### -nostdlib --target=x86_64-unknown-openbsd %s 2>&1 | FileCheck %s
9+
! RUN: %flang -### -nostdlib --target=x86_64-unknown-dragonfly %s 2>&1 | FileCheck %s
10+
! RUN: %flang -### -nostdlib --target=x86_64-unknown-haiku %s 2>&1 | FileCheck %s
11+
! RUN: %flang -### -nostdlib --target=x86_64-windows-gnu %s 2>&1 | FileCheck %s
12+
13+
! RUN: %flang -### -nodefaultlibs --target=ppc64le-linux-gnu %s 2>&1 | FileCheck %s
14+
! RUN: %flang -### -nodefaultlibs --target=aarch64-apple-darwin %s 2>&1 | FileCheck %s
15+
! RUN: %flang -### -nodefaultlibs --target=sparc-sun-solaris2.11 %s 2>&1 | FileCheck %s
16+
! RUN: %flang -### -nodefaultlibs --target=x86_64-unknown-freebsd %s 2>&1 | FileCheck %s
17+
! RUN: %flang -### -nodefaultlibs --target=x86_64-unknown-netbsd %s 2>&1 | FileCheck %s
18+
! RUN: %flang -### -nodefaultlibs --target=x86_64-unknown-openbsd %s 2>&1 | FileCheck %s
19+
! RUN: %flang -### -nodefaultlibs --target=x86_64-unknown-dragonfly %s 2>&1 | FileCheck %s
20+
! RUN: %flang -### -nodefaultlibs --target=x86_64-unknown-haiku %s 2>&1 | FileCheck %s
21+
! RUN: %flang -### -nodefaultlibs --target=x86_64-windows-gnu %s 2>&1 | FileCheck %s
22+
23+
! -lgcc will not be linked on all platforms, so checking for that is redundant
24+
! in certain cases. But it is not clear that it is worth checking for each
25+
! platform individually.
26+
27+
! CHECK-NOT: "-lFortranRuntime"
28+
! CHECK-NOT: "-lFortranDecimal"
29+
! CHECK-NOT: "-lgcc"

0 commit comments

Comments
 (0)