From aafc3f7be804d117a632365489a18c3e484a3931 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Markus=20B=C3=B6ck?= Date: Fri, 19 Mar 2021 17:47:07 +0100 Subject: [PATCH] [Driver] Add -print-runtime-dir This patch adds a new command line option to clang which outputs the directory containing clangs runtime libraries to stdout. The primary use case for this command line flag is for build systems using clang-cl. Build systems when using clang-cl invoke the linker, that is either link or lld-link in this case, directly instead of invoking the compiler for the linking process as is common with the other drivers. This leads to issues when runtime libraries of clang, such as sanitizers or profiling, have to be linked in as the compiler cannot communicate the link directory to the linker. Using this flag, build systems would be capable of getting the directory containing all of clang's runtime libraries and add it to the linker path. Differential Revision: https://reviews.llvm.org/D98868 --- clang/include/clang/Driver/Options.td | 2 ++ clang/lib/Driver/Driver.cpp | 9 +++++++++ .../resource_dir/lib/windows/clang_rt.builtins-x86_64.lib | 0 .../lib/x86_64-pc-windows-msvc/clang_rt.builtins.lib | 0 clang/test/Driver/immediate-options.c | 12 ++++++++++++ 5 files changed, 23 insertions(+) create mode 100644 clang/test/Driver/Inputs/resource_dir/lib/windows/clang_rt.builtins-x86_64.lib create mode 100644 clang/test/Driver/Inputs/resource_dir_with_per_target_subdir/lib/x86_64-pc-windows-msvc/clang_rt.builtins.lib diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index a9b43a8..b7efb74 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -3566,6 +3566,8 @@ def print_targets : Flag<["-", "--"], "print-targets">, HelpText<"Print the registered targets">; def print_rocm_search_dirs : Flag<["-", "--"], "print-rocm-search-dirs">, HelpText<"Print the paths used for finding ROCm installation">; +def print_runtime_dir : Flag<["-", "--"], "print-runtime-dir">, + HelpText<"Print the directory pathname containing clangs runtime libraries">; def private__bundle : Flag<["-"], "private_bundle">; def pthreads : Flag<["-"], "pthreads">; defm pthread : BoolOption<"", "pthread", diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index dbd365e..e70263e 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -1824,6 +1824,15 @@ bool Driver::HandleImmediateArgs(const Compilation &C) { return false; } + if (C.getArgs().hasArg(options::OPT_print_runtime_dir)) { + if (auto RuntimePath = TC.getRuntimePath()) { + llvm::outs() << *RuntimePath << '\n'; + return false; + } + llvm::outs() << TC.getCompilerRTPath() << '\n'; + return false; + } + // FIXME: The following handlers should use a callback mechanism, we don't // know what the client would like to do. if (Arg *A = C.getArgs().getLastArg(options::OPT_print_file_name_EQ)) { diff --git a/clang/test/Driver/Inputs/resource_dir/lib/windows/clang_rt.builtins-x86_64.lib b/clang/test/Driver/Inputs/resource_dir/lib/windows/clang_rt.builtins-x86_64.lib new file mode 100644 index 0000000..e69de29 diff --git a/clang/test/Driver/Inputs/resource_dir_with_per_target_subdir/lib/x86_64-pc-windows-msvc/clang_rt.builtins.lib b/clang/test/Driver/Inputs/resource_dir_with_per_target_subdir/lib/x86_64-pc-windows-msvc/clang_rt.builtins.lib new file mode 100644 index 0000000..e69de29 diff --git a/clang/test/Driver/immediate-options.c b/clang/test/Driver/immediate-options.c index 723a6fa..c398e0d4 100644 --- a/clang/test/Driver/immediate-options.c +++ b/clang/test/Driver/immediate-options.c @@ -17,3 +17,15 @@ // Allow unspecified output because the value of CLANG_RESOURCE_DIR is unknown. // RUN: %clang -print-resource-dir | FileCheck %s -check-prefix=PRINT-RESOURCE-DIR // PRINT-RESOURCE-DIR: {{.+}} + +// Default resource-dir layout +// RUN: %clang -print-runtime-dir --target=x86_64-pc-windows-msvc \ +// RUN: -resource-dir=%S/Inputs/resource_dir \ +// RUN: | FileCheck --check-prefix=PRINT-RUNTIME-DIR %s +// PRINT-RUNTIME-DIR: lib{{/|\\}}windows + +// Per target dir layout +// RUN: %clang -print-runtime-dir --target=x86_64-pc-windows-msvc \ +// RUN: -resource-dir=%S/Inputs/resource_dir_with_per_target_subdir \ +// RUN: | FileCheck --check-prefix=PRINT-RUNTIME-DIR-PER-TARGET %s +// PRINT-RUNTIME-DIR-PER-TARGET: lib{{/|\\}}x86_64-pc-windows-msvc -- 2.7.4