Skip to content

Add a SWIFT_STDLIB_HAS_STDIN flag to compile out the readLine API (for the 'freestanding' preset) #39332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions stdlib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ option(SWIFT_RUNTIME_MACHO_NO_DYLD
FALSE)

option(SWIFT_STDLIB_HAS_DARWIN_LIBMALLOC
"Build stdlib assuming the Darwin build of stdlib can use extended libmalloc APIs"
)
"Build stdlib assuming the Darwin build of stdlib can use extended libmalloc APIs"
TRUE)

option(SWIFT_STDLIB_HAS_STDIN
"Build stdlib assuming the platform supports stdin and getline API."
TRUE)

option(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME
"Build the standard libraries assuming that they will be used in an environment with only a single thread."
Expand Down
4 changes: 4 additions & 0 deletions stdlib/cmake/modules/AddSwiftStdlib.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,10 @@ function(_add_target_variant_c_compile_flags)
list(APPEND result "-DSWIFT_STDLIB_HAS_DARWIN_LIBMALLOC=0")
endif()

if(SWIFT_STDLIB_HAS_STDIN)
list(APPEND result "-DSWIFT_STDLIB_HAS_STDIN")
endif()

if(SWIFT_STDLIB_SINGLE_THREADED_RUNTIME)
list(APPEND result "-DSWIFT_STDLIB_SINGLE_THREADED_RUNTIME")
endif()
Expand Down
4 changes: 4 additions & 0 deletions stdlib/cmake/modules/SwiftSource.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ function(_add_target_variant_swift_compile_flags
list(APPEND result "-D" "SWIFT_RUNTIME_OS_VERSIONING")
endif()

if(SWIFT_STDLIB_HAS_STDIN)
list(APPEND result "-D" "SWIFT_STDLIB_HAS_STDIN")
endif()

set("${result_var_name}" "${result}" PARENT_SCOPE)
endfunction()

Expand Down
4 changes: 4 additions & 0 deletions stdlib/public/core/InputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

import SwiftShims

#if SWIFT_STDLIB_HAS_STDIN

/// Returns a string read from standard input through the end of the current
/// line or until EOF is reached.
///
Expand Down Expand Up @@ -42,3 +44,5 @@ public func readLine(strippingNewline: Bool = true) -> String? {
}
return result
}

#endif
4 changes: 4 additions & 0 deletions stdlib/public/stubs/Stubs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ uint64_t swift_float80ToString(char *Buffer, size_t BufferLength,
}
#endif

#if SWIFT_STDLIB_HAS_STDIN

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

#endif // SWIFT_STDLIB_HAS_STDIN

static bool swift_stringIsSignalingNaN(const char *nptr) {
if (nptr[0] == '+' || nptr[0] == '-') {
++nptr;
Expand Down
1 change: 1 addition & 0 deletions test/stdlib/InputStream.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// -*- swift -*-
// RUN: %target-run-simple-swiftgyb
// REQUIRES: executable_test
// UNSUPPORTED: freestanding

import StdlibUnittest

Expand Down
1 change: 1 addition & 0 deletions utils/build-presets.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2455,6 +2455,7 @@ swift-enable-compatibility-overrides=0
swift-enable-reflection=0
swift-runtime-macho-no-dyld=1
swift-stdlib-has-darwin-libmalloc=0
swift-stdlib-has-stdin=0
swift-stdlib-single-threaded-runtime=1
swift-stdlib-os-versioning=0
extra-cmake-options=
Expand Down
2 changes: 2 additions & 0 deletions utils/build-script-impl
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ KNOWN_SETTINGS=(
swift-stdlib-os-versioning "1" "whether to build stdlib with availability based on OS versions (Darwin only)"
swift-stdlib-stable-abi "" "should stdlib be built with stable ABI, if not set defaults to true on Darwin, false otherwise"
swift-stdlib-has-darwin-libmalloc "1" "whether the Darwin build of stdlib can use extended libmalloc APIs"
swift-stdlib-has-stdin "1" "whether to build stdlib assuming the platform supports stdin and getline API"
swift-disable-dead-stripping "0" "turns off Darwin-specific dead stripping for Swift host tools"
common-swift-flags "" "Flags used for Swift targets other than the stdlib, like the corelibs"

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