Fix Clang lib folder detection on Windows

First, more recent versions of Clang do no longer have trailing info in
parentheses, so the detection would fail.  Since we're not interested
in this information, we just ignore it.

Then we should not rely on Clang being installed in the default program
folder.  Instead we look up where the first clang.exe can be found, and
assume that it is located in a bin/ folder in the installation path.
From there we construct the library path, which is
`lib\clang\<ver>\lib\windows` where `<ver>` is either the full version
(older Clang) or only the major version.  Note that this is the case
for stand-alone LLVM installations as well as Visual Studio supplied
ones.

Finally, we clean up by improving the error messages, and removing the
duplicate clang version detection in `add_asan_opts()`.

While we're at it, we also apply a cosmetic improvement to avoid
(trailing) whitespace in the compiler name (e.g. shown by `-v`).
This commit is contained in:
Christoph M. Becker 2025-01-14 14:11:00 +01:00
parent ddfa3934d8
commit e1c4c0300e

View file

@ -3137,7 +3137,7 @@ function toolset_get_compiler_name(short)
var command = 'cmd /c ""' + PHP_CL + '" -v"';
var full = execute(command + '" 2>&1"');
return full.split(/\n/)[0].replace(/\s/g, ' ');
return trim(full.split(/\n/)[0].replace(/\s/g, ' '));
}
WARNING("Unsupported toolset");
@ -3703,31 +3703,31 @@ function check_binary_tools_sdk()
function get_clang_lib_dir()
{
var ret = null;
var ver = null;
var ver = null, major = null;
if (COMPILER_NAME_LONG.match(/clang version ([\d\.]+) \((.*)\)/)) {
if (COMPILER_NAME_LONG.match(/clang version ((\d+)\.[\d\.]+)/)) {
ver = RegExp.$1;
major = RegExp.$2;
} else {
ERROR("Failed to determine clang lib path");
ERROR("Failed to determine clang version");
}
if (TARGET_ARCH != 'x86') {
ret = PROGRAM_FILES + "\\LLVM\\lib\\clang\\" + ver + "\\lib";
var cmd = "cmd /c where clang.exe";
var path = trim(execute(cmd).split(/\n/)[0]).replace(/bin\\clang\.exe$/, "");
if (!FSO.FolderExists(path)) {
ERROR("Failed to determine clang installation folder");
}
ret = path + "lib\\clang\\" + major + "\\lib\\";
if (!FSO.FolderExists(ret)) {
ret = path + "lib\\clang\\" + ver + "\\lib\\";
if (!FSO.FolderExists(ret)) {
ret = null;
}
} else {
ret = PROGRAM_FILESx86 + "\\LLVM\\lib\\clang\\" + ver + "\\lib";
if (!FSO.FolderExists(ret)) {
ret = PROGRAM_FILES + "\\LLVM\\lib\\clang\\" + ver + "\\lib";
if (!FSO.FolderExists(ret)) {
ret = null;
}
}
}
if (null == ret) {
ERROR("Invalid clang lib path encountered");
ERROR("Failed to determine clang lib folder");
}
return ret;
@ -3736,13 +3736,7 @@ function get_clang_lib_dir()
function add_asan_opts(cflags_name, libs_name, ldflags_name)
{
var ver = null;
if (COMPILER_NAME_LONG.match(/clang version ([\d\.]+) \((.*)\)/)) {
ver = RegExp.$1;
} else {
ERROR("Failed to determine clang lib path");
}
var lib_dir = get_clang_lib_dir();
if (!!cflags_name) {
ADD_FLAG(cflags_name, "-fsanitize=address,undefined");
@ -3759,7 +3753,7 @@ function add_asan_opts(cflags_name, libs_name, ldflags_name)
}
if (!!ldflags_name) {
ADD_FLAG(ldflags_name, "/libpath:\"" + get_clang_lib_dir() + "\\windows\"");
ADD_FLAG(ldflags_name, "/libpath:\"" + lib_dir + "\\windows\"");
}
}