Skip to content

Reapply "[libc] Finetune libc.src.__support.OSUtil.osutil dependency." (#190033)#190065

Merged
lntue merged 3 commits intollvm:mainfrom
lntue:osutil
Apr 1, 2026
Merged

Reapply "[libc] Finetune libc.src.__support.OSUtil.osutil dependency." (#190033)#190065
lntue merged 3 commits intollvm:mainfrom
lntue:osutil

Conversation

@lntue
Copy link
Copy Markdown
Contributor

@lntue lntue commented Apr 1, 2026

This reverts commit 84f23eb and fix GPU builds.

@llvmbot llvmbot added the libc label Apr 1, 2026
@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Apr 1, 2026

@llvm/pr-subscribers-libc

Author: lntue

Changes

This reverts commit 84f23eb and fix GPU builds.


Full diff: https://github.com/llvm/llvm-project/pull/190065.diff

9 Files Affected:

  • (modified) libc/cmake/modules/LLVMLibCCompileOptionRules.cmake (+4)
  • (modified) libc/config/config.json (+6)
  • (modified) libc/src/__support/CMakeLists.txt (+17-9)
  • (modified) libc/src/__support/time/CMakeLists.txt (+3-1)
  • (modified) libc/src/assert/gpu/__assert_fail.cpp (+1)
  • (modified) libc/src/time/linux/CMakeLists.txt (+6)
  • (modified) libc/src/unistd/CMakeLists.txt (+6)
  • (modified) libc/test/UnitTest/CMakeLists.txt (+19-5)
  • (modified) libc/test/UnitTest/TestLogger.cpp (+21-2)
diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index cb1a3ec534a97..426fcef7e36f4 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -182,6 +182,10 @@ function(_get_compile_options_from_config output_var)
     libc_add_definition(config_options "LIBC_COPT_PRINTF_DISABLE_BITINT")
   endif()
 
+  if(LIBC_COPT_USE_C_ASSERT)
+    list(APPEND config_options "-DLIBC_COPT_USE_C_ASSERT")
+  endif()
+
   set(${output_var} ${config_options} PARENT_SCOPE)
 endfunction(_get_compile_options_from_config)
 
diff --git a/libc/config/config.json b/libc/config/config.json
index 39cb2d8cfa331..506526bbb1fc4 100644
--- a/libc/config/config.json
+++ b/libc/config/config.json
@@ -186,5 +186,11 @@
       "value": false,
       "doc": "Trap with SIGFPE when feraiseexcept is called with unmasked floating point exceptions, similar to glibc's behavior.  This is currently working only on x86 with SSE."
     }
+  },
+  "assert": {
+    "LIBC_COPT_USE_C_ASSERT": {
+      "value": false,
+      "doc": "Use the system assert macro for LIBC_ASSERT."
+    }
   }
 }
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 6a3d90e531599..08a4db8e49a73 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -369,15 +369,23 @@ add_header_library(
     libc.src.__support.macros.properties.types
 )
 
-add_header_library(
-  libc_assert
-  HDRS
-    libc_assert.h
-  DEPENDS
-    .integer_to_string
-    libc.src.__support.OSUtil.osutil
-    libc.src.__support.macros.optimization
-)
+if(LIBC_COPT_USE_C_ASSERT OR NOT LLVM_LIBC_FULL_BUILD)
+  add_header_library(
+    libc_assert
+    HDRS
+      libc_assert.h
+  )
+else()
+  add_header_library(
+    libc_assert
+    HDRS
+      libc_assert.h
+    DEPENDS
+      .integer_to_string
+      libc.src.__support.OSUtil.osutil
+      libc.src.__support.macros.optimization
+  )
+endif()
 
 add_header_library(
   hash
diff --git a/libc/src/__support/time/CMakeLists.txt b/libc/src/__support/time/CMakeLists.txt
index 3cc3fd873220a..f007f95bbee42 100644
--- a/libc/src/__support/time/CMakeLists.txt
+++ b/libc/src/__support/time/CMakeLists.txt
@@ -7,9 +7,11 @@ add_header_library(
     libc.hdr.types.time_t
 )
 
-if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
+if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS}
+   AND TARGET libc.src.__support.OSUtil.osutil)
   add_subdirectory(${LIBC_TARGET_OS})
 else()
+  message(STATUS "Skip libc.src.__support.time.* targets.")
   return()
 endif()
 
diff --git a/libc/src/assert/gpu/__assert_fail.cpp b/libc/src/assert/gpu/__assert_fail.cpp
index 5b772426f28bc..29bb9f9225a44 100644
--- a/libc/src/assert/gpu/__assert_fail.cpp
+++ b/libc/src/assert/gpu/__assert_fail.cpp
@@ -10,6 +10,7 @@
 
 #include "src/__support/CPP/atomic.h"
 #include "src/__support/GPU/utils.h"
+#include "src/__support/common.h"
 #include "src/__support/libc_assert.h"
 #include "src/__support/macros/config.h"
 #include "src/stdlib/abort.h"
diff --git a/libc/src/time/linux/CMakeLists.txt b/libc/src/time/linux/CMakeLists.txt
index 6ea04597063cb..623f01eb0d78c 100644
--- a/libc/src/time/linux/CMakeLists.txt
+++ b/libc/src/time/linux/CMakeLists.txt
@@ -1,3 +1,9 @@
+if(NOT TARGET libc.src.__support.OSUtil.osutil)
+  message(STATUS "libc.src.__support.OSUtil.osutil is not available. "
+                  "Skip libc.src.time.linux.* targets.")
+  return()
+endif()
+
 add_entrypoint_object(
   timespec_get
   SRCS
diff --git a/libc/src/unistd/CMakeLists.txt b/libc/src/unistd/CMakeLists.txt
index 4d78b4a7fbc84..5e6df74ac92db 100644
--- a/libc/src/unistd/CMakeLists.txt
+++ b/libc/src/unistd/CMakeLists.txt
@@ -1,3 +1,9 @@
+if(NOT TARGET libc.src.__support.OSUtil.osutil)
+  message(STATUS "libc.src.__support.OSUtil.osutil is not avaiable. "
+                 "Skip unistd targets.")
+  return()
+endif()
+
 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
   add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
 endif()
diff --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt
index 54e41ece5f4d9..aa245b2234793 100644
--- a/libc/test/UnitTest/CMakeLists.txt
+++ b/libc/test/UnitTest/CMakeLists.txt
@@ -3,7 +3,7 @@ function(add_unittest_framework_library name)
     "TEST_LIB"
     "" # No optional arguments
     "" # No single value arguments
-    "SRCS;HDRS;DEPENDS" # Multi value arguments
+    "SRCS;HDRS;DEPENDS;COMPILE_OPTIONS" # Multi value arguments
     ${ARGN}
   )
   if(NOT TEST_LIB_SRCS)
@@ -30,14 +30,17 @@ function(add_unittest_framework_library name)
     # making LibcFPExceptionHelpers and LibcDeathTestExecutors hermetic.
     set(LLVM_LIBC_FULL_BUILD "")
     _get_common_test_compile_options(compile_options "" "")
-    target_compile_options(${name}.unit PRIVATE ${compile_options})
     set(LLVM_LIBC_FULL_BUILD ON)
   else()
     _get_common_test_compile_options(compile_options "" "")
-    target_compile_options(${name}.unit PRIVATE ${compile_options})
   endif()
 
-  _get_hermetic_test_compile_options(compile_options "")
+  if (TEST_LIB_COMPILE_OPTIONS)
+    list(APPEND compile_options ${TEST_LIB_COMPILE_OPTIONS})
+  endif()
+  target_compile_options(${name}.unit PRIVATE ${compile_options})
+
+    _get_hermetic_test_compile_options(compile_options "")
   target_include_directories(${name}.hermetic PRIVATE ${LIBC_INCLUDE_DIR})
   target_compile_options(${name}.hermetic PRIVATE ${compile_options} -nostdinc++)
 
@@ -57,6 +60,15 @@ function(add_unittest_framework_library name)
   endif()
 endfunction()
 
+if (NOT TARGET libc.src.__support.OSUtil.osutil OR NOT LLVM_LIBC_FULL_BUILD)
+  message(STATUS "TestLogger will use system libc's stdio to print.")
+  set(test_logger_compile_options "-DLIBC_TEST_USE_SYSTEM_PRINTF")
+  set(test_logger_osutil "")
+else()
+  set(test_logger_compile_options "")
+  set(test_logger_osutil "libc.src.__support.OSUtil.osutil")
+endif()
+
 add_unittest_framework_library(
   LibcTest
   SRCS
@@ -68,6 +80,8 @@ add_unittest_framework_library(
     LibcTest.h
     Test.h
     TestLogger.h
+  COMPILE_OPTIONS
+    ${test_logger_compile_options}
   DEPENDS
     libc.hdr.stdint_proxy
     libc.src.__support.big_int
@@ -78,8 +92,8 @@ add_unittest_framework_library(
     libc.src.__support.fixed_point.fx_rep
     libc.src.__support.macros.properties.compiler
     libc.src.__support.macros.properties.types
-    libc.src.__support.OSUtil.osutil
     libc.src.__support.uint128
+    ${test_logger_osutil}
 )
 
 set(libc_death_test_srcs LibcDeathTestExecutors.cpp)
diff --git a/libc/test/UnitTest/TestLogger.cpp b/libc/test/UnitTest/TestLogger.cpp
index 3d95d48a5f5a2..170dd8704a923 100644
--- a/libc/test/UnitTest/TestLogger.cpp
+++ b/libc/test/UnitTest/TestLogger.cpp
@@ -2,19 +2,38 @@
 #include "hdr/stdint_proxy.h"
 #include "src/__support/CPP/string.h"
 #include "src/__support/CPP/string_view.h"
-#include "src/__support/OSUtil/io.h" // write_to_stderr
 #include "src/__support/big_int.h"   // is_big_int
 #include "src/__support/macros/config.h"
 #include "src/__support/macros/properties/types.h" // LIBC_TYPES_HAS_INT128
 #include "src/__support/uint128.h"
 
+#ifdef LIBC_TEST_USE_SYSTEM_PRINTF
+
+#include <stdio.h>
+
+namespace LIBC_NAMESPACE_DECL {
+namespace testing {
+
+void write_to_stderr(cpp::string_view str) {
+  fprintf(stderr, "%.*s", static_cast<int>(str.size()), str.data());
+}
+
+} // namespace testing
+} // namespace LIBC_NAMESPACE_DECL
+
+#else // !LIBC_TEST_USE_SYSTEM_PRINTF
+
+#include "src/__support/OSUtil/io.h" // write_to_stderr
+
+#endif // LIBC_TEST_USE_SYSTEM_PRINTF
+
 namespace LIBC_NAMESPACE_DECL {
 namespace testing {
 
 // cpp::string_view specialization
 template <>
 TestLogger &TestLogger::operator<< <cpp::string_view>(cpp::string_view str) {
-  LIBC_NAMESPACE::write_to_stderr(str);
+  write_to_stderr(str);
   return *this;
 }
 

@lntue lntue merged commit e87ea84 into llvm:main Apr 1, 2026
33 checks passed
@lntue lntue deleted the osutil branch April 1, 2026 22:38
joaovam pushed a commit to joaovam/llvm-project that referenced this pull request Apr 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants