[flang-commits] [flang] [flang][Driver] Support -rpath, -shared, and -static in the frontend (PR #66702)

Tarun Prabhu via flang-commits flang-commits at lists.llvm.org
Thu Oct 26 07:23:12 PDT 2023


https://github.com/tarunprabhu updated https://github.com/llvm/llvm-project/pull/66702

>From ba3bb1ae28c14f6720e17d26e037c7406a638436 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun at lanl.gov>
Date: Mon, 18 Sep 2023 15:18:24 -0600
Subject: [PATCH 1/6] [flang][Driver] Support -rpath, -shared, and -static in
 the frontend

Enable -rpath, -shared, and -static for the flang frontend. This brings it in
line with clang. Fixes issue #65546.
---
 clang/include/clang/Driver/Options.td |  7 +++++--
 flang/test/Driver/linker-flags.f90    | 16 ++++++++++++++++
 2 files changed, 21 insertions(+), 2 deletions(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 2eb86caa6e6d40e..bc000856f469e30 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5218,7 +5218,8 @@ def resource_dir : Separate<["-"], "resource-dir">,
 def resource_dir_EQ : Joined<["-"], "resource-dir=">, Flags<[NoXarchOption]>,
   Visibility<[ClangOption, CLOption, DXCOption]>,
   Alias<resource_dir>;
-def rpath : Separate<["-"], "rpath">, Flags<[LinkerInput]>, Group<Link_Group>;
+def rpath : Separate<["-"], "rpath">, Flags<[LinkerInput]>, Group<Link_Group>,
+  Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>;
 def rtlib_EQ : Joined<["-", "--"], "rtlib=">, Visibility<[ClangOption, CLOption]>,
   HelpText<"Compiler runtime library to use">;
 def frtlib_add_rpath: Flag<["-"], "frtlib-add-rpath">, Flags<[NoArgumentUnused]>,
@@ -5269,7 +5270,8 @@ def segs__read__only__addr : Separate<["-"], "segs_read_only_addr">;
 def segs__read__write__addr : Separate<["-"], "segs_read_write_addr">;
 def segs__read__ : Joined<["-"], "segs_read_">;
 def shared_libgcc : Flag<["-"], "shared-libgcc">;
-def shared : Flag<["-", "--"], "shared">, Group<Link_Group>;
+def shared : Flag<["-", "--"], "shared">, Group<Link_Group>,
+  Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>;
 def single__module : Flag<["-"], "single_module">;
 def specs_EQ : Joined<["-", "--"], "specs=">, Group<Link_Group>;
 def specs : Separate<["-", "--"], "specs">, Flags<[Unsupported]>;
@@ -5279,6 +5281,7 @@ def start_no_unused_arguments : Flag<["--"], "start-no-unused-arguments">,
 def static_libgcc : Flag<["-"], "static-libgcc">;
 def static_libstdcxx : Flag<["-"], "static-libstdc++">;
 def static : Flag<["-", "--"], "static">, Group<Link_Group>,
+  Visibility<[ClangOption, CLOption, DXCOption, FlangOption]>,
   Flags<[NoArgumentUnused]>;
 def std_default_EQ : Joined<["-"], "std-default=">;
 def std_EQ : Joined<["-", "--"], "std=">,
diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index 213bc032d964504..a04a7877e52d10c 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -48,3 +48,19 @@
 ! MSVC-SAME: FortranDecimal.lib
 ! MSVC-SAME: /subsystem:console
 ! MSVC-SAME: "[[object_file]]"
+
+! Verify that certain linker flags are known to the frontend and are passed on
+! to the linker.
+
+! RUN: %flang -### -rpath /path/to/dir %s 2>&1 \
+! RUN:   | FileCheck --check-prefix=CHECK-RPATH %s
+! CHECK-RPATH: ld{{.*}} "-rpath" "/path/to/dir"
+
+! RUN: %flang -### -shared %s 2>&1 \
+! RUN:   | FileCheck --check-prefix=CHECK-SHARED %s
+! CHECK-SHARED: ld{{.*}} "-shared"
+
+! RUN: %flang -### -static %s 2>&1 \
+! RUN:   | FileCheck --check-prefix=CHECK-STATIC %s
+! CHECK-STATIC: ld{{.*}} "-static"
+

>From f80d1cb29bc595dcdf71e88864756731f7741668 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun at lanl.gov>
Date: Thu, 21 Sep 2023 07:47:28 -0600
Subject: [PATCH 2/6] Test all link flags in a single test.

---
 flang/test/Driver/linker-flags.f90 | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index a04a7877e52d10c..495d5ef2ebda0e2 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -52,15 +52,11 @@
 ! Verify that certain linker flags are known to the frontend and are passed on
 ! to the linker.
 
-! RUN: %flang -### -rpath /path/to/dir %s 2>&1 \
-! RUN:   | FileCheck --check-prefix=CHECK-RPATH %s
-! CHECK-RPATH: ld{{.*}} "-rpath" "/path/to/dir"
+! RUN: %flang -### -rpath /path/to/dir -shared -static %s 2>&1 \
+! RUN:   | FileCheck --check-prefix=CHECK-LINKER-FLAGS %s
+! CHECK-LINKER-FLAGS: ld
+! CHECK-LINKER-FLAGS-DAG: "-rpath" "/path/to/dir"
+! CHECK-LINKER-FLAGS-DAG: "-shared"
+! CHECK-LINKER-FLAGS-DAG: "-static"
 
-! RUN: %flang -### -shared %s 2>&1 \
-! RUN:   | FileCheck --check-prefix=CHECK-SHARED %s
-! CHECK-SHARED: ld{{.*}} "-shared"
-
-! RUN: %flang -### -static %s 2>&1 \
-! RUN:   | FileCheck --check-prefix=CHECK-STATIC %s
-! CHECK-STATIC: ld{{.*}} "-static"
 

>From abbf9254f8789a2e9cef37208160c4b7c0b0cd9c Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun at lanl.gov>
Date: Thu, 21 Sep 2023 12:36:31 -0600
Subject: [PATCH 3/6] Delete trailing newlines. Rename prefix.

---
 flang/test/Driver/linker-flags.f90 | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index 495d5ef2ebda0e2..e3c6bae8a694051 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -53,10 +53,8 @@
 ! to the linker.
 
 ! RUN: %flang -### -rpath /path/to/dir -shared -static %s 2>&1 \
-! RUN:   | FileCheck --check-prefix=CHECK-LINKER-FLAGS %s
-! CHECK-LINKER-FLAGS: ld
-! CHECK-LINKER-FLAGS-DAG: "-rpath" "/path/to/dir"
-! CHECK-LINKER-FLAGS-DAG: "-shared"
-! CHECK-LINKER-FLAGS-DAG: "-static"
-
-
+! RUN:   | FileCheck --check-prefix=CHECK-LINKER-OPTIONS %s
+! CHECK-LINKER-OPTIONS: ld
+! CHECK-LINKER-OPTIONS-DAG: "-rpath" "/path/to/dir"
+! CHECK-LINKER-OPTIONS-DAG: "-shared"
+! CHECK-LINKER-OPTIONS-DAG: "-static"

>From 33e03db1098f451ba0323f79d2ef33bdef6d07d2 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun at lanl.gov>
Date: Thu, 21 Sep 2023 13:52:39 -0600
Subject: [PATCH 4/6] Mark the test as unsupported on Windows. Windows probably
 does not use ld.

---
 flang/test/Driver/linker-flags.f90 | 1 +
 1 file changed, 1 insertion(+)

diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index e3c6bae8a694051..97cd6e645a917eb 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -52,6 +52,7 @@
 ! Verify that certain linker flags are known to the frontend and are passed on
 ! to the linker.
 
+! UNSUPPORTED: system-windows
 ! RUN: %flang -### -rpath /path/to/dir -shared -static %s 2>&1 \
 ! RUN:   | FileCheck --check-prefix=CHECK-LINKER-OPTIONS %s
 ! CHECK-LINKER-OPTIONS: ld

>From 9053d3373af124e8f51f5466805d0ef9fa2d5840 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun at lanl.gov>
Date: Mon, 25 Sep 2023 08:05:26 -0600
Subject: [PATCH 5/6] Remove check for the linker executable since this might
 not be the same on all platforms e.g. Windows. Re-enable the test on Windows.

---
 flang/test/Driver/linker-flags.f90 | 2 --
 1 file changed, 2 deletions(-)

diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index 97cd6e645a917eb..7cdf70e67ea67e6 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -52,10 +52,8 @@
 ! Verify that certain linker flags are known to the frontend and are passed on
 ! to the linker.
 
-! UNSUPPORTED: system-windows
 ! RUN: %flang -### -rpath /path/to/dir -shared -static %s 2>&1 \
 ! RUN:   | FileCheck --check-prefix=CHECK-LINKER-OPTIONS %s
-! CHECK-LINKER-OPTIONS: ld
 ! CHECK-LINKER-OPTIONS-DAG: "-rpath" "/path/to/dir"
 ! CHECK-LINKER-OPTIONS-DAG: "-shared"
 ! CHECK-LINKER-OPTIONS-DAG: "-static"

>From 3bc2d83f8d30b718bbf3d36ffa4992279412a3f9 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun at lanl.gov>
Date: Thu, 19 Oct 2023 07:49:37 -0600
Subject: [PATCH 6/6] Specify an explicit target triple for ELF-based plaforms
 and Windows and update the checks for each. Suggestions provided by Fangrui
 Song and Leandro Lupori.

---
 flang/test/Driver/linker-flags.f90 | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index 7cdf70e67ea67e6..23146809fa0ba49 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -52,8 +52,13 @@
 ! Verify that certain linker flags are known to the frontend and are passed on
 ! to the linker.
 
-! RUN: %flang -### -rpath /path/to/dir -shared -static %s 2>&1 \
-! RUN:   | FileCheck --check-prefix=CHECK-LINKER-OPTIONS %s
+! RUN: %flang -### --target=x86_64-linux-gnu -rpath /path/to/dir -shared \
+! RUN:     -static %s 2>&1 | FileCheck \
+! RUN:     --check-prefixes=CHECK-LINKER-OPTIONS,GNU-LINKER-OPTIONS %s
+! RUN: %flang -### --target=x86_64-windows-msvc -rpath /path/to/dir -shared \
+! RUN:     -static %s 2>&1 | FileCheck \
+! RUN:     --check-prefixes=CHECK-LINKER-OPTIONS,MSVC-LINKER-OPTIONS %s
 ! CHECK-LINKER-OPTIONS-DAG: "-rpath" "/path/to/dir"
-! CHECK-LINKER-OPTIONS-DAG: "-shared"
-! CHECK-LINKER-OPTIONS-DAG: "-static"
+! GNU-LINKER-OPTIONS-DAG: "-shared"
+! MSVC-LINKER-OPTIONS-DAG: "-dll"
+! GNU-LINKER-OPTIONS-DAG: "-static"



More information about the flang-commits mailing list