-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcreate-toolchain
executable file
·119 lines (105 loc) · 4.02 KB
/
create-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
#!/usr/bin/env bash
set -e
cd "$(dirname "$0")"
DARWIN_TOOLS_VERSION="2.2.1"
[[ "$(uname -s)" = "Darwin" ]] && is_mac=true
if [[ $# -ne 1 && $# -ne 2 ]]; then
if [[ "${is_mac}" = true ]]; then
echo "Usage: $0 <linux version such as ubuntu20.04> [path to xctoolchain]"
else
echo "Usage: $0 <swift version> [linux version such as ubuntu20.04]"
fi
exit 1
fi
info() { echo "[info] $1"; }
err() {
echo "[error] $1"
exit 1
}
mkdir -p toolchains
rm -rf toolchains/*.tmp
# Download and cache
dl() {
[[ -f "$1" ]] && return
curl -#Lo "$1.tmp" "$2"
mv "$1.tmp" "$1"
}
if [[ "${is_mac}" = true ]]; then
linux_version="$1"
mac_toolchain="$(cd "${2:-"$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain"}" && pwd)"
swift_version="$("${mac_toolchain}/usr/bin/swift" --version 2>/dev/null | head -1 | cut -f4 -d" ")"
info "Detected Swift version ${swift_version}"
cd toolchains
else
swift_version="$1"
linux_version="$2"
if [[ -z "${linux_version}" ]]; then
if [[ -f /etc/os-release ]]; then
. /etc/os-release
linux_version="${ID}${VERSION_ID}"
info "Detected Linux version ${linux_version}"
else
err "Could not auto-detect Linux version; please pass it as the second argument to '$0'."
fi
fi
file_name_mac="swift-${swift_version}-RELEASE-osx"
package_mac="${file_name_mac}-package.pkg"
info "Downloading macOS toolchain"
cd toolchains
dl "toolchain-mac-${swift_version}.pkg" "https://swift.org/builds/swift-${swift_version}-release/xcode/swift-${swift_version}-RELEASE/${file_name_mac}.pkg"
info "Unpacking macOS toolchain"
bsdtar -xvf "toolchain-mac-${swift_version}.pkg" "${package_mac}/Payload"
mkdir -p mac
rm -rf mac/*
bsdtar -xvf "${package_mac}/Payload" -C mac
rm -rf *.pkg
mac_toolchain="mac"
fi
file_name_linux="swift-${swift_version}-RELEASE-${linux_version}"
dl "toolchain-${linux_version}-${swift_version}.tar.gz" "https://swift.org/builds/swift-${swift_version}-release/${linux_version/./}/swift-${swift_version}-RELEASE/${file_name_linux}.tar.gz"
info "Unpacking Linux toolchain"
rm -rf linux
tar -xzvf "toolchain-${linux_version}-${swift_version}.tar.gz"
mv "${file_name_linux}" linux
mv linux/{usr,host}
link_file() {
# example of $1: linux/host/bin/swiftc
usr_path="${1#linux/host/}"
iphone_path="linux/iphone/${usr_path}"
mkdir -p "$(dirname "${iphone_path}")"
# calculate relative path to host file; based on
# https://stackoverflow.com/a/27175714/3769927
usr_path_curr="${usr_path}"
rel_prefix="../"
while [[ "${usr_path_curr}" = */* ]]; do
usr_path_curr="${usr_path_curr%/*}"
rel_prefix="../${rel_prefix}"
done
ln -s "${rel_prefix}host/${usr_path}" "${iphone_path}"
}
export -f link_file
info "Modifying Linux toolchain"
# symlink all files in these dirs from the host toolchain...
find linux/host \( -path 'linux/host/bin/*' -o -path 'linux/host/local/*' -o -path 'linux/host/share/*' -o -path 'linux/host/lib/*' \) -and -not -type d -print0 | xargs -0 -I{} bash -c 'link_file "$@"' _ {}
# ...but replace some components with mac ones...
rm -rf linux/iphone/lib/swift{,_static}
if [[ "${is_mac}" = true ]]; then
cp -a "${mac_toolchain}"/usr/lib/swift-5.0 linux/iphone/lib/
else
info "Note: this toolchain will ONLY support iOS 12.2+."
fi
cp -a "${mac_toolchain}"/usr/lib/{swift,swift-5.5} linux/iphone/lib/
cp -a "${mac_toolchain}"/usr/lib/clang/*/lib/* linux/iphone/lib/clang/*/lib/
cp -a "${mac_toolchain}"/usr/include linux/iphone/include
# ...but-but replace the mac lib/swift/clang symlink with the linux one
rm linux/iphone/lib/swift/clang
cp -a linux/{host,iphone}/lib/swift/clang
info "Installing Darwin tools"
# The hierarchy lines up with ours just right so we dont need to -C
dl "darwin-tools-${linux_version}.tar.gz" "https://github.com/kabiroberai/darwin-tools-linux/releases/download/v${DARWIN_TOOLS_VERSION}/darwin-tools-${linux_version}.tar.gz"
tar -xzvf "darwin-tools-${linux_version}.tar.gz"
info "Packaging toolchain"
mkdir -p ../packages
# this takes forever but xz is around half the size of gz so imo worth it
tar -cJvf "../packages/swift-${swift_version}-${linux_version}.tar.xz" linux
rm -rf linux mac