├── .gitignore
├── LICENSE.txt
├── README.md
├── build_arm64v8_ubuntu_cross_compilation_toolchain
├── images
├── curl-cows-service.png
└── firefox-cows-service.png
└── macos
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | *.tgz
2 | *.bz2
3 | *.gz
4 | swift-checkout
5 | buildSwiftOnARM
6 | helloworld
7 | vaca
8 | CowsServiceX
9 |
10 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
203 |
204 |
205 | ## Runtime Library Exception to the Apache 2.0 License: ##
206 |
207 |
208 | As an exception, if you use this Software to compile your source code and
209 | portions of this Software are embedded into the binary product as a result,
210 | you may redistribute such product without providing attribution as would
211 | otherwise be required by Sections 4(a), 4(b) and 4(d) of the License.
212 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
macOS -> RasPi Cross Compilation Toolchain
2 |
4 |
5 |
6 | 
7 | 
8 | 
9 | 
10 |
11 | 2019-05-11: There is now a great fork of this: [SwiftCrossCompilers](https://github.com/CSCIX65G/SwiftCrossCompilers) which provides ready-made toolchains. Highly recommended!
12 |
13 | End of April 2017
14 | [Johannes Weiß](https://github.com/weissi)
15 | added
16 | [custom toolchain support](https://github.com/apple/swift-package-manager/pull/1098)
17 | to
18 | [Swift Package Manager](https://github.com/apple/swift-package-manager).
19 | Johannes also provided
20 | [a script](https://github.com/apple/swift-package-manager/blob/master/Utilities/build_ubuntu_cross_compilation_toolchain)
21 | which shows how to build an Ubuntu toolchain for x86-64.
22 | So what we did is take that script and make it produce a Swift 4.2.1 cross
23 | compiler toolchain for the Raspberry Pi (arm64v8) Ubuntu Bionic port.
24 |
25 | What this is good for?
26 | You can build Raspberry Pi Swift binaries on a Mac. Like this:
27 | ```
28 | mkdir helloworld && cd helloworld
29 | swift package init --type=executable
30 | swift build --destination /tmp/cross-toolchain/arm64v8-ubuntu-bionic-destination.json
31 | file .build/debug/helloworld
32 | .build/debug/helloworld: ELF 64-bit LSB shared object, ARM aarch64, \
33 | version 1 (SYSV), dynamically linked, \
34 | interpreter /lib/ld-linux-aarch64.so.1, \
35 | for GNU/Linux 3.7.0, with debug_info, not stripped
36 | ```
37 |
38 | (We also have a toolchain kit which does the reverse, compile macOS Swift
39 | binaries on a Raspberry Pi: [macos](macos/README.md))
40 |
41 |
42 | ## Building the ARM toolchain
43 |
44 | What we are going to do is build a Swift 4.2 cross compilation toolchain
45 | for ARM Ubuntu Bionic.
46 | Here we are going to use the Swift 4.2 package manager and the Swift 4.2
47 | compiler.
48 |
49 | Requirements:
50 | - Xcode 10 or later (http://developer.apple.com/)
51 | - a Raspi 3 w/ Ubuntu Bionic (e.g. via Hypriot)
52 |
53 | Recommended:
54 | - [Docker for Mac](https://docs.docker.com/docker-for-mac/install/)
55 |
56 | ### Build Toolchain using Script
57 |
58 | First download our script and make it executable:
59 | [build_arm64v8_ubuntu_cross_compilation_toolchain](https://raw.githubusercontent.com/AlwaysRightInstitute/swift-mac2arm-x-compile-toolchain/swift-4.2-arm64v8/build_arm64v8_ubuntu_cross_compilation_toolchain),
60 | e.g. like:
61 |
62 | ```
63 | pushd /tmp
64 | curl https://raw.githubusercontent.com/AlwaysRightInstitute/swift-mac2arm-x-compile-toolchain/swift-4.2-arm64v8/build_arm64v8_ubuntu_cross_compilation_toolchain \
65 | | sed "s/$(printf '\r')\$//" \
66 | > build_arm64v8_ubuntu_cross_compilation_toolchain
67 | chmod +x build_arm64v8_ubuntu_cross_compilation_toolchain
68 | ```
69 |
70 | Next step is to download Swift 4.2 tarballs.
71 | We need the macOS pkg for the host compiler and a Raspberry Pi tarball for the
72 | Swift runtime.
73 | On Raspi arm64v8 we are using the 4.2 build by @futurejones (thanks!):
74 |
75 | ```
76 | pushd /tmp
77 | curl -L -o swift-4.2.1-futurejones-ubuntu-bionic.tar.gz https://www.dropbox.com/s/fzaxi1hz18w01rz/swift-4.2.1-futurejones-ubuntu-bionic.tar.gz?dl=1
78 | curl -o swift-4.2.1-RELEASE-osx.pkg https://swift.org/builds/swift-4.2.1-release/xcode/swift-4.2.1-RELEASE/swift-4.2.1-RELEASE-osx.pkg
79 | ```
80 | Those are a little heavy (~500 MB), so grab a 🍺 or 🍻.
81 | Once they are available, build the actual toolchain using the script
82 | (it will take a minute or two to build through the dependencies):
83 |
84 | ```
85 | pushd /tmp
86 | ./build_arm64v8_ubuntu_cross_compilation_toolchain \
87 | . \
88 | swift-4.2.1-RELEASE-osx.pkg \
89 | swift-4.2.1-futurejones-ubuntu-bionic.tar.gz
90 | ```
91 |
92 | If everything worked fine, it'll end like that:
93 | ```
94 | OK, your cross compilation toolchain for Raspi Ubuntu Bionic is now ready to be used
95 | - SDK: /tmp/cross-toolchain/arm64v8-ubuntu-bionic.sdk
96 | - toolchain: /tmp/cross-toolchain/swift.xctoolchain
97 | - SwiftPM destination.json: /tmp/cross-toolchain/arm64v8-ubuntu-bionic-destination.json
98 | ```
99 |
100 |
101 | ### Use the Toolchain
102 |
103 | Lets create a simple `helloworld` tool first:
104 |
105 | ```
106 | mkdir helloworld && cd helloworld
107 | swift package init --type=executable
108 | swift build --destination /tmp/cross-toolchain/arm64v8-ubuntu-bionic-destination.json
109 | ```
110 |
111 | Which gives:
112 | ```
113 | Compile Swift Module 'helloworld' (1 sources)
114 | Linking ./.build/aarch64-unknown-linux/debug/helloworld
115 | ```
116 |
117 | Check whether it actually produced an ARM binary:
118 | ```
119 | file ./.build/aarch64-unknown-linux/debug/helloworld
120 | ./.build/aarch64-unknown-linux/debug/helloworld: \
121 | ELF 64-bit LSB shared object, ARM aarch64, \
122 | version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-aarch64.so.1, \
123 | for GNU/Linux 3.7.0, with debug_info, not stripped
124 | ```
125 |
126 | Excellent! It worked. Now either copy your binary to a Raspi or test it using
127 | QEmu as described below.
128 |
129 | If you are not using our
130 | [Docker image](https://hub.docker.com/r/helje5/arm64v8-swift/),
131 | you may need to setup the `LD_LIBRARY_PATH`, so that the dynamic linker finds
132 | the Swift runtime. E.g. like that:
133 |
134 | ```
135 | sudo cat > /etc/ld.so.conf.d/swift.conf < Package.swift < main.swift < Package.swift < Sources/CowsServiceX/main.swift <
356 |
357 | Reload to get a new random cow. It is a proper microservice, so test it out
358 | using curl:
359 |
360 |
361 |
362 |
363 | ### SwiftXcode
364 |
365 | All this works as well from within
366 | [SwiftXcode](https://swiftxcode.github.io)!
367 | Just create a new SwiftXcode NIO project, develop your app on macOS using
368 | the default template.
369 |
370 | To deploy, create an aggregate target with a script build phase that
371 | invokes `swift build` and copies the result to your Raspi (or directly
372 | builds on NFS).
373 |
374 |
375 | ## Notes of interest
376 |
377 | - Ubuntu system headers and such for the toolchain are directly pulled
378 | from the Debian packages (which are retrieved from the Ubuntu repository)
379 | - The cross compiler is just a regular clang/swiftc provided as part of
380 | a macOS toolchain. Yes, clang/swift are always setup as cross compilers
381 | and can produce binaries for all supported targets! (didn't know that)
382 | - To trace filesystem calls on macOS you can use `fs_usage`, e.g.:
383 | `sudo fs_usage -w -f pathname swift` (I only knew `strace` ;-)
384 | - `swift build --static-swift-stdlib` does not currently work, but that
385 | is an easy fix if desired (`static-stdlib-args.lnk` needs to be fixed to
386 | point to the right location)
387 |
388 | Links:
389 |
390 | - [SwiftyLinkerKit](https://github.com/SwiftyLinkerKit/SwiftyLinkerKit)
391 | - [LinkerKitIRCBot](https://github.com/SwiftyLinkerKit/LinkerKitIRCBot)
392 | - [MicroExpress](https://github.com/NozeIO/MicroExpress)
393 | - [SwiftNIO](https://github.com/apple/swift-nio)
394 | - [SwiftXcode](https://swiftxcode.github.io)
395 | - [Hypriot](https://blog.hypriot.com) - [64bit images](https://github.com/DieterReuter/image-builder-rpi64/releases)
396 | - DockerHub [helje5/arm64v8-swift](https://cloud.docker.com/u/helje5/repository/docker/helje5/arm64v8-swift) image
397 | - DockerHub [helje5/arm64v8-swift-dev](https://cloud.docker.com/u/helje5/repository/docker/helje5/arm64v8-swift-dev) image (w/ Emacs etc.)
398 | - [SwiftNIO on Raspi](https://helgehess.eu/SwiftNIO-Raspi-Screens.pdf) presentation PDF at [ServerSide.swift Conf](https://www.serversideswift.info)
399 |
400 | ### Who
401 |
402 | Brought to you by
403 | [The Always Right Institute](http://www.alwaysrightinstitute.com)
404 | and
405 | [ZeeZide](http://zeezide.de).
406 | We like
407 | [feedback](https://twitter.com/ar_institute),
408 | GitHub stars,
409 | cool [contract work](http://zeezide.com/en/services/services.html),
410 | presumably any form of praise you can think of.
411 | We don't like people who are wrong.
412 |
413 | There is the [swift-arm](https://slackpass.io/swift-arm) Slack channel
414 | if you have questions about running Swift on ARM/Raspberry Pi.
415 |
416 |
417 |
418 |
419 |
421 |
422 | ApacheExpress
423 | |
424 |
425 |
427 |
428 | mod_swift
429 | |
430 |
431 |
433 |
434 | ZeeQL
435 | |
436 |
437 |
439 |
440 | Noze.io
441 | |
442 |
443 |
445 |
446 | UXKit
447 | |
448 |
449 |
450 |
--------------------------------------------------------------------------------
/build_arm64v8_ubuntu_cross_compilation_toolchain:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | #
3 | # Based on
4 | # - https://github.com/apple/swift-package-manager/blob/master/Utilities/build_ubuntu_cross_compilation_toolchain
5 |
6 | # by Johannes Weiß
7 | # Raspi adjustments by Helge Heß
8 |
9 | set -eu
10 |
11 | export PATH="/bin:/usr/bin"
12 |
13 | # Note: aarch64-linux-gnu is rejected by swift-pm. We need
14 | # aarch64-unknown-linux (arch-vendor-os)
15 | TARGET_ARCH=aarch64
16 | export TC_TARGET="${TARGET_ARCH}-linux-gnu"
17 | export SPM_TARGET="${TARGET_ARCH}-unknown-linux"
18 |
19 | function usage() {
20 | echo >&2 "Usage: $0 SWIFT-FOR-MACOS.pkg SWIFT-FOR-LINUX.tar.gz"
21 | echo >&2
22 | echo >&2 "Example: $0 /tmp/ ~/Downloads/swift-4.2.1-RELEASE-osx.pkg ~/Downloads/swift-4.2.1-futurejones-ubuntu-bionic.tar.gz"
23 | echo >&2
24 | echo >&2 "Complete example:"
25 | echo >&2 " # Download the Swift binaries for Ubuntu and macOS"
26 | echo >&2 " curl -L -o ~/Downloads/swift-4.2.1-futurejones-ubuntu-bionic.tar.gz https://www.dropbox.com/s/fzaxi1hz18w01rz/swift-4.2.1-futurejones-ubuntu-bionic.tar.gz?dl=1"
27 | echo >&2 " curl -o ~/Downloads/swift-4.2.1-osx.pkg https://swift.org/builds/swift-4.2.1-release/xcode/swift-4.2.1-RELEASE/swift-4.2.1-RELEASE-osx.pkg"
28 | echo >&2 " # Compile the SDK and toolchain from that"
29 | echo >&2 " $0 /tmp/ ~/Downloads/swift-4.2.1-RELEASE-osx.pkg ~/Downloads/swift-4.2.1-futurejones-ubuntu-bionic.tar.gz"
30 | echo >&2 " # Create a test application"
31 | echo >&2 " mkdir my-test-app"
32 | echo >&2 " cd my-test-app"
33 | echo >&2 " swift package init --type=executable"
34 | echo >&2 " # Build it for Ubuntu"
35 | echo >&2 " swift build --destination /tmp/cross-toolchain/arm64v8-ubuntu-bionic-destination.json"
36 |
37 | }
38 |
39 | if [[ $# -ne 3 ]]; then
40 | usage
41 | exit 1
42 | fi
43 |
44 | function realpath() {
45 | if [[ "${1:0:1}" = / ]]; then
46 | echo "$1"
47 | else
48 | (
49 | cd "$(dirname "$1")"
50 | echo "$(pwd)/$(basename "$1")"
51 | )
52 | fi
53 | }
54 |
55 | # This is a funny function. The Glibc.modulemap contains absolute include
56 | # pathes like so:
57 | # header "/usr/include/aarch64-linux-gnu/sys/ioctl.h"
58 | # This thing creates a new directory:
59 | # swift.xctoolchain/usr/lib/swift/linux/aarch64/private_includes
60 | # and for each header in the modmap it creates a shim header which includes
61 | # a relative path, like:
62 | # swift.xctoolchain/usr/lib/swift/linux/aarch64/private_includes/aarch64-linux-gnu_sys_ioctl.h
63 | # which includes:
64 | # #include
65 | function fix_glibc_modulemap() {
66 | local glc_mm
67 | local tmp
68 | local inc_dir
69 |
70 | glc_mm="$1"
71 | echo "glibc.modulemap at '$glc_mm' (`pwd`)"
72 | if ! test -f "$glc_mm"; then
73 | echo "Missing: $glc_mm"
74 | exit 42
75 | fi
76 |
77 | tmp=$(mktemp "$glc_mm"_orig_XXXXXX)
78 | inc_dir="$(dirname "$glc_mm")/private_includes"
79 | cat "$glc_mm" >> "$tmp"
80 | echo "Paths:"
81 | echo " - original glibc.modulemap: $tmp"
82 | echo " - new glibc.modulemap: $glc_mm"
83 | echo " - private includes dir : $inc_dir"
84 | echo -n > "$glc_mm"
85 | rm -rf "$inc_dir"
86 | mkdir "$inc_dir"
87 | cat "$tmp" | while IFS='' read line; do
88 | # hh: apparently the modmap started w/ two slashes? ///usr/local/
89 | # if [[ "$line" =~ ^(\ *header\ )\"\/\/\/usr\/include\/(${TC_TARGET}\/)?([^\"]+)\" ]]; then
90 | if [[ "$line" =~ ^(\ *header\ )\"\/usr\/include\/(${TC_TARGET}\/)?([^\"]+)\" ]]; then
91 | local orig_inc
92 | local rel_repl_inc
93 | local repl_inc
94 |
95 | orig_inc="${BASH_REMATCH[3]}"
96 | rel_repl_inc="$(echo "$orig_inc" | tr / _)"
97 | repl_inc="$inc_dir/$rel_repl_inc"
98 | echo "${BASH_REMATCH[1]} \"$(basename "$inc_dir")/$rel_repl_inc\"" >> "$glc_mm"
99 | if [[ "$orig_inc" == "uuid/uuid.h" ]]; then
100 | # no idea why ;)
101 | echo "#include " >> "$repl_inc"
102 | else
103 | echo "#include <$orig_inc>" >> "$repl_inc"
104 | fi
105 | true
106 | else
107 | echo "$line" >> "$glc_mm"
108 | fi
109 | done
110 | }
111 |
112 | # set -xv
113 | # where to get stuff from
114 | dest=$(realpath "$1")
115 | macos_swift_pkg=$(realpath "$2")
116 | linux_swift_pkg=$(realpath "$3")
117 | if ! test -f "$macos_swift_pkg"; then
118 | echo "Missing macOS toolchain: $macos_swift_pkg"
119 | exit 42
120 | fi
121 | if ! test -f "$linux_swift_pkg"; then
122 | echo "Missing Raspi toolchain: $linux_swift_pkg"
123 | exit 42
124 | fi
125 |
126 | # config
127 | blocks_h_url="https://raw.githubusercontent.com/apple/swift-corelibs-libdispatch/master/src/BlocksRuntime/Block.h"
128 | xc_tc_name="swift.xctoolchain"
129 | linux_sdk_name="arm64v8-ubuntu-bionic.sdk"
130 | cross_tc_basename="cross-toolchain"
131 | binutils_pkg_url="https://ftp.gnu.org/gnu/binutils/binutils-2.31.tar.gz"
132 | ubuntu_mirror="http://ports.ubuntu.com/ubuntu-ports"
133 | packages_file="$ubuntu_mirror/dists/bionic/main/binary-arm64/Packages.gz"
134 | # was libgcc-5-dev, on Bionic we need libgcc-7-dev, libstdc++-7-dev
135 | pkg_names=( libc6-dev linux-libc-dev libicu60 libgcc-7-dev libicu-dev libc6 libgcc1 libstdc++-7-dev libstdc++6 zlib1g zlib1g-dev)
136 | pkgs=()
137 |
138 | # url
139 | function download_stdout() {
140 | curl --fail -s "$1"
141 | }
142 |
143 | # url, key
144 | function download_with_cache() {
145 | mkdir -p "$dest/cache"
146 | local out
147 | out="$dest/cache/$2"
148 | if [[ ! -f "$out" ]]; then
149 | curl --fail -s -o "$out" "$1"
150 | fi
151 | echo "$out"
152 | }
153 |
154 | # dst, file
155 | function unpack_deb() {
156 | local tmp
157 | tmp=$(mktemp -d /tmp/.unpack_deb_XXXXXX)
158 | (
159 | cd "$tmp"
160 | ar -x "$2"
161 | tar -C "$1" -xf data.tar.*
162 | )
163 | rm -rf "$tmp"
164 | }
165 |
166 | # dst, file
167 | function unpack_pkg() {
168 | local tmp
169 | tmp=$(mktemp -d /tmp/.unpack_pkg_XXXXXX)
170 | (
171 | cd "$tmp"
172 | xar -xf "$2"
173 | )
174 | (
175 | cd "$1"
176 | cat "$tmp"/*.pkg/Payload | gunzip -dc | cpio -i
177 | )
178 | rm -rf "$tmp"
179 | }
180 |
181 | # dst, file
182 | function unpack() {
183 | ext=${2##*.}
184 | "unpack_$ext" "$@"
185 | }
186 |
187 | cd "$dest"
188 |
189 | rm -rf $cross_tc_basename
190 | mkdir -p "$cross_tc_basename/$linux_sdk_name"
191 |
192 | # oopsie, this is slow but seemingly fast enough :)
193 | while read -r line; do
194 | for pkg_name in "${pkg_names[@]}"; do
195 | if [[ "$line" =~ ^Filename:\ (.*\/([^/_]+)_.*$) ]]; then
196 | # echo "${BASH_REMATCH[2]}"
197 | if [[ "${BASH_REMATCH[2]}" == "$pkg_name" ]]; then
198 | new_pkg="$ubuntu_mirror/${BASH_REMATCH[1]}"
199 | pkgs+=( "$new_pkg" )
200 | echo "- will download $new_pkg"
201 | fi
202 | fi
203 | done
204 | done < <(download_stdout "$packages_file" | gunzip -d -c | grep ^Filename:)
205 |
206 |
207 | # --- Unpack Linux Platform SDK ---------------------------------------------
208 | tmp=$(mktemp -d "$dest/tmp_pkgs_XXXXXX")
209 | (
210 | cd "$tmp"
211 | for f in "${pkgs[@]}"; do
212 | name="$(basename "$f")"
213 | archive="$(download_with_cache "$f" "$name")"
214 | unpack "$dest/$cross_tc_basename/$linux_sdk_name" "$archive"
215 | done
216 | )
217 | rm -rf "$tmp"
218 |
219 |
220 | # --- Build Binutils and Gold ------------------------------------------------
221 | (
222 | cd $cross_tc_basename
223 | mkdir -p "$xc_tc_name/usr/bin"
224 |
225 | binutils_pkg="$(download_with_cache "$binutils_pkg_url" binutils.tar.gz)"
226 | tmp=$(mktemp -d "$dest/tmp_pkgs_XXXXXX")
227 | (
228 | cd "$tmp"
229 | echo "Unpacking binutils: ${binutils_pkg} to ${tmp} .."
230 | tar -xf "$binutils_pkg"
231 | cd binutils-*
232 | # 4secs on ZeaPro
233 | echo " Configuring binutils in ${tmp} (~4s) .."
234 | ./configure --enable-gold > xt-binutils-configure.log 2>&1
235 | echo " .. building binutils (~1m) .."
236 | time make -j > xt-binutils-make.log 2>&1
237 | echo " done."
238 | cd gold
239 | echo " Configuring gold (~11s).."
240 | ./configure --enable-gold > xt-gold-configure.log 2>&1
241 | echo " .. building gold (~1m).."
242 | time make -j > xt-gold-make.log 2>&1
243 | echo " done."
244 | )
245 | echo "done with gold, copy to: ${xc_tc_name}/usr/bin/ld.gold"
246 | cp "$tmp"/binutils-*/gold/ld-new "$xc_tc_name/usr/bin/ld.gold"
247 | rm -rf "$tmp"
248 |
249 |
250 | # --- Patch Absolute Links ---------------------------------------------------
251 | echo "Fixing absolute links in ${linux_sdk_name} ..."
252 | # fix absolute symlinks
253 | find "$linux_sdk_name" -type l | while read -r line; do
254 | dst=$(readlink "$line")
255 | if [[ "${dst:0:1}" = / ]]; then
256 | echo " Patch Link: ${line} dest ${dst}"
257 | rm "$line"
258 | #echo ln -s "$dest/$cross_tc_basename/$linux_sdk_name$dst" "$line"
259 | ln -s "$dest/$cross_tc_basename/$linux_sdk_name$dst" "$line"
260 | #else
261 | # echo " Link: ${line} OK."
262 | fi
263 | done
264 | # HH: can't find that
265 | #ln -s 5 "$linux_sdk_name/usr/lib/gcc/${TC_TARGET}/5.4.0"
266 | echo "Fixed absolute links."
267 |
268 |
269 | # --- Unpack macOS Swift Toolchain -------------------------------------------
270 | tmp=$(mktemp -d "$dest/tmp_pkgs_XXXXXX")
271 | echo "Unpacking macOS toolchain: ${macos_swift_pkg} to ${tmp} .."
272 | unpack "$tmp" "$macos_swift_pkg"
273 | echo ".. copy to ${xc_tc_name} .."
274 | rsync -a "$tmp/" "$xc_tc_name"
275 | echo ".. delete tmpdir ..."
276 | rm -rf "$tmp"
277 | echo "Did unpack into ${xc_tc_name}."
278 |
279 |
280 | # --- Unpack Raspi Swift toolchain -------------------------------------------
281 | tmp=$(mktemp -d "$dest/tmp_pkgs_XXXXXX")
282 | # official release tarballs have a dirname, the Raspi ones usually don't but
283 | # just directly contain 'usr'
284 | # --strip-components 1
285 | echo "Unpacking Raspi toolchain: ${linux_swift_pkg} to ${tmp} .."
286 | tar -C "$tmp" -xf "$linux_swift_pkg"
287 |
288 | echo ".. copy to ${xc_tc_name} .."
289 | rsync -a "$tmp/usr/lib/swift/linux" "$xc_tc_name/usr/lib/swift/"
290 | rsync -a "$tmp/usr/lib/swift_static/linux" "$xc_tc_name/usr/lib/swift_static/"
291 | # HH: not necessary, name is aarch64
292 | #(cd "$xc_tc_name/usr/lib/swift/linux"; ln -s armv7 arm)
293 |
294 | rsync -a "$tmp/usr/lib/swift/dispatch" "$linux_sdk_name/usr/include/"
295 | rsync -a "$tmp/usr/lib/swift/os" "$linux_sdk_name/usr/include/"
296 | rsync -a "$tmp/usr/lib/swift/CoreFoundation" "$linux_sdk_name/usr/include/"
297 | echo ".. delete tmpdir ..."
298 | rm -rf "$tmp"
299 | echo "Did unpack into ${xc_tc_name}."
300 |
301 |
302 | # ----------------------------------------------------------------------------
303 | # HH: we do have a Block.h
304 | #curl --fail -s -o "$linux_sdk_name/usr/include/Block.h" "$blocks_h_url"
305 | # this exists
306 | #ln -s swift "$xc_tc_name/usr/bin/swift-autolink-extract"
307 | ln -s "$dest/$cross_tc_basename/$xc_tc_name/usr/lib/swift/linux/libdispatch.so" "$linux_sdk_name/usr/lib/"
308 | #ln -s "$dest/$cross_tc_basename/$xc_tc_name/usr/lib/swift/linux/libdispatch.a" "$linux_sdk_name/usr/lib/"
309 |
310 | # fix up
311 | )
312 |
313 | # HH DEBUG
314 | ls -la "$cross_tc_basename/$xc_tc_name/usr/lib/swift/linux/"
315 | fix_glibc_modulemap "$cross_tc_basename/$xc_tc_name/usr/lib/swift/linux/${TARGET_ARCH}/glibc.modulemap"
316 |
317 |
318 | # ----------------------------------------------------------------------------
319 | echo "**** emit json"
320 |
321 | cat > "$cross_tc_basename/arm64v8-ubuntu-bionic-destination.json" <RasPi -> macOS Cross Compilation Toolchain
2 |
4 |
5 |
6 | 
7 | 
8 | 
9 |
10 | **NOTE**: This has been moved into its own repository:
11 | [swift-arm2mac-x-compile-toolchain](https://github.com/AlwaysRightInstitute/swift-arm2mac-x-compile-toolchain).
12 |
13 | So, I got that new late-2016 MacBook Pro (yes, the touchbar is aweful,
14 | except for the
15 | [Lemmings](https://github.com/erikolsson/Touch-Bar-Lemmings),
16 | those are cool).
17 | Everyone knows that with just 16GB of RAM it isn't really a Pro machine and
18 | you can hardly do any work on it.
19 | As an occasional Swift developer who also
20 | [runs the Slack app](https://twitter.com/helje5/status/771699792808386560)
21 | once in a while
22 | I'm always looking for ways to workaround that frustrating limitation.
23 |
24 | So while working on the cross-compiler toolchain which allows you to compile
25 | Swift code for the Raspberry Pi on a Mac (read about it [here](../README.md)),
26 | a brilliant idea was formed:
27 |
28 | > "The real question is can you cross compile to macOS from your Raspberry Pi
29 | > build fleet"
30 | > *ducks*
31 |
32 | That of course is the solution. Instead of bothering the Mac to produce Swift
33 | binaries for the Mac, why not cross compile the Swift code for the Mac on one
34 | of the Raspberry Pis sitting around in the house, usually idle.
35 |
36 | Like that:
37 |
38 |
39 |
40 |
41 | ## Building the macOS toolchain for ARM
42 |
43 | What we are going to do is build a Swift 3.1.1 cross compilation toolchain
44 | running on ARM Ubuntu Xenial, targetting macOS.
45 |
46 | This is a little harder that the [Mac to Raspi cross compiler](../README.md),
47 | but still reasonably easy.
48 |
49 | ### Step 1: Prepare toolchain on the Mac
50 |
51 | Before switching to the Raspi, we need to grab system headers and libraries
52 | from the Mac.
53 | We also need to convert the Swift 3.1.1 toolchain for Mac to a tarball
54 | (for whatever reason I couldn't get `xar` to run on ARM - works fine on x86-64).
55 |
56 | Grab macOS system headers and libs:
57 | ```
58 | (cd /; tar zcf /tmp/x-macos-linux-x86_64-usr.tar.gz usr/lib usr/include)"
59 | ```
60 |
61 | Download the Swift 3.1.1 Mac toolchain and convert it to a tarball
62 | (use our `extract-toolchain-pkg-to-tar.sh` to do the latter):
63 | ```
64 | curl -o /tmp/swift-3.1.1-RELEASE-osx.pkg \
65 | https://swift.org/builds/swift-3.1.1-release/xcode/swift-3.1.1-RELEASE/swift-3.1.1-RELEASE-osx.pkg
66 | ./extract-toolchain-pkg-to-tar.sh \
67 | /tmp/swift-3.1-RELEASE-osx.pkg \
68 | /tmp/swift-3.1-RELEASE-osx.tar.gz
69 | ```
70 |
71 | Transfer the two files to your Raspi, e.g. using `scp`:
72 | ```
73 | scp /tmp/x-macos-linux-x86_64-usr.tar.gz \
74 | /tmp/swift-3.1-RELEASE-osx.tar.gz \
75 | pirate@black-pearl.local:/tmp
76 | ```
77 |
78 |
79 | ### Step 2: Build Toolchain using Script
80 |
81 | On the Raspi we first need to make sure we have some packages necessary
82 | for the build:
83 |
84 | ```bash
85 | sudo apt-get install -y bison libxml2-dev cpio lsb-release libssl-dev
86 | ```
87 |
88 | Next download our toolchain script onto your Raspi and make it executable:
89 | [build-rpi-ubuntu-2-macos-x-toolchain](https://raw.githubusercontent.com/helje5/dockSwiftOnARM/master/toolchain/macosx/build-rpi-ubuntu-2-macos-x-toolchain),
90 | e.g. like:
91 |
92 | ```bash
93 | pushd /tmp
94 | curl https://raw.githubusercontent.com/helje5/dockSwiftOnARM/master/toolchain/macosx/build-rpi-ubuntu-2-macos-x-toolchain \
95 | | sed "s/$(printf '\r')\$//" \
96 | > build-rpi-ubuntu-2-macos-x-toolchain
97 | chmod +x build-rpi-ubuntu-2-macos-x-toolchain
98 | ```
99 |
100 | You can call the script and it'll give you instructions, but let's just
101 | go ahead.
102 | Next step is to download Swift 3.1.1 tarballs.
103 | We need the macOS toolchain tarballs we created in Step 1
104 | and a Raspberry Pi tarball for the Swift compiler.
105 | We use the one provided by Florian Friedrich for the latter:
106 |
107 | ```bash
108 | pushd /tmp
109 | curl -o /tmp/swift-3.1.1-armv7l-ubuntu16.04.tar.gz https://cloud.sersoft.de/nextcloud/index.php/s/0qty8wJxlgfVCcx/download
110 | ```
111 |
112 | Once those are available, build the actual toolchain using the script:
113 |
114 | ```bash
115 | pushd /tmp
116 | ./build-rpi-ubuntu-2-macos-x-toolchain \
117 | /tmp/ \
118 | /tmp/swift-3.1.1-armv7l-ubuntu16.04.tar.gz
119 | /tmp/swift-3.1.1-RELEASE-osx.tar.gz \
120 | /tmp/x-macos-darwin-x86_64-usr.tar.gz
121 | ```
122 |
123 | It takes about 20 minutes on a Raspi 3b, so grab a 🍺 or 🍻 while you wait.
124 | If everything worked fine, it'll end like that:
125 | ```
126 | OK, your cross compilation toolchain for macOS is now ready to be used
127 | - SDK: /tmp/cross-toolchain/MacOSX.sdk
128 | - toolchain: /tmp/cross-toolchain/swift.xctoolchain
129 | - SwiftPM destination.json: /tmp/cross-toolchain/macos-destination.json
130 | ```
131 |
132 |
133 | ### Step 3: Install a recent Swift Package Manager
134 |
135 | To use the cross compilation support, we need a pretty recent Swift Package
136 | Manager.
137 | On a regular x86-64 Ubuntu or macOS we could just grab a snapshot,
138 | but the Swift project doesn't yet provide such for Raspberry Pis.
139 |
140 | Instead we are going to fetch a recent Swift Package Manager,
141 | but use it with the Swift 3.1 installation.
142 |
143 | ```bash
144 | git clone https://github.com/apple/swift-package-manager.git
145 | cd swift-package-manager
146 | git checkout 5c88e044abb8943598749d2e95007605e0660377
147 | swift build -c release
148 | ```
149 |
150 | *Note*: We are indeed building a 3.1.1 toolchain! We are just using a new
151 | Swift Package Manager.
152 |
153 | Subsequently I assume you are using my
154 | [helje5/rpi-swift-dev](https://hub.docker.com/r/helje5/rpi-swift-dev/)
155 | Docker image.
156 | If you are using a different setup, adjust as necessary.
157 |
158 | Integrate the SPM we just built into the existing Swift setup:
159 |
160 | ```bash
161 | sudo mv /usr/bin/swift-build /usr/bin/swift-build.org
162 | sudo mv /usr/bin/swift-test /usr/bin/swift-test.org
163 | sudo mv /usr/bin/swift-package /usr/bin/swift-package.org
164 | sudo mv /usr/lib/swift/pm/PackageDescription.swiftmodule \
165 | /usr/lib/swift/pm/PackageDescription.swiftmodule.org
166 |
167 | sudo mkdir -p /usr/lib/swift/pm/3 /usr/lib/swift/pm/4
168 |
169 | sudo cp .build/release/swift-* /usr/bin/
170 | sudo cp .build/release/libclibc.so /usr/lib/swift/pm/
171 | sudo cp .build/release/*.so /usr/lib/swift/pm/3/
172 | sudo cp .build/release/*.so /usr/lib/swift/pm/4/
173 | sudo cp .build/release/PackageDescription.swiftmodule /usr/lib/swift/pm/3/
174 | sudo cp .build/release/PackageDescription4.swiftmodule /usr/lib/swift/pm/4/
175 | sudo cp .build/release/PackageDescription4.swiftmodule \
176 | /usr/lib/swift/pm/4/PackageDescription.swiftmodule
177 | sudo ldconfig
178 | ```
179 |
180 | After that your environment should look like this:
181 | ```bash
182 | swift@xyz: swift --version
183 | Swift version 3.1.1 (swift-3.1.1-RELEASE)
184 | Target: armv7-unknown-linux-gnueabihf
185 |
186 | swift@xyz: swift build --version
187 | Swift Package Manager - Swift 4.0.0-dev
188 | ```
189 |
190 |
191 | ### Step 4: Use the Toolchain
192 |
193 | Lets create a simple `helloworld` tool first. We cannot use
194 | `swift package init` here, because we want to setup a Swift 3 module,
195 | not a Swift 4 one.
196 |
197 | ```bash
198 | mkdir helloworld && cd helloworld
199 |
200 | cat > Package.swift < main.swift
206 |
207 | swift build --destination /tmp/cross-toolchain/macos-destination.json
208 | ```
209 |
210 | Which gives:
211 | ```
212 | Compile Swift Module 'helloworld' (1 sources)
213 | Linking ./.build/debug/helloworld
214 | ```
215 |
216 | Check whether it actually produced a MACH-O (macOS) binary:
217 | ```
218 | file .build/debug/helloworld
219 | .build/debug/helloworld: Mach-O 64-bit x86_64 executable
220 | ```
221 |
222 | Excellent! It worked. Copy your binary to a Mac and run it! :-)
223 |
224 | ## OK, no README w/o 🐄🐄🐄
225 |
226 | Lets build something very useful, an ASCII cow generator.
227 | The snapshot's `swift package init` produces a Swift 4 setup by default.
228 | We want to use 3.1, so we do the setup manually:
229 |
230 | ```
231 | mkdir vaca && cd vaca
232 | cat > Package.swift < main.swift <