• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2
3#TODO(b/35570956): Do with Soong instead.
4
5#Note: see do_makefiles_update below.
6
7function package_root_to_package() {
8  echo $1 | cut -f1 -d:
9}
10
11function package_root_to_root() {
12  echo $1 | cut -f2 -d:
13}
14
15##
16# Makes sure the appropriate directories are visible.
17# Usage: check_dirs root_or_cwd [package:root ...]
18function check_dirs() {
19  local root_or_cwd=$1
20  shift 1
21
22  for package_root in "$@"; do
23    dir=$(package_root_to_root $package_root)
24    if [ ! -d $root_or_cwd$dir ] ; then
25      echo "Where is $dir?";
26      return 1;
27    fi
28  done
29}
30
31##
32# Gets all packages in a directory.
33# Usage: get_packages package root
34function get_packages() {
35  local current_dir=$1
36  local current_package=$2
37  pushd $current_dir > /dev/null;
38  find . -type f -name \*.hal -exec dirname {} \; | sort -u | \
39    cut -c3- | \
40    awk -F'/' \
41    '{printf("'$current_package'"); for(i=1;i<NF;i++){printf(".%s", $i);}; printf("@%s\n", $NF);}';
42  popd > /dev/null;
43}
44
45##
46# Package roots to arguments.
47# Usage: get_root_arguments [package:root ...]
48function get_root_arguments() {
49  for package_root in "$@"; do
50    printf "%s" "-r$package_root "
51  done
52}
53
54##
55# Returns directory path for a package
56# Usage: get_package_dir package_root_dir package_prefix package
57function get_package_dir() {
58  local package_dir=`echo $3 | cut -f1 -d@ | sed "s/$2\.//" | sed "s/\./\//g"`
59  local package_version=`echo $3 | cut -f2 -d@`
60  echo $1/$package_dir/$package_version
61}
62
63##
64# Returns the number of processors to run on, on this machine
65function get_num_processors() {
66  if command -v nproc >/dev/null 2>&1; then
67    PROCS=$(nproc --all 2>/dev/null) && echo $PROCS && return 0
68  elif command -v sysctl >/dev/null 2>&1; then
69    PROCS=$(sysctl -n hw.logicalcpu 2>/dev/null) && echo $PROCS && return 0
70  fi
71
72  echo 1
73}
74
75##
76# Helps manage the package root of a HAL directory.
77# Should be called from the android root directory.
78#
79# Usage: do_makefiles_update [-O owner-name] [package:root ...]
80# Where the first package root is the current one.
81#
82function do_makefiles_update() {
83  if ! command -v hidl-gen 1>/dev/null; then
84    echo "Cannot find hidl-gen, try lunching or making it ('m hidl-gen')?"
85    exit 1
86  fi
87
88  local owner=
89  if [[ "$1" = "-O" ]]; then
90    owner="$2"
91    shift 2
92  fi
93
94  local root_or_cwd=${ANDROID_BUILD_TOP%%/}${ANDROID_BUILD_TOP:+/}
95
96  local current_package=$(package_root_to_package $1)
97  local current_dir=$root_or_cwd$(package_root_to_root $1)
98
99  echo "Updating makefiles for $current_package in $current_dir."
100
101  check_dirs "$root_or_cwd" $@ || return 1
102
103  local packages=$(get_packages $current_dir $current_package) || return 1
104  local root_arguments=$(get_root_arguments $@) || return 1
105
106  function __update_internal() {
107    local owner="$1"
108    local root_arguments="$2"
109    local package="$3"
110    echo "Updating $package"
111    hidl-gen -O "$owner" -Landroidbp $root_arguments $package || {
112      echo "Command failed: hidl-gen -O \"$owner\" -Landroidbp $root_arguments $package";
113      return 1;
114    }
115  }
116  export -f __update_internal
117
118  echo "$packages" |\
119      xargs -P $(get_num_processors) -I {} \
120      bash -c "__update_internal \"$owner\" \"$root_arguments\" \"{}\"" || return 1
121}
122