[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 19 06:51:38 PDT 2023


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

>From 3302fb4937d75c0920e7bcf8f215e12fbe770a36 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 553c7928c4f949e..bf4c76d4555d152 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5165,7 +5165,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=">,
   HelpText<"Compiler runtime library to use">;
 def frtlib_add_rpath: Flag<["-"], "frtlib-add-rpath">, Flags<[NoArgumentUnused]>,
@@ -5216,7 +5217,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]>;
@@ -5226,6 +5228,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 09b8a224df13828..9a9ec5d28acb9c7 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -50,3 +50,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 653d215a59fc1ab71784776d52a32005359ee93e 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 9a9ec5d28acb9c7..c9cc851fa607ae8 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -54,15 +54,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 e080beb011db6ad26c07deab0fe7178895ed3c99 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 c9cc851fa607ae8..56a9bbf33fd01ed 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -55,10 +55,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 654205e339d027b60c9526c0acb4c3f41096fa15 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 56a9bbf33fd01ed..162e0922631c096 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -54,6 +54,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 8a74d9961f36059eda4997777deaeec8c37ff8b6 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 162e0922631c096..80a6cd1c0880c2c 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -54,10 +54,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 fae3410237dc63930c4179e00f1229b26679feb9 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 80a6cd1c0880c2c..3e89ceb2b5e5272 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -54,8 +54,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