From 445aacb4217cbf5fb7be604c5484eb84c3c06497 Mon Sep 17 00:00:00 2001 From: Antoine Coutant Date: Thu, 30 Nov 2023 11:10:20 +0100 Subject: [PATCH] clc: retrieve libclang path at runtime. LLVM_LIB_DIR is a variable used for runtime compilations. When cross compiling, LLVM_LIB_DIR must be set to the libclang path on the target. So, this path should not be retrieved during compilation but at runtime. dladdr uses an address to search for a loaded library. If a library is found, it returns information about it. The path to the libclang library can therefore be retrieved using one of its functions. This is useful because we don't know the name of the libclang library (libclang.so.X or libclang-cpp.so.X) v2 (Karol): use clang::CompilerInvocation::CreateFromArgs for dladdr v3 (Karol): follow symlinks to fix errors on debian Fixes: e22491c8326 ("clc: fetch clang resource dir at runtime") Signed-off-by: Antoine Coutant Reviewed-by: Karol Herbst Reviewed-by (v1): Jesse Natalie Part-of: --- src/compiler/clc/clc_helpers.cpp | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/compiler/clc/clc_helpers.cpp b/src/compiler/clc/clc_helpers.cpp index 15af4b44c6c..b074596ad73 100644 --- a/src/compiler/clc/clc_helpers.cpp +++ b/src/compiler/clc/clc_helpers.cpp @@ -23,6 +23,7 @@ // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. +#include #include #include #include @@ -60,6 +61,10 @@ #include "spirv.h" +#if DETECT_OS_UNIX +#include +#endif + #ifdef USE_STATIC_OPENCL_C_H #include "opencl-c-base.h.h" #endif @@ -854,12 +859,24 @@ clc_compile_to_llvm_module(LLVMContext &llvm_ctx, c->getPreprocessorOpts().Includes.push_back("opencl-c-base.h"); } #else + + Dl_info info; + if (dladdr((void *)clang::CompilerInvocation::CreateFromArgs, &info) == 0) { + clc_error(logger, "Couldn't find libclang path.\n"); + return {}; + } + + char *clang_path = realpath(info.dli_fname, NULL); + if (clang_path == nullptr) { + clc_error(logger, "Couldn't find libclang path.\n"); + return {}; + } + // GetResourcePath is a way to retrive the actual libclang resource dir based on a given binary - // or library. The path doesn't even need to exist, we just have to put something in there, - // because we might have linked clang statically. - auto libclang_path = fs::path(LLVM_LIB_DIR) / "libclang.so"; + // or library. auto clang_res_path = - fs::path(Driver::GetResourcesPath(libclang_path.string(), CLANG_RESOURCE_DIR)) / "include"; + fs::path(Driver::GetResourcesPath(std::string(clang_path), CLANG_RESOURCE_DIR)) / "include"; + free(clang_path); c->getHeaderSearchOpts().UseBuiltinIncludes = true; c->getHeaderSearchOpts().UseStandardSystemIncludes = true;