Skip to content

Commit 106ca4f

Browse files
committed
devtool: added libc build option
Added a new option, to allow specifying the libc used when building with `devtool build`. I.e. musl or gnu. Signed-off-by: Dan Horobeanu <[email protected]>
1 parent eba6680 commit 106ca4f

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

tools/devtool

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,14 @@ cmd_help() {
367367
echo ""
368368
echo "Available commands:"
369369
echo ""
370-
echo " build [--debug|--release] [-- [<cargo args>]]"
370+
echo " build [--debug|--release] [-l|--libc musl|gnu] [-- [<cargo args>]]"
371371
echo " Build the Firecracker binaries."
372372
echo " Firecracker is built using the Rust build system (cargo). All arguments after --"
373373
echo " will be passed through to cargo."
374-
echo " --debug Build the debug binaries. This is the default."
375-
echo " --release Build the release binaries."
374+
echo " --debug Build the debug binaries. This is the default."
375+
echo " --release Build the release binaries."
376+
echo " -l, --libc musl|gnu Choose the libc flavor against which Firecracker will"
377+
echo " be linked. Default is musl."
376378
echo ""
377379
echo " fmt"
378380
echo " Auto-format all Rust source files, to match the Firecracker requirements."
@@ -414,13 +416,20 @@ cmd_build() {
414416

415417
# By default, we'll build the debug binaries.
416418
profile="debug"
419+
libc="musl"
417420

418421
# Parse any command line args.
419422
while [ $# -gt 0 ]; do
420423
case "$1" in
421424
"-h"|"--help") { cmd_help; exit 1; } ;;
422425
"--debug") { profile="debug"; } ;;
423426
"--release") { profile="release"; } ;;
427+
"-l"|"--libc")
428+
shift
429+
[[ "$1" =~ ^(musl|gnu)$ ]] || \
430+
die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"."
431+
libc="$1"
432+
;;
424433
"--") { shift; break; } ;;
425434
*)
426435
die "Unknown argument: $1. Please use --help for help."
@@ -429,48 +438,56 @@ cmd_build() {
429438
shift
430439
done
431440

441+
target="$(uname -m)-unknown-linux-${libc}"
442+
432443
# Check prerequisites
433444
ensure_devctr
434445
ensure_build_dir
435446

436-
say "Starting build ($profile) ..."
447+
say "Starting build ($profile, $libc) ..."
437448

438449
# Cargo uses the debug profile by default. If we're building the release
439450
# binaries, we need to pass an extra argument to cargo.
440451
cargo_args=("$@")
441452

442-
# When musl build is triggered, make sure that cargo finds the path to
443-
# the musl-gcc binary.
444-
TARGET_CC=""
445-
if [[ ${cargo_args[@]} = *"musl"* ]]; then
446-
TARGET_CC=/usr/bin/musl-gcc
447-
fi
448453

449454
[ $profile = "release" ] && cargo_args+=("--release")
450455

451456
# Run the cargo build process inside the container.
452457
# We don't need any special privileges for the build phase, so we run the
453458
# container as the current user/group.
459+
#
460+
# Note: we need to pass an explicit TARGET_CC to cargo, because the `cc`
461+
# crate gets confused for aarch64 musl.
454462
run_devctr \
455463
--user "$(id -u):$(id -g)" \
456464
--workdir "$CTR_FC_ROOT_DIR" \
457-
--env TARGET_CC=$TARGET_CC \
465+
--env TARGET_CC=musl-gcc \
458466
-- \
459467
cargo build \
460-
--target-dir "${CTR_CARGO_TARGET_DIR}" \
468+
--target-dir "$CTR_CARGO_TARGET_DIR" \
469+
--target "$target" \
461470
"${cargo_args[@]}"
462471
ret=$?
463472

464473
# If `cargo build` was successful, let's copy the binaries to a more
465-
# accessible location (under build/release or build/debug).
466-
if [ $ret -eq 0 ]; then
467-
mkdir -p "${FC_BUILD_DIR}/${profile}"
468-
cp -f "${CARGO_TARGET_DIR}/${profile}"/{firecracker,jailer} \
469-
"${FC_BUILD_DIR}/${profile}/"
470-
ret=$?
471-
fi
472-
[ $ret -eq 0 ] && \
473-
say "Build complete. Binaries placed under ${FC_BUILD_DIR}/${profile}/"
474+
# accessible location.
475+
[ $ret -eq 0 ] && {
476+
cargo_bin_dir="$CARGO_TARGET_DIR/$target/$profile"
477+
friendly_bin_dir="$FC_BUILD_DIR/$profile-$libc"
478+
say "Build successful."
479+
480+
mkdir -p "$friendly_bin_dir"
481+
[ $? -eq 0 ] || {
482+
say_warn "Unable to create dir $friendly_bin_dir."
483+
say_warn "Binaries placed under $cargo_bin_dir/"
484+
return $ret
485+
}
486+
cp -f "$cargo_bin_dir"/{firecracker,jailer} "$friendly_bin_dir/"
487+
say "Build complete."
488+
say "Binaries placed under $friendly_bin_dir"
489+
}
490+
474491
return $ret
475492
}
476493

0 commit comments

Comments
 (0)