Skip to content

Commit 7022509

Browse files
Update Embedded Swift macOS guides (#147)
Combines and updates the documentation for trying out Embedded Swift on macOS. Fixes #146
1 parent 733dadf commit 7022509

File tree

2 files changed

+33
-37
lines changed

2 files changed

+33
-37
lines changed

Sources/EmbeddedSwift/Documentation.docc/GuidedExamples/macOSGuide.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,49 @@ And building it into an executable can be done by calling the `swiftc` compiler
1919
$ swiftc HelloEmbedded.swift -o HelloEmbedded -enable-experimental-feature Embedded -wmo
2020
```
2121

22-
This will produce a regular executable binary, but notice that it's very small in size and that it also *does not actually depend on the Swift runtime in the OS* (all Embedded Swift binaries carry their runtime+stdlib dependencies within):
22+
This will produce a regular executable binary, and we can execute it right away:
23+
24+
```shell
25+
$ ./HelloEmbedded
26+
Hello, Embedded Swift 😊
27+
```
28+
Hooray! Our first *host-side* Embedded Swift program is working!
29+
30+
## Details
31+
32+
Looking closer, this binary *does not actually depend on the Swift runtime in the OS* (all Embedded Swift binaries carry their runtime+stdlib dependencies within), but it is still a *dynamically-linked executable*, so it's not fully standalone in the embedded sense:
2333

2434
```shell
25-
$ ls -al
26-
-rwxr-xr-x 1 kuba staff 18K May 16 17:19 HelloEmbedded*
27-
-rw-r--r-- 1 kuba staff 59B May 16 17:16 HelloEmbedded.swift
2835
$ otool -L HelloEmbedded
2936
HelloEmbedded:
30-
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1000.0.0)
37+
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1000.0.0)
3138
```
3239

33-
Let's run it:
40+
However, the singular object file that was used to build this executable was produced by the compiler in the same fashion that a real embedded build would. If we ask the compiler and linker to minimize the size of the outputs and to remove any unused code, we can observe that the binary has no dependencies other than `putchar` from `libSystem` and that the machine code section is very small (176 bytes in the `__text` section):
3441

3542
```shell
36-
$ ./HelloEmbedded
37-
Hello, Embedded Swift 😊
43+
$ swiftc HelloEmbedded.swift -o HelloEmbedded -enable-experimental-feature Embedded -wmo -Osize -Xlinker -dead_strip
44+
45+
$ nm -um HelloEmbedded
46+
(undefined) external _putchar (from libSystem)
47+
48+
$ size -m HelloEmbedded
49+
Segment __TEXT: 16384
50+
Section __text: 176
51+
...
3852
```
3953

40-
Hooray! Our first *host-side* Embedded Swift program is working!
54+
In contrast, if compiled without the Embedded Swift flags, we can see the dependencies on the Swift runtime in the OS:
55+
56+
```shell
57+
$ swiftc HelloEmbedded.swift -o HelloEmbedded
58+
59+
$ otool -L HelloEmbedded
60+
HelloEmbedded:
61+
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1000.0.0)
62+
/usr/lib/swift/libswiftCore.dylib (compatibility version 1.0.0, current version 0.0.0)
63+
/usr/lib/swift/libswiftSwiftOnoneSupport.dylib (compatibility version 1.0.0, current version 0.0.0)
64+
```
4165

4266
## Where to go next
4367

Sources/EmbeddedSwift/Documentation.docc/UsingEmbeddedSwift/Basics.md

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,3 @@ It's very common to integrate with existing SDKs in embedded development. This t
7676
Most embedded SDKs provide a build system integration, commonly with CMake, Make, or their own custom build scripts. At the most basic level, it's always possible to manually call the compiler (`swiftc`) as described above from any build system. This will produce a .o file for the entire Swift module, and then a .o file can typically be directly used in the build system.
7777

7878
For details and concrete examples of how to integrate with more common platforms, SDKs and build systems, see <doc:IntegratingWithPlatforms>.
79-
80-
### Building a macOS Embedded Swift program:
81-
82-
It's also possible to build in Embedded Swift mode for regular non-embedded operating systems, like macOS. This is very useful for testing purposes, or if you just want to observe and experiment with Embedded Swift. A simple source code like this:
83-
84-
```swift
85-
print("Hello, embedded world!")
86-
```
87-
88-
...can be compiled using the `-enable-experimental-feature Embedded` flag (the implicit `-target` matches the host OS):
89-
90-
```bash
91-
$ swiftc hello.swift -enable-experimental-feature Embedded -wmo
92-
$ ./hello
93-
Hello, embedded world!
94-
```
95-
96-
Note that the resulting executable is still a *dynamically-linked executable*, so it's not fully standalone in the embedded sense. Namely is still uses `putchar` from Libsystem. But the singular object file that was used to build this executable was produced by the compiler in the same fashion that a real embedded build would. If we ask the compiler and linker to minimize the size of the outputs and to remove any unused code, we can observe that the binary has no other dependencies other than `putchar` and that the machine code section is very small (172 bytes in the `__text` section):
97-
98-
```bash
99-
$ swiftc hello.swift -enable-experimental-feature Embedded -wmo -Osize -Xlinker -dead_strip
100-
$ nm -um ./hello
101-
(undefined) external _putchar (from libSystem)
102-
$ size -m ./hello
103-
Segment __TEXT: 16384
104-
Section __text: 172
105-
...
106-
```

0 commit comments

Comments
 (0)