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

Tarun Prabhu via flang-commits flang-commits at lists.llvm.org
Tue Nov 14 11:37:47 PST 2023


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

>From f149c390f892c4e3f4b67ac9ae833b487b7c026d 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/8] [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 d1b67a448b2a59b..885b2b0e93e88e7 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5253,7 +5253,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]>,
@@ -5304,7 +5305,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]>;
@@ -5314,6 +5316,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 a1417057d4da068..32c0f8ffb110cbe 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -88,3 +88,19 @@
 ! MSVC-DLL-DEBUG-SAME: FortranDecimal.dynamic_dbg.lib
 ! MSVC-DLL-DEBUG-SAME: /subsystem:console
 ! MSVC-DLL-DEBUG-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 9e24cff770e601b69e59905c132ef4534b46a286 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/8] 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 32c0f8ffb110cbe..257f6fb457174a5 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -92,15 +92,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 b4d0ac573c8daa780707b92734db0ccfa47bcf1a 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/8] 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 257f6fb457174a5..cf8e3bab8912779 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -93,10 +93,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 edd529e4fb14c063039377670224f6bfaf8e1b18 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/8] 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 cf8e3bab8912779..04c00fe6eea7ebf 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -92,6 +92,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 7b41d523560da85eab9360a8c7198e762b53f256 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/8] 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 04c00fe6eea7ebf..3f8cbda21a27580 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -92,10 +92,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 8ea1681ffdeede9528ab666fbc1f12182392333d 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/8] 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 3f8cbda21a27580..87562967fd318a7 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -92,8 +92,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"

>From ba1ac3be4f29f51be81775034c06a3b7fb9aa965 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun at lanl.gov>
Date: Tue, 14 Nov 2023 08:36:55 -0700
Subject: [PATCH 7/8] Move the tests for -shared and -static to a separate
 file. Make the tests more strict.

---
 flang/test/Driver/dynamic-linker.f90 | 20 ++++++++++++++++++++
 flang/test/Driver/linker-flags.f90   | 14 --------------
 2 files changed, 20 insertions(+), 14 deletions(-)
 create mode 100644 flang/test/Driver/dynamic-linker.f90

diff --git a/flang/test/Driver/dynamic-linker.f90 b/flang/test/Driver/dynamic-linker.f90
new file mode 100644
index 000000000000000..adac3bbb61d4e16
--- /dev/null
+++ b/flang/test/Driver/dynamic-linker.f90
@@ -0,0 +1,20 @@
+! Verify that certain linker flags are known to the frontend and are passed on
+! to the linker.
+
+! RUN: %flang -### --target=x86_64-linux-gnu -rpath /path/to/dir -shared \
+! RUN:     -static %s 2>&1 | FileCheck \
+! RUN:     --check-prefixes=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=MSVC-LINKER-OPTIONS %s
+
+! The linker may have an extension, or a suffix.
+! GNU-LINKER-OPTIONS: "{{.*}}ld{{[^"]*}}"
+! GNU-LINKER-OPTIONS-SAME: "-shared"
+! GNU-LINKER-OPTIONS-SAME: "-static"
+! GNU-LINKER-OPTIONS-SAME: "-rpath" "/path/to/dir"
+
+! For MSVC, adding -static does not add any additional linker options.
+! MSVC-LINKER-OPTIONS: "{{.*}}link.exe"
+! MSVC-LINKER-OPTIONS-SAME: "-dll"
+! MSVC-LINKER-OPTIONS-SAME: "-rpath" "/path/to/dir"
diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index 87562967fd318a7..a1417057d4da068 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -88,17 +88,3 @@
 ! MSVC-DLL-DEBUG-SAME: FortranDecimal.dynamic_dbg.lib
 ! MSVC-DLL-DEBUG-SAME: /subsystem:console
 ! MSVC-DLL-DEBUG-SAME: "[[object_file]]"
-
-! Verify that certain linker flags are known to the frontend and are passed on
-! to the linker.
-
-! 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"
-! GNU-LINKER-OPTIONS-DAG: "-shared"
-! MSVC-LINKER-OPTIONS-DAG: "-dll"
-! GNU-LINKER-OPTIONS-DAG: "-static"

>From 7edd9f4e6756cce047ebdd603157c8b4734707f2 Mon Sep 17 00:00:00 2001
From: Tarun Prabhu <tarun at lanl.gov>
Date: Tue, 14 Nov 2023 12:35:34 -0700
Subject: [PATCH 8/8] Update regex in test which matches the wrong line in
 certain cases.

---
 flang/test/Driver/dynamic-linker.f90 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/flang/test/Driver/dynamic-linker.f90 b/flang/test/Driver/dynamic-linker.f90
index adac3bbb61d4e16..2745822dc107769 100644
--- a/flang/test/Driver/dynamic-linker.f90
+++ b/flang/test/Driver/dynamic-linker.f90
@@ -8,8 +8,8 @@
 ! RUN:     -static %s 2>&1 | FileCheck \
 ! RUN:     --check-prefixes=MSVC-LINKER-OPTIONS %s
 
-! The linker may have an extension, or a suffix.
-! GNU-LINKER-OPTIONS: "{{.*}}ld{{[^"]*}}"
+! TODO: Could the linker have an extension or a suffix?
+! GNU-LINKER-OPTIONS: "{{.*}}ld{{(.exe)?}}"
 ! GNU-LINKER-OPTIONS-SAME: "-shared"
 ! GNU-LINKER-OPTIONS-SAME: "-static"
 ! GNU-LINKER-OPTIONS-SAME: "-rpath" "/path/to/dir"



More information about the flang-commits mailing list