Skip to content

Commit 9edcf7a

Browse files
authored
[flang][driver] Add support for -isysroot in the frontend (#77365)
If DEFAULT_SYSROOT is not specfied when building flang, then the -isysroot flag is needed to link binaries against system libraries on Darwin. It's also needed when linking against a non-default sysroot.
1 parent d553934 commit 9edcf7a

File tree

15 files changed

+77
-29
lines changed

15 files changed

+77
-29
lines changed

clang/docs/InternalsManual.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ the option appears on the command line, the argument value is simply copied.
931931
.. code-block:: text
932932
933933
def isysroot : JoinedOrSeparate<["-"], "isysroot">,
934-
Visibility<[ClangOption, CC1Option]>,
934+
Visibility<[ClangOption, CC1Option, FlangOption]>,
935935
MarshallingInfoString<HeaderSearchOpts<"Sysroot">, [{"/"}]>;
936936
937937
**List of Strings**

clang/include/clang/Driver/Options.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4267,7 +4267,7 @@ def iquote : JoinedOrSeparate<["-"], "iquote">, Group<clang_i_Group>,
42674267
Visibility<[ClangOption, CC1Option]>,
42684268
HelpText<"Add directory to QUOTE include search path">, MetaVarName<"<directory>">;
42694269
def isysroot : JoinedOrSeparate<["-"], "isysroot">, Group<clang_i_Group>,
4270-
Visibility<[ClangOption, CC1Option]>,
4270+
Visibility<[ClangOption, CC1Option, FlangOption]>,
42714271
HelpText<"Set the system root directory (usually /)">, MetaVarName<"<dir>">,
42724272
MarshallingInfoString<HeaderSearchOpts<"Sysroot">, [{"/"}]>;
42734273
def isystem : JoinedOrSeparate<["-"], "isystem">, Group<clang_i_Group>,

flang/docs/FlangDriver.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ static or shared library, the generated library does not need a `main`
220220
function, as a final link stage will occur that will provide the `Fortran_main`
221221
library when creating the final executable.
222222

223+
On Darwin, the logical root where the system libraries are located (sysroot)
224+
must be specified. This can be done with the CMake build flag `DEFAULT_SYSROOT`
225+
or by using the `-isysroot` flag when linking a binary. On other targets
226+
`-isysroot` doesn't change the linker command line (it only affects the header
227+
search path). While with Clang `-isysroot` also changes the sysroot for
228+
includes, with Flang (and Fortran in general) it only affects Darwin libraries'
229+
sysroot.
230+
223231
## Frontend Driver
224232
Flang's frontend driver is the main interface between compiler developers and
225233
the Flang frontend. The high-level design is similar to Clang's frontend

flang/test/Driver/compiler-options.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
! RUN: %flang -S -emit-llvm -flang-deprecated-no-hlfir -o - %s | FileCheck %s
22
! Test communication of COMPILER_OPTIONS from flang-new to flang-new -fc1.
3-
! CHECK: [[OPTSVAR:@_QQclX[0-9a-f]+]] = {{[a-z]+}} constant [[[OPTSLEN:[0-9]+]] x i8] c"{{.*}}flang-new{{(\.exe)?}} -S -emit-llvm -flang-deprecated-no-hlfir -o - {{.*}}compiler-options.f90"
3+
! CHECK: [[OPTSVAR:@_QQclX[0-9a-f]+]] = {{[a-z]+}} constant [[[OPTSLEN:[0-9]+]] x i8] c"{{.*}}flang-new{{(\.exe)?}} {{.*}}-S -emit-llvm -flang-deprecated-no-hlfir -o - {{.*}}compiler-options.f90"
44
program main
55
use ISO_FORTRAN_ENV, only: compiler_options
66
implicit none

flang/test/Driver/ctofortran.f90

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
! MacOS needs -isysroot <osx_sysroot> with clang and flang to build binaries.
2-
! UNSUPPORTED: system-windows, system-darwin
1+
! UNSUPPORTED: system-windows
32
! RUN: split-file %s %t
43
! RUN: chmod +x %t/runtest.sh
5-
! RUN: %t/runtest.sh %t %flang $t/ffile.f90 $t/cfile.c
4+
! RUN: %t/runtest.sh %t %t/ffile.f90 %t/cfile.c %flang | FileCheck %s
65

76
!--- ffile.f90
87
program fmain
@@ -68,15 +67,21 @@ end subroutine foo
6867
}
6968
!--- runtest.sh
7069
#!/bin/bash
71-
export BINDIR=`dirname $2`
72-
export CCOMP=$BINDIR/clang
70+
TMPDIR=$1
71+
FFILE=$2
72+
CFILE=$3
73+
FLANG=$4
74+
shift 4
75+
FLAGS="$*"
76+
BINDIR=`dirname $FLANG`
77+
LIBDIR=$BINDIR/../lib
78+
CCOMP=$BINDIR/clang
7379
if [ -x $CCOMP ]
7480
then
75-
export LIBDIR=$BINDIR/../lib
7681
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBDIR
77-
$CCOMP -c $1/$4 -o $1/cfile.o
78-
$2 $1/$3 $1/cfile.o -o $1/ctofortran
79-
$1/ctofortran # should print "PASS"
82+
$CCOMP $FLAGS -c $CFILE -o $TMPDIR/cfile.o
83+
$FLANG $FLAGS $FFILE $TMPDIR/cfile.o -o $TMPDIR/ctofortran
84+
$TMPDIR/ctofortran # should print "PASS"
8085
else
8186
# No clang compiler, just pass by default
8287
echo "PASS"

flang/test/Driver/driver-help-hidden.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
! CHECK-NEXT: -g Generate source-level debug information
113113
! CHECK-NEXT: --help-hidden Display help for hidden options
114114
! CHECK-NEXT: -help Display available options
115+
! CHECK-NEXT: -isysroot <dir> Set the system root directory (usually /)
115116
! CHECK-NEXT: -I <dir> Add directory to the end of the list of include search paths
116117
! CHECK-NEXT: -L <dir> Add directory to library search path
117118
! CHECK-NEXT: -march=<value> For a list of available architectures for the target use '-mcpu=help'

flang/test/Driver/driver-help.f90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
! HELP-NEXT: -g Generate source-level debug information
9999
! HELP-NEXT: --help-hidden Display help for hidden options
100100
! HELP-NEXT: -help Display available options
101+
! HELP-NEXT: -isysroot <dir> Set the system root directory (usually /)
101102
! HELP-NEXT: -I <dir> Add directory to the end of the list of include search paths
102103
! HELP-NEXT: -L <dir> Add directory to library search path
103104
! HELP-NEXT: -march=<value> For a list of available architectures for the target use '-mcpu=help'

flang/test/Driver/exec.f90

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
! Verify that flang can correctly build executables.
2+
3+
! RUN: %flang %s -o %t
4+
! RUN: %t | FileCheck %s
5+
! RUN: rm -f %t
6+
7+
! CHECK: Hello, World!
8+
program hello
9+
print *, "Hello, World!"
10+
end program

flang/test/Driver/falias-analysis.f90

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
! RUN: %flang -c -emit-llvm -O0 %s -o - | llvm-dis | FileCheck %s --check-prefix=CHECK-NOAA --check-prefix=CHECK-ALL
1010
! RUN: %flang -c -emit-llvm %s -o - | llvm-dis | FileCheck %s --check-prefix=CHECK-NOAA --check-prefix=CHECK-ALL
1111

12-
! RUN: %flang -fc1 -emit-llvm -O3 %s -o - | FileCheck %s --check-prefix=CHECK-AA --check-prefix=CHECK-ALL
13-
! RUN: %flang -fc1 -emit-llvm -O2 %s -o - | FileCheck %s --check-prefix=CHECK-AA --check-prefix=CHECK-ALL
14-
! RUN: %flang -fc1 -emit-llvm -O1 %s -o - | FileCheck %s --check-prefix=CHECK-AA --check-prefix=CHECK-ALL
12+
! RUN: %flang_fc1 -emit-llvm -O3 %s -o - | FileCheck %s --check-prefix=CHECK-AA --check-prefix=CHECK-ALL
13+
! RUN: %flang_fc1 -emit-llvm -O2 %s -o - | FileCheck %s --check-prefix=CHECK-AA --check-prefix=CHECK-ALL
14+
! RUN: %flang_fc1 -emit-llvm -O1 %s -o - | FileCheck %s --check-prefix=CHECK-AA --check-prefix=CHECK-ALL
1515

16-
! RUN: %flang -fc1 -emit-llvm -O0 %s -o - | FileCheck %s --check-prefix=CHECK-NOAA --check-prefix=CHECK-ALL
17-
! RUN: %flang -fc1 -emit-llvm %s -o - | FileCheck %s --check-prefix=CHECK-NOAA --check-prefix=CHECK-ALL
16+
! RUN: %flang_fc1 -emit-llvm -O0 %s -o - | FileCheck %s --check-prefix=CHECK-NOAA --check-prefix=CHECK-ALL
17+
! RUN: %flang_fc1 -emit-llvm %s -o - | FileCheck %s --check-prefix=CHECK-NOAA --check-prefix=CHECK-ALL
1818

1919
subroutine simple(a)
2020
integer, intent(inout) :: a(:)

flang/test/Driver/input-from-stdin/input-from-stdin.f90

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
! FLANG FRONTEND DRIVER (flang -fc1)
1818
!---------------------------------------
1919
! Test `-E`: for the corresponding frontend actions the driver relies on the prescanner API to handle file I/O
20-
! RUN: cat %s | %flang -fc1 -E -cpp | FileCheck %s --check-prefix=PP-NOT-DEFINED
21-
! RUN: cat %s | %flang -fc1 -DNEW -E -cpp | FileCheck %s --check-prefix=PP-DEFINED
20+
! RUN: cat %s | %flang_fc1 -E -cpp | FileCheck %s --check-prefix=PP-NOT-DEFINED
21+
! RUN: cat %s | %flang_fc1 -DNEW -E -cpp | FileCheck %s --check-prefix=PP-DEFINED
2222

2323
! Test `-test-io`: for the corresponding frontend action (`InputOutputTestAction`) the driver handles the file I/O on its own
2424
! the corresponding action (`PrintPreprocessedAction`)
25-
! RUN: cat %s | %flang -fc1 -test-io -cpp | FileCheck %s --check-prefix=IO --match-full-lines
26-
! RUN: cat %s | %flang -fc1 -DNEW -cpp -test-io | FileCheck %s --check-prefix=IO --match-full-lines
25+
! RUN: cat %s | %flang_fc1 -test-io -cpp | FileCheck %s --check-prefix=IO --match-full-lines
26+
! RUN: cat %s | %flang_fc1 -DNEW -cpp -test-io | FileCheck %s --check-prefix=IO --match-full-lines
2727

2828
! PP-NOT-DEFINED: Program B
2929
! PP-DEFINED: Program A

flang/test/Driver/isysroot.f90

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
! Verify that the -isysroot flag is known to the frontend and, on Darwin,
2+
! is passed on to the linker.
3+
4+
! RUN: %flang -### --target=aarch64-apple-darwin -isysroot /path/to/sysroot \
5+
! RUN: %s 2>&1 | FileCheck %s --check-prefix=CHECK-DARWIN
6+
! RUN: %flang -### --target=aarch64-linux-gnu -isysroot /path/to/sysroot \
7+
! RUN: %s 2>&1 | FileCheck %s --check-prefix=CHECK-LINUX
8+
9+
! CHECK-DARWIN: "{{.*[\/]}}ld" {{.*}}"-syslibroot" "/path/to/sysroot"
10+
! Unused on Linux.
11+
! CHECK-LINUX: warning: argument unused during compilation: '-isysroot /path/to/sysroot'
12+
! CHECK-LINUX-NOT: /path/to/sysroot

flang/test/Preprocessing/preprocessed-dirs.F90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
! RUN: %flang -fc1 -E -fopenacc %s 2>&1 | FileCheck %s
1+
! RUN: %flang_fc1 -E -fopenacc %s 2>&1 | FileCheck %s
22
!CHECK: subroutine r4(x) Z real :: x Z !$acc routine Z print *, x Z end
33
#define SUB(s, t) subroutine s(x) Z\
44
t :: x Z\

flang/test/Semantics/OpenMP/use_device_addr.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
! RUN: %flang -fc1 -fopenmp -fdebug-dump-symbols %s | FileCheck %s
1+
! RUN: %flang_fc1 -fopenmp -fdebug-dump-symbols %s | FileCheck %s
22
! OpenMP Version 5.1
33
! 2.14.2 use_device_addr clause
44
! List item that appears in a use_device_addr clause has corresponding storage

flang/test/Semantics/OpenMP/use_device_ptr.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
! RUN: %flang -fc1 -fopenmp -fdebug-dump-symbols %s | FileCheck %s
1+
! RUN: %flang_fc1 -fopenmp -fdebug-dump-symbols %s | FileCheck %s
22
! OpenMP Version 5.0
33
! 2.10.1 use_device_ptr clause
44
! List items that appear in a use_device_ptr clause are converted into device

flang/test/lit.cfg.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,20 @@
118118
"PATH", config.flang_llvm_tools_dir, append_path=True
119119
)
120120

121+
# On MacOS, -isysroot is needed to build binaries.
122+
isysroot_flag = []
123+
if config.osx_sysroot:
124+
isysroot_flag = ["-isysroot", config.osx_sysroot]
125+
121126
# For each occurrence of a flang tool name, replace it with the full path to
122127
# the build directory holding that tool.
123128
tools = [
124-
ToolSubst("%flang", command=FindTool("flang-new"), unresolved="fatal"),
129+
ToolSubst(
130+
"%flang",
131+
command=FindTool("flang-new"),
132+
extra_args=isysroot_flag,
133+
unresolved="fatal",
134+
),
125135
ToolSubst(
126136
"%flang_fc1",
127137
command=FindTool("flang-new"),
@@ -163,10 +173,11 @@
163173
and os.path.isdir(include)
164174
):
165175
config.available_features.add("c-compiler")
166-
cc_cmd = config.cc
167-
if config.osx_sysroot:
168-
cc_cmd += " -isysroot " + config.osx_sysroot
169-
tools.append(ToolSubst("%cc", command=cc_cmd, unresolved="fatal"))
176+
tools.append(
177+
ToolSubst(
178+
"%cc", command=config.cc, extra_args=isysroot_flag, unresolved="fatal"
179+
)
180+
)
170181
tools.append(ToolSubst("%libruntime", command=libruntime, unresolved="fatal"))
171182
tools.append(ToolSubst("%libdecimal", command=libdecimal, unresolved="fatal"))
172183
tools.append(ToolSubst("%include", command=include, unresolved="fatal"))

0 commit comments

Comments
 (0)