1#!/bin/bash --posix
2# Copyright 2021 The Bazel 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
16out="${1}"
17manifest="${2}"
18apk="${3}"
19is_coverage="${4}"
20is_java8="${5}"
21lib_label="${6}"
22xmllint="${7}"
23unzip="${8}"
24
25if [[ -n "$manifest" ]]; then
26  node_count=$("$xmllint" --xpath "count(//manifest/*)" "$manifest")
27  module_count=$("$xmllint" --xpath "count(//manifest/*[local-name()='module'])" "$manifest")
28  application_count=$("$xmllint" --xpath "count(//manifest/*[local-name()='application'])" "$manifest")
29  application_attr_count=$("$xmllint" --xpath "count(//manifest/application/@*)" "$manifest")
30  application_content_count=$("$xmllint" --xpath "count(//manifest/application/*)" "$manifest")
31  module_title=$("$xmllint" --xpath "string(//manifest/*[local-name()='module'][1]/@*[local-name()='title'])" "$manifest")
32  valid=0
33
34  # Valid manifest, containing a dist:module and an empty <application/>
35  if [[ "$node_count" == "2" &&
36  "$module_count" == "1" &&
37  "$application_count" == "1" &&
38  "$application_attr_count" == "0" &&
39  "$application_content_count" == "0" ]]; then
40    valid=1
41  fi
42
43  # Valid manifest, containing a dist:module
44  if [[ "$node_count" == "1" && "$module_count" == "1" ]]; then
45    valid=1
46  fi
47
48  if [[ "$valid" == "0" ]]; then
49    echo ""
50    echo "$manifest should only contain a single <dist:module /> element (and optional empty <application/>), nothing else"
51    echo "Manifest contents: "
52    cat "$manifest"
53    exit 1
54  fi
55
56  if [[ "$module_title" != "\${MODULE_TITLE}" ]]; then
57    echo ""
58    echo "$manifest dist:title should be \${MODULE_TITLE} placeholder"
59    echo ""
60    exit 1
61  fi
62fi
63
64# Skip dex validation when running under code coverage.
65# When running under code coverage an additional dep is implicitly added to all
66# binary targets, causing a validation failure.
67if [[ "$is_coverage" == "false" ]]; then
68  dexes=$("$unzip" -l "$apk" | grep ".dex" | wc -l)
69  if [[ ("$is_java8" == "true" && "$dexes" -gt 1 ) || ( "$is_java8" == "false" && "$dexes" -gt 0)]]; then
70    echo ""
71    echo "android_feature_module does not support Java or Kotlin sources."
72    echo "Check $lib_label for any srcs or deps."
73    echo ""
74    exit 1
75  fi
76fi
77
78touch "$out"
79