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