[flang-commits] [clang] [flang] [Flang][Driver] Add -print-resource-dir command line flag to emit Flang's resource directory (PR #90886)

Michael Klemm via flang-commits flang-commits at lists.llvm.org
Fri May 3 00:53:03 PDT 2024


https://github.com/mjklemm updated https://github.com/llvm/llvm-project/pull/90886

>From 1a994159025f127f0f7d11da80b74035788d52c9 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Thu, 2 May 2024 14:50:45 +0200
Subject: [PATCH 1/6] Enable -print-resource-dir also for Flang

---
 clang/include/clang/Driver/Options.td | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 864da4e1157f7d..a3b81fa338bdcd 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -5474,7 +5474,7 @@ def print_prog_name_EQ : Joined<["-", "--"], "print-prog-name=">,
   Visibility<[ClangOption, CLOption]>;
 def print_resource_dir : Flag<["-", "--"], "print-resource-dir">,
   HelpText<"Print the resource directory pathname">,
-  Visibility<[ClangOption, CLOption]>;
+  Visibility<[ClangOption, CLOption, FlangOption]>;
 def print_search_dirs : Flag<["-", "--"], "print-search-dirs">,
   HelpText<"Print the paths used for finding libraries and programs">,
   Visibility<[ClangOption, CLOption]>;

>From beee04f6f2d411694f5ae1ee6130a81a632164df Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Thu, 2 May 2024 16:19:38 +0200
Subject: [PATCH 2/6] Add setResourceDir function to set resource directory for
 Flang

This should be an NFC change for anythign, but Flang.
---
 clang/include/clang/Driver/Driver.h |  3 +++
 clang/lib/Driver/Driver.cpp         | 23 ++++++++++++++++++++---
 2 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Driver/Driver.h b/clang/include/clang/Driver/Driver.h
index 2ffc52bcb7ad3b..c36595e62e2daf 100644
--- a/clang/include/clang/Driver/Driver.h
+++ b/clang/include/clang/Driver/Driver.h
@@ -752,6 +752,9 @@ class Driver {
   /// option.
   void setDriverMode(StringRef DriverModeValue);
 
+  /// Set the resource directory, depending on which driver is being used.
+  void setResourceDirectory();
+
   /// Parse the \p Args list for LTO options and record the type of LTO
   /// compilation based on which -f(no-)?lto(=.*)? option occurs last.
   void setLTOMode(const llvm::opt::ArgList &Args);
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 114320f5d31468..d75d2e88fc1024 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -229,9 +229,6 @@ Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
     UserConfigDir = static_cast<std::string>(P);
   }
 #endif
-
-  // Compute the path to the resource directory.
-  ResourceDir = GetResourcesPath(ClangExecutable, CLANG_RESOURCE_DIR);
 }
 
 void Driver::setDriverMode(StringRef Value) {
@@ -250,6 +247,25 @@ void Driver::setDriverMode(StringRef Value) {
     Diag(diag::err_drv_unsupported_option_argument) << OptName << Value;
 }
 
+void Driver::setResourceDirectory() {
+  // Compute the path to the resource directory, depending on the driver mode.
+  switch (Mode) {
+  case GCCMode:
+  case GXXMode:
+  case CPPMode:
+  case CLMode:
+  case DXCMode:
+    ResourceDir = GetResourcesPath(ClangExecutable, CLANG_RESOURCE_DIR);
+    break;
+  case FlangMode:
+    // TODO: Is there a better way to add the "../include/flang/" component?
+    SmallString<64> relPath{};
+    llvm::sys::path::append(relPath, "..", "include", "flang");
+    ResourceDir = GetResourcesPath(ClangExecutable, relPath);
+    break;
+  }
+}
+
 InputArgList Driver::ParseArgStrings(ArrayRef<const char *> ArgStrings,
                                      bool UseDriverMode, bool &ContainsError) {
   llvm::PrettyStackTraceString CrashInfo("Command line argument parsing");
@@ -1202,6 +1218,7 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
   if (!DriverMode.empty())
     setDriverMode(DriverMode);
 
+  setResourceDirectory();
   // FIXME: What are we going to do with -V and -b?
 
   // Arguments specified in command line.

>From 5ca34138043ab621582085e7034349c7e726a970 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Thu, 2 May 2024 20:31:04 +0200
Subject: [PATCH 3/6] Update/add tests fir -print-resource-dir

---
 flang/test/Driver/driver-help-hidden.f90 | 1 +
 flang/test/Driver/print-resource-dir.F90 | 3 +++
 2 files changed, 4 insertions(+)
 create mode 100644 flang/test/Driver/print-resource-dir.F90

diff --git a/flang/test/Driver/driver-help-hidden.f90 b/flang/test/Driver/driver-help-hidden.f90
index 706b2cb6c2452c..73b34bd321c5f6 100644
--- a/flang/test/Driver/driver-help-hidden.f90
+++ b/flang/test/Driver/driver-help-hidden.f90
@@ -143,6 +143,7 @@
 ! CHECK-NEXT: -o <file>               Write output to <file>
 ! CHECK-NEXT: -pedantic               Warn on language extensions
 ! CHECK-NEXT: -print-effective-triple Print the effective target triple
+! CHECK-NEXT: -print-resource-dir     Print the resource directory pathname
 ! CHECK-NEXT: -print-target-triple    Print the normalized target triple
 ! CHECK-NEXT: -pthread                Support POSIX threads in generated code
 ! CHECK-NEXT: -P                      Disable linemarker output in -E mode
diff --git a/flang/test/Driver/print-resource-dir.F90 b/flang/test/Driver/print-resource-dir.F90
new file mode 100644
index 00000000000000..5c934312e1f68a
--- /dev/null
+++ b/flang/test/Driver/print-resource-dir.F90
@@ -0,0 +1,3 @@
+! RUN: %flang -print-resource-dir -resource-dir=%S/Inputs/resource_dir \
+! RUN:  | FileCheck -check-prefix=PRINT-RESOURCE-DIR -DFILE=%S/Inputs/resource_dir %s
+! PRINT-RESOURCE-DIR: [[FILE]]

>From 99f61034d5c5e66919670723ef881b46ed006321 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Fri, 3 May 2024 09:35:25 +0200
Subject: [PATCH 4/6] Simplify test by using DEFINE to avoid repitition

---
 flang/test/Driver/print-resource-dir.F90 | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/flang/test/Driver/print-resource-dir.F90 b/flang/test/Driver/print-resource-dir.F90
index 5c934312e1f68a..8fd35f1800df21 100644
--- a/flang/test/Driver/print-resource-dir.F90
+++ b/flang/test/Driver/print-resource-dir.F90
@@ -1,3 +1,4 @@
-! RUN: %flang -print-resource-dir -resource-dir=%S/Inputs/resource_dir \
-! RUN:  | FileCheck -check-prefix=PRINT-RESOURCE-DIR -DFILE=%S/Inputs/resource_dir %s
+! DEFINE: %{resource_dir} = %S/Inputs/resource_dir
+! RUN: %flang -print-resource-dir -resource-dir=%{resource_dir}.. \
+! RUN:  | FileCheck -check-prefix=PRINT-RESOURCE-DIR -DFILE=%{resource_dir} %s
 ! PRINT-RESOURCE-DIR: [[FILE]]

>From c0e0cdb0960fc76545f88ca74c4d2d68dc5a4928 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Fri, 3 May 2024 09:35:47 +0200
Subject: [PATCH 5/6] Be more verbose about the variable name for the relative
 path

---
 clang/lib/Driver/Driver.cpp | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index d75d2e88fc1024..54c662d30f0280 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -259,9 +259,11 @@ void Driver::setResourceDirectory() {
     break;
   case FlangMode:
     // TODO: Is there a better way to add the "../include/flang/" component?
-    SmallString<64> relPath{};
-    llvm::sys::path::append(relPath, "..", "include", "flang");
-    ResourceDir = GetResourcesPath(ClangExecutable, relPath);
+    SmallString<64> customResourcePathReleativeToDriver{};
+    llvm::sys::path::append(customResourcePathReleativeToDriver, "..",
+                            "include", "flang");
+    ResourceDir =
+        GetResourcesPath(ClangExecutable, customResourcePathReleativeToDriver);
     break;
   }
 }

>From 462b3931fac6f7a54ad822573bf9f90fd59e1706 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Fri, 3 May 2024 02:52:43 -0500
Subject: [PATCH 6/6] Move to folder containing lib and include, also
 simplifies path construction.

---
 clang/lib/Driver/Driver.cpp | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 54c662d30f0280..6433d96b3517a5 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -258,12 +258,9 @@ void Driver::setResourceDirectory() {
     ResourceDir = GetResourcesPath(ClangExecutable, CLANG_RESOURCE_DIR);
     break;
   case FlangMode:
-    // TODO: Is there a better way to add the "../include/flang/" component?
-    SmallString<64> customResourcePathReleativeToDriver{};
-    llvm::sys::path::append(customResourcePathReleativeToDriver, "..",
-                            "include", "flang");
+    SmallString<64> customResourcePathRelativeToDriver{".."};
     ResourceDir =
-        GetResourcesPath(ClangExecutable, customResourcePathReleativeToDriver);
+        GetResourcesPath(ClangExecutable, customResourcePathRelativeToDriver);
     break;
   }
 }



More information about the flang-commits mailing list