build_policies.sh 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/bin/bash
  2. # Ensure that GNU parallel is installed.
  3. # We use this to build multiple targets at the same time.
  4. if [[ -z $(command -v parallel) ]]; then
  5. echo "Please install GNU Parallel."
  6. exit
  7. fi
  8. if [[ $# -lt 2 ]]; then
  9. echo "Usage: $0 <Android root directory> <output directory> [specific targets to build]"
  10. exit
  11. fi
  12. android_root_dir=$1
  13. export out_dir=$2
  14. shift 2
  15. all_targets="$@"
  16. echo "Android tree: $android_root_dir"
  17. echo "Output directory: $out_dir"
  18. mkdir -p $out_dir
  19. cd $android_root_dir
  20. source build/envsetup.sh > /dev/null
  21. # Collect the list of targets by parsing the output of lunch.
  22. # TODO: This misses some targets.
  23. if [[ "$all_targets" = "" ]]; then
  24. all_targets=`lunch 2>/dev/null <<< _ | grep "[0-9]" | sed 's/^.* //'`
  25. fi
  26. # Clean up targets by replacing eng with userdebug using non-aosp variants.
  27. declare -A targets_map
  28. for target in $all_targets; do
  29. targets_map[$target]=$target
  30. done
  31. targets=""
  32. for target in $all_targets; do
  33. clean_target=$(echo $target | sed 's/-eng/-userdebug/' | sed 's/aosp_//')
  34. if [[ $clean_target != $target ]] && [[ ${targets_map[$clean_target]} == $clean_target ]]; then
  35. echo "Ignoring $target in favor of $clean_target"
  36. else
  37. if [[ -z $targets ]]; then
  38. targets=$target
  39. else
  40. targets="$targets $target"
  41. fi
  42. fi
  43. done
  44. # Calculate the number of targets to build at once.
  45. # This heuristic could probably be improved.
  46. cores=$(nproc --all)
  47. num_targets=$(echo "$targets" | sed 's/ /\n/g' | wc -l)
  48. parallel_jobs=$(expr $cores / 4)
  49. if [[ $num_targets -lt $parallel_jobs ]]; then
  50. export mmma_jobs=$(expr $cores / $num_targets \* 2)
  51. else
  52. export mmma_jobs=4
  53. fi
  54. echo "$num_targets target(s): $(echo $targets | paste -sd' ')"
  55. compile_target () {
  56. target=$1
  57. source build/envsetup.sh > /dev/null
  58. lunch $target &> /dev/null
  59. # Some targets can't lunch properly.
  60. if [ $? -ne 0 ]; then
  61. echo "$target cannot be lunched"
  62. return 1
  63. fi
  64. my_out_file="$out_dir/log.$target"
  65. rm -f $my_out_file
  66. # Build the policy.
  67. OUT_DIR=$out_dir/out.$target mmma -j$mmma_jobs system/sepolicy &>> $my_out_file
  68. if [ $? -ne 0 ]; then
  69. echo "$target failed to build"
  70. return 2
  71. fi
  72. return 0
  73. }
  74. export -f compile_target
  75. parallel --no-notice -j $parallel_jobs --bar --joblog $out_dir/joblog compile_target ::: $targets
  76. echo "Failed to lunch: $(grep "\s1\s0\scompile_target" $out_dir/joblog | sed 's/^.* //' | sort | paste -sd' ')"
  77. echo "Failed to build: $(grep "\s2\s0\scompile_target" $out_dir/joblog | sed 's/^.* //' | sort | paste -sd' ')"