setup-android-build.sh 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #! /bin/bash
  2. #
  3. # Sets the current directory as Android build output directory for a
  4. # given target by writing the "prefix script" to it. Commands prefixed
  5. # by this script are executed in the Android build environment. E.g.,
  6. # running
  7. # ./run <command>
  8. # runs <command> as if we issued
  9. # cd <source>
  10. # mount --bind <build dir> out
  11. # . build/envsetup.sh
  12. # lunch <config>
  13. # <command>
  14. # exit
  15. #
  16. # This arrangement eliminates the need to issue envsetup/lunch commands
  17. # manually, and allows to run multiple builds from the same shell.
  18. # Thus, if your source tree is in ~/aosp and you are building for
  19. # 'blueline' and 'cuttlefish', issuing
  20. # cd /sdx/blueline && \
  21. # ~/aosp/build/soong/scripts/setup-android-build.sh aosp_blueline-userdebug
  22. # cd /sdx/cuttlefish && \
  23. # ~/aosp/build/soong/scripts/setup-android-build.sh aosp_cf_arm64_phone-userdebug
  24. # sets up build directories in /sdx/blueline and /sdx/cuttlefish respectively.
  25. # After that, issue
  26. # /sdx/blueline/run m
  27. # to build blueline image, and issue
  28. # /sdx/cuttlefish atest CtsSecurityBulletinHostTestCases
  29. # to run CTS tests. Notice there is no need to change to a specific directory for that.
  30. #
  31. # Argument:
  32. # * configuration (one of those shown by `lunch` command).
  33. #
  34. set -e
  35. function die() { printf "$@"; exit 1; }
  36. # Find out where the source tree using the fact that we are in its
  37. # build/ subdirectory.
  38. [[ "$(uname)" == Linux ]] || die "This setup runs only on Linux\n"
  39. declare -r mydir="${0%/*}"
  40. declare -r source="${mydir%/build/soong/scripts}"
  41. [[ "/${mydir}/" =~ '/build/soong/scripts/' ]] || \
  42. die "$0 should be in build/soong/scripts/ subdirectory of the source tree\n"
  43. [[ ! -e .repo && ! -e .git ]] || \
  44. die "Current directory looks like source. You should be in the _target_ directory.\n"
  45. # Do not override old run script.
  46. if [[ -x ./run ]]; then
  47. # Set variables from config=xxx and source=xxx comments in the existing script.
  48. . <(sed -nr 's/^# *source=(.*)/oldsource=\1/p;s/^# *config=(.*)/oldconfig=\1/p' run)
  49. die "This directory has been already set up to build Android for %s from %s.\n\
  50. Remove 'run' file if you want to set it up afresh\n" "$oldconfig" "$oldsource"
  51. fi
  52. (($#<2)) || die "usage: %s [<config>]\n" $0
  53. if (($#==1)); then
  54. # Configuration is provided, emit run script.
  55. declare -r config="$1"
  56. declare -r target="$PWD"
  57. cat >./run <<EOF
  58. #! /bin/bash
  59. # source=$source
  60. # config=$config
  61. declare -r cmd=\$(printf ' %q' "\$@")
  62. "$source/prebuilts/build-tools/linux-x86/bin/nsjail"\
  63. -Mo -q -e -t 0\
  64. -EANDROID_QUIET_BUILD=true \
  65. -B / -B "$target:$source/out"\
  66. --cwd "$source"\
  67. --skip_setsid \
  68. --keep_caps\
  69. --disable_clone_newcgroup\
  70. --disable_clone_newnet\
  71. --rlimit_as soft\
  72. --rlimit_core soft\
  73. --rlimit_cpu soft\
  74. --rlimit_fsize soft\
  75. --rlimit_nofile soft\
  76. --proc_rw\
  77. --hostname $(hostname) \
  78. --\
  79. /bin/bash -i -c ". build/envsetup.sh && lunch "$config" &&\$cmd"
  80. EOF
  81. chmod +x ./run
  82. else
  83. # No configuration, show available ones.
  84. printf "Please specify build target. Common values:\n"
  85. (cd "$source"
  86. . build/envsetup.sh
  87. get_build_var COMMON_LUNCH_CHOICES | tr ' ' '\n' | pr -c4 -tT -W"$(tput cols)"
  88. )
  89. exit 1
  90. fi