[llvm] [cmake][Windows] Do not strip library prefixes on Windows (PR #226635)

Alan Zhao via llvm-commits llvm-commits at lists.llvm.org
Sat Sep 26 00:29:15 PDT 2026


https://github.com/alanzhao1 updated https://github.com/llvm/llvm-project/pull/226635

>From 44a9be5423e9229723acd597c9092992eea5ad35 Mon Sep 17 00:00:00 2001
From: Alan Zhao <ayzhao at google.com>
Date: Fri, 25 Sep 2026 21:16:26 -0700
Subject: [PATCH] [cmake][Windows] Do not strip library prefixes on Windows

On Windows, libraries should not have their prefixes stripped because
unlike Unix, MSVC-style linkers do not implicitly add back the prefixes
back when searching for libraries to link (e.g. on Unix we pass `-lxml2`
but on Windows we need to pass `libxml2s.lib`. In CMake versions prior
to 3.29.0 this isn't a problem because `CMAKE_FIND_LIBRARY_SUFFIXES` is
empty on Windodws, but `CMAKE_FIND_LIBRARY_SUFFIXES` contains the string
'lib' on Windows for CMake >= 3.29.0 [0]. Therefore, we need to
explicitly prevent stripping library prefixes in `get_library_name(...)`
on Windows.

This was [originally reported by Chrome](https://crbug.com/566234899) in
the Rust builds as the Rust build consumes the output of
`llvm-config --system-libs` as the source of truth for system libraries
and fails to link because it was trying to search for the wrong file
containing `libxml2`.

[0]: https://gitlab.kitware.com/cmake/cmake/-/blob/v3.31.12/Modules/Platform/Windows.cmake#L16-24
---
 llvm/cmake/modules/GetLibraryName.cmake | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/llvm/cmake/modules/GetLibraryName.cmake b/llvm/cmake/modules/GetLibraryName.cmake
index 13c0080671a3c6..5aa42c343a3d3f 100644
--- a/llvm/cmake/modules/GetLibraryName.cmake
+++ b/llvm/cmake/modules/GetLibraryName.cmake
@@ -5,7 +5,9 @@ function(get_library_name path name)
   set(suffixes ${CMAKE_FIND_LIBRARY_SUFFIXES})
   list(FILTER prefixes EXCLUDE REGEX "^\\s*$")
   list(FILTER suffixes EXCLUDE REGEX "^\\s*$")
-  if(prefixes)
+  # Do not strip the "lib" prefix for Windows because MSVC-style linkers don't
+  # implicitly add the "lib" prefix.
+  if(prefixes AND NOT Win32)
     string(REPLACE ";" "|" prefixes "${prefixes}")
     string(REGEX REPLACE "^(${prefixes})" "" path ${path})
   endif()



More information about the llvm-commits mailing list