[flang-commits] [clang] [flang] [flang][driver] Add support for -isysroot in the frontend (PR #77365)

Leandro Lupori via flang-commits flang-commits at lists.llvm.org
Tue Jan 9 04:29:58 PST 2024


https://github.com/luporl updated https://github.com/llvm/llvm-project/pull/77365

>From 01a2a8d315af2edb9fe3f0c9b57b5c74935521f1 Mon Sep 17 00:00:00 2001
From: Leandro Lupori <leandro.lupori at linaro.org>
Date: Mon, 8 Jan 2024 16:37:54 -0300
Subject: [PATCH 1/3] [flang][driver] Add support for -isysroot in the frontend

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.
---
 clang/include/clang/Driver/Options.td    |  2 +-
 flang/test/Driver/driver-help-hidden.f90 |  1 +
 flang/test/Driver/driver-help.f90        |  1 +
 flang/test/Driver/isysroot.f90           | 12 ++++++++++++
 4 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/Driver/isysroot.f90

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 6aff37f1336871..f42e9c7eb92a67 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -4265,7 +4265,7 @@ def iquote : JoinedOrSeparate<["-"], "iquote">, Group<clang_i_Group>,
   Visibility<[ClangOption, CC1Option]>,
   HelpText<"Add directory to QUOTE include search path">, MetaVarName<"<directory>">;
 def isysroot : JoinedOrSeparate<["-"], "isysroot">, Group<clang_i_Group>,
-  Visibility<[ClangOption, CC1Option]>,
+  Visibility<[ClangOption, CC1Option, FlangOption]>,
   HelpText<"Set the system root directory (usually /)">, MetaVarName<"<dir>">,
   MarshallingInfoString<HeaderSearchOpts<"Sysroot">, [{"/"}]>;
 def isystem : JoinedOrSeparate<["-"], "isystem">, Group<clang_i_Group>,
diff --git a/flang/test/Driver/driver-help-hidden.f90 b/flang/test/Driver/driver-help-hidden.f90
index 9a11a7a571ffcc..a4444ca043d0ea 100644
--- a/flang/test/Driver/driver-help-hidden.f90
+++ b/flang/test/Driver/driver-help-hidden.f90
@@ -111,6 +111,7 @@
 ! CHECK-NEXT: -g                      Generate source-level debug information
 ! CHECK-NEXT: --help-hidden           Display help for hidden options
 ! CHECK-NEXT: -help                   Display available options
+! CHECK-NEXT: -isysroot <dir>         Set the system root directory (usually /)
 ! CHECK-NEXT: -I <dir>                Add directory to the end of the list of include search paths
 ! CHECK-NEXT: -L <dir>                Add directory to library search path
 ! CHECK-NEXT: -march=<value>          For a list of available architectures for the target use '-mcpu=help'
diff --git a/flang/test/Driver/driver-help.f90 b/flang/test/Driver/driver-help.f90
index e0e74dc56f331e..07189264104592 100644
--- a/flang/test/Driver/driver-help.f90
+++ b/flang/test/Driver/driver-help.f90
@@ -97,6 +97,7 @@
 ! HELP-NEXT: -g                      Generate source-level debug information
 ! HELP-NEXT: --help-hidden           Display help for hidden options
 ! HELP-NEXT: -help                   Display available options
+! HELP-NEXT: -isysroot <dir>         Set the system root directory (usually /)
 ! HELP-NEXT: -I <dir>                Add directory to the end of the list of include search paths
 ! HELP-NEXT: -L <dir>                Add directory to library search path
 ! HELP-NEXT: -march=<value>          For a list of available architectures for the target use '-mcpu=help'
diff --git a/flang/test/Driver/isysroot.f90 b/flang/test/Driver/isysroot.f90
new file mode 100644
index 00000000000000..70d2fc0345ce50
--- /dev/null
+++ b/flang/test/Driver/isysroot.f90
@@ -0,0 +1,12 @@
+! Verify that the -isysroot flag is known to the frontend and, on Darwin,
+! is passed on to the linker.
+
+! RUN: %flang -### --target=aarch64-apple-darwin -isysroot /path/to/sysroot \
+! RUN:        %s 2>&1 | FileCheck %s --check-prefix=CHECK-DARWIN
+! RUN: %flang -### --target=aarch64-linux-gnu -isysroot /path/to/sysroot \
+! RUN:        %s 2>&1 | FileCheck %s --check-prefix=CHECK-LINUX
+
+! CHECK-DARWIN: "{{.*}}/ld" {{.*}}"-syslibroot" "/path/to/sysroot"
+! Unused on Linux.
+! CHECK-LINUX: warning: argument unused during compilation: '-isysroot /path/to/sysroot'
+! CHECK-LINUX-NOT: /path/to/sysroot

>From b12c6abdffcdf1972db6b1ec59c910c92eb38f9d Mon Sep 17 00:00:00 2001
From: Leandro Lupori <leandro.lupori at linaro.org>
Date: Tue, 9 Jan 2024 08:45:10 -0300
Subject: [PATCH 2/3] Fix test on Windows

---
 flang/test/Driver/isysroot.f90 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/flang/test/Driver/isysroot.f90 b/flang/test/Driver/isysroot.f90
index 70d2fc0345ce50..b3871b352fcfb2 100644
--- a/flang/test/Driver/isysroot.f90
+++ b/flang/test/Driver/isysroot.f90
@@ -6,7 +6,7 @@
 ! RUN: %flang -### --target=aarch64-linux-gnu -isysroot /path/to/sysroot \
 ! RUN:        %s 2>&1 | FileCheck %s --check-prefix=CHECK-LINUX
 
-! CHECK-DARWIN: "{{.*}}/ld" {{.*}}"-syslibroot" "/path/to/sysroot"
+! CHECK-DARWIN: "{{.*[\/]}}ld" {{.*}}"-syslibroot" "/path/to/sysroot"
 ! Unused on Linux.
 ! CHECK-LINUX: warning: argument unused during compilation: '-isysroot /path/to/sysroot'
 ! CHECK-LINUX-NOT: /path/to/sysroot

>From c47d15b3e947e4a55de8b7c276dca1eacfa7caa4 Mon Sep 17 00:00:00 2001
From: Leandro Lupori <leandro.lupori at linaro.org>
Date: Tue, 9 Jan 2024 09:27:46 -0300
Subject: [PATCH 3/3] Update driver docs

---
 clang/docs/InternalsManual.rst | 2 +-
 flang/docs/FlangDriver.md      | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/clang/docs/InternalsManual.rst b/clang/docs/InternalsManual.rst
index 05fadf5a034464..aa0a47648e511b 100644
--- a/clang/docs/InternalsManual.rst
+++ b/clang/docs/InternalsManual.rst
@@ -931,7 +931,7 @@ the option appears on the command line, the argument value is simply copied.
 .. code-block:: text
 
   def isysroot : JoinedOrSeparate<["-"], "isysroot">,
-    Visibility<[ClangOption, CC1Option]>,
+    Visibility<[ClangOption, CC1Option, FlangOption]>,
     MarshallingInfoString<HeaderSearchOpts<"Sysroot">, [{"/"}]>;
 
 **List of Strings**
diff --git a/flang/docs/FlangDriver.md b/flang/docs/FlangDriver.md
index fa39889927e0eb..8741a394528b11 100644
--- a/flang/docs/FlangDriver.md
+++ b/flang/docs/FlangDriver.md
@@ -220,6 +220,13 @@ static or shared library, the generated library does not need a `main`
 function, as a final link stage will occur that will provide the `Fortran_main`
 library when creating the final executable.
 
+On Darwin, the logical root where the system libraries are located (sysroot)
+must be specified. This can be done with the cmake build flag `DEFAULT_SYSROOT`
+or by using the `-isysroot` flag when linking a binary. On other targets
+`-isysroot` doesn't change the linker command line. While with Clang
+`-isysroot` also changes the sysroot for includes, with Flang it only affects
+Darwin libraries' sysroot.
+
 ## Frontend Driver
 Flang's frontend driver is the main interface between compiler developers and
 the Flang frontend. The high-level design is similar to Clang's frontend



More information about the flang-commits mailing list