[libc-commits] [libc] 5981335 - [libc] Fix scanf cmake for targets without FILE (#128056)

via libc-commits libc-commits at lists.llvm.org
Thu Feb 20 13:47:49 PST 2025


Author: Michael Jones
Date: 2025-02-20T13:47:45-08:00
New Revision: 5981335d75412977862003816d67d9b01fcd04b6

URL: https://github.com/llvm/llvm-project/commit/5981335d75412977862003816d67d9b01fcd04b6
DIFF: https://github.com/llvm/llvm-project/commit/5981335d75412977862003816d67d9b01fcd04b6.diff

LOG: [libc] Fix scanf cmake for targets without FILE (#128056)

Another followup fix to #121215

The new cmake wouldn't define the readerat all if the target wasn't GPU
or didn't have a definition of FILE. This patch rewrites the cmake to be
more general.

As a followup, I'd like to make `use_system_file` consistent between
/test and /src. Currently in /src it includes the `COMPILE_OPTIONS` and
in /test it does not.

Added: 
    

Modified: 
    libc/src/stdio/scanf_core/CMakeLists.txt
    libc/test/src/stdio/scanf_core/CMakeLists.txt

Removed: 
    


################################################################################
diff  --git a/libc/src/stdio/scanf_core/CMakeLists.txt b/libc/src/stdio/scanf_core/CMakeLists.txt
index 96e0e1400cb6a..ce639fe65a106 100644
--- a/libc/src/stdio/scanf_core/CMakeLists.txt
+++ b/libc/src/stdio/scanf_core/CMakeLists.txt
@@ -8,6 +8,20 @@ if(scanf_config_copts)
   list(PREPEND scanf_config_copts "COMPILE_OPTIONS")
 endif()
 
+
+list(APPEND file_deps libc.hdr.types.FILE)
+if(LIBC_TARGET_OS_IS_GPU)
+  list(APPEND file_deps
+    libc.src.stdio.getc
+    libc.src.stdio.ungetc
+    libc.src.stdio.ferror
+  )
+elseif(LLVM_LIBC_FULL_BUILD)
+  list(APPEND file_deps
+    libc.src.__support.File.file
+  )
+endif()
+
 add_header_library(
   scanf_config
   HDRS
@@ -52,30 +66,19 @@ add_object_library(
     .converter
     .core_structs
     libc.src.__support.arg_list
+    ${file_deps}
+  ${use_system_file}
 )
 
-if(LIBC_TARGET_OS_IS_GPU)
-add_header_library(
-  reader
-  HDRS
-    reader.h
-  DEPENDS
-    libc.src.__support.macros.attributes
-    libc.src.stdio.getc
-    libc.src.stdio.ungetc
-)
-elseif((TARGET libc.src.__support.File.file) OR (NOT LLVM_LIBC_FULL_BUILD))
 add_header_library(
   reader
   HDRS
     reader.h
   DEPENDS
     libc.src.__support.macros.attributes
-    libc.hdr.types.FILE
-    libc.src.__support.File.file
+    ${file_deps}
   ${use_system_file}
 )
-endif()
 
 add_object_library(
   converter
@@ -103,33 +106,19 @@ add_object_library(
     libc.src.__support.CPP.limits
     libc.src.__support.char_vector
     libc.src.__support.str_to_float
+    ${file_deps}
+  ${use_system_file}
 )
 
-if(LIBC_TARGET_OS_IS_GPU)
-  add_header_library(
-    vfscanf_internal
-    HDRS
-      vfscanf_internal.h
-    DEPENDS
-      .reader
-      .scanf_main
-      libc.include.stdio
-      libc.src.__support.arg_list
-      libc.src.stdio.getc
-      libc.src.stdio.ungetc
-      libc.src.stdio.ferror
-  )
-elseif(TARGET libc.src.__support.File.file OR (NOT LLVM_LIBC_FULL_BUILD))
-  add_header_library(
+#TODO: condense the file-related code as possible.
+add_header_library(
     vfscanf_internal
     HDRS
       vfscanf_internal.h
     DEPENDS
       .reader
       .scanf_main
-      libc.include.stdio
-      libc.src.__support.File.file
       libc.src.__support.arg_list
+      ${file_deps}
     ${use_system_file}
-  )
-endif()
+)

diff  --git a/libc/test/src/stdio/scanf_core/CMakeLists.txt b/libc/test/src/stdio/scanf_core/CMakeLists.txt
index 06735ddb23be7..9cdc6547821ee 100644
--- a/libc/test/src/stdio/scanf_core/CMakeLists.txt
+++ b/libc/test/src/stdio/scanf_core/CMakeLists.txt
@@ -1,3 +1,8 @@
+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")
+endif()
+
 add_libc_unittest(
   parser_test
   SUITE
@@ -22,14 +27,10 @@ add_libc_unittest(
   DEPENDS
     libc.src.stdio.scanf_core.reader
     libc.src.__support.CPP.string_view
+  COMPILE_OPTIONS
+    ${use_system_file}
 )
 
-if(NOT (TARGET libc.src.__support.File.file))
-  # Not all platforms have a file implementation. If file is unvailable,
-  # then we must skip all the parts that need file.
-  return()
-endif()
-
 add_libc_unittest(
   converter_test
   SUITE
@@ -40,4 +41,6 @@ add_libc_unittest(
     libc.src.stdio.scanf_core.reader
     libc.src.stdio.scanf_core.converter
     libc.src.__support.CPP.string_view
+  COMPILE_OPTIONS
+    ${use_system_file}
 )


        


More information about the libc-commits mailing list