Skip to content

Commit cf1bde9

Browse files
authored
[Driver] Link Flang runtime on Solaris (#65644)
I noticed that `flang-new` cannot link Fortran executables on Solaris since the runtime libs are missing. This patch fixes this, following `Gnu.cpp`. The `linker-flags.f90` testcase is augmented to test for this, renaming the `GNU` label to `UNIX` so it can be reused. Also use the current form `--target=` in the tests and join the `-l` lines in the test for readibility. Tested on `amd64-pc-solaris2.11`, `sparcv9-sun-solaris2.11`, and `x86_64-pc-linux-gnu`.
1 parent 178619d commit cf1bde9

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

clang/lib/Driver/ToolChains/Solaris.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,14 @@ void solaris::Linker::ConstructJob(Compilation &C, const JobAction &JA,
222222
getToolChain().AddCXXStdlibLibArgs(Args, CmdArgs);
223223
CmdArgs.push_back("-lm");
224224
}
225+
// Additional linker set-up and flags for Fortran. This is required in order
226+
// to generate executables. As Fortran runtime depends on the C runtime,
227+
// these dependencies need to be listed before the C runtime below.
228+
if (D.IsFlangMode()) {
229+
addFortranRuntimeLibraryPath(getToolChain(), Args, CmdArgs);
230+
addFortranRuntimeLibs(getToolChain(), CmdArgs);
231+
CmdArgs.push_back("-lm");
232+
}
225233
if (Args.hasArg(options::OPT_fstack_protector) ||
226234
Args.hasArg(options::OPT_fstack_protector_strong) ||
227235
Args.hasArg(options::OPT_fstack_protector_all)) {

flang/test/Driver/linker-flags.f90

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
! invocation. These libraries are added on top of other standard runtime
33
! libraries that the Clang driver will include.
44

5-
! RUN: %flang -### -target ppc64le-linux-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,GNU
6-
! RUN: %flang -### -target aarch64-apple-darwin %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,DARWIN
7-
! RUN: %flang -### -target x86_64-windows-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MINGW
5+
! RUN: %flang -### --target=ppc64le-linux-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,UNIX
6+
! RUN: %flang -### --target=aarch64-apple-darwin %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,DARWIN
7+
! 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-windows-gnu %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MINGW
89

910
! NOTE: Clang's driver library, clangDriver, usually adds 'libcmt' and
1011
! 'oldnames' on Windows, but they are not needed when compiling
1112
! Fortran code and they might bring in additional dependencies.
1213
! Make sure they're not added.
13-
! RUN: %flang -### -target aarch64-windows-msvc -fuse-ld= %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MSVC --implicit-check-not libcmt --implicit-check-not oldnames
14+
! RUN: %flang -### --target=aarch64-windows-msvc -fuse-ld= %S/Inputs/hello.f90 2>&1 | FileCheck %s --check-prefixes=CHECK,MSVC --implicit-check-not libcmt --implicit-check-not oldnames
1415

1516
! Compiler invocation to generate the object file
1617
! CHECK-LABEL: {{.*}} "-emit-obj"
@@ -21,12 +22,9 @@
2122
! run on any other platform, such as Windows that use a .exe
2223
! suffix. Clang's driver will try to resolve the path to the ld
2324
! executable and may find the GNU linker from MinGW or Cygwin.
24-
! GNU-LABEL: "{{.*}}ld{{(\.exe)?}}"
25-
! GNU-SAME: "[[object_file]]"
26-
! GNU-SAME: -lFortran_main
27-
! GNU-SAME: -lFortranRuntime
28-
! GNU-SAME: -lFortranDecimal
29-
! GNU-SAME: -lm
25+
! UNIX-LABEL: "{{.*}}ld{{(\.exe)?}}"
26+
! UNIX-SAME: "[[object_file]]"
27+
! UNIX-SAME: "-lFortran_main" "-lFortranRuntime" "-lFortranDecimal" "-lm"
3028

3129
! DARWIN-LABEL: "{{.*}}ld{{(\.exe)?}}"
3230
! DARWIN-SAME: "[[object_file]]"

0 commit comments

Comments
 (0)