Skip to content

Commit 2fa7968

Browse files
authored
Turn build-swift-stdlib-static-print on for freestanding preset (#41260)
1 parent 4e3d4d2 commit 2fa7968

22 files changed

+163
-47
lines changed

stdlib/private/StdlibUnittest/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ if(SWIFT_ENABLE_REFLECTION)
1414
list(APPEND swift_stdlib_unittest_compile_flags "-DSWIFT_ENABLE_REFLECTION")
1515
endif()
1616

17+
if(SWIFT_STDLIB_STATIC_PRINT)
18+
list(APPEND swift_stdlib_unittest_compile_flags "-D" "SWIFT_STDLIB_STATIC_PRINT")
19+
endif()
20+
1721
set(swift_stdlib_unittest_link_libraries "")
1822
set(swift_stdlib_unittest_modules "")
1923
if (SWIFT_ENABLE_EXPERIMENTAL_CONCURRENCY)

stdlib/private/StdlibUnittest/StdlibCoreExtras.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,3 +304,16 @@ public func dump<T, TargetStream: TextOutputStream>(_ value: T, to target: inout
304304
}
305305

306306
#endif
307+
308+
#if SWIFT_STDLIB_STATIC_PRINT
309+
310+
public func print(_ s: Any, terminator: String = "\n") {
311+
let data = Array("\(s)\(terminator)".utf8)
312+
write(STDOUT_FILENO, data, data.count)
313+
}
314+
315+
public func print<Target>(_ s: Any, terminator: String = "\n", to output: inout Target) where Target : TextOutputStream {
316+
output.write("\(s)\(terminator)")
317+
}
318+
319+
#endif

stdlib/private/SwiftPrivateLibcExtras/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ if(SWIFT_STDLIB_HAS_ENVIRON)
33
set(swift_private_libc_extras_flags "-D" "SWIFT_STDLIB_HAS_ENVIRON")
44
endif()
55

6+
if(SWIFT_STDLIB_STATIC_PRINT)
7+
list(APPEND swift_private_libc_extras_flags "-D" "SWIFT_STDLIB_STATIC_PRINT")
8+
endif()
9+
610
set(swift_private_libc_extras_incorporate_object_libraries)
711
if(SWIFT_STDLIB_HAS_COMMANDLINE)
812
list(APPEND swift_private_libc_extras_flags "-D" "SWIFT_STDLIB_HAS_COMMANDLINE")

stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ var environ: UnsafeMutablePointer<UnsafeMutablePointer<CChar>?> {
244244
}
245245
#endif
246246

247+
#if SWIFT_STDLIB_STATIC_PRINT
248+
func print(_ s: String) {
249+
let data = Array("\(s)\n".utf8)
250+
write(STDOUT_FILENO, data, data.count)
251+
}
252+
#endif
253+
247254
/// Start the same executable as a child process, redirecting its stdout and
248255
/// stderr.
249256
public func spawnChild(_ args: [String])

stdlib/public/core/ArrayShared.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ extension Collection {
9898
internal func _makeCollectionDescription(
9999
withTypeName type: String? = nil
100100
) -> String {
101+
#if !SWIFT_STDLIB_STATIC_PRINT
101102
var result = ""
102103
if let type = type {
103104
result += "\(type)(["
@@ -116,6 +117,9 @@ extension Collection {
116117
}
117118
result += type != nil ? "])" : "]"
118119
return result
120+
#else
121+
return "(collection printing not available)"
122+
#endif
119123
}
120124
}
121125

stdlib/public/core/Dictionary.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,7 @@ extension Collection {
16771677
internal func _makeKeyValuePairDescription<K, V>(
16781678
withTypeName type: String? = nil
16791679
) -> String where Element == (key: K, value: V) {
1680+
#if !SWIFT_STDLIB_STATIC_PRINT
16801681
if self.isEmpty {
16811682
return "[:]"
16821683
}
@@ -1695,6 +1696,9 @@ extension Collection {
16951696
}
16961697
result += "]"
16971698
return result
1699+
#else
1700+
return "(collection printing not available)"
1701+
#endif
16981702
}
16991703
}
17001704

stdlib/public/core/Optional.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,10 +295,14 @@ extension Optional: CustomDebugStringConvertible {
295295
public var debugDescription: String {
296296
switch self {
297297
case .some(let value):
298+
#if !SWIFT_STDLIB_STATIC_PRINT
298299
var result = "Optional("
299300
debugPrint(value, terminator: "", to: &result)
300301
result += ")"
301302
return result
303+
#else
304+
return "(optional printing not available)"
305+
#endif
302306
case .none:
303307
return "nil"
304308
}

stdlib/public/core/Print.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if !SWIFT_STDLIB_STATIC_PRINT
14+
1315
/// Writes the textual representations of the given items into the standard
1416
/// output.
1517
///
@@ -247,3 +249,5 @@ internal func _debugPrint<Target: TextOutputStream>(
247249
}
248250
output.write(terminator)
249251
}
252+
253+
#endif

stdlib/public/core/REPL.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
#if !SWIFT_STDLIB_STATIC_PRINT
14+
1315
/// Print a string as is to stdout.
1416
public // COMPILER_INTRINSIC
1517
func _replPrintLiteralString(_ text: String) {
@@ -22,3 +24,5 @@ public // COMPILER_INTRINSIC
2224
func _replDebugPrintln<T>(_ value: T) {
2325
debugPrint(value)
2426
}
27+
28+
#endif

stdlib/public/core/StaticPrint.swift

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,9 @@ public struct ConstantVPrintFMessage :
789789
@_semantics("oslog.message.init_interpolation")
790790
@_semantics("constant_evaluable")
791791
public init(stringInterpolation: ConstantVPrintFInterpolation) {
792-
self.interpolation = stringInterpolation
792+
var s = stringInterpolation
793+
s.appendLiteral("\n")
794+
self.interpolation = s
793795
}
794796

795797
@inlinable
@@ -802,6 +804,7 @@ public struct ConstantVPrintFMessage :
802804
interpolationCount: 0
803805
)
804806
s.appendLiteral(value)
807+
s.appendLiteral("\n")
805808
self.interpolation = s
806809
}
807810
}
@@ -836,13 +839,18 @@ internal func constant_vprintf_backend(
836839
if let closure = argumentClosures.first {
837840
closure { newArg in
838841
args.append(contentsOf: newArg)
839-
print(argumentClosures.count)
840842
constant_vprintf_backend_recurse(
841843
fmt: fmt,
842844
argumentClosures: argumentClosures.dropFirst(),
843845
args: &args
844846
)
845847
}
848+
} else {
849+
constant_vprintf_backend_recurse(
850+
fmt: fmt,
851+
argumentClosures: ArraySlice(argumentClosures),
852+
args: &args
853+
)
846854
}
847855
}
848856

@@ -851,10 +859,10 @@ internal func constant_vprintf_backend(
851859
@_transparent
852860
@_alwaysEmitIntoClient
853861
@_optimize(none)
854-
public func constant_vprintf(_ message: ConstantVPrintFMessage) {
862+
public func print(_ message: ConstantVPrintFMessage) {
855863
let formatString = message.interpolation.formatString
856864
let argumentClosures = message.interpolation.arguments.argumentClosures
857-
if Bool(_builtinBooleanLiteral: Builtin.ifdef_PRINT_DISABLED()) { return }
865+
if Bool(_builtinBooleanLiteral: Builtin.ifdef_SWIFT_STDLIB_PRINT_DISABLED()) { return }
858866
let formatStringPointer = _getGlobalStringTablePointer(formatString)
859867
constant_vprintf_backend(
860868
fmt: formatStringPointer,

0 commit comments

Comments
 (0)