Skip to content

[clang][flang][windows] Prefer user-provided library paths (-L) #90758

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 3 commits into from
May 16, 2024

Conversation

DavidTruby
Copy link
Member

Currently the paths to compiler-rt and the Flang runtimes from the LLVM build/install directory are preferred over any user-provided library paths. This means a user can't override compiler-rt or the Flang runtimes with custom versions.

This patch changes the link order to prefer library paths specified with -L over the LLVM paths. This matches the behaviour of clang and flang on Linux.

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

llvmbot commented May 1, 2024

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: David Truby (DavidTruby)

Changes

Currently the paths to compiler-rt and the Flang runtimes from the LLVM build/install directory are preferred over any user-provided library paths. This means a user can't override compiler-rt or the Flang runtimes with custom versions.

This patch changes the link order to prefer library paths specified with -L over the LLVM paths. This matches the behaviour of clang and flang on Linux.


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

1 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/MSVC.cpp (+4-4)
diff --git a/clang/lib/Driver/ToolChains/MSVC.cpp b/clang/lib/Driver/ToolChains/MSVC.cpp
index fbf2f45b543844..b7021d4b996ddd 100644
--- a/clang/lib/Driver/ToolChains/MSVC.cpp
+++ b/clang/lib/Driver/ToolChains/MSVC.cpp
@@ -134,6 +134,10 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
           Args.MakeArgString(std::string("-libpath:") + WindowsSdkLibPath));
   }
 
+  if (!C.getDriver().IsCLMode() && Args.hasArg(options::OPT_L))
+    for (const auto &LibPath : Args.getAllArgValues(options::OPT_L))
+      CmdArgs.push_back(Args.MakeArgString("-libpath:" + LibPath));
+
   if (C.getDriver().IsFlangMode()) {
     addFortranRuntimeLibraryPath(TC, Args, CmdArgs);
     addFortranRuntimeLibs(TC, Args, CmdArgs);
@@ -154,10 +158,6 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   if (TC.getVFS().exists(CRTPath))
     CmdArgs.push_back(Args.MakeArgString("-libpath:" + CRTPath));
 
-  if (!C.getDriver().IsCLMode() && Args.hasArg(options::OPT_L))
-    for (const auto &LibPath : Args.getAllArgValues(options::OPT_L))
-      CmdArgs.push_back(Args.MakeArgString("-libpath:" + LibPath));
-
   CmdArgs.push_back("-nologo");
 
   if (Args.hasArg(options::OPT_g_Group, options::OPT__SLASH_Z7))

@MaskRay
Copy link
Member

MaskRay commented May 2, 2024

Thanks. Can you add a test? Possibly clang/test/Driver/msvc-link.c.
linux-cross.cpp has a good example about how to test the default -L paths.

Copy link
Contributor

@banach-space banach-space left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

I've left some nits, feel free to ignore

@@ -0,0 +1,8 @@
! REQUIRES: system-windows
!
! RUN: %clang --driver-mode=flang -### %s -Ltest 2>&1 | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

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

[nit] test -> random_test_dir or something else that will make this stand out a bit more (makes parsing tests a bit easier)

! RUN: %clang --driver-mode=flang -### %s -Ltest 2>&1 | FileCheck %s
!
! Test that user provided paths come before the Flang runtimes and compiler-rt
! CHECK: "-libpath:test"
Copy link
Contributor

Choose a reason for hiding this comment

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

Wondering how to check that there's no -libapth before -libpath:test 🤔

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.

@@ -0,0 +1,7 @@
// REQUIRES: system-windows
Copy link
Member

Choose a reason for hiding this comment

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

Add tests to existing msvc-link.c instead of a new file. Specify --target= to avoid reliance on the default target triple and avoid the need of REQUIRES.

@MaskRay
Copy link
Member

MaskRay commented May 4, 2024

The code checks whether the directory is present auto CRTPath = TC.getCompilerRTPath(); and only adds the libpath when it is present. This is related to the LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=off file hierarchy we are migrating way from.

With LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=on, TC.getCompilerRTPath() will not be needed.

Since there is no good way to test this, I'm fine with the patch without a test.

@DavidTruby
Copy link
Member Author

The code checks whether the directory is present auto CRTPath = TC.getCompilerRTPath(); and only adds the libpath when it is present. This is related to the LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=off file hierarchy we are migrating way from.

With LLVM_ENABLE_PER_TARGET_RUNTIME_DIR=on, TC.getCompilerRTPath() will not be needed.

Since there is no good way to test this, I'm fine with the patch without a test.

Hi, I'm not sure I understand exactly what you mean by without a test; do you mean I should just remove the tests I tried to add or is there some extra behaviour that you're talking about that can't be tested?

DavidTruby and others added 3 commits May 13, 2024 13:22
Currently the paths to compiler-rt and the Flang runtimes from the LLVM
build/install directory are preferred over any user-provided library
paths. This means a user can't override compiler-rt or the Flang
runtimes with custom versions.

This patch changes the link order to prefer library paths specified
with -L over the LLVM paths. This matches the behaviour of clang
and flang on Linux.
@DavidTruby
Copy link
Member Author

I've left the flang test as the flang directory doesn't change with LLVM_ENABLE_PER_TARGET_RUNTIME_DIR and removed the other test. I hope this is what you meant @MaskRay 👍

@banach-space
Copy link
Contributor

I've left the flang test as the flang directory doesn't change with LLVM_ENABLE_PER_TARGET_RUNTIME_DIR and removed the other test. I hope this is what you meant @MaskRay 👍

That's matches how I read that suggestion re LLVM_ENABLE_PER_TARGET_RUNTIME_DIR, thanks!

From what I can tell, you've already made all the changes requested from @MaskRay . If there are no new comments today, I suggest landing this.

@DavidTruby DavidTruby merged commit 7ce8d2e into llvm:main May 16, 2024
4 checks passed
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.

4 participants