-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-rpi-ubuntu-2-macos-x-toolchain
executable file
·263 lines (212 loc) · 8.31 KB
/
build-rpi-ubuntu-2-macos-x-toolchain
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/bin/bash
#
# Based on
# - https://github.com/apple/swift-package-manager/blob/master/Utilities/build_ubuntu_cross_compilation_toolchain
# by Johannes Weiß
# MacOS adjustments by Helge Heß <[email protected]>
#
# In this setup:
# - SWIFT-FOR-LINUX.tar.gz is the cross compiler
# - the Swift binary files come from the .pkg (libs and such)
# But we also need a tarball containing the macOS system headers:
# (cd /; tar zcf /tmp/x-macos-$(uname -s | tr [:upper:] [:lower:])-$(uname -m)-usr.tgz usr/lib usr/include)
# NOTE:
# - the Ubuntu variant also contains some /etc and /lib
#
# Requirements:
# - Make sure you have those installed:
# - clang (3.8)
# - bison
# - libxml2-dev
# - cpio
# - lsb-release
# - libssl-dev
set -eu
export PATH="/bin:/usr/bin"
export TC_TARGET="x86_64-apple-darwin16.5.0"
function usage() {
echo >&2 "Usage: $0 SWIFT-FOR-LINUX.tar.gz SWIFT-FOR-MACOS.tar.gz MACOS-SDK.tar.gz"
echo >&2
echo >&2 "Example: $0 /tmp/ ~/Downloads/swift-3.1-RELEASE-osx.pkg ~/Downloads/swift-3.1-RELEASE-ubuntu16.04.tar.gz"
echo >&2
echo >&2 "Complete example:"
echo >&2 " # grab /usr/include and /usr/lib from a Mac"
echo >&2 " (cd /; tar zcf /tmp/x-macos-linux-x86_64-usr.tgz usr/lib usr/include)"
echo >&2
echo >&2 " # Download the macOS Swift toolchain package on the Mac and convert to tarball"
echo >&2 " curl -o /tmp/swift-3.1.1-RELEASE-osx.pkg https://swift.org/builds/swift-3.1.1-release/xcode/swift-3.1.1-RELEASE/swift-3.1.1-RELEASE-osx.pkg"
echo >&2 " ./extract-toolchain-pkg-to-tar.sh /tmp/swift-3.1-RELEASE-osx.pkg /tmp/swift-3.1-RELEASE-osx.tar.gz"
echo >&2
echo >&2 " # copy toolchain stuff to Raspi"
echo >&2 " scp /tmp/x-macos-linux-x86_64-usr.tgz /tmp/swift-3.1-RELEASE-osx.tar.gz [email protected]:/tmp"
echo >&2
echo >&2 " # Download the Swift binaries for Ubuntu and macOS"
echo >&2 " curl -o /tmp/swift-3.1.1-armv7l-ubuntu16.04.tar.gz https://cloud.sersoft.de/nextcloud/index.php/s/0qty8wJxlgfVCcx/download"
echo >&2
echo >&2 " # Compile the SDK and toolchain from that"
echo >&2 " $0 /tmp/ /tmp/swift-3.1.1-armv7l-ubuntu16.04.tar.gz /tmp/swift-3.1-RELEASE-osx.tar.gz /tmp/x-macos-darwin-x86_64-usr.tgz"
echo >&2
echo >&2 " # Create a test application"
echo >&2 " mkdir my-test-app && cd my-test-app"
echo >&2 " swift package init --type=executable"
echo >&2 " # Build it for macOS"
echo >&2 " swift build --destination /tmp/cross-toolchain/macos-destination.json"
}
if [[ $# -ne 4 ]]; then
usage
exit 1
fi
function realpath() {
if [[ "${1:0:1}" = / ]]; then
echo "$1"
else
(
cd "$(dirname "$1")"
echo "$(pwd)/$(basename "$1")"
)
fi
}
# set -xv
# where to get stuff from
dest=$(realpath "$1")
linux_swift_pkg=$(realpath "$2")
macos_swift_pkg=$(realpath "$3")
macos_sdk_pkg=$(realpath "$4")
if [[ ! -f "$linux_swift_pkg" ]]; then echo "missing: $linux_swift_pkg"; exit 42; fi
if [[ ! -f "$macos_swift_pkg" ]]; then echo "missing: $macos_swift_pkg"; exit 42; fi
if [[ ! -f "$macos_sdk_pkg" ]]; then echo "missing $macos_sdk_pkg"; exit 42; fi
# config
xc_tc_name="swift.xctoolchain"
target_sdk_name="MacOSX.sdk"
cross_tc_basename="cross-toolchain"
cctools_pkg_url="https://github.com/tpoechtrager/osxcross/raw/master/tarballs/cctools-895-ld64-274.2_8e9c3f2.tar.xz"
# url
function download_stdout() {
curl --fail -s "$1"
}
# url, key
function download_with_cache() {
mkdir -p "$dest/cache"
local out
out="$dest/cache/$2"
if [[ ! -f "$out" ]]; then
curl --fail -L -s -o "$out" "$1"
fi
echo "$out"
}
# ------------------------------------------------
echo "Starting, go to destination directory: $dest ..."
cd "$dest"
rm -rf $cross_tc_basename # cross-toolchain
mkdir -p "$cross_tc_basename/$target_sdk_name"
echo " target: $cross_tc_basename/$target_sdk_name"
# ------------------ untar SDK (macos.sdk/usr/) ----------------------------
echo " unpacking SDK to: $dest/$cross_tc_basename/$target_sdk_name"
# unpack macOS SDK
# $macos_sdk_pkg - tarball containing usr/include, usr/lib
tar -C "$dest/$cross_tc_basename/$target_sdk_name" -xf "$macos_sdk_pkg"
# ------------------ cctools ---------------------------
echo " grabbing cctools ..."
(
cd $cross_tc_basename # cross-toolchain
mkdir -p "$xc_tc_name/usr/bin" # xc_tc_name = swift.xctoolchain
cctools_pkg="$(download_with_cache "$cctools_pkg_url" cctools.tar.gz)"
echo " download to: $cctools_pkg"
tmp=$(mktemp -d "$dest/tmp_pkgs_cctools_XXXXXX")
(
cd "$tmp"
tar -Jxf "$cctools_pkg"
cd cctools-*
cd cctools
echo " configure cctools in $(pwd) ..."
mv configure configure.org
mv configure.ac configure.ac.org
sed < configure.ac.org >configure.ac "s/-Wno-shift-negative-value//g"
sed < configure.org >configure "s/-Wno-shift-negative-value//g"
chmod +x configure
./configure "--prefix=${dest}/${cross_tc_basename}/${xc_tc_name}/usr" \
--target=${TC_TARGET}
# --target=x86_64-apple-darwin11
# remove libobjc and otool in Makefiles
mv Makefile Makefile.org
sed < Makefile.org >Makefile "s/libobjc2 otool//g"
make -j
make install
)
ln "${xc_tc_name}/usr/bin/${TC_TARGET}-ld" "${xc_tc_name}/usr/bin/ld"
rm -rf "$tmp"
)
echo " cctools ld in: $xc_tc_name/usr/bin/x86_64-apple-darwin11-ld"
# ------------------ dsymutil ---------------------------
# Yeah, hack. Would need to port that ;->
(
cd $cross_tc_basename # cross-toolchain
mkdir -p "$xc_tc_name/usr/bin" # xc_tc_name = swift.xctoolchain
cd "$xc_tc_name/usr/bin"
echo "#!/bin/bash" > dsymutil
echo "echo 'dsymutil ... not doing anything ...'" >> dsymutil
chmod +x dsymutil
)
# ------------------ symlinks ----------------------------
echo " fixing absolute symlinks in $target_sdk_name ..."
# fix absolute symlinks
cd "$dest"
cd $cross_tc_basename # cross-toolchain
find "$target_sdk_name" -type l | while read -r line; do
dst=$(readlink "$line")
if [[ "${dst:0:1}" = / ]]; then
rm "$line"
echo ln -s "$dest/$cross_tc_basename/$target_sdk_name$dst" "$line"
ln -s "$dest/$cross_tc_basename/$target_sdk_name$dst" "$line"
fi
done
# no gcc on macOS? ;-)
#ln -s 5 "$target_sdk_name/usr/lib/gcc/${TC_TARGET}/5.4.0"
# ------------------ untar Linux Swift ----------------------------
# In this setup:
# - SWIFT-FOR-LINUX.tar.gz is the cross compiler
# aka what goes into "swift.xctoolchain"
tmp=$(mktemp -d "$dest/tmp_pkgs_XXXXXX")
echo " Unpacking Linux Swift SDK: $linux_swift_pkg to $tmp..."
# official release tarballs have a dirname, the Raspi ones usually don't but
# just directly contain 'usr'
# --strip-components 1
tar -C "$tmp" -xf "$linux_swift_pkg"
echo " Moving Linux Swift SDK from $tmp to $xc_tc_name ..." # xc_tc_name = swift.xctoolchain
rsync -a "$tmp/" "$xc_tc_name"
rm -rf "$tmp"
# ------------------ untar macOS Swift ----------------------------
tmp=$(mktemp -d "$dest/tmp_pkgs_XXXXXX")
echo " Unpacking macOS Swift SDK: $macos_swift_pkg to $tmp..."
tar -C "$tmp" -xf "$macos_swift_pkg"
mkdir -p "$xc_tc_name/usr/lib/swift/" "$xc_tc_name/usr/lib/swift_static/"
echo " Moving macOS Swift SDK $tmp/usr/lib/swift... to $xc_tc_name/usr/lib/swift ..."
rsync -a "$tmp/usr/lib/swift/macosx" "$xc_tc_name/usr/lib/swift/"
rsync -a "$tmp/usr/lib/swift_static/macosx" "$xc_tc_name/usr/lib/swift_static/"
rsync -a "$tmp/usr/lib/swift/shims" "$xc_tc_name/usr/lib/swift/"
rm -rf "$tmp"
cd $dest
echo "**** emit json to $cross_tc_basename/macos-destination.json"
cat > "$cross_tc_basename/macos-destination.json" <<EOF
{
"version": 1,
"sdk": "$(pwd)/$cross_tc_basename/$target_sdk_name",
"toolchain-bin-dir": "$(pwd)/$cross_tc_basename/$xc_tc_name/usr/bin",
"target": "${TC_TARGET}",
"dynamic-library-extension": "dylib",
"extra-cc-flags": [
"-fPIC"
],
"extra-swiftc-flags": [
"-tools-directory", "$(pwd)/$cross_tc_basename/$xc_tc_name/usr/bin"
],
"extra-cpp-flags": [
"-lstdc++"
]
}
EOF
echo
echo "OK, your cross compilation toolchain for macOS is now ready to be used"
echo " - SDK: $(pwd)/$cross_tc_basename/$target_sdk_name"
echo " - toolchain: $(pwd)/$cross_tc_basename/$xc_tc_name"
echo " - SwiftPM destination.json: $(pwd)/$cross_tc_basename/macos-destination.json"