[llvm] [Runtimes] Allow HandleLibc.cmake to be called multiple times (PR #193540)

Joseph Huber via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 22 09:55:26 PDT 2026


https://github.com/jhuber6 created https://github.com/llvm/llvm-project/pull/193540

Summary:
This needs to check to see if it's already been called now that we want
to use it more places than just libcxx.


>From daf3c671e52f7c3842c22fce85afaf548463454c Mon Sep 17 00:00:00 2001
From: Joseph Huber <huberjn at outlook.com>
Date: Wed, 22 Apr 2026 11:54:01 -0500
Subject: [PATCH] [Runtimes] Allow HandleLibc.cmake to be called multiple times

Summary:
This needs to check to see if it's already been called now that we want
to use it more places than just libcxx.
---
 runtimes/cmake/Modules/HandleLibC.cmake | 62 +++++++++++++------------
 1 file changed, 32 insertions(+), 30 deletions(-)

diff --git a/runtimes/cmake/Modules/HandleLibC.cmake b/runtimes/cmake/Modules/HandleLibC.cmake
index aa076441345d6..2825bec4f46ee 100644
--- a/runtimes/cmake/Modules/HandleLibC.cmake
+++ b/runtimes/cmake/Modules/HandleLibC.cmake
@@ -10,42 +10,44 @@
 
 include_guard(GLOBAL)
 
-set(RUNTIMES_SUPPORTED_C_LIBRARIES system llvm-libc picolibc newlib)
-set(RUNTIMES_USE_LIBC "system" CACHE STRING "Specify C library to use. Supported values are ${RUNTIMES_SUPPORTED_C_LIBRARIES}.")
-if (NOT "${RUNTIMES_USE_LIBC}" IN_LIST RUNTIMES_SUPPORTED_C_LIBRARIES)
-  message(FATAL_ERROR "Unsupported C library: '${RUNTIMES_USE_LIBC}'. Supported values are ${RUNTIMES_SUPPORTED_C_LIBRARIES}.")
-endif()
+if(NOT TARGET runtimes-libc-headers)
+  set(RUNTIMES_SUPPORTED_C_LIBRARIES system llvm-libc picolibc newlib)
+  set(RUNTIMES_USE_LIBC "system" CACHE STRING "Specify C library to use. Supported values are ${RUNTIMES_SUPPORTED_C_LIBRARIES}.")
+  if (NOT "${RUNTIMES_USE_LIBC}" IN_LIST RUNTIMES_SUPPORTED_C_LIBRARIES)
+    message(FATAL_ERROR "Unsupported C library: '${RUNTIMES_USE_LIBC}'. Supported values are ${RUNTIMES_SUPPORTED_C_LIBRARIES}.")
+  endif()
 
 # Link against a system-provided libc
-if (RUNTIMES_USE_LIBC STREQUAL "system")
-  add_library(runtimes-libc-headers INTERFACE)
+  if (RUNTIMES_USE_LIBC STREQUAL "system")
+    add_library(runtimes-libc-headers INTERFACE)
 
-  add_library(runtimes-libc-static INTERFACE)
-  add_library(runtimes-libc-shared INTERFACE)
+    add_library(runtimes-libc-static INTERFACE)
+    add_library(runtimes-libc-shared INTERFACE)
 
 # Link against the in-tree LLVM libc
-elseif (RUNTIMES_USE_LIBC STREQUAL "llvm-libc")
-  add_library(runtimes-libc-headers INTERFACE)
-  target_link_libraries(runtimes-libc-headers INTERFACE libc-headers)
-  check_cxx_compiler_flag(-nostdlibinc CXX_SUPPORTS_NOSTDLIBINC_FLAG)
-  if(CXX_SUPPORTS_NOSTDLIBINC_FLAG)
-    target_compile_options(runtimes-libc-headers INTERFACE "-nostdlibinc")
-    if(LIBC_KERNEL_HEADERS)
-      target_compile_options(runtimes-libc-headers INTERFACE "-idirafter${LIBC_KERNEL_HEADERS}")
+  elseif (RUNTIMES_USE_LIBC STREQUAL "llvm-libc")
+    add_library(runtimes-libc-headers INTERFACE)
+    target_link_libraries(runtimes-libc-headers INTERFACE libc-headers)
+    check_cxx_compiler_flag(-nostdlibinc CXX_SUPPORTS_NOSTDLIBINC_FLAG)
+    if(CXX_SUPPORTS_NOSTDLIBINC_FLAG)
+      target_compile_options(runtimes-libc-headers INTERFACE "-nostdlibinc")
+      if(LIBC_KERNEL_HEADERS)
+        target_compile_options(runtimes-libc-headers INTERFACE "-idirafter${LIBC_KERNEL_HEADERS}")
+      endif()
     endif()
-  endif()
 
-  add_library(runtimes-libc-static INTERFACE)
-  if (TARGET libc)
-    target_link_libraries(runtimes-libc-static INTERFACE libc)
-  endif()
-  if (TARGET libm)
-    target_link_libraries(runtimes-libc-static INTERFACE libm)
-  endif()
-  if (CXX_SUPPORTS_NOLIBC_FLAG)
-    target_link_options(runtimes-libc-static INTERFACE "-nolibc")
-  endif()
+    add_library(runtimes-libc-static INTERFACE)
+    if (TARGET libc)
+      target_link_libraries(runtimes-libc-static INTERFACE libc)
+    endif()
+    if (TARGET libm)
+      target_link_libraries(runtimes-libc-static INTERFACE libm)
+    endif()
+    if (CXX_SUPPORTS_NOLIBC_FLAG)
+      target_link_options(runtimes-libc-static INTERFACE "-nolibc")
+    endif()
 
-  # TODO: There's no support for building LLVM libc as a shared library yet.
-  add_library(runtimes-libc-shared INTERFACE)
+    # TODO: There's no support for building LLVM libc as a shared library yet.
+    add_library(runtimes-libc-shared INTERFACE)
+  endif()
 endif()



More information about the llvm-commits mailing list