mirror of
https://gitlab.freedesktop.org/mesa/mesa.git
synced 2026-05-03 03:28:09 +02:00
util: Refactoring util_dl_get_path_from_proc out of clc/clc_helpers.cpp
For getting clc_helpers.cpp can be compiled with gcc/mingw Signed-off-by: Yonggang Luo <luoyonggang@gmail.com> Reviewed-by: Eric Engestrom <eric@igalia.com> Reviewed-by: Erik Faye-Lund <erik.faye-lund@collabora.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36597>
This commit is contained in:
parent
38db7119cc
commit
723eeac89b
8 changed files with 109 additions and 11 deletions
|
|
@ -1530,7 +1530,7 @@ if host_machine.system() != 'windows'
|
|||
dep_dl = cc.find_library('dl', required : true)
|
||||
endif
|
||||
if cc.has_function('dladdr', dependencies : dep_dl)
|
||||
# This is really only required for util/disk_cache.h
|
||||
# This is required for src/util
|
||||
pre_args += '-DHAVE_DLADDR'
|
||||
endif
|
||||
endif
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@
|
|||
#endif
|
||||
|
||||
#include "util/macros.h"
|
||||
#include "util/u_dl.h"
|
||||
#include "glsl_types.h"
|
||||
|
||||
#include "spirv.h"
|
||||
|
|
@ -905,14 +906,7 @@ clc_compile_to_llvm_module(LLVMContext &llvm_ctx,
|
|||
::llvm::MemoryBuffer::getMemBuffer(llvm::StringRef(opencl_c_source, ARRAY_SIZE(opencl_c_source) - 1)).release());
|
||||
}
|
||||
#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);
|
||||
char *clang_path = util_dl_get_path_from_proc((const void *)clang::CompilerInvocation::CreateFromArgs);
|
||||
if (clang_path == nullptr) {
|
||||
clc_error(logger, "Couldn't find libclang path.\n");
|
||||
return {};
|
||||
|
|
|
|||
|
|
@ -140,6 +140,7 @@ files_mesa_util = files(
|
|||
'u_pointer.h',
|
||||
'u_queue.c',
|
||||
'u_queue.h',
|
||||
'u_string.c',
|
||||
'u_string.h',
|
||||
'u_thread.c',
|
||||
'u_thread.h',
|
||||
|
|
@ -410,6 +411,7 @@ if with_tests
|
|||
'tests/u_call_once_test.cpp',
|
||||
'tests/u_debug_stack_test.cpp',
|
||||
'tests/u_debug_test.cpp',
|
||||
'tests/u_dl_test.cpp',
|
||||
'tests/u_memstream_test.cpp',
|
||||
'tests/u_printf_test.cpp',
|
||||
'tests/u_qsort_test.cpp',
|
||||
|
|
|
|||
25
src/util/tests/u_dl_test.cpp
Normal file
25
src/util/tests/u_dl_test.cpp
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* Copyright 2022 Yonggang Luo
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "util/u_dl.h"
|
||||
|
||||
TEST(u_dl_test, util_dl_get_path_from_proc_test)
|
||||
{
|
||||
void *func_name = util_dl_get_path_from_proc((const void *)util_dl_get_path_from_proc);
|
||||
bool dl_get_path_implemented =
|
||||
#if defined(HAVE_DLADDR)
|
||||
true;
|
||||
#elif DETECT_OS_WINDOWS
|
||||
true;
|
||||
#else
|
||||
false;
|
||||
#endif
|
||||
bool func_name_fetched = func_name != NULL;
|
||||
free(func_name);
|
||||
EXPECT_EQ(func_name_fetched, dl_get_path_implemented);
|
||||
}
|
||||
|
|
@ -38,6 +38,7 @@
|
|||
|
||||
#include "u_dl.h"
|
||||
#include "u_pointer.h"
|
||||
#include "u_string.h"
|
||||
|
||||
|
||||
struct util_dl_library *
|
||||
|
|
@ -66,6 +67,39 @@ util_dl_get_proc_address(struct util_dl_library *library,
|
|||
#endif
|
||||
}
|
||||
|
||||
char *
|
||||
util_dl_get_path_from_proc(const void *func_proc)
|
||||
{
|
||||
#if defined(HAVE_DLADDR)
|
||||
Dl_info info;
|
||||
if (dladdr(func_proc, &info) == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return realpath(info.dli_fname, NULL);
|
||||
#elif DETECT_OS_WINDOWS
|
||||
HMODULE mod = NULL;
|
||||
GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
|
||||
(LPCWSTR)func_proc,
|
||||
&mod);
|
||||
if (!mod)
|
||||
return NULL;
|
||||
|
||||
WCHAR filename[MAX_PATH];
|
||||
DWORD filename_length = GetModuleFileNameW(mod, filename, ARRAY_SIZE(filename));
|
||||
if (filename_length == 0 || filename_length == ARRAY_SIZE(filename))
|
||||
return NULL;
|
||||
|
||||
WCHAR filename_full[MAX_PATH];
|
||||
DWORD filename_full_length = GetFullPathNameW(filename, ARRAY_SIZE(filename_full), filename_full, NULL);
|
||||
if (filename_full_length == 0 || filename_full_length == ARRAY_SIZE(filename_full))
|
||||
return NULL;
|
||||
|
||||
return strdup_wstr_utf8(filename_full);
|
||||
#else
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
util_dl_close(struct util_dl_library *library)
|
||||
|
|
|
|||
|
|
@ -68,6 +68,11 @@ util_dl_proc
|
|||
util_dl_get_proc_address(struct util_dl_library *library,
|
||||
const char *procname);
|
||||
|
||||
/**
|
||||
* Lookup the dll absolute path of a function, the return value need free
|
||||
*/
|
||||
char *
|
||||
util_dl_get_path_from_proc(const void *func_proc);
|
||||
|
||||
/**
|
||||
* Close a library.
|
||||
|
|
|
|||
35
src/util/u_string.c
Normal file
35
src/util/u_string.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
* Copyright 2022 Yonggang Luo
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
* String utils
|
||||
*/
|
||||
|
||||
#include "util/u_string.h"
|
||||
#if DETECT_OS_WINDOWS
|
||||
#include <windows.h>
|
||||
#endif
|
||||
|
||||
#if DETECT_OS_WINDOWS
|
||||
|
||||
static char *
|
||||
strdup_wstr_codepage(unsigned codepage, const wchar_t *utf16_str)
|
||||
{
|
||||
if (!utf16_str)
|
||||
return NULL;
|
||||
const int multi_byte_length = WideCharToMultiByte(codepage, 0, utf16_str, -1, NULL,
|
||||
0, NULL, NULL);
|
||||
char* multi_byte = malloc(multi_byte_length + 1);
|
||||
WideCharToMultiByte(codepage, 0, utf16_str, -1, multi_byte, multi_byte_length, NULL,
|
||||
NULL);
|
||||
multi_byte[multi_byte_length] = 0;
|
||||
return multi_byte;
|
||||
}
|
||||
|
||||
char *
|
||||
strdup_wstr_utf8(const wchar_t *wstr)
|
||||
{
|
||||
return strdup_wstr_codepage(CP_UTF8, wstr);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
@ -42,6 +42,7 @@
|
|||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "util/detect_os.h"
|
||||
#include "util/macros.h" // PRINTFLIKE
|
||||
|
||||
|
||||
|
|
@ -62,7 +63,7 @@ util_strchrnul(const char *s, char c)
|
|||
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#if DETECT_OS_WINDOWS
|
||||
|
||||
#define sprintf util_sprintf
|
||||
static inline int
|
||||
|
|
@ -121,8 +122,10 @@ util_asprintf(char **str, const char *fmt, ...)
|
|||
#define strtok_r strtok_s
|
||||
#endif
|
||||
|
||||
#endif /* _WIN32 */
|
||||
char *
|
||||
strdup_wstr_utf8(const wchar_t *wstr);
|
||||
|
||||
#endif /* DETECT_OS_WINDOWS */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue