From 07d404a95c8d7b46975e1c76ab3bca73c98fca2c Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 14 Jan 2025 14:11:00 +0100 Subject: [PATCH 1/2] 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\\lib\windows` where `` 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`). --- win32/build/confutils.js | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/win32/build/confutils.js b/win32/build/confutils.js index 032e534bb0d19..3fccc0a8f077a 100644 --- a/win32/build/confutils.js +++ b/win32/build/confutils.js @@ -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"); @@ -3698,31 +3698,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; @@ -3731,13 +3731,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"); @@ -3754,7 +3748,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\""); } } From 79af31479a7197d39be490a87f992a16d2e1969d Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Tue, 14 Jan 2025 14:14:34 +0100 Subject: [PATCH 2/2] Suppress UB warnings regarding indirect function calls Like for POSIX systems, we pass `-fno-sanitize=function`. Closes GH-17462. --- win32/build/confutils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/win32/build/confutils.js b/win32/build/confutils.js index 3fccc0a8f077a..39756d9903f55 100644 --- a/win32/build/confutils.js +++ b/win32/build/confutils.js @@ -3734,7 +3734,7 @@ function add_asan_opts(cflags_name, libs_name, ldflags_name) var lib_dir = get_clang_lib_dir(); if (!!cflags_name) { - ADD_FLAG(cflags_name, "-fsanitize=address,undefined"); + ADD_FLAG(cflags_name, "-fsanitize=address,undefined -fno-sanitize=function"); } if (!!libs_name) { if (TARGET_ARCH == 'x64') {