@@ -367,12 +367,14 @@ cmd_help() {
367
367
echo " "
368
368
echo " Available commands:"
369
369
echo " "
370
- echo " build [--debug|--release] [-- [<cargo args>]]"
370
+ echo " build [--debug|--release] [-l|--libc musl|gnu] [- - [<cargo args>]]"
371
371
echo " Build the Firecracker binaries."
372
372
echo " Firecracker is built using the Rust build system (cargo). All arguments after --"
373
373
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."
376
378
echo " "
377
379
echo " fmt"
378
380
echo " Auto-format all Rust source files, to match the Firecracker requirements."
@@ -414,13 +416,20 @@ cmd_build() {
414
416
415
417
# By default, we'll build the debug binaries.
416
418
profile=" debug"
419
+ libc=" musl"
417
420
418
421
# Parse any command line args.
419
422
while [ $# -gt 0 ]; do
420
423
case " $1 " in
421
424
" -h" |" --help" ) { cmd_help; exit 1; } ;;
422
425
" --debug" ) { profile=" debug" ; } ;;
423
426
" --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
+ ;;
424
433
" --" ) { shift ; break ; } ;;
425
434
* )
426
435
die " Unknown argument: $1 . Please use --help for help."
@@ -429,48 +438,56 @@ cmd_build() {
429
438
shift
430
439
done
431
440
441
+ target=" $( uname -m) -unknown-linux-${libc} "
442
+
432
443
# Check prerequisites
433
444
ensure_devctr
434
445
ensure_build_dir
435
446
436
- say " Starting build ($profile ) ..."
447
+ say " Starting build ($profile , $libc ) ..."
437
448
438
449
# Cargo uses the debug profile by default. If we're building the release
439
450
# binaries, we need to pass an extra argument to cargo.
440
451
cargo_args=(" $@ " )
441
452
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
448
453
449
454
[ $profile = " release" ] && cargo_args+=(" --release" )
450
455
451
456
# Run the cargo build process inside the container.
452
457
# We don't need any special privileges for the build phase, so we run the
453
458
# 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.
454
462
run_devctr \
455
463
--user " $( id -u) :$( id -g) " \
456
464
--workdir " $CTR_FC_ROOT_DIR " \
457
- --env TARGET_CC=$TARGET_CC \
465
+ --env TARGET_CC=musl-gcc \
458
466
-- \
459
467
cargo build \
460
- --target-dir " ${CTR_CARGO_TARGET_DIR} " \
468
+ --target-dir " $CTR_CARGO_TARGET_DIR " \
469
+ --target " $target " \
461
470
" ${cargo_args[@]} "
462
471
ret=$?
463
472
464
473
# 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
+
474
491
return $ret
475
492
}
476
493
0 commit comments