-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathbuild-curl-upstream.sh
More file actions
255 lines (224 loc) · 6.39 KB
/
Copy pathbuild-curl-upstream.sh
File metadata and controls
255 lines (224 loc) · 6.39 KB
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
#!/usr/bin/env bash
set -euo pipefail
usage() {
cat <<'EOF'
Usage: build-curl-upstream.sh \
--version <curl-version> \
--tag <curl-tag> \
--url <release-url> \
--cache-dir <cache-dir> \
--build-dir <build-dir> \
--overlay-dir <overlay-dir> \
--cc <cc> \
--ar <ar> \
--ranlib <ranlib> \
--output <output>
EOF
}
VERSION=""
TAG=""
URL=""
CACHE_DIR=""
BUILD_DIR=""
OVERLAY_DIR=""
CC_CMD=""
AR_CMD=""
RANLIB_CMD=""
OUTPUT=""
while [[ $# -gt 0 ]]; do
case "$1" in
--version)
VERSION="$2"
shift 2
;;
--tag)
TAG="$2"
shift 2
;;
--url)
URL="$2"
shift 2
;;
--cache-dir)
CACHE_DIR="$2"
shift 2
;;
--build-dir)
BUILD_DIR="$2"
shift 2
;;
--overlay-dir)
OVERLAY_DIR="$2"
shift 2
;;
--cc)
CC_CMD="$2"
shift 2
;;
--ar)
AR_CMD="$2"
shift 2
;;
--ranlib)
RANLIB_CMD="$2"
shift 2
;;
--output)
OUTPUT="$2"
shift 2
;;
*)
echo "Unknown argument: $1" >&2
usage >&2
exit 1
;;
esac
done
if [[ -z "$VERSION" || -z "$TAG" || -z "$URL" || -z "$CACHE_DIR" || -z "$BUILD_DIR" || -z "$OVERLAY_DIR" || -z "$CC_CMD" || -z "$AR_CMD" || -z "$RANLIB_CMD" || -z "$OUTPUT" ]]; then
usage >&2
exit 1
fi
fetch() {
local url="$1"
local out="$2"
if command -v curl >/dev/null 2>&1; then
curl -fSL "$url" -o "$out"
elif command -v wget >/dev/null 2>&1; then
wget -q "$url" -O "$out"
else
echo "Neither curl nor wget is available to fetch $url" >&2
exit 1
fi
}
mkdir -p "$CACHE_DIR"
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"
TARBALL="$CACHE_DIR/curl-${VERSION}.tar.xz"
if [[ ! -f "$TARBALL" ]]; then
echo "Fetching upstream curl ${VERSION} release tarball..."
fetch "$URL" "$TARBALL"
fi
echo "Extracting upstream curl ${VERSION}..."
tar -xf "$TARBALL" -C "$BUILD_DIR"
SRC_DIR="$BUILD_DIR/curl-${VERSION}"
if [[ ! -d "$SRC_DIR" ]]; then
echo "Expected extracted source at $SRC_DIR" >&2
exit 1
fi
echo "Applying agentOS overlay..."
while IFS= read -r -d '' file; do
rel="${file#$OVERLAY_DIR/}"
mkdir -p "$SRC_DIR/$(dirname "$rel")"
cp "$file" "$SRC_DIR/$rel"
done < <(find "$OVERLAY_DIR" -type f -print0)
pushd "$SRC_DIR" >/dev/null
echo "Patching WASI-incompatible signal/setjmp includes..."
python3 - <<'PY'
from pathlib import Path
replacements = {
"lib/hostip.h": [
(
'#include <setjmp.h>\n',
'#ifndef __wasi__\n#include <setjmp.h>\n#endif\n',
),
],
"lib/hostip.c": [
(
'#include <setjmp.h>\n#include <signal.h>\n',
'#ifndef __wasi__\n#include <setjmp.h>\n#include <signal.h>\n#endif\n',
),
],
"lib/transfer.c": [
(
'#include <signal.h>\n',
'#ifndef __wasi__\n#include <signal.h>\n#endif\n',
),
],
"src/tool_main.c": [
(
'#include <signal.h>\n',
'#ifndef __wasi__\n#include <signal.h>\n#endif\n',
),
],
}
for rel_path, edits in replacements.items():
path = Path(rel_path)
updated = path.read_text()
for old, new in edits:
text = updated
if new in text:
continue
if old not in text:
raise SystemExit(f"Expected to patch {rel_path}, but no replacement matched")
updated = text.replace(old, new)
path.write_text(updated)
PY
echo "Configuring upstream curl for wasm32-wasip1..."
CC="$CC_CMD" \
AR="$AR_CMD" \
RANLIB="$RANLIB_CMD" \
CFLAGS="-O2 -flto" \
./configure \
--host=wasm32-unknown-wasi \
--disable-shared \
--disable-threaded-resolver \
--disable-ldap \
--without-zlib \
--without-brotli \
--without-zstd \
--without-libpsl \
--without-ca-bundle \
--without-ca-path \
--without-ssl
echo "Enabling the agentOS WASI TLS backend in generated curl config..."
python3 - <<'PY'
from pathlib import Path
config = Path("lib/curl_config.h")
text = config.read_text()
updates = {
"#define CURL_DISABLE_HSTS 1": "/* #undef CURL_DISABLE_HSTS */",
}
for old, new in updates.items():
text = text.replace(old, new)
if "#define USE_WASI_TLS 1" not in text:
text += "\n#define USE_WASI_TLS 1\n"
if "#define CURL_DISABLE_OPENSSL_AUTO_LOAD_CONFIG 1" not in text:
text += "#define CURL_DISABLE_OPENSSL_AUTO_LOAD_CONFIG 1\n"
config.write_text(text)
PY
echo "Patching generated lib/Makefile to compile wasi_tls.c and wasi_getsockopt.c..."
cat >> lib/Makefile <<'EOF'
am_libcurl_la_OBJECTS += vtls/libcurl_la-wasi_tls.lo libcurl_la-wasi_getsockopt.lo
libcurl_la_LIBADD += vtls/libcurl_la-wasi_tls.lo libcurl_la-wasi_getsockopt.lo
libcurl.la: vtls/libcurl_la-wasi_tls.lo libcurl_la-wasi_getsockopt.lo
vtls/libcurl_la-wasi_tls.lo: vtls/$(am__dirstamp) vtls/$(DEPDIR)/$(am__dirstamp) vtls/wasi_tls.c
$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libcurl_la_CPPFLAGS) $(CPPFLAGS) $(libcurl_la_CFLAGS) $(CFLAGS) -MT vtls/libcurl_la-wasi_tls.lo -MD -MP -MF vtls/$(DEPDIR)/libcurl_la-wasi_tls.Tpo -c -o vtls/libcurl_la-wasi_tls.lo `test -f 'vtls/wasi_tls.c' || echo '$(srcdir)/'`vtls/wasi_tls.c
$(AM_V_at)$(am__mv) vtls/$(DEPDIR)/libcurl_la-wasi_tls.Tpo vtls/$(DEPDIR)/libcurl_la-wasi_tls.Plo
libcurl_la-wasi_getsockopt.lo: wasi_getsockopt.c
$(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libcurl_la_CPPFLAGS) $(CPPFLAGS) $(libcurl_la_CFLAGS) $(CFLAGS) -MT libcurl_la-wasi_getsockopt.lo -MD -MP -MF $(DEPDIR)/libcurl_la-wasi_getsockopt.Tpo -c -o libcurl_la-wasi_getsockopt.lo `test -f 'wasi_getsockopt.c' || echo '$(srcdir)/'`wasi_getsockopt.c
$(AM_V_at)$(am__mv) $(DEPDIR)/libcurl_la-wasi_getsockopt.Tpo $(DEPDIR)/libcurl_la-wasi_getsockopt.Plo
EOF
echo "Building upstream libcurl..."
make -C lib libcurl.la
echo "Building upstream curl tool..."
make -C src curl
BIN=""
for candidate in "src/.libs/curl" "src/curl" "src/curl.wasm"; do
if [[ -f "$candidate" ]]; then
BIN="$candidate"
break
fi
done
if [[ -z "$BIN" ]]; then
echo "Unable to locate built curl binary in src/" >&2
exit 1
fi
mkdir -p "$(dirname "$OUTPUT")"
if command -v wasm-opt >/dev/null 2>&1; then
echo "Optimizing curl WASM binary..."
wasm-opt -O3 --strip-debug --all-features "$BIN" -o "$OUTPUT"
else
cp "$BIN" "$OUTPUT"
fi
popd >/dev/null
echo "Built upstream curl at $OUTPUT"