Skip to content

[Clang][Driver] Support relative paths for CLANG_CONFIG_FILE_SYSTEM_DIR #110962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2024

Conversation

carlocab
Copy link
Member

@carlocab carlocab commented Oct 3, 2024

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.

Copy link

github-actions bot commented Oct 3, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Oct 3, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 3, 2024

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: Carlo Cabrera (carlocab)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/110962.diff

1 Files Affected:

  • (modified) clang/lib/Driver/Driver.cpp (+10-1)
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)
   {

Copy link
Member

@MaskRay MaskRay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM if you remove the unneeded, outer brace

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.
@carlocab carlocab force-pushed the relative-system-clang-config branch from 775a54f to e31545f Compare October 3, 2024 07:45
@carlocab
Copy link
Member Author

carlocab commented Oct 3, 2024

Thanks, done! I thought that was preferred given the handling of CLANG_CONFIG_FILE_USER_DIR below it.

@MaskRay
Copy link
Member

MaskRay commented Oct 4, 2024

Note: you are using github's private email address feature, but llvm-project prefers to show the real email address. Actually, there is a bot detecting the issue and making a message, though it has some issues and does not trigger on this PR.

@carlocab
Copy link
Member Author

carlocab commented Oct 4, 2024

I authored my commit using a working email address. See the header of the patch:

From e31545f666eb4ca32030956a38dbc4078a64068c Mon Sep 17 00:00:00 2001
From: Carlo Cabrera <[email protected]>
Date: Thu, 3 Oct 2024 14:15:07 +0800
Subject: [PATCH] [Clang][Driver] Support relative paths for
 CLANG_CONFIG_FILE_SYSTEM_DIR

Is that insufficient?

@MaskRay
Copy link
Member

MaskRay commented Oct 4, 2024

This commit will be authored by [email protected]

"Squash and merge" uses the PR author's github's setting. The commit email is completely ignored (unless there is another contributor, which would lead to a Co-authored-by:)

@carlocab
Copy link
Member Author

carlocab commented Oct 4, 2024

That's annoying. Thanks GitHub. I've updated my settings now.

@MaskRay MaskRay merged commit 1682c99 into llvm:main Oct 4, 2024
7 checks passed
Copy link

github-actions bot commented Oct 4, 2024

@carlocab Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants