Skip to content

Commit 6766824

Browse files
authored
Add a SWIFT_STDLIB_HAS_STDIN flag to compile out the Swift.readLine() API from the stdlib (for the 'freestanding' preset) (swiftlang#39332)
1 parent e69f7eb commit 6766824

File tree

8 files changed

+26
-2
lines changed

8 files changed

+26
-2
lines changed

stdlib/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ option(SWIFT_RUNTIME_MACHO_NO_DYLD
7676
FALSE)
7777

7878
option(SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
79-
"Build stdlib assuming the Darwin build of stdlib can use extended libmalloc APIs"
80-
)
79+
"Build stdlib assuming the Darwin build of stdlib can use extended libmalloc APIs"
80+
TRUE)
81+
82+
option(SWIFT_STDLIB_HAS_STDIN
83+
"Build stdlib assuming the platform supports stdin and getline API."
84+
TRUE)
8185

8286
option(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
8387
"Build the standard libraries assuming that they will be used in an environment with only a single thread."

stdlib/cmake/modules/AddSwiftStdlib.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,10 @@ function(_add_target_variant_c_compile_flags)
329329
list(APPEND result "-DSWIFT_STDLIB_HAS_DARWIN_LIBMALLOC=0")
330330
endif()
331331

332+
if(SWIFT_STDLIB_HAS_STDIN)
333+
list(APPEND result "-DSWIFT_STDLIB_HAS_STDIN")
334+
endif()
335+
332336
if(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
333337
list(APPEND result "-DSWIFT_STDLIB_SINGLE_THREADED_RUNTIME")
334338
endif()

stdlib/cmake/modules/SwiftSource.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,10 @@ function(_add_target_variant_swift_compile_flags
280280
list(APPEND result "-D" "SWIFT_RUNTIME_OS_VERSIONING")
281281
endif()
282282

283+
if(SWIFT_STDLIB_HAS_STDIN)
284+
list(APPEND result "-D" "SWIFT_STDLIB_HAS_STDIN")
285+
endif()
286+
283287
set("${result_var_name}" "${result}" PARENT_SCOPE)
284288
endfunction()
285289

stdlib/public/core/InputStream.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
import SwiftShims
1414

15+
#if SWIFT_STDLIB_HAS_STDIN
16+
1517
/// Returns a string read from standard input through the end of the current
1618
/// line or until EOF is reached.
1719
///
@@ -42,3 +44,5 @@ public func readLine(strippingNewline: Bool = true) -> String? {
4244
}
4345
return result
4446
}
47+
48+
#endif

stdlib/public/stubs/Stubs.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ uint64_t swift_float80ToString(char *Buffer, size_t BufferLength,
202202
}
203203
#endif
204204

205+
#if SWIFT_STDLIB_HAS_STDIN
206+
205207
/// \param[out] LinePtr Replaced with the pointer to the malloc()-allocated
206208
/// line. Can be NULL if no characters were read. This buffer should be
207209
/// freed by the caller.
@@ -265,6 +267,8 @@ swift_stdlib_readLine_stdin(unsigned char **LinePtr) {
265267
#endif
266268
}
267269

270+
#endif // SWIFT_STDLIB_HAS_STDIN
271+
268272
static bool swift_stringIsSignalingNaN(const char *nptr) {
269273
if (nptr[0] == '+' || nptr[0] == '-') {
270274
++nptr;

test/stdlib/InputStream.swift.gyb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
// -*- swift -*-
1313
// RUN: %target-run-simple-swiftgyb
1414
// REQUIRES: executable_test
15+
// UNSUPPORTED: freestanding
1516

1617
import StdlibUnittest
1718

utils/build-presets.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2455,6 +2455,7 @@ swift-enable-compatibility-overrides=0
24552455
swift-enable-reflection=0
24562456
swift-runtime-macho-no-dyld=1
24572457
swift-stdlib-has-darwin-libmalloc=0
2458+
swift-stdlib-has-stdin=0
24582459
swift-stdlib-single-threaded-runtime=1
24592460
swift-stdlib-os-versioning=0
24602461
extra-cmake-options=

utils/build-script-impl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ KNOWN_SETTINGS=(
210210
swift-stdlib-os-versioning "1" "whether to build stdlib with availability based on OS versions (Darwin only)"
211211
swift-stdlib-stable-abi "" "should stdlib be built with stable ABI, if not set defaults to true on Darwin, false otherwise"
212212
swift-stdlib-has-darwin-libmalloc "1" "whether the Darwin build of stdlib can use extended libmalloc APIs"
213+
swift-stdlib-has-stdin "1" "whether to build stdlib assuming the platform supports stdin and getline API"
213214
swift-disable-dead-stripping "0" "turns off Darwin-specific dead stripping for Swift host tools"
214215
common-swift-flags "" "Flags used for Swift targets other than the stdlib, like the corelibs"
215216

@@ -1970,6 +1971,7 @@ for host in "${ALL_HOSTS[@]}"; do
19701971
-DSWIFT_RUNTIME_MACHO_NO_DYLD:BOOL=$(true_false "${SWIFT_RUNTIME_MACHO_NO_DYLD}")
19711972
-DSWIFT_STDLIB_OS_VERSIONING:BOOL=$(true_false "${SWIFT_STDLIB_OS_VERSIONING}")
19721973
-DSWIFT_STDLIB_HAS_DARWIN_LIBMALLOC:BOOL=$(true_false "${SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC}")
1974+
-DSWIFT_STDLIB_HAS_STDIN:BOOL=$(true_false "${SWIFT_STDLIB_HAS_STDIN}")
19731975
-DSWIFT_NATIVE_LLVM_TOOLS_PATH:STRING="${native_llvm_tools_path}"
19741976
-DSWIFT_NATIVE_CLANG_TOOLS_PATH:STRING="${native_clang_tools_path}"
19751977
-DSWIFT_NATIVE_SWIFT_TOOLS_PATH:STRING="${native_swift_tools_path}"

0 commit comments

Comments
 (0)