forked from just-every/code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zsh
More file actions
executable file
·319 lines (244 loc) · 8.7 KB
/
Copy pathbuild.zsh
File metadata and controls
executable file
·319 lines (244 loc) · 8.7 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env zsh
# Build script for code with multi-platform and multi-configuration support
# Usage: ./build.zsh platform="android" --release
# ./build.zsh (default: native build)
# ./build.zsh --release (default platform, release mode)
# ./build.zsh platform="android" (Android, debug mode)
emulate -L zsh
setopt errexit nounset pipefail
typeset SCRIPT_DIR WORKSPACE_ROOT CODE_RS_DIR TOOLCHAIN
typeset -l PLATFORM BUILD_MODE
typeset -a BUILD_FLAGS CARGO_BUILD_FLAGS
# Script configuration
SCRIPT_DIR="${0:a:h}"
WORKSPACE_ROOT="${SCRIPT_DIR}"
CODE_RS_DIR="${WORKSPACE_ROOT}/code-rs"
# Build configuration variables
PLATFORM="${PLATFORM:-native}"
BUILD_MODE="debug"
# Color output
typeset -r RED='\033[0;31m'
typeset -r GREEN='\033[0;32m'
typeset -r YELLOW='\033[1;33m'
typeset -r BLUE='\033[0;34m'
typeset -r NC='\033[0m'
function .log.info
{ print "${BLUE}ℹ${NC} $*" }
function .log.success
{ print "${GREEN}✓${NC} $*" }
function .log.warn
{ print "${YELLOW}⚠${NC} $*" }
function ..log.error
{ print "${RED}✗${NC} $*" >&2 }
function show-usage
{ emulate -L zsh
cat << 'EOF'
Build script for code - Multi-platform build orchestration
USAGE:
./build.zsh [OPTIONS]
OPTIONS:
platform="<name>" Target platform (native, android)
Default: native
--release Build in release mode (optimized, smaller size)
Default: debug mode
--help Show this help message
EXAMPLES:
# Build for native platform in debug mode
./build.zsh
# Build for Android in release mode
./build.zsh platform="android" --release
# Build for native platform in release mode
./build.zsh --release
# Build for Android in debug mode
./build.zsh platform="android"
SUPPORTED PLATFORMS:
native Build for macOS (host system)
android Build for Android aarch64 (ARM64)
EOF
}
# Parse command line arguments
function parse-args
{ emulate -L zsh
typeset arg
for arg in "$@"; do
case "$arg" in
platform=*)
PLATFORM="${arg#platform=}"
;;
--release)
BUILD_MODE="release"
CARGO_BUILD_FLAGS+=("--release")
;;
--debug)
BUILD_MODE="debug"
;;
--help|-h)
show-usage
exit 0
;;
*)
.log.error "Unknown argument: ${arg}"
show-usage
exit 1
;;
esac
done
}
# Validate platform
function validate-platform
{ emulate -L zsh
case "$PLATFORM" in
native|android) return 0 ;;
*)
.log.error "Unknown platform: ${PLATFORM}"
print "Supported platforms: native, android"
exit 1
;;
esac
}
function resolve-toolchain
{ emulate -L zsh
# Prefer the repo-pinned toolchain (CODE_RS_DIR/rust-toolchain.toml) so
# local builds do not depend on whatever "stable" happens to be.
TOOLCHAIN="${RUSTUP_TOOLCHAIN:-}"
if [[ -z "${TOOLCHAIN}" && -f "${CODE_RS_DIR}/rust-toolchain.toml" ]]; then
TOOLCHAIN="$(
awk -F'"' '/^channel[[:space:]]*=/{print $2; exit}' "${CODE_RS_DIR}/rust-toolchain.toml"
)"
fi
TOOLCHAIN="${TOOLCHAIN:-stable}"
}
# Setup Android environment
function setup-android-env
{ emulate -L zsh
.log.info "Setting up Android build environment..."
# Android builds are expected to use the "small build" feature set by
# default (see docs/architecture/termux_support.md).
CARGO_BUILD_FLAGS+=("--no-default-features")
export ANDROID_NDK_ROOT="/opt/homebrew/share/android-ndk"
typeset TOOLCHAIN_PATH
TOOLCHAIN_PATH="${ANDROID_NDK_ROOT}/toolchains/llvm/prebuilt/darwin-x86_64"
export PATH="${TOOLCHAIN_PATH}/bin:${PATH}"
export CC="${TOOLCHAIN_PATH}/bin/aarch64-linux-android24-clang"
export AR="${TOOLCHAIN_PATH}/bin/llvm-ar"
export CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER="${CC}"
export CARGO_TARGET_AARCH64_LINUX_ANDROID_AR="${AR}"
BUILD_FLAGS+=("--target" "aarch64-linux-android")
.log.success "Android environment ready"
}
function setup-native-env
{ .log.info "Setting up native build environment..."
# Native builds don't need special setup
.log.success "Native environment ready"
}
# Validate environment
function validate-env
{ emulate -L zsh
.log.info "Validating environment..."
if [[ ! -d "$CODE_RS_DIR" ]]; then
.log.error "code-rs directory not found at $CODE_RS_DIR"
exit 1
fi
if ! command -v rustup &> /dev/null; then
.log.error "rustup not found. Please install Rust."
exit 1
fi
if ! command -v cargo &> /dev/null; then
.log.error "cargo not found. Please install Rust."
exit 1
fi
if [[ "$PLATFORM" == "android" ]]; then
.log.info "Ensuring rust target is installed for toolchain: ${TOOLCHAIN}"
rustup target add --toolchain "${TOOLCHAIN}" aarch64-linux-android
if [[ ! -d "/opt/homebrew/share/android-ndk" ]]; then
.log.error "Android NDK not found at /opt/homebrew/share/android-ndk"
.log.info "Install with: brew install android-ndk"
exit 1
fi
fi
.log.success "Environment validated"
}
# Perform the build
function perform-build
{ emulate -L zsh
typeset OUTPUT_DIR BINARY_SIZE BINARY_TYPE CARGO_BIN RUSTC_BIN
OUTPUT_DIR="${CODE_RS_DIR}/target"
if [[ "${PLATFORM}" == "android" ]]; then
OUTPUT_DIR="${OUTPUT_DIR}/aarch64-linux-android"
fi
if [[ "${BUILD_MODE}" == "release" ]]; then
OUTPUT_DIR="${OUTPUT_DIR}/release"
else
OUTPUT_DIR="${OUTPUT_DIR}/debug"
fi
.log.info "Building code for ${PLATFORM} in ${BUILD_MODE} mode..."
.log.info "Output will be: ${OUTPUT_DIR}/code"
cd "${CODE_RS_DIR}"
# Build with appropriate flags
.log.info "Running: cargo build -p code-cli --bin code ${BUILD_FLAGS} ${CARGO_BUILD_FLAGS}"
# For Android, ensure all environment variables are passed to cargo
CARGO_BIN="$(rustup which cargo --toolchain "${TOOLCHAIN}")"
RUSTC_BIN="$(rustup which rustc --toolchain "${TOOLCHAIN}")"
if [[ "${PLATFORM}" == "android" ]]; then
if ! env RUSTC="${RUSTC_BIN}" \
"${CARGO_BIN}" build -p code-cli \
--bin code \
$BUILD_FLAGS \
$CARGO_BUILD_FLAGS; then
.log.error "Build failed"
exit 1
fi
else
if ! env RUSTC="${RUSTC_BIN}" "${CARGO_BIN}" build -p code-cli \
--bin code \
$BUILD_FLAGS \
$CARGO_BUILD_FLAGS; then
.log.error "Build failed"
exit 1
fi
fi
# Verify output
if [[ ! -f "${OUTPUT_DIR}/code" ]]; then
.log.error "Binary not found at ${OUTPUT_DIR}/code"
exit 1
fi
# Get binary info
BINARY_SIZE=${${"$(ls -lh -- "${OUTPUT_DIR}/code")"}[(w)5]}
BINARY_TYPE=${"$(file -- "${OUTPUT_DIR}/code")"#*:}
.log.success "Build completed successfully!"
.log.info "Binary size: ${BINARY_SIZE}"
.log.info "Binary type: ${BINARY_TYPE}"
# Show platform-specific next steps
case "${PLATFORM}" in
android)
print
.log.info "Android binary ready for deployment to Termux:"
.log.info " adb push '${OUTPUT_DIR}/code' /sdcard/Download/code"
.log.info " adb shell chmod +x /sdcard/Download/code"
.log.info " # In Termux (once): termux-setup-storage"
.log.info " # In Termux: cp ~/storage/downloads/code \"$PREFIX/bin/code\" && chmod +x \"$PREFIX/bin/code\""
.log.info " # In Termux: code --version"
;;
native)
print
.log.info "Native binary ready:"
.log.info " ${OUTPUT_DIR}/code"
esac
}
function main
{ emulate -L zsh
.log.info "Code build system"
parse-args "$@"
validate-platform
resolve-toolchain
.log.info "Platform: ${PLATFORM}"
.log.info "Build mode: ${BUILD_MODE}"
.log.info "Toolchain: ${TOOLCHAIN}"
validate-env
case "${PLATFORM}" in
android) setup-android-env ;;
native) setup-native-env ;;
esac
perform-build
}
main "$@"