1#!/bin/bash
2
3#
4# Copyright (C) 2017 The Android Open Source Project
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10#      http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18NNAPI_VERSION="
19V1_0
20V1_1
21"
22
23# Process one test spec, and optionally provide the log file argument
24# for the slicing tool. The first argument is the test spec file; the
25# second optional argument specifies the log file this test should dump
26# results into. Only used by the test slicing tool to collect reference
27# outputs from the CPU. Also, it outputs the right #includes in the
28# test harness so the test would be invoked by TestGenerated.cpp
29#
30# This function shouldn't be directly called from other scripts. Use
31# generate_wrapper below for generating models and examples and updating the
32# test framework in one shot.
33
34export NNAPI_BASE=$ANDROID_BUILD_TOP/frameworks/ml/nn
35: ${TEST_DIR:=frameworks/ml/nn/runtime/test}
36
37function generate_one_testcase {
38  # Generate one testcase
39  local LOGFILE=$2
40  if [ -n "$2" ]; then
41    local LOGFILE=", \"$2\""
42  fi
43  local BASENAME=`basename -s .mod.py $1`
44  local EXAMPLE="-e $ANDROID_BUILD_TOP/$TEST_DIR/generated/examples/$BASENAME.example.cpp"
45
46  $NNAPI_BASE/tools/test_generator/test_generator.py ./`basename $1`\
47    -m $ANDROID_BUILD_TOP/$TEST_DIR/generated/models/$BASENAME.model.cpp $EXAMPLE
48  ret=$?
49  # Paste these lines into TestGenerated.cpp
50  echo
51  echo namespace $BASENAME {
52  echo std::vector\<MixedTypedExample\> examples \= {
53  echo // Generated $BASENAME test
54  echo \#include \"generated/examples/$BASENAME.example.cpp\"
55  echo }\;
56  echo // Generated model constructor
57  echo \#include \"generated/models/$BASENAME.model.cpp\"
58  echo }  // namespace $BASENAME
59  echo TEST_F\(GeneratedTests\, $BASENAME\) {
60  echo '    execute'\($BASENAME\:\:CreateModel\,
61  echo '            '$BASENAME\:\:is_ignored\,
62  echo '            '$BASENAME\:\:examples${LOGFILE}\)\;
63  echo }
64  return $ret
65}
66
67# Driver for generate_one_testcase. Append the output of generate_one_testcase
68# (which are C++ snippets that invokes the test harness) to the
69# all_generated_tests.cpp
70# Optionally, the "LOG" file ($2), only used by the slicing tool, would be
71# passed to generate_one_testcase.
72#
73# This function should be called to process one test spec from other scripts.
74function generate_wrapper {
75  local LOGFILE=""
76  if [ $1 = "log" ]; then
77    local LOGFILE=$2
78    shift
79    shift
80  fi
81  cd $ANDROID_BUILD_TOP/$TEST_DIR/specs
82  OUTFILE=$ANDROID_BUILD_TOP/$TEST_DIR/generated/all_generated_tests.cpp
83  echo "// DO NOT EDIT;" > $OUTFILE
84  echo "// Generated by ml/nn/runtime/test/specs/generate_test.sh" >> $OUTFILE
85  FOUND=0
86
87  for ver in $NNAPI_VERSION;
88  do
89    VER_DIR=$ANDROID_BUILD_TOP/$TEST_DIR/specs/$ver
90    [ ! -d $VER_DIR ] && continue
91    pushd $VER_DIR > /dev/null
92    for f in $@;
93    do
94      if [ -f $(basename $f) ]; then
95        generate_one_testcase $f "$LOGFILE" >> $OUTFILE
96        if [ $? -ne 0 ]; then
97          echo "Failed processing $f"
98          return $?
99        fi
100        FOUND=1
101      fi
102    done
103    popd > /dev/null
104  done
105  if [[ $FOUND -eq 0 ]]; then
106    echo did not find any files for $@
107    exit 1
108  fi
109  return $?
110}
111
112# Only run the following when not sourced by another script
113if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
114  set -eu
115  if [ $# -eq 0 ]; then
116    FILES=${ANDROID_BUILD_TOP}/${TEST_DIR}/specs/V*/*.mod.py
117  else
118    FILES="$@"
119  fi
120  generate_wrapper $FILES
121  if [ $? -ne 0 ]; then
122    exit $?
123  fi
124  echo "Generated file in ml/nn/runtime/test/generated/"`basename $OUTFILE`
125fi # [[ "${BASH_SOURCE[0]}" == "${0}" ]]
126
127