[clang] [Clang][Driver] Support relative paths for CLANG_CONFIG_FILE_SYSTEM_DIR (PR #110962)

Carlo Cabrera via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 2 23:28:09 PDT 2024


https://github.com/carlocab created https://github.com/llvm/llvm-project/pull/110962

Shipping a system configuration file for Clang is useful, but it limits
the relocatability of the toolchain because it bakes in a reference to
an absolute path on the file system.

Let's fix that by allowing for `CLANG_CONFIG_FILE_SYSTEM_DIR` to be set
to a relative path, and then interpreting that relative to the location
of the driver if applicable.

This would be useful for the LLVM package we ship at Homebrew. We
currently have to bake in a `DEFAULT_SYSROOT` in order to ship a
toolchain that works out of the box on macOS. If
`CLANG_CONFIG_FILE_SYSTEM_DIR` supported relative paths, we could
replace that with a configuration file which would be easier to update
when the compiled-in `DEFAULT_SYSROOT` becomes stale (e.g. on macOS
version upgrades).

We could, of course, set `CLANG_CONFIG_FILE_SYSTEM_DIR` to an absolute
path to begin with. However, we do have users who install Homebrew into
a prefix that is different from the one used on our buildbots, so doing
this would result in a broken toolchain for those users.


>From 775a54f42b673ab9d3e6e86f73848dec75795d4b Mon Sep 17 00:00:00 2001
From: Carlo Cabrera <github at carlo.cab>
Date: Thu, 3 Oct 2024 14:15:07 +0800
Subject: [PATCH] [Clang][Driver] Support relative paths for
 CLANG_CONFIG_FILE_SYSTEM_DIR

Shipping a system configuration file for Clang is useful, but it limits
the relocatability of the toolchain because it bakes in a reference to
an absolute path on the file system.

Let's fix that by allowing for `CLANG_CONFIG_FILE_SYSTEM_DIR` to be set
to a relative path, and then interpreting that relative to the location
of the driver if applicable.

This would be useful for the LLVM package we ship at Homebrew. We
currently have to bake in a `DEFAULT_SYSROOT` in order to ship a
toolchain that works out of the box on macOS. If
`CLANG_CONFIG_FILE_SYSTEM_DIR` supported relative paths, we could
replace that with a configuration file which would be easier to update
when the compiled-in `DEFAULT_SYSROOT` becomes stale (e.g. on macOS
version upgrades).

We could, of course, set `CLANG_CONFIG_FILE_SYSTEM_DIR` to an absolute
path to begin with. However, we do have users who install Homebrew into
a prefix that is different from the one used on our buildbots, so doing
this would result in a broken toolchain for those users.
---
 clang/lib/Driver/Driver.cpp | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 10d72be2c3d658..aaedfbc31579b7 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -229,7 +229,16 @@ Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
   }
 
 #if defined(CLANG_CONFIG_FILE_SYSTEM_DIR)
-  SystemConfigDir = CLANG_CONFIG_FILE_SYSTEM_DIR;
+  {
+    if (llvm::sys::path::is_absolute(CLANG_CONFIG_FILE_SYSTEM_DIR)) {
+      SystemConfigDir = CLANG_CONFIG_FILE_SYSTEM_DIR;
+    } else {
+      SmallString<128> configFileDir(Dir);
+      llvm::sys::path::append(configFileDir, CLANG_CONFIG_FILE_SYSTEM_DIR);
+      llvm::sys::path::remove_dots(configFileDir, true);
+      SystemConfigDir = static_cast<std::string>(configFileDir);
+    }
+  }
 #endif
 #if defined(CLANG_CONFIG_FILE_USER_DIR)
   {



More information about the cfe-commits mailing list