-
Notifications
You must be signed in to change notification settings - Fork 5
/
fetch_toolchain
executable file
·228 lines (192 loc) · 6.08 KB
/
fetch_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
#!/usr/bin/env bash
# This script downloads, builds and deploys a D-to-LLVM-to-ASMJS toolchain.
# Usage:
# $ ./fetch_toolchain
# A painfully long time after, you've got a full toolchain in '/tmp/toolchains/'
set -euo pipefail
readonly PREFIX=${PREFIX:-/tmp/toolchains}
readonly tmpDir=${tmpDir:-/tmp/dscripten-tmp}
echo "Using prefix: $PREFIX"
echo "Using tmpDir: $tmpDir"
readonly PREFIX_JS=$PREFIX/llvm-js
readonly PREFIX_EMS=$PREFIX/emscripten
readonly scriptDir=$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)
readonly MAKE=(make -j"$(nproc)")
function main
{
mkdir -p "$tmpDir"
ensureTool make
ensureTool cmake
ensureTool pkg-config
ensureTool patch
ensureTool git
ensureTool dmd # ldc requires this
buildEmscripten
buildLdc
configureEmcc
PATH="$PREFIX_EMS:$PATH" \
buildExtraLibs
echo ""
echo "================================================================"
echo "Success. Your asmjs toolchain is now ready."
}
function ensureTool
{
local progName=$1
echo -n "Checking for $progName ... "
if ! which $progName 1>/dev/null 2>/dev/null; then
echo "Can't find '$progName' in PATH. Aborting."
return 1
fi
echo "OK"
}
function configureEmcc
{
# will create $HOME/.emscripten
PATH=$PREFIX_JS/bin:$PATH \
"$PREFIX_EMS"/emcc --version
}
function buildExtraLibs
{
# This causes the emscripten ports to be built
echo "int main() { return 0; }" > $tmpDir/simple.cpp
emcc $tmpDir/simple.cpp \
-s USE_SDL=2\
-s USE_OGG=1\
-s USE_VORBIS=1\
-s USE_SDL_IMAGE=2\
-o $tmpDir/simple.html
echo "All extra libs OK"
}
# Here, we build and install:
# - ldc D2 compiler (requires LLVM >= 3.5)
function buildLdc
{
if [ ! -d "$tmpDir"/ldc ] ; then
echo "Cloning LDC ..."
git clone -b master --single-branch --recursive https://github.com/ldc-developers/ldc.git "$tmpDir"/ldc
gitCheckout "$tmpDir/ldc" '187d8198e63564c633f22f2ef4db2a31a8a600ce'
runFrom "$tmpDir"/ldc git submodule init
runFrom "$tmpDir"/ldc git submodule update
patch --merge -d "$tmpDir"/ldc -p1 -i "$scriptDir"/ldc.patch
fi
mkdir -p "$tmpDir"/bin/ldc
runFrom "$tmpDir"/bin/ldc cmake \
-G "Unix Makefiles" \
-D "CMAKE_INSTALL_PREFIX=$PREFIX_JS" \
-D "LDC_DYNAMIC_COMPILE=OFF" \
-D "LLVM_CONFIG=$PREFIX_JS/bin/llvm-config" \
../../ldc
runFrom "$tmpDir"/bin/ldc "${MAKE[@]}" -k || true
runFrom "$tmpDir"/bin/ldc make -j2 # retry with less parallelism (workaround potential memory limitations)
runFrom "$tmpDir"/bin/ldc "${MAKE[@]}" install
}
# Here, we build and install:
# - 'fastcomp' LLVM
# - 'fastcomp' clang frontend (which requires 'fastcomp' LLVM)
# - emscripten (python wrapper scripts + header files + pkg-config files)
function buildEmscripten
{
# emcc and other python wrappers
if [ ! -d "$PREFIX_EMS" ] ; then
echo "Cloning emscripten ..."
git clone -q --depth=1000 -b incoming --single-branch https://github.com/kripken/emscripten.git "$PREFIX_EMS"
gitCheckout "$PREFIX_EMS" '1.38.26'
# fake pkg-config files to keep happy the build systems of user projects, which don't know about emscripten
cp "$PREFIX_EMS"/system/lib/pkgconfig/sdl.pc "$PREFIX_EMS"/system/lib/pkgconfig/SDL_image.pc
cp "$PREFIX_EMS"/system/lib/pkgconfig/sdl.pc "$PREFIX_EMS"/system/lib/pkgconfig/SDL_gfx.pc
generateGlPc > "$PREFIX_EMS"/system/lib/pkgconfig/gl.pc
generateSdl2Pc > "$PREFIX_EMS"/system/lib/pkgconfig/sdl2.pc
generateSdl2ImagePc > "$PREFIX_EMS"/system/lib/pkgconfig/SDL2_image.pc
generateOggPc > "$PREFIX_EMS"/system/lib/pkgconfig/ogg.pc
generateVorbisfilePc > "$PREFIX_EMS"/system/lib/pkgconfig/vorbisfile.pc
fi
# the asmjs backend
if [ ! -d "$tmpDir"/emscripten-llvm ] ; then
echo "Cloning emscripten-fastcomp ..."
git clone -q --depth=100 -b incoming --single-branch https://github.com/kripken/emscripten-fastcomp.git "$tmpDir"/emscripten-llvm
gitCheckout "$tmpDir"/emscripten-llvm '1.38.26'
fi
# forked clang front-end
if [ ! -d "$tmpDir"/emscripten-llvm/tools/clang ] ; then
echo "Cloning emscripten-fastcomp-clang ..."
git clone -q --depth=100 -b incoming --single-branch https://github.com/kripken/emscripten-fastcomp-clang.git "$tmpDir"/emscripten-llvm/tools/clang
gitCheckout "$tmpDir"/emscripten-llvm/tools/clang '1.38.26'
fi
mkdir -p "$tmpDir"/bin/emscripten-llvm
runFrom "$tmpDir"/bin/emscripten-llvm cmake \
-G "Unix Makefiles" \
-D "CMAKE_INSTALL_PREFIX=$PREFIX_JS" \
-D "CMAKE_BUILD_TYPE=Release" \
-D "LLVM_BUILD_GLOBAL_ISEL=off" \
-D "LLVM_TARGETS_TO_BUILD=host;JSBackend" \
-D "LLVM_ENABLE_ASSERTIONS=off" \
-D "LLVM_ENABLE_BACKTRACES=off" \
-D "LLVM_ENABLE_WARNINGS=off" \
-D "LLVM_INCLUDE_EXAMPLES=off" \
-D "LLVM_INCLUDE_DOCS=off" \
-D "CLANG_ENABLE_STATIC_ANALYZER=off" \
-D "CLANG_ENABLE_ARCMT=off" \
../../emscripten-llvm
runFrom "$tmpDir"/bin/emscripten-llvm "${MAKE[@]}" -k || true
runFrom "$tmpDir"/bin/emscripten-llvm make -j2 # retry with less parallelism (workaround potential memory limitations)
runFrom "$tmpDir"/bin/emscripten-llvm "${MAKE[@]}" install
}
function generateGlPc
{
echo "Name: gl"
echo "Description: gl"
echo "Version: 2.0.0"
echo "Cflags: "
echo "Libs: -lGL"
}
function generateSdl2Pc
{
echo "Name: sdl2"
echo "Description: SDL2"
echo "Version: 2.0.0"
echo "Cflags: -s USE_SDL=2"
echo "Libs: -s USE_SDL=2"
}
function generateOggPc
{
echo "Name: ogg"
echo "Description: OGG"
echo "Version: 2.0.0"
echo "Cflags: -s USE_OGG=1"
echo "Libs: -s USE_OGG=1"
}
function generateVorbisfilePc
{
echo "Name: vorbisfile"
echo "Description: VorbisFile"
echo "Version: 2.0.0"
echo "Cflags: -s USE_VORBIS=1"
echo "Libs: -s USE_VORBIS=1"
}
function generateSdl2ImagePc
{
echo "Name: SDL2_image"
echo "Description: SDL2_image"
echo "Version: 2.0.0"
echo "Cflags: -s USE_SDL_IMAGE=2"
echo "Libs: -s USE_SDL_IMAGE=2"
}
function gitCheckout
{
local dir=$1
local commit=$2
runFrom "$dir" git fetch --depth 100
runFrom "$dir" git checkout -q "$commit"
echo "[$dir] at $commit"
}
function runFrom
{
local readonly dir=$1
shift
(
cd "$dir"
"$@"
)
}
main "$@"