-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.sh
executable file
·221 lines (182 loc) · 7.28 KB
/
build.sh
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
#!/usr/bin/env bash
set -e
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
ROOT_DIR=$SCRIPT_DIR
cd $ROOT_DIR
CPPFRONT=$ROOT_DIR/.cache/tools/cppfront
CPP2B_DIST=$ROOT_DIR/dist/debug/cpp2b
MODULES_DIR=$ROOT_DIR/.cache/modules
mkdir -p $ROOT_DIR/.cache/repos
mkdir -p $MODULES_DIR
mkdir -p $ROOT_DIR/.cache/tools
mkdir -p $ROOT_DIR/dist/debug
mkdir -p $ROOT_DIR/.cache/cpp2/source/src
mkdir -p $ROOT_DIR/.cache/cpp2/source/_build
function log_info() {
echo "INFO: $1"
}
function log_error() {
echo "ERROR: $1"
}
function fatal() {
echo "FATAL: $1"
exit 1
}
if [[ -z "$CC" ]]; then
COMPILER_VERSION=$(clang --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1)
COMPILER_MAJOR_VERSION=$(echo $COMPILER_VERSION | cut -d. -f1)
if [ "$COMPILER_MAJOR_VERSION" -lt 19 ]; then
CC=clang-19
else
CC=clang
fi
fi
CPP2B_COMPILER=${CC}
if ! [ -x "$(command -v $CPP2B_COMPILER)" ]; then
fatal "cannot find $CPP2B_COMPILER in your PATH"
fi
if ! [[ $CPP2B_COMPILER == *"clang"* ]]; then
fatal "only clang is supported: detected $CPP2B_COMPILER"
fi
COMPILER_VERSION=$($CPP2B_COMPILER --version | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n 1)
COMPILER_MAJOR_VERSION=$(echo $COMPILER_VERSION | cut -d. -f1)
if [ "$COMPILER_MAJOR_VERSION" -lt 19 ]; then
fatal "clang version 19 or newer only supported: detected $COMPILER_VERSION"
fi
log_info "using compiler '$CPP2B_COMPILER' version '$COMPILER_VERSION'"
function ensure_gh_repo() {
local repo=$1
local branch=$2
local repo_path=$ROOT_DIR/.cache/repos/$repo
if ! [ -d $repo_path ]; then
mkdir -p $repo_path
git clone --quiet --depth=1 --branch=$branch --filter=blob:none --sparse https://github.com/$repo $repo_path
fi
}
function ensure_gh_repo_subdir() {
local repo=$1
local repo_path=$ROOT_DIR/.cache/repos/$repo
local repo_subdir=$2
local repo_subdir_path=$repo_path/$repo_subdir
if ! [ -d $repo_subdir_path ]; then
cd $repo_path
log_info "checking out repo $repo/$reposubdir"
git sparse-checkout add $repo_subdir
cd $ROOT_DIR
fi
}
ensure_gh_repo "hsutter/cppfront" "v0.8.0"
ensure_gh_repo_subdir "hsutter/cppfront" "source"
ensure_gh_repo_subdir "hsutter/cppfront" "include"
CPPFRONT_INCLUDE_DIR=$ROOT_DIR/.cache/repos/hsutter/cppfront/include
LLVM_ROOT=/usr/lib/llvm-$COMPILER_MAJOR_VERSION
if ! [ -x $CPPFRONT ]; then
log_info "compiling cppfront..."
cd $ROOT_DIR/.cache/repos/hsutter/cppfront/source
$CPP2B_COMPILER \
-std=c++23 \
-stdlib=libc++ \
-fexperimental-library \
-Wno-unused-result \
-Wno-deprecated-declarations \
-fprebuilt-module-path=$MODULES_DIR \
-L$LLVM_ROOT/lib \
-isystem $LLVM_ROOT/include/c++/v1 \
-lc++abi \
-lc++ \
-lm \
-static \
-fuse-ld=lld \
-I"$CPPFRONT_INCLUDE_DIR" \
cppfront.cpp -o $CPPFRONT
cd $ROOT_DIR
fi
if ! [ -f $MODULES_DIR/std.pcm ]; then
cd $LLVM_ROOT/share/libc++/v1
log_info "compiling std module..."
$CPP2B_COMPILER \
-stdlib=libc++ \
-std=c++23 \
-fexperimental-library \
-isystem $LLVM_ROOT/include/c++/v1 \
-Wno-reserved-module-identifier \
std.cppm \
--precompile -o $MODULES_DIR/std.pcm
cd $ROOT_DIR
fi
if ! [ -f $MODULES_DIR/std.compat.pcm ]; then
cd $LLVM_ROOT/share/libc++/v1
log_info "compiling std.compat module..."
$CPP2B_COMPILER \
-stdlib=libc++ \
-std=c++23 \
-fexperimental-library \
-isystem $LLVM_ROOT/include/c++/v1 \
-Wno-reserved-module-identifier \
-fprebuilt-module-path=$MODULES_DIR \
std.compat.cppm \
--precompile -o $MODULES_DIR/std.compat.pcm
cd $ROOT_DIR
fi
if ! [ -f $MODULES_DIR/dylib.pcm ]; then
log_info "compiling dylib module..."
$CPP2B_COMPILER \
-stdlib=libc++ \
-std=c++23 \
-fexperimental-library \
-isystem $LLVM_ROOT/include/c++/v1 \
-fprebuilt-module-path=$MODULES_DIR \
"$ROOT_DIR/src/dylib.cppm" \
--precompile -o $MODULES_DIR/dylib.pcm
cd $ROOT_DIR
fi
if ! [ -f $MODULES_DIR/nlohmann.json.pcm ]; then
log_info "compiling nlohmann.json module..."
$CPP2B_COMPILER \
-stdlib=libc++ \
-std=c++23 \
-fexperimental-library \
-isystem $LLVM_ROOT/include/c++/v1 \
-fprebuilt-module-path=$MODULES_DIR \
"$ROOT_DIR/src/nlohmann.json.cppm" \
--precompile -o $MODULES_DIR/nlohmann.json.pcm
cd $ROOT_DIR
fi
log_info "compiling cpp2b module..."
if [ -f "$ROOT_DIR/.cache/cpp2/source/_build/cpp2b.cppm" ]; then
rm "$ROOT_DIR/.cache/cpp2/source/_build/cpp2b.cppm"
fi
cat "$ROOT_DIR/share/cpp2b.cppm.tpl" | sed "s\`@CPP2B_PROJECT_ROOT@\`$ROOT_DIR\`g" > "$ROOT_DIR/.cache/cpp2/source/_build/cpp2b.cppm"
$CPP2B_COMPILER \
-stdlib=libc++ \
-std=c++23 \
-fexperimental-library \
-isystem $LLVM_ROOT/include/c++/v1 \
-fprebuilt-module-path=$MODULES_DIR \
"$ROOT_DIR/.cache/cpp2/source/_build/cpp2b.cppm" \
--precompile -o $MODULES_DIR/cpp2b.pcm
log_info "running cppfront..."
$CPPFRONT src/main.cpp2 -pure -import-std -l -format-colon-errors -o "$ROOT_DIR/.cache/cpp2/source/src/main.cpp"
log_info "compiling..."
$CPP2B_COMPILER \
-stdlib=libc++ \
"$MODULES_DIR/cpp2b.pcm" \
"$MODULES_DIR/dylib.pcm" \
"$MODULES_DIR/std.compat.pcm" \
"$MODULES_DIR/nlohmann.json.pcm" \
"$ROOT_DIR/.cache/cpp2/source/src/main.cpp" \
-std=c++23 \
-fexperimental-library \
-Wno-unused-result \
-Wno-deprecated-declarations \
-fprebuilt-module-path=$MODULES_DIR \
-L$LLVM_ROOT/lib \
-isystem $LLVM_ROOT/include/c++/v1 \
-lc++abi \
-lc++ \
-lm \
-static \
-fuse-ld=lld \
-I"$CPPFRONT_INCLUDE_DIR" \
-o $CPP2B_DIST
log_info "successfully built $CPP2B_DIST"