Skip to content

Commit c149ff3

Browse files
authored
[Driver] Link Flang runtime on FreeBSD, NetBSD, OpenBSD, DragonFly and Haiku (#69817)
1 parent b8b491c commit c149ff3

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

clang/lib/Driver/ToolChains/DragonFly.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,16 @@ void dragonfly::Linker::ConstructJob(Compilation &C, const JobAction &JA,
144144
CmdArgs.push_back("-lm");
145145
}
146146

147+
// Additional linker set-up and flags for Fortran. This is required in order
148+
// to generate executables. As Fortran runtime depends on the C runtime,
149+
// these dependencies need to be listed before the C runtime below (i.e.
150+
// AddRuntTimeLibs).
151+
if (D.IsFlangMode()) {
152+
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
153+
addFortranRuntimeLibs(ToolChain, CmdArgs);
154+
CmdArgs.push_back("-lm");
155+
}
156+
147157
if (Args.hasArg(options::OPT_pthread))
148158
CmdArgs.push_back("-lpthread");
149159

clang/lib/Driver/ToolChains/FreeBSD.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,20 @@ void freebsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
290290
else
291291
CmdArgs.push_back("-lm");
292292
}
293+
294+
// Additional linker set-up and flags for Fortran. This is required in order
295+
// to generate executables. As Fortran runtime depends on the C runtime,
296+
// these dependencies need to be listed before the C runtime below (i.e.
297+
// AddRuntTimeLibs).
298+
if (D.IsFlangMode()) {
299+
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
300+
addFortranRuntimeLibs(ToolChain, CmdArgs);
301+
if (Profiling)
302+
CmdArgs.push_back("-lm_p");
303+
else
304+
CmdArgs.push_back("-lm");
305+
}
306+
293307
if (NeedsSanitizerDeps)
294308
linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs);
295309
if (NeedsXRayDeps)

clang/lib/Driver/ToolChains/Haiku.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,15 @@ void haiku::Linker::ConstructJob(Compilation &C, const JobAction &JA,
9595
if (D.CCCIsCXX() && ToolChain.ShouldLinkCXXStdlib(Args))
9696
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
9797

98+
// Additional linker set-up and flags for Fortran. This is required in order
99+
// to generate executables. As Fortran runtime depends on the C runtime,
100+
// these dependencies need to be listed before the C runtime below (i.e.
101+
// AddRuntTimeLibs).
102+
if (D.IsFlangMode()) {
103+
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
104+
addFortranRuntimeLibs(ToolChain, CmdArgs);
105+
}
106+
98107
CmdArgs.push_back("-lgcc");
99108

100109
CmdArgs.push_back("--push-state");

clang/lib/Driver/ToolChains/NetBSD.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,17 @@ void netbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
314314
ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
315315
CmdArgs.push_back("-lm");
316316
}
317+
318+
// Additional linker set-up and flags for Fortran. This is required in order
319+
// to generate executables. As Fortran runtime depends on the C runtime,
320+
// these dependencies need to be listed before the C runtime below (i.e.
321+
// AddRuntTimeLibs).
322+
if (D.IsFlangMode()) {
323+
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
324+
addFortranRuntimeLibs(ToolChain, CmdArgs);
325+
CmdArgs.push_back("-lm");
326+
}
327+
317328
if (NeedsSanitizerDeps)
318329
linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs);
319330
if (NeedsXRayDeps)

clang/lib/Driver/ToolChains/OpenBSD.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,20 @@ void openbsd::Linker::ConstructJob(Compilation &C, const JobAction &JA,
213213
else
214214
CmdArgs.push_back("-lm");
215215
}
216+
217+
// Additional linker set-up and flags for Fortran. This is required in order
218+
// to generate executables. As Fortran runtime depends on the C runtime,
219+
// these dependencies need to be listed before the C runtime below (i.e.
220+
// AddRuntTimeLibs).
221+
if (D.IsFlangMode()) {
222+
addFortranRuntimeLibraryPath(ToolChain, Args, CmdArgs);
223+
addFortranRuntimeLibs(ToolChain, CmdArgs);
224+
if (Profiling)
225+
CmdArgs.push_back("-lm_p");
226+
else
227+
CmdArgs.push_back("-lm");
228+
}
229+
216230
if (NeedsSanitizerDeps) {
217231
CmdArgs.push_back(ToolChain.getCompilerRTArgString(Args, "builtins"));
218232
linkSanitizerRuntimeDeps(ToolChain, Args, CmdArgs);

flang/test/Driver/linker-flags.f90

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
! RUN: %flang -### --target=ppc64le-linux-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,UNIX
66
! RUN: %flang -### --target=aarch64-apple-darwin %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,DARWIN
77
! RUN: %flang -### --target=sparc-sun-solaris2.11 %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,UNIX
8+
! RUN: %flang -### --target=x86_64-unknown-freebsd %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,UNIX
9+
! RUN: %flang -### --target=x86_64-unknown-netbsd %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,UNIX
10+
! RUN: %flang -### --target=x86_64-unknown-openbsd %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,UNIX
11+
! RUN: %flang -### --target=x86_64-unknown-dragonfly %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,UNIX
12+
! RUN: %flang -### --target=x86_64-unknown-haiku %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,HAIKU
813
! RUN: %flang -### --target=x86_64-windows-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MINGW
914

1015
! NOTE: Clang's driver library, clangDriver, usually adds 'libcmt' and
@@ -32,6 +37,10 @@
3237
! DARWIN-SAME: -lFortranRuntime
3338
! DARWIN-SAME: -lFortranDecimal
3439

40+
! HAIKU-LABEL: "{{.*}}ld{{(\.exe)?}}"
41+
! HAIKU-SAME: "[[object_file]]"
42+
! HAIKU-SAME: "-lFortran_main" "-lFortranRuntime" "-lFortranDecimal"
43+
3544
! MINGW-LABEL: "{{.*}}ld{{(\.exe)?}}"
3645
! MINGW-SAME: "[[object_file]]"
3746
! MINGW-SAME: -lFortran_main

0 commit comments

Comments
 (0)