1#!/usr/bin/env bash
2# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15# ==============================================================================
16
17set -e
18
19SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
20ROOT_DIR=${SCRIPT_DIR}/../../../../..
21cd "${ROOT_DIR}"
22
23source tensorflow/lite/micro/tools/ci_build/helper_functions.sh
24
25# explicitly call third_party_downloads since we need pigweed for the license
26# and clang-format checks.
27make -f tensorflow/lite/micro/tools/make/Makefile third_party_downloads
28
29# Explicitly disable exit on error so that we can report all the style errors in
30# one pass and clean up the temporary git repository even when one of the
31# scripts fail with an error code.
32set +e
33
34pushd tensorflow/lite/
35
36############################################################
37# License Check
38############################################################
39micro/tools/make/downloads/pigweed/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py \
40  kernels/internal/reference/ \
41  micro/ \
42  ../../third_party/ \
43  -p copyright_notice \
44  -e kernels/internal/reference/integer_ops/ \
45  -e kernels/internal/reference/reference_ops.h \
46  -e tools/make/downloads \
47  -e tools/make/targets/ecm3531 \
48  -e BUILD\
49  -e leon_commands \
50  -e "\.bzl" \
51  -e "\.h5" \
52  -e "\.ipynb" \
53  -e "\.inc" \
54  -e "\.patch" \
55  -e "\.properties" \
56  -e "\.txt" \
57  -e "\.tpl" \
58  --output-directory /tmp
59
60LICENSE_CHECK_RESULT=$?
61
62############################################################
63# Formatting Check
64############################################################
65
66if [[ ${1} == "--fix_formatting" ]]
67then
68  FIX_FORMAT_OPTIONS="--fix"
69else
70  FIX_FORMAT_OPTIONS=""
71fi
72
73micro/tools/make/downloads/pigweed/pw_presubmit/py/pw_presubmit/format_code.py \
74  ${FIX_FORMAT_OPTIONS} \
75  -e c/common.c \
76  -e core/api/error_reporter.cc \
77  -e kernels/internal/reference/integer_ops/ \
78  -e kernels/internal/reference/reference_ops.h \
79  -e kernels/internal/types.h \
80  -e experimental \
81  -e schema/schema_generated.h \
82  -e schema/schema_utils.h \
83  -e "\.inc" \
84  -e "\.md"
85
86FORMAT_RESULT=$?
87
88#############################################################################
89# Avoided specific-code snippets for TFLM
90#############################################################################
91
92CHECK_CONTENTS_PATHSPEC=\
93"micro"\
94" :(exclude)micro/tools/ci_build/test_code_style.sh"\
95" :(exclude)*\.md"
96
97# See https://github.com/tensorflow/tensorflow/issues/46297 for more context.
98check_contents "gtest|gmock" "${CHECK_CONTENTS_PATHSPEC}" \
99  "These matches can likely be deleted."
100GTEST_RESULT=$?
101
102# See http://b/175657165 for more context.
103ERROR_REPORTER_MESSAGE=\
104"TF_LITE_REPORT_ERROR should be used instead, so that log strings can be "\
105"removed to save space, if needed."
106
107check_contents "error_reporter.*Report\(|context->ReportError\(" \
108  "${CHECK_CONTENTS_PATHSPEC}" "${ERROR_REPORTER_MESSAGE}"
109ERROR_REPORTER_RESULT=$?
110
111# See http://b/175657165 for more context.
112ASSERT_PATHSPEC=\
113"${CHECK_CONTENTS_PATHSPEC}"\
114" :(exclude)micro/examples/micro_speech/esp/ringbuf.c"\
115" :(exclude)*\.ipynb"\
116" :(exclude)*\.py"\
117" :(exclude)*zephyr_riscv/Makefile.inc"
118
119check_contents "\<assert\>" "${ASSERT_PATHSPEC}" \
120  "assert should not be used in TFLM code.."
121ASSERT_RESULT=$?
122
123###########################################################################
124# All checks are complete, clean up.
125###########################################################################
126
127popd
128
129# Re-enable exit on error now that we are done with the temporary git repo.
130set -e
131
132if [[ ${FORMAT_RESULT}  != 0 ]]
133then
134  echo "The formatting errors can be fixed with tensorflow/lite/micro/tools/ci_build/test_code_style.sh --fix_formatting"
135fi
136if [[ ${LICENSE_CHECK_RESULT}  != 0 || \
137      ${FORMAT_RESULT}         != 0 || \
138      ${GTEST_RESULT}          != 0 || \
139      ${ERROR_REPORTER_RESULT} != 0 || \
140      ${ASSERT_RESULT}         != 0    \
141   ]]
142then
143  exit 1
144fi
145