[libc-commits] [libc] [libc] Add libc_set_definition and libc_add_definition to properly pass definitions to MSVC. (PR #189664)

via libc-commits libc-commits at lists.llvm.org
Tue Mar 31 06:36:01 PDT 2026


https://github.com/lntue created https://github.com/llvm/llvm-project/pull/189664

None

>From 52965104070ff2f7e60d62b47c4197cd8a96bcec Mon Sep 17 00:00:00 2001
From: Tue Ly <lntue.h at gmail.com>
Date: Tue, 31 Mar 2026 13:30:51 +0000
Subject: [PATCH] [libc] Add libc_set_definition and libc_add_definition to
 properly pass definitions to MSVC.

---
 .../modules/LLVMLibCCompileOptionRules.cmake  | 87 +++++++++++--------
 libc/cmake/modules/LLVMLibCTestRules.cmake    | 16 +++-
 libc/src/__support/CMakeLists.txt             |  3 +-
 libc/src/__support/HashTable/CMakeLists.txt   |  2 +-
 .../__support/threads/linux/CMakeLists.txt    | 11 +--
 libc/src/setjmp/aarch64/CMakeLists.txt        | 12 ++-
 libc/src/stdio/CMakeLists.txt                 |  3 +-
 libc/src/stdio/printf_core/CMakeLists.txt     | 22 ++---
 libc/src/stdio/scanf_core/CMakeLists.txt      |  4 +-
 libc/test/src/stdio/CMakeLists.txt            | 18 ++--
 libc/test/src/stdio/scanf_core/CMakeLists.txt |  2 +-
 libc/test/src/stdlib/CMakeLists.txt           |  2 +-
 12 files changed, 105 insertions(+), 77 deletions(-)

diff --git a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
index 2e2c5e3ad7471..cb1a3ec534a97 100644
--- a/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
+++ b/libc/cmake/modules/LLVMLibCCompileOptionRules.cmake
@@ -10,6 +10,31 @@ if(NOT DEFINED LLVM_LIBC_COMPILER_IS_GCC_COMPATIBLE)
   endif()
 endif()
 
+function(libc_add_definition output_var def)
+  if(MSVC)
+    list(APPEND ${output_var} "/D${def}")
+  else()
+    list(APPEND ${output_var} "-D${def}")
+  endif()
+
+  set(${output_var} ${${output_var}} PARENT_SCOPE)
+endfunction()
+
+function(libc_set_definition output_var)
+  set(defs "")
+  if(MSVC)
+    foreach(def IN LISTS ARGN)
+      list(APPEND defs "/D${def}")
+    endforeach()
+  else()
+    foreach(def IN LISTS ARGN)
+      list(APPEND defs "-D${def}")
+    endforeach()
+  endif()
+
+  set(${output_var} "${defs}" PARENT_SCOPE)
+endfunction()
+
 function(_get_compile_options_from_flags output_var)
   set(compile_options "")
 
@@ -45,9 +70,6 @@ function(_get_compile_options_from_flags output_var)
         list(APPEND compile_options "-D__LIBC_USE_BUILTIN_ROUNDEVEN")
       endif()
     endif()
-    if(ADD_EXPLICIT_SIMD_OPT_FLAG)
-      list(APPEND compile_options "-D__LIBC_EXPLICIT_SIMD_OPT")
-    endif()
     if(ADD_MISC_MATH_BASIC_OPS_OPT_FLAG)
       list(APPEND compile_options "-D__LIBC_MISC_MATH_BASIC_OPS_OPT")
       if(LIBC_COMPILER_HAS_BUILTIN_FMAX_FMIN)
@@ -66,9 +88,10 @@ function(_get_compile_options_from_flags output_var)
     if(ADD_FMA_FLAG)
       list(APPEND compile_options "/arch:AVX2")
     endif()
-    if(ADD_EXPLICIT_SIMD_OPT_FLAG)
-      list(APPEND compile_options "/D__LIBC_EXPLICIT_SIMD_OPT")
-    endif()
+  endif()
+
+  if(ADD_EXPLICIT_SIMD_OPT_FLAG)
+    libc_add_definition(compile_options "__LIBC_EXPLICIT_SIMD_OPT")
   endif()
 
   set(${output_var} ${compile_options} PARENT_SCOPE)
@@ -78,89 +101,85 @@ function(_get_compile_options_from_config output_var)
   set(config_options "")
 
   if(LIBC_CONF_STRTOFLOAT_DISABLE_EISEL_LEMIRE)
-    list(APPEND config_options "-DLIBC_COPT_STRTOFLOAT_DISABLE_EISEL_LEMIRE")
+    libc_add_definition(config_options "LIBC_COPT_STRTOFLOAT_DISABLE_EISEL_LEMIRE")
   endif()
 
   if(LIBC_CONF_STRTOFLOAT_DISABLE_SIMPLE_DECIMAL_CONVERSION)
-    list(APPEND config_options "-DLIBC_COPT_STRTOFLOAT_DISABLE_SIMPLE_DECIMAL_CONVERSION")
+    libc_add_definition(config_options "LIBC_COPT_STRTOFLOAT_DISABLE_SIMPLE_DECIMAL_CONVERSION")
   endif()
 
   if(LIBC_CONF_STRTOFLOAT_DISABLE_CLINGER_FAST_PATH)
-    list(APPEND config_options "-DLIBC_COPT_STRTOFLOAT_DISABLE_CLINGER_FAST_PATH")
+    libc_add_definition(config_options "LIBC_COPT_STRTOFLOAT_DISABLE_CLINGER_FAST_PATH")
   endif()
 
   if(LIBC_CONF_QSORT_IMPL)
-    list(APPEND config_options "-DLIBC_QSORT_IMPL=${LIBC_CONF_QSORT_IMPL}")
+    libc_add_definition(config_options "LIBC_QSORT_IMPL=${LIBC_CONF_QSORT_IMPL}")
   endif()
 
-  list(APPEND config_options "-DLIBC_COPT_STRING_LENGTH_IMPL=${LIBC_CONF_STRING_LENGTH_IMPL}")
-  list(APPEND config_options "-DLIBC_COPT_FIND_FIRST_CHARACTER_IMPL=${LIBC_CONF_FIND_FIRST_CHARACTER_IMPL}")
+  libc_add_definition(config_options "LIBC_COPT_STRING_LENGTH_IMPL=${LIBC_CONF_STRING_LENGTH_IMPL}")
+  libc_add_definition(config_options "LIBC_COPT_FIND_FIRST_CHARACTER_IMPL=${LIBC_CONF_FIND_FIRST_CHARACTER_IMPL}")
 
   if(LIBC_CONF_MEMSET_X86_USE_SOFTWARE_PREFETCHING)
-    list(APPEND config_options "-DLIBC_COPT_MEMSET_X86_USE_SOFTWARE_PREFETCHING")
+    libc_add_definition(config_options "LIBC_COPT_MEMSET_X86_USE_SOFTWARE_PREFETCHING")
   endif()
 
   if(LIBC_CONF_COPT_MEMCPY_X86_USE_NTA_STORES)
-    list(APPEND config_options "-DLIBC_COPT_MEMCPY_X86_USE_NTA_STORES")
+    libc_add_definition(config_options "LIBC_COPT_MEMCPY_X86_USE_NTA_STORES")
   endif()
 
   if(LIBC_TYPES_TIME_T_IS_32_BIT AND LLVM_LIBC_FULL_BUILD)
-    list(APPEND config_options "-DLIBC_TYPES_TIME_T_IS_32_BIT")
+    libc_add_definition(config_options "LIBC_TYPES_TIME_T_IS_32_BIT")
   endif()
 
   if(LIBC_ADD_NULL_CHECKS)
-    list(APPEND config_options "-DLIBC_ADD_NULL_CHECKS")
+    libc_add_definition(config_options "LIBC_ADD_NULL_CHECKS")
   endif()
 
   if(NOT "${LIBC_CONF_FREXP_INF_NAN_EXPONENT}" STREQUAL "")
-    list(APPEND config_options "-DLIBC_FREXP_INF_NAN_EXPONENT=${LIBC_CONF_FREXP_INF_NAN_EXPONENT}")
+    libc_add_definition(config_options "LIBC_FREXP_INF_NAN_EXPONENT=${LIBC_CONF_FREXP_INF_NAN_EXPONENT}")
   endif()
 
   if(LIBC_CONF_MATH_OPTIMIZATIONS)
-    list(APPEND config_options "-DLIBC_MATH=${LIBC_CONF_MATH_OPTIMIZATIONS}")
+    libc_add_definition(config_options "LIBC_MATH=${LIBC_CONF_MATH_OPTIMIZATIONS}")
     if(LIBC_CONF_MATH_OPTIMIZATIONS MATCHES "LIBC_MATH_NO_ERRNO")
       list(APPEND config_options "-fno-math-errno")
     endif()
   endif()
 
   if(LIBC_CONF_ERRNO_MODE)
-    list(APPEND config_options "-DLIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}")
+    libc_add_definition(config_options "LIBC_ERRNO_MODE=${LIBC_CONF_ERRNO_MODE}")
   endif()
 
   if(LIBC_CONF_THREAD_MODE)
-    list(APPEND config_options "-DLIBC_THREAD_MODE=${LIBC_CONF_THREAD_MODE}")
+    libc_add_definition(config_options "LIBC_THREAD_MODE=${LIBC_CONF_THREAD_MODE}")
   endif()
 
   if(LIBC_CONF_TRAP_ON_RAISE_FP_EXCEPT)
-    list(APPEND config_options "-DLIBC_TRAP_ON_RAISE_FP_EXCEPT")
+    libc_add_definition(config_options "LIBC_TRAP_ON_RAISE_FP_EXCEPT")
   endif()
 
   if(LIBC_CONF_WCTYPE_MODE)
-    list(APPEND config_options "-DLIBC_CONF_WCTYPE_MODE=${LIBC_CONF_WCTYPE_MODE}")
+    libc_add_definition(config_options "LIBC_CONF_WCTYPE_MODE=${LIBC_CONF_WCTYPE_MODE}")
   endif()
 
   if(LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT)
-    list(APPEND config_options "-DLIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT=${LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT}")
+    libc_add_definition(config_options "LIBC_COPT_RAW_MUTEX_DEFAULT_SPIN_COUNT=${LIBC_CONF_RAW_MUTEX_DEFAULT_SPIN_COUNT}")
   endif()
 
   if(LIBC_CONF_MATH_USE_SYSTEM_FENV)
-    if(MSVC)
-      list(APPEND config_options "/DLIBC_MATH_USE_SYSTEM_FENV")
-    else()
-      list(APPEND config_options "-DLIBC_MATH_USE_SYSTEM_FENV")
-    endif()
+    libc_add_definition(config_options "LIBC_MATH_USE_SYSTEM_FENV")
   endif()
 
   if(LIBC_CONF_CTYPE_SMALLER_ASCII)
-    list(APPEND config_options "-DLIBC_COPT_CTYPE_SMALLER_ASCII")
+    libc_add_definition(config_options "LIBC_COPT_CTYPE_SMALLER_ASCII")
   endif()
 
   if(LIBC_CONF_PRINTF_DISABLE_WIDE)
-    list(APPEND config_options "-DLIBC_COPT_PRINTF_DISABLE_WIDE")
+    libc_add_definition(config_options "LIBC_COPT_PRINTF_DISABLE_WIDE")
   endif()
 
   if(LIBC_COPT_PRINTF_DISABLE_BITINT)
-    list(APPEND config_options "-DLIBC_COPT_PRINTF_DISABLE_BITINT")
+    libc_add_definition(config_options "LIBC_COPT_PRINTF_DISABLE_BITINT")
   endif()
 
   set(${output_var} ${config_options} PARENT_SCOPE)
@@ -173,13 +192,13 @@ function(_get_compile_options_from_arch output_var)
   set(config_options "")
 
   if (LIBC_TARGET_OS_IS_BAREMETAL)
-    list(APPEND config_options "-DLIBC_TARGET_OS_IS_BAREMETAL")
+    libc_add_definition(config_options "LIBC_TARGET_OS_IS_BAREMETAL")
   endif()
   if (LIBC_TARGET_OS_IS_GPU)
-    list(APPEND config_options "-DLIBC_TARGET_OS_IS_GPU")
+    libc_add_definition(config_options "LIBC_TARGET_OS_IS_GPU")
   endif()
   if (LIBC_TARGET_OS_IS_UEFI)
-    list(APPEND config_options "-DLIBC_TARGET_OS_IS_UEFI")
+    libc_add_definition(config_options "LIBC_TARGET_OS_IS_UEFI")
   endif()
 
   set(${output_var} ${config_options} PARENT_SCOPE)
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index e25f739408b99..f08d9012ac771 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -5,7 +5,11 @@ function(_get_common_test_compile_options output_var c_test flags)
 
   # Death test executor is only available in Linux for now.
   if(NOT ${LIBC_TARGET_OS} STREQUAL "linux")
-    list(REMOVE_ITEM config_flags "-DLIBC_ADD_NULL_CHECKS")
+    if (MSVC)
+      list(REMOVE_ITEM config_flags "/DLIBC_ADD_NULL_CHECKS")
+    else()
+      list(REMOVE_ITEM config_flags "-DLIBC_ADD_NULL_CHECKS")
+    endif()
   endif()
 
   set(compile_options
@@ -68,11 +72,15 @@ endfunction()
 
 function(_get_hermetic_test_compile_options output_var)
   _get_common_test_compile_options(compile_options "" "")
-  list(APPEND compile_options "-DLIBC_TEST=HERMETIC")
+  libc_add_definition(compile_options "LIBC_TEST=HERMETIC")
 
   # null check tests are death tests, remove from hermetic tests for now.
   if(LIBC_ADD_NULL_CHECKS)
-    list(REMOVE_ITEM compile_options "-DLIBC_ADD_NULL_CHECKS")
+    if(MSVC)
+      list(REMOVE_ITEM compile_options "/DLIBC_ADD_NULL_CHECKS")
+    else()
+      list(REMOVE_ITEM compile_options "-DLIBC_ADD_NULL_CHECKS")
+    endif()
   endif()
 
   # The GPU build requires overriding the default CMake triple and architecture.
@@ -229,7 +237,7 @@ function(create_libc_unittest fq_target_name)
 
   _get_common_test_compile_options(compile_options "${LIBC_UNITTEST_C_TEST}"
                                    "${LIBC_UNITTEST_FLAGS}")
-  list(APPEND compile_options "-DLIBC_TEST=UNIT")
+  libc_add_definition(compile_options "LIBC_TEST=UNIT")
   # TODO: Ideally we would have a separate function for link options.
   set(link_options
     ${compile_options}
diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 19b3a1d554f1c..6a3d90e531599 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -59,6 +59,7 @@ add_header_library(
     .freetrie
 )
 
+libc_set_definition(libc_freelist_malloc_size "LIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}")
 add_object_library(
   freelist_heap
   SRCS
@@ -66,7 +67,7 @@ add_object_library(
   HDRS
     freelist_heap.h
   COMPILE_OPTIONS
-    -DLIBC_FREELIST_MALLOC_SIZE=${LIBC_CONF_FREELIST_MALLOC_BUFFER_SIZE}
+    ${libc_freelist_malloc_size}
   DEPENDS
     .block
     .freelist
diff --git a/libc/src/__support/HashTable/CMakeLists.txt b/libc/src/__support/HashTable/CMakeLists.txt
index 82e88c02ae443..6dfe2391e7316 100644
--- a/libc/src/__support/HashTable/CMakeLists.txt
+++ b/libc/src/__support/HashTable/CMakeLists.txt
@@ -14,7 +14,7 @@ add_header_library(
 list(FIND TARGET_ENTRYPOINT_NAME_LIST getrandom getrandom_index)
 if (NOT ${getrandom_index} EQUAL -1)
   message(STATUS "Using getrandom for hashtable randomness")
-  set(randomness_compile_flags -DLIBC_HASHTABLE_USE_GETRANDOM)
+  libc_set_definition(randomness_compile_flags LIBC_HASHTABLE_USE_GETRANDOM)
   set(randomness_extra_depends
     libc.src.__support.OSUtil.linux.syscall_wrappers.getrandom
     libc.hdr.errno_macros)
diff --git a/libc/src/__support/threads/linux/CMakeLists.txt b/libc/src/__support/threads/linux/CMakeLists.txt
index 200a3b1529fec..9d292003f21cb 100644
--- a/libc/src/__support/threads/linux/CMakeLists.txt
+++ b/libc/src/__support/threads/linux/CMakeLists.txt
@@ -26,15 +26,19 @@ add_header_library(
 
 set(monotonicity_flags)
 if (LIBC_CONF_TIMEOUT_ENSURE_MONOTONICITY)
-  set(monotonicity_flags -DLIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=1)
+  libc_set_definition(monotonicity_flags LIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=1)
 else()
-  set(monotonicity_flags -DLIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=0)
+  libc_set_definition(monotonicity_flags LIBC_COPT_TIMEOUT_ENSURE_MONOTONICITY=0)
 endif()
 
+libc_set_definition(rwlock_default_spin_count "LIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT=${LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT}")
 add_header_library(
   rwlock
   HDRS
     rwlock.h
+  COMPILE_OPTIONS
+    ${rwlock_default_spin_count}
+    ${monotonicity_flags}
   DEPENDS
     .futex_utils
     libc.src.__support.threads.raw_mutex
@@ -42,9 +46,6 @@ add_header_library(
     libc.src.__support.OSUtil.osutil
     libc.src.__support.CPP.limits
     libc.src.__support.threads.identifier
-  COMPILE_OPTIONS
-    -DLIBC_COPT_RWLOCK_DEFAULT_SPIN_COUNT=${LIBC_CONF_RWLOCK_DEFAULT_SPIN_COUNT}
-    ${monotonicity_flags}
 )
 
 add_object_library(
diff --git a/libc/src/setjmp/aarch64/CMakeLists.txt b/libc/src/setjmp/aarch64/CMakeLists.txt
index 12991101b0fdd..d9c1d5ef1afc7 100644
--- a/libc/src/setjmp/aarch64/CMakeLists.txt
+++ b/libc/src/setjmp/aarch64/CMakeLists.txt
@@ -1,9 +1,5 @@
 if(LIBC_CONF_SETJMP_AARCH64_RESTORE_PLATFORM_REGISTER)
-  list(APPEND setjmp_config_options "-DLIBC_COPT_SETJMP_AARCH64_RESTORE_PLATFORM_REGISTER")
-endif()
-if(setjmp_config_options)
-  list(PREPEND setjmp_config_options "COMPILE_OPTIONS")
-endif()
+  libc_set_definition(setjmp_config_options "LIBC_COPT_SETJMP_AARCH64_RESTORE_PLATFORM_REGISTER")
 
 add_entrypoint_object(
   setjmp
@@ -11,9 +7,10 @@ add_entrypoint_object(
     setjmp.cpp
   HDRS
     ../setjmp_impl.h
+  COMPILE_OPTIONS
+    "${setjmp_config_options}"
   DEPENDS
     libc.hdr.types.jmp_buf
-  ${setjmp_config_options}
 )
 
 add_entrypoint_object(
@@ -22,9 +19,10 @@ add_entrypoint_object(
     longjmp.cpp
   HDRS
     ../longjmp.h
+  COMPILE_OPTIONS
+    "${setjmp_config_options}"
   DEPENDS
     libc.hdr.types.jmp_buf
-  ${setjmp_config_options}
 )
 
 add_entrypoint_object(
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index 9486a53499792..1079a6ac51dfc 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -24,7 +24,8 @@ endif()
 add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/generic)
 
 if(NOT LLVM_LIBC_FULL_BUILD)
-  list(APPEND use_system_file "COMPILE_OPTIONS" "-DLIBC_COPT_STDIO_USE_SYSTEM_FILE")
+  set(use_system_file "COMPILE_OPTIONS")
+  libc_add_definition(use_system_file "LIBC_COPT_STDIO_USE_SYSTEM_FILE")
 endif()
 
 add_entrypoint_object(
diff --git a/libc/src/stdio/printf_core/CMakeLists.txt b/libc/src/stdio/printf_core/CMakeLists.txt
index 697c65e6aeb25..4d6f25a2ddd00 100644
--- a/libc/src/stdio/printf_core/CMakeLists.txt
+++ b/libc/src/stdio/printf_core/CMakeLists.txt
@@ -1,35 +1,35 @@
 if(LIBC_CONF_PRINTF_DISABLE_FLOAT)
-  list(APPEND printf_config_copts "-DLIBC_COPT_PRINTF_DISABLE_FLOAT")
+  libc_add_definition(printf_config_copts "LIBC_COPT_PRINTF_DISABLE_FLOAT")
 endif()
 if(LIBC_CONF_PRINTF_DISABLE_INDEX_MODE)
-  list(APPEND printf_config_copts "-DLIBC_COPT_PRINTF_DISABLE_INDEX_MODE")
+  libc_add_definition(printf_config_copts "LIBC_COPT_PRINTF_DISABLE_INDEX_MODE")
 endif()
 if(LIBC_CONF_PRINTF_DISABLE_WRITE_INT)
-  list(APPEND printf_config_copts "-DLIBC_COPT_PRINTF_DISABLE_WRITE_INT")
+  libc_add_definition(printf_config_copts "LIBC_COPT_PRINTF_DISABLE_WRITE_INT")
 endif()
 if(LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE)
-  list(APPEND printf_config_copts "-DLIBC_COPT_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE")
+  libc_add_definition(printf_config_copts "LIBC_COPT_FLOAT_TO_STR_USE_MEGA_LONG_DOUBLE_TABLE")
 endif()
 if(LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_DYADIC_FLOAT)
-  list(APPEND printf_config_copts "-DLIBC_COPT_FLOAT_TO_STR_USE_DYADIC_FLOAT")
+  libc_add_definition(printf_config_copts "LIBC_COPT_FLOAT_TO_STR_USE_DYADIC_FLOAT")
 endif()
 if(LIBC_CONF_PRINTF_FLOAT_TO_STR_NO_SPECIALIZE_LD)
-  list(APPEND printf_config_copts "-DLIBC_COPT_FLOAT_TO_STR_NO_SPECIALIZE_LD")
+  libc_add_definition(printf_config_copts "LIBC_COPT_FLOAT_TO_STR_NO_SPECIALIZE_LD")
 endif()
 if(LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_FLOAT320)
-  list(APPEND printf_config_copts "-DLIBC_COPT_FLOAT_TO_STR_USE_FLOAT320")
+  libc_add_definition(printf_config_copts "LIBC_COPT_FLOAT_TO_STR_USE_FLOAT320")
 endif()
 if(LIBC_CONF_PRINTF_DISABLE_FIXED_POINT)
-  list(APPEND printf_config_copts "-DLIBC_COPT_PRINTF_DISABLE_FIXED_POINT")
+  libc_add_definition(printf_config_copts "LIBC_COPT_PRINTF_DISABLE_FIXED_POINT")
 endif()
 if(LIBC_CONF_PRINTF_DISABLE_STRERROR)
-  list(APPEND printf_config_copts "-DLIBC_COPT_PRINTF_DISABLE_STRERROR")
+  libc_add_definition(printf_config_copts "LIBC_COPT_PRINTF_DISABLE_STRERROR")
 endif()
 if(LIBC_CONF_PRINTF_RUNTIME_DISPATCH)
-  list(APPEND printf_config_copts "-DLIBC_COPT_PRINTF_RUNTIME_DISPATCH")
+  libc_add_definition(printf_config_copts "LIBC_COPT_PRINTF_RUNTIME_DISPATCH")
 endif()
 if(LIBC_CONF_PRINTF_MODULAR)
-  list(APPEND printf_config_copts "-DLIBC_COPT_PRINTF_MODULAR")
+  libc_add_definition(printf_config_copts "LIBC_COPT_PRINTF_MODULAR")
 endif()
 if(printf_config_copts)
   list(PREPEND printf_config_copts "COMPILE_OPTIONS")
diff --git a/libc/src/stdio/scanf_core/CMakeLists.txt b/libc/src/stdio/scanf_core/CMakeLists.txt
index 561180c6100b4..35578f899c2eb 100644
--- a/libc/src/stdio/scanf_core/CMakeLists.txt
+++ b/libc/src/stdio/scanf_core/CMakeLists.txt
@@ -1,8 +1,8 @@
 if(LIBC_CONF_SCANF_DISABLE_FLOAT)
-  list(APPEND scanf_config_copts "-DLIBC_COPT_SCANF_DISABLE_FLOAT")
+  libc_add_definition(scanf_config_copts "LIBC_COPT_SCANF_DISABLE_FLOAT")
 endif()
 if(LIBC_CONF_SCANF_DISABLE_INDEX_MODE)
-  list(APPEND scanf_config_copts "-DLIBC_COPT_SCANF_DISABLE_INDEX_MODE")
+  libc_add_definition(scanf_config_copts "LIBC_COPT_SCANF_DISABLE_INDEX_MODE")
 endif()
 if(scanf_config_copts)
   list(PREPEND scanf_config_copts "COMPILE_OPTIONS")
diff --git a/libc/test/src/stdio/CMakeLists.txt b/libc/test/src/stdio/CMakeLists.txt
index fde2023caaac1..689845ea231ef 100644
--- a/libc/test/src/stdio/CMakeLists.txt
+++ b/libc/test/src/stdio/CMakeLists.txt
@@ -118,23 +118,23 @@ add_libc_test(
 )
 
 if(LIBC_CONF_PRINTF_DISABLE_FLOAT)
-  list(APPEND sprintf_test_copts "-DLIBC_COPT_PRINTF_DISABLE_FLOAT")
+  libc_add_definition(sprintf_test_copts "LIBC_COPT_PRINTF_DISABLE_FLOAT")
 endif()
 if(LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_DYADIC_FLOAT OR
     LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_FLOAT320)
-  list(APPEND sprintf_test_copts "-DLIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION")
+  libc_add_definition(sprintf_test_copts "LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION")
 endif()
 if(LIBC_CONF_PRINTF_DISABLE_INDEX_MODE)
-  list(APPEND sprintf_test_copts "-DLIBC_COPT_PRINTF_DISABLE_INDEX_MODE")
+  libc_add_definition(sprintf_test_copts "LIBC_COPT_PRINTF_DISABLE_INDEX_MODE")
 endif()
 if(LIBC_CONF_PRINTF_DISABLE_WRITE_INT)
-  list(APPEND sprintf_test_copts "-DLIBC_COPT_PRINTF_DISABLE_WRITE_INT")
+  libc_add_definition(sprintf_test_copts "LIBC_COPT_PRINTF_DISABLE_WRITE_INT")
 endif()
 if(LIBC_CONF_PRINTF_DISABLE_FIXED_POINT)
-  list(APPEND sprintf_test_copts "-DLIBC_COPT_PRINTF_DISABLE_FIXED_POINT")
+  libc_add_definition(sprintf_test_copts "LIBC_COPT_PRINTF_DISABLE_FIXED_POINT")
 endif()
 if(LIBC_CONF_PRINTF_DISABLE_STRERROR)
-  list(APPEND sprintf_test_copts "-DLIBC_COPT_PRINTF_DISABLE_STRERROR")
+  libc_add_definition(sprintf_test_copts "LIBC_COPT_PRINTF_DISABLE_STRERROR")
 endif()
 
 if(LIBC_CONF_PRINTF_DISABLE_WIDE)
@@ -185,7 +185,7 @@ if(LLVM_LIBC_FULL_BUILD)
   set(hermetic_test_only HERMETIC_TEST_ONLY)
 else()
 # Else in overlay mode they use the system's FILE.
- set(use_system_file "-DLIBC_COPT_STDIO_USE_SYSTEM_FILE")
+ libc_set_definition(use_system_file "LIBC_COPT_STDIO_USE_SYSTEM_FILE")
 endif()
 
 add_libc_test(
@@ -328,10 +328,10 @@ if(NOT LIBC_TARGET_ARCHITECTURE_IS_AMDGPU)
 endif()
 
 if(LIBC_CONF_SCANF_DISABLE_FLOAT)
-  list(APPEND sscanf_test_copts "-DLIBC_COPT_SCANF_DISABLE_FLOAT")
+  libc_add_definition(sscanf_test_copts "LIBC_COPT_SCANF_DISABLE_FLOAT")
 endif()
 if(LIBC_CONF_SCANF_DISABLE_INDEX_MODE)
-  list(APPEND sscanf_test_copts "-DLIBC_COPT_SCANF_DISABLE_INDEX_MODE")
+  libc_add_definition(sscanf_test_copts "LIBC_COPT_SCANF_DISABLE_INDEX_MODE")
 endif()
 
 add_libc_test(
diff --git a/libc/test/src/stdio/scanf_core/CMakeLists.txt b/libc/test/src/stdio/scanf_core/CMakeLists.txt
index 64ff7d324c6fd..abf982e4ea00e 100644
--- a/libc/test/src/stdio/scanf_core/CMakeLists.txt
+++ b/libc/test/src/stdio/scanf_core/CMakeLists.txt
@@ -1,6 +1,6 @@
 if(NOT(LLVM_LIBC_FULL_BUILD))
   # in overlay mode, use the system's file to test the reader.
-  set(use_system_file "-DLIBC_COPT_STDIO_USE_SYSTEM_FILE")
+  libc_set_definition(use_system_file "LIBC_COPT_STDIO_USE_SYSTEM_FILE")
 endif()
 
 add_libc_unittest(
diff --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt
index 05a74be4ca21f..a7b7c9269fcee 100644
--- a/libc/test/src/stdlib/CMakeLists.txt
+++ b/libc/test/src/stdlib/CMakeLists.txt
@@ -177,7 +177,7 @@ add_libc_test(
 
 if(LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_DYADIC_FLOAT OR
     LIBC_CONF_PRINTF_FLOAT_TO_STR_USE_FLOAT320)
-  list(APPEND strfrom_test_copts "-DLIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION")
+  libc_set_definition(strfrom_test_copts "LIBC_COPT_FLOAT_TO_STR_REDUCED_PRECISION")
 endif()
 
 add_header_library(



More information about the libc-commits mailing list