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|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}/packages/modules/NeuralNetworks/runtime/include
31CANONICALDIR=${ANDROID_BUILD_TOP}/packages/modules/NeuralNetworks/common/types/include/nnapi
32AIDLDIR=${ANDROID_BUILD_TOP}/hardware/interfaces/neuralnetworks/aidl/android/hardware/neuralnetworks
33
34RET=0
35function doit {
36  typeset -r kind="$1" in="$2" out="$3"
37  echo "=== $kind"
38  ${DRYRUN} ${TOOL} --kind ${kind} --specification ${SPECFILE} --template ${in} --out ${out}
39  if [[ $? -ne 0 ]] ; then RET=1 ; fi
40}
41
42function check {
43  typeset -r kind="$1" in="$2" out="$3"
44  TEMPFILE=$(mktemp)
45  doit ${kind} ${in} ${TEMPFILE}
46  if [[ ${RET} -eq 0 ]] ; then
47    ${DRYRUN} cmp -s ${out} ${TEMPFILE} || {
48      RET=1
49      echo >&2 "Error: $(basename ${out}) is out of sync with $(basename ${in}) or types.spec. Please run generate_api.sh before uploading."
50    }
51  fi
52}
53
54case "${MODE}" in
55  update)
56    doit canonical $(dirname $0)/Types.t ${CANONICALDIR}/Types.h
57    doit canonical $(dirname $0)/OperandTypes.t ${CANONICALDIR}/OperandTypes.h
58    doit canonical $(dirname $0)/OperationTypes.t ${CANONICALDIR}/OperationTypes.h
59    doit ndk $(dirname $0)/NeuralNetworksTypes.t ${NDKDIR}/NeuralNetworksTypes.h
60    doit hal_1.0 ${HALDIR}/1.0/types.t ${HALDIR}/1.0/types.hal
61    doit hal_1.1 ${HALDIR}/1.1/types.t ${HALDIR}/1.1/types.hal
62    doit hal_1.2 ${HALDIR}/1.2/types.t ${HALDIR}/1.2/types.hal
63    doit hal_1.3 ${HALDIR}/1.3/types.t ${HALDIR}/1.3/types.hal
64    doit aidl $(dirname $0)/OperandTypeAidl.t ${AIDLDIR}/OperandType.aidl
65    doit aidl $(dirname $0)/OperationTypeAidl.t ${AIDLDIR}/OperationType.aidl
66    ;;
67  hook)
68    check canonical $(dirname $0)/Types.t ${CANONICALDIR}/Types.h
69    check canonical $(dirname $0)/OperandTypes.t ${CANONICALDIR}/OperandTypes.h
70    check canonical $(dirname $0)/OperationTypes.t ${CANONICALDIR}/OperationTypes.h
71    check ndk $(dirname $0)/NeuralNetworksTypes.t ${NDKDIR}/NeuralNetworksTypes.h
72    # Avoids checking HAL/AIDL because of cross-repo dependencies.
73    ;;
74  *)
75    echo >&2 "*** Unknown mode: ${MODE}"
76    exit 1
77    ;;
78esac
79
80if [[ ${RET} -ne 0 ]] ; then
81  echo >&2 "*** FAILED"
82fi
83exit ${RET}
84