@@ -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,39 +438,55 @@ 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=(" $@ " )
452+
441453 [ $profile = " release" ] && cargo_args+=(" --release" )
442454
443455 # Run the cargo build process inside the container.
444456 # We don't need any special privileges for the build phase, so we run the
445457 # container as the current user/group.
458+ #
459+ # Note: we need to pass an explicit TARGET_CC to cargo, because the `cc`
460+ # crate gets confused for aarch64 musl.
446461 run_devctr \
447462 --user " $( id -u) :$( id -g) " \
448463 --workdir " $CTR_FC_ROOT_DIR " \
464+ --env TARGET_CC=musl-gcc \
449465 -- \
450466 cargo build \
451- --target-dir " ${CTR_CARGO_TARGET_DIR} " \
467+ --target-dir " $CTR_CARGO_TARGET_DIR " \
468+ --target " $target " \
452469 " ${cargo_args[@]} "
453470 ret=$?
454471
455472 # If `cargo build` was successful, let's copy the binaries to a more
456- # accessible location (under build/release or build/debug).
457- if [ $ret -eq 0 ]; then
458- mkdir -p " ${FC_BUILD_DIR} /${profile} "
459- cp -f " ${CARGO_TARGET_DIR} /${profile} " /{firecracker,jailer} \
460- " ${FC_BUILD_DIR} /${profile} /"
461- ret=$?
462- fi
463- [ $ret -eq 0 ] && \
464- say " Build complete. Binaries placed under ${FC_BUILD_DIR} /${profile} /"
473+ # accessible location.
474+ [ $ret -eq 0 ] && {
475+ cargo_bin_dir=" $CARGO_TARGET_DIR /$target /$profile "
476+ friendly_bin_dir=" $FC_BUILD_DIR /$profile -$libc "
477+ say " Build successful."
478+
479+ mkdir -p " $friendly_bin_dir "
480+ [ $? -eq 0 ] || {
481+ say_warn " Unable to create dir $friendly_bin_dir ."
482+ say_warn " Binaries placed under $cargo_bin_dir /"
483+ return $ret
484+ }
485+ cp -f " $cargo_bin_dir " /{firecracker,jailer} " $friendly_bin_dir /"
486+ say " Build complete."
487+ say " Binaries placed under $friendly_bin_dir "
488+ }
489+
465490 return $ret
466491}
467492
0 commit comments