Skip to content

Commit ea499a5

Browse files
authored
Merge pull request #70055 from kubamracek/embedded-darwin
[embedded] Port and start building Darwin.swiftmodule as embedded
2 parents 95fa962 + 7c84cdb commit ea499a5

File tree

7 files changed

+108
-2
lines changed

7 files changed

+108
-2
lines changed

stdlib/public/Concurrency/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB AND SWIFT_SHOULD_BUILD_EMBEDDED_CONCURRENC
260260
SDK "embedded"
261261
ARCHITECTURE "${mod}"
262262
DEPENDS embedded-stdlib-${mod}
263-
INSTALL_IN_COMPONENT "never_install"
263+
INSTALL_IN_COMPONENT stdlib
264264
)
265265
set_property(TARGET embedded-concurrency-${mod} PROPERTY OSX_ARCHITECTURES "${arch}")
266266

stdlib/public/Platform/CMakeLists.txt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,55 @@ add_swift_target_library(swiftDarwin ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES}
7777
INSTALL_IN_COMPONENT sdk-overlay
7878
MACCATALYST_BUILD_FLAVOR "zippered")
7979

80+
if(SWIFT_SHOULD_BUILD_EMBEDDED_STDLIB)
81+
set(SWIFT_ENABLE_REFLECTION OFF)
82+
83+
add_custom_target(embedded-darwin ALL)
84+
foreach(entry ${EMBEDDED_STDLIB_TARGET_TRIPLES})
85+
string(REGEX REPLACE "[ \t]+" ";" list "${entry}")
86+
list(GET list 0 arch)
87+
list(GET list 1 mod)
88+
list(GET list 2 triple)
89+
90+
if(NOT "${mod}" MATCHES "-macos$")
91+
continue()
92+
endif()
93+
94+
set(SWIFT_SDK_embedded_ARCH_${arch}_MODULE "${mod}")
95+
set(SWIFT_SDK_embedded_LIB_SUBDIR "embedded")
96+
set(SWIFT_SDK_embedded_ARCH_${arch}_TRIPLE "${triple}")
97+
set(SWIFT_SDK_embedded_PATH ${SWIFT_SDK_OSX_PATH})
98+
set(SWIFT_SDK_embedded_ARCH_${arch}_PATH ${SWIFT_SDK_OSX_PATH})
99+
set(SWIFT_SDK_embedded_USE_ISYSROOT TRUE)
100+
add_swift_target_library_single(
101+
embedded-darwin-${mod}
102+
swiftDarwin
103+
ONLY_SWIFTMODULE
104+
IS_SDK_OVERLAY IS_FRAGILE
105+
106+
Platform.swift
107+
TiocConstants.swift
108+
POSIXError.swift
109+
MachError.swift
110+
111+
GYB_SOURCES
112+
tgmath.swift.gyb
113+
Darwin.swift.gyb
114+
115+
SWIFT_COMPILE_FLAGS
116+
-Xcc -D__MACH__ -Xcc -D__APPLE__ -Xcc -ffreestanding -enable-experimental-feature Embedded
117+
C_COMPILE_FLAGS
118+
-D__MACH__ -D__APPLE__ -ffreestanding
119+
MODULE_DIR "${CMAKE_BINARY_DIR}/lib/swift/embedded"
120+
SDK "embedded"
121+
ARCHITECTURE "${arch}"
122+
DEPENDS embedded-stdlib-${mod}
123+
INSTALL_IN_COMPONENT stdlib
124+
)
125+
add_dependencies(embedded-darwin embedded-darwin-${mod})
126+
endforeach()
127+
endif()
128+
80129
set(swiftGlibc_target_sdks ANDROID CYGWIN FREEBSD OPENBSD LINUX HAIKU)
81130
if(SWIFT_FREESTANDING_FLAVOR STREQUAL "linux")
82131
set(swiftGlibc_target_sdks ANDROID CYGWIN FREEBSD OPENBSD LINUX HAIKU FREESTANDING)

stdlib/public/Platform/Platform.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ extension DarwinBoolean : CustomReflectable {
5555
}
5656
#endif
5757

58+
@_unavailableInEmbedded
5859
extension DarwinBoolean : CustomStringConvertible {
5960
/// A textual representation of `self`.
6061
public var description: String {
@@ -129,6 +130,7 @@ public var stderr : UnsafeMutablePointer<FILE> {
129130
}
130131
}
131132

133+
#if !$Embedded
132134
public func dprintf(_ fd: Int, _ format: UnsafePointer<Int8>, _ args: CVarArg...) -> Int32 {
133135
return withVaList(args) { va_args in
134136
vdprintf(Int32(fd), format, va_args)
@@ -140,6 +142,8 @@ public func snprintf(ptr: UnsafeMutablePointer<Int8>, _ len: Int, _ format: Unsa
140142
return vsnprintf(ptr, len, format, va_args)
141143
}
142144
}
145+
#endif
146+
143147
#elseif os(OpenBSD)
144148
public var stdin: UnsafeMutablePointer<FILE> { return _swift_stdlib_stdin() }
145149
public var stdout: UnsafeMutablePointer<FILE> { return _swift_stdlib_stdout() }

stdlib/public/Platform/tgmath.swift.gyb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ public func remquo(_ x: ${T}, _ y: ${T}) -> (${T}, Int) {
329329
@available(swift, deprecated: 4.2/*, obsoleted: 5.1*/, message:
330330
"use ${T}(nan: ${T}.RawSignificand).")
331331
@_transparent
332+
@_unavailableInEmbedded
332333
public func nan(_ tag: String) -> ${T} {
333334
return ${T}(nan${f}(tag))
334335
}

stdlib/public/core/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ split_embedded_sources(
4949
NORMAL BridgeStorage.swift
5050
NORMAL BridgingBuffer.swift
5151
EMBEDDED Builtin.swift
52-
NORMAL BuiltinMath.swift
52+
EMBEDDED BuiltinMath.swift
5353
NORMAL Character.swift
5454
NORMAL CocoaArray.swift
5555
NORMAL Codable.swift
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// RUN: %target-swift-frontend %t/Main.swift %S/Inputs/print.swift -import-bridging-header %t/BridgingHeader.h -enable-experimental-feature Embedded -c -o %t/main.o
5+
// RUN: %target-clang %t/main.o -o %t/a.out -dead_strip
6+
// RUN: %target-run %t/a.out | %FileCheck %s
7+
8+
// REQUIRES: swift_in_compiler
9+
// REQUIRES: executable_test
10+
// REQUIRES: optimized_stdlib
11+
// REQUIRES: VENDOR=apple
12+
// REQUIRES: OS=macosx
13+
14+
// BEGIN BridgingHeader.h
15+
16+
#include <unistd.h>
17+
18+
// BEGIN Main.swift
19+
20+
@main
21+
struct Main {
22+
static func main() {
23+
let x = getuid()
24+
print("User id: ")
25+
print(x)
26+
}
27+
}
28+
29+
// CHECK: User id:

test/embedded/darwin.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s %S/Inputs/print.swift -enable-experimental-feature Embedded -throws-as-traps -enable-builtin-module -c -o %t/main.o
3+
// RUN: %target-clang %t/main.o -o %t/a.out -dead_strip
4+
// RUN: %target-run %t/a.out | %FileCheck %s
5+
6+
// REQUIRES: swift_in_compiler
7+
// REQUIRES: executable_test
8+
// REQUIRES: optimized_stdlib
9+
// REQUIRES: VENDOR=apple
10+
// REQUIRES: OS=macosx
11+
12+
import Darwin
13+
14+
@main
15+
struct Main {
16+
static func main() {
17+
let x = getuid()
18+
print("User id: ")
19+
print(x)
20+
}
21+
}
22+
23+
// CHECK: User id:

0 commit comments

Comments
 (0)