Skip to content

[flang][Driver] Enable FLANG_DEFAULT_LINKER #149786

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
Aug 9, 2025

Conversation

parabola94
Copy link
Contributor

@parabola94 parabola94 commented Jul 21, 2025

The default linker can be changed by a CMake variable CLANG_DEFAULT_LINKER. However, it also changes the default linker invoked by clang. In fact, there already exists FLANG_DEFAULT_LINKER, but it does not work. This patch fixes it.

Note that FLANG_DEFAULT_LINKER will have the same value as CLANG_DEFAULT_LINKER unless it is defined explicitly. That means this patch does not affect the current behavior.

Fixes #73153

Copy link
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

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

Thanks. LGTM, but please wait for someone on the clang side to take a look as well.

[EDIT] Ah, didn't notice that it was a draft. Let me know when you think it is ready.

@yus3710-fj yus3710-fj force-pushed the flang-default-linker branch from 7a97f53 to 2f39e8a Compare July 24, 2025 21:37
@parabola94 parabola94 marked this pull request as ready for review July 24, 2025 21:57
@llvmbot llvmbot added flang:driver flang Flang issues not falling into any other category labels Jul 24, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 24, 2025

@llvm/pr-subscribers-flang-driver

Author: None (parabola94)

Changes

The default linker can be changed by a CMake variable CLANG_DEFAULT_LINKER at this moment. However, it also changes the default linker invoked by clang. In fact, there already exists FLANG_DEFAULT_LINKER, but it does not work. This patch fixes it.

Note that FLANG_DEFAULT_LINKER will have the same value as CLANG_DEFAULT_LINKER unless it is defined explicitly. That means this patch does not affect the current behavior.

Fixes #73153


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

4 Files Affected:

  • (modified) flang/CMakeLists.txt (+1-1)
  • (modified) flang/include/flang/Config/config.h.cmake (+9-7)
  • (modified) flang/test/Driver/linker-flags.f90 (+1-1)
  • (modified) flang/tools/flang-driver/driver.cpp (+2)
diff --git a/flang/CMakeLists.txt b/flang/CMakeLists.txt
index 0bfada476348a..6cbe6d4ed6a3e 100644
--- a/flang/CMakeLists.txt
+++ b/flang/CMakeLists.txt
@@ -317,7 +317,7 @@ if (NOT ENABLE_LINKER_BUILD_ID)
   set(ENABLE_LINKER_BUILD_ID OFF CACHE BOOL "pass --build-id to ld")
 endif()
 
-set(FLANG_DEFAULT_LINKER "" CACHE STRING
+set(FLANG_DEFAULT_LINKER ${CLANG_DEFAULT_LINKER} CACHE STRING
   "Default linker to use (linker name or absolute path, empty for platform default)")
 
 set(FLANG_DEFAULT_RTLIB "" CACHE STRING
diff --git a/flang/include/flang/Config/config.h.cmake b/flang/include/flang/Config/config.h.cmake
index fd34d3f403631..92fbd14c3d6db 100644
--- a/flang/include/flang/Config/config.h.cmake
+++ b/flang/include/flang/Config/config.h.cmake
@@ -1,10 +1,10 @@
-#===-- include/flang/Config/config.h.cmake ---------------------------------===#
-#
-# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-# See https://llvm.org/LICENSE.txt for license information.
-# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-#
-#===------------------------------------------------------------------------===#
+//===-- include/flang/Config/config.h.cmake -------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
 
 /* This generated file is for internal use. Do not include it from headers. */
 
@@ -16,6 +16,8 @@
 
 #define FLANG_VERSION            "${FLANG_VERSION}"
 
+#define FLANG_DEFAULT_LINKER     "${FLANG_DEFAULT_LINKER}"
+
 #endif
 
 
diff --git a/flang/test/Driver/linker-flags.f90 b/flang/test/Driver/linker-flags.f90
index ad48ea1b9e9b1..2b56fdfb8da05 100644
--- a/flang/test/Driver/linker-flags.f90
+++ b/flang/test/Driver/linker-flags.f90
@@ -77,7 +77,7 @@
 ! MINGW-SAME: -lflang_rt.runtime
 ! MINGW-STATIC-FLANGRT: "{{.*}}{{\\|/}}libflang_rt.runtime.a"
 
-! NOTE: This also matches lld-link (when CLANG_DEFAULT_LINKER=lld) and
+! NOTE: This also matches lld-link (when FLANG_DEFAULT_LINKER=lld) and
 !       any .exe suffix that is added when resolving to the full path of
 !       (lld-)link.exe on Windows platforms. The suffix may not be added
 !       when the executable is not found or on non-Windows platforms.
diff --git a/flang/tools/flang-driver/driver.cpp b/flang/tools/flang-driver/driver.cpp
index 3a2dffc66428f..b1e02383cd33d 100644
--- a/flang/tools/flang-driver/driver.cpp
+++ b/flang/tools/flang-driver/driver.cpp
@@ -16,6 +16,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "clang/Driver/Driver.h"
+#include "flang/Config/config.h"
 #include "flang/Frontend/CompilerInvocation.h"
 #include "flang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Basic/Diagnostic.h"
@@ -138,6 +139,7 @@ int main(int argc, const char **argv) {
                                   llvm::sys::getDefaultTargetTriple(), diags,
                                   "flang LLVM compiler");
   theDriver.setTargetAndMode(targetandMode);
+  theDriver.setPreferredLinker(FLANG_DEFAULT_LINKER);
 #ifdef FLANG_RUNTIME_F128_MATH_LIB
   theDriver.setFlangF128MathLibrary(FLANG_RUNTIME_F128_MATH_LIB);
 #endif

@parabola94
Copy link
Contributor Author

@tarunprabhu @kkwli Could you review this PR?

Copy link
Contributor

@tarunprabhu tarunprabhu left a comment

Choose a reason for hiding this comment

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

Thanks. Still LGTM. :-)

Please wait a while before merging to give others a chance to take a look.

@parabola94
Copy link
Contributor Author

@kkwli Ping.
I would like to merge it next week because Tarun has already approved this PR.

@tarunprabhu tarunprabhu requested a review from kkwli August 4, 2025 13:12
@tarunprabhu
Copy link
Contributor

@parabola94

Do you have permissions to add reviewers to your PR? If so, it generally works better to explicitly request reviews from people that way.

@parabola94
Copy link
Contributor Author

@parabola94

Do you have permissions to add reviewers to your PR? If so, it generally works better to explicitly request reviews from people that way.

Unfortunately, I do not have permissions to do so. Thank you for your help!

Copy link
Collaborator

@kkwli kkwli left a comment

Choose a reason for hiding this comment

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

Sorry for the delay.

LG. Thanks.

@parabola94 parabola94 requested a review from Meinersbur August 8, 2025 11:31
@parabola94 parabola94 merged commit dea50a1 into llvm:main Aug 9, 2025
9 checks passed
@parabola94 parabola94 deleted the flang-default-linker branch August 9, 2025 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:driver flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[flang] FLANG_DEFAULT_LINKER=lld does not use ld.lld as the linker
5 participants