[libcxx-commits] [libcxx] [libcxx] adds way for clang-tidy to be built with non-default stdlib (PR #154573)

Christopher Di Bella via libcxx-commits libcxx-commits at lists.llvm.org
Wed Aug 20 10:13:02 PDT 2025


https://github.com/cjdb updated https://github.com/llvm/llvm-project/pull/154573

>From 921acdb79cd0401f91261eea4a04a0819e98505a Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Wed, 20 Aug 2025 16:43:22 +0000
Subject: [PATCH 1/2] [libcxx] adds way for clang-tidy to be built with
 non-default stdlib

The default standard library for Clang on Linux is libstdc++. As such,
testing libc++ with a clang-tidy that's been built with libc++ results
in a host of linker errors, because the libc++ clang-tidy plugin is
built with the compiler default.

This commit alleviates the problem by allowing the developer to choose
which implementation the plugin should be built with.
---
 libcxx/CMakeLists.txt                              | 3 +++
 libcxx/test/tools/clang_tidy_checks/CMakeLists.txt | 7 +++++++
 2 files changed, 10 insertions(+)

diff --git a/libcxx/CMakeLists.txt b/libcxx/CMakeLists.txt
index 85514cc7547a9..8c3b9c289f91a 100644
--- a/libcxx/CMakeLists.txt
+++ b/libcxx/CMakeLists.txt
@@ -186,6 +186,9 @@ cmake_dependent_option(LIBCXX_INSTALL_STATIC_LIBRARY
 cmake_dependent_option(LIBCXX_INSTALL_SHARED_LIBRARY
   "Install the shared libc++ library." ON
   "LIBCXX_ENABLE_SHARED;LIBCXX_INSTALL_LIBRARY" OFF)
+set(LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB "" CACHE STRING
+  "Builds the clang-tidy plugin with the specified standard library implementation. Must either be blank (use compiler default), 'libc++', or 'libstdc++'.
+   This should only be set when the clang-tidy binary is built with a different standard library implementation to the compiler's default choice.")
 
 option(LIBCXX_ABI_UNSTABLE "Use the unstable ABI of libc++. This is equivalent to specifying LIBCXX_ABI_VERSION=n, where n is the not-yet-stable version." OFF)
 if (LIBCXX_ABI_UNSTABLE)
diff --git a/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt b/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
index 521a60c0fc498..1f796fb4005b0 100644
--- a/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
+++ b/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
@@ -103,6 +103,13 @@ set(SOURCES
    )
 
 add_library(cxx-tidy MODULE ${SOURCES})
+if(LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB)
+  if(LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB STREQUAL "libc++" OR LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB STREQUAL "libstdc++")
+    target_compile_options(cxx-tidy PUBLIC -stdlib=${LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB})
+  else()
+    message(ERROR "${LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB} must either be empty, 'libc++', or 'libstdc++'.")
+  endif()
+endif()
 target_link_libraries(cxx-tidy clangTidy)
 
 set_target_properties(cxx-tidy PROPERTIES

>From 0ca1667ed13d60397d4a2bf1f1478246f54fd149 Mon Sep 17 00:00:00 2001
From: Christopher Di Bella <cjdb at google.com>
Date: Wed, 20 Aug 2025 17:11:21 +0000
Subject: [PATCH 2/2] gets this working for the buildbot script

---
 .../tools/clang_tidy_checks/CMakeLists.txt    |  8 +++-
 libcxx/utils/ci/run-buildbot                  | 38 +++++++++++++------
 2 files changed, 32 insertions(+), 14 deletions(-)

diff --git a/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt b/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
index 1f796fb4005b0..73f4264abee40 100644
--- a/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
+++ b/libcxx/test/tools/clang_tidy_checks/CMakeLists.txt
@@ -105,9 +105,13 @@ set(SOURCES
 add_library(cxx-tidy MODULE ${SOURCES})
 if(LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB)
   if(LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB STREQUAL "libc++" OR LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB STREQUAL "libstdc++")
-    target_compile_options(cxx-tidy PUBLIC -stdlib=${LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB})
+    if(CMAKE_CXX_COMPILER_ID MATCHES Clang)
+      target_compile_options(cxx-tidy PUBLIC -stdlib=${LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB})
+    else()
+      message(ERROR "LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB can only be set with Clang or AppleClang (currently using ${CMAKE_CXX_COMPILER_ID}).")
+    endif()
   else()
-    message(ERROR "${LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB} must either be empty, 'libc++', or 'libstdc++'.")
+    message(ERROR "LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB must either be empty, 'libc++', or 'libstdc++' (got '${LIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB}').")
   endif()
 endif()
 target_link_libraries(cxx-tidy clangTidy)
diff --git a/libcxx/utils/ci/run-buildbot b/libcxx/utils/ci/run-buildbot
index 57ecf1e49dbf2..6a34b26ad42c5 100755
--- a/libcxx/utils/ci/run-buildbot
+++ b/libcxx/utils/ci/run-buildbot
@@ -20,25 +20,34 @@ cat <<EOF
 Usage:
 ${PROGNAME} [options] <BUILDER>
 
-[-h|--help]         Display this help and exit.
+[-h|--help]                  Display this help and exit.
 
---llvm-root <DIR>   Path to the root of the LLVM monorepo. By default, we try
-                    to figure it out based on the current working directory.
+--llvm-root <DIR>            Path to the root of the LLVM monorepo. By default, we try
+                             to figure it out based on the current working directory.
 
---build-dir <DIR>   The directory to use for building the library. By default,
-                    this is '<llvm-root>/build/<builder>'.
+--build-dir <DIR>            The directory to use for building the library. By default,
+                             this is '<llvm-root>/build/<builder>'.
+
+--clang-tidy-stdlib <STDLIB> Builds the clang-tidy plugin with the specified standard
+                             library implementation. By default, this is empty. Valid
+                             options are empty, 'libc++', and 'libstdc++'. You should
+                             only set this option when clang-tidy needs to be linked
+                             against a different standard library implementation to
+                             the compiler's default choice.
+
+                             This option only works for Clang.
 
 Environment variables
-CC                  The C compiler to use, this value is used by CMake. This
-                    variable is optional.
+CC                           The C compiler to use, this value is used by CMake. This
+                             variable is optional.
 
-CXX                 The C++ compiler to use, this value is used by CMake. This
-                    variable is optional.
+CXX                          The C++ compiler to use, this value is used by CMake. This
+                             variable is optional.
 
-CMAKE               The CMake binary to use. This variable is optional.
+CMAKE                        The CMake binary to use. This variable is optional.
 
-CLANG_FORMAT        The clang-format binary to use when generating the format
-                    ignore list.
+CLANG_FORMAT                 The clang-format binary to use when generating the format
+                             ignore list.
 
 EOF
 }
@@ -62,6 +71,10 @@ while [[ $# -gt 0 ]]; do
             BUILD_DIR="${2}"
             shift; shift
             ;;
+        --clang-tidy-stdlib)
+            BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB="${2}"
+            shift; shift
+            ;;
         *)
             BUILDER="${1}"
             shift
@@ -136,6 +149,7 @@ function generate-cmake-base() {
           -DCMAKE_BUILD_TYPE=RelWithDebInfo \
           -DCMAKE_INSTALL_PREFIX="${INSTALL_DIR}" \
           -DLIBCXX_ENABLE_WERROR=YES \
+          -DLIBCXX_BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB="${BUILD_CLANG_TIDY_PLUGIN_WITH_STDLIB}" \
           -DLIBCXXABI_ENABLE_WERROR=YES \
           -DLIBUNWIND_ENABLE_WERROR=YES \
           -DCMAKE_INSTALL_MESSAGE=NEVER \



More information about the libcxx-commits mailing list