1#!/bin/bash -u
2
3if [[ -z "${ANDROID_BUILD_TOP:-}" ]] ; then
4  echo >&2 "*** ERROR: $(basename $0) requires envar ANDROID_BUILD_TOP to be set"
5  exit 1
6fi
7
8DRYRUN=""
9MODE="update"
10while [[ $# -ne 0 ]] ; do
11  case "${1}" in
12    --dryrun)
13      DRYRUN="echo"
14      shift
15      ;;
16    --mode=*)
17      MODE=${1#"--mode="}
18      shift
19      ;;
20    *)
21      echo >&2 "*** USAGE: $(basename $0) [--dryrun] [--mode={update|ndk_hook}]"
22      exit 1
23      ;;
24  esac
25done
26
27TOOL=$(dirname $0)/generate_api.py
28SPECFILE=$(dirname $0)/types.spec
29HALDIR=${ANDROID_BUILD_TOP}/hardware/interfaces/neuralnetworks
30NDKDIR=${ANDROID_BUILD_TOP}/frameworks/ml/nn/runtime/include
31
32RET=0
33function doit {
34  typeset -r kind="$1" in="$2" out="$3"
35  echo "=== $kind"
36  ${DRYRUN} ${TOOL} --kind ${kind} --specification ${SPECFILE} --template ${in} --out ${out}
37  if [[ $? -ne 0 ]] ; then RET=1 ; fi
38}
39
40case "${MODE}" in
41  update)
42    doit ndk $(dirname $0)/NeuralNetworks.t ${NDKDIR}/NeuralNetworks.h
43    doit hal_1.0 ${HALDIR}/1.0/types.t ${HALDIR}/1.0/types.hal
44    doit hal_1.1 ${HALDIR}/1.1/types.t ${HALDIR}/1.1/types.hal
45    doit hal_1.2 ${HALDIR}/1.2/types.t ${HALDIR}/1.2/types.hal
46    doit hal_1.3 ${HALDIR}/1.3/types.t ${HALDIR}/1.3/types.hal
47    ;;
48  ndk_hook)
49    TEMPDIR=$(mktemp -d)
50    doit ndk $(dirname $0)/NeuralNetworks.t ${TEMPDIR}/NeuralNetworks.h
51    if [[ ${RET} -eq 0 ]] ; then
52      ${DRYRUN} cmp -s ${NDKDIR}/NeuralNetworks.h ${TEMPDIR}/NeuralNetworks.h || {
53        RET=1
54        echo >&2 "Error: NeuralNetworks.h is out of sync with NeuralNetworks.t or types.spec. Please run generate_api.sh before uploading."
55      }
56    fi
57    ;;
58  *)
59    echo >&2 "*** Unknown mode: ${MODE}"
60    exit 1
61    ;;
62esac
63
64if [[ ${RET} -ne 0 ]] ; then
65  echo >&2 "*** FAILED"
66fi
67exit ${RET}
68