summaryrefslogtreecommitdiff
path: root/tests/apex_comparison_tests.sh
blob: 8893060084eb0837ca0b0cd684572a55da6f3b32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash

# Copyright (C) 2022 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -euo pipefail

# Soong/Bazel integration test for building unbundled apexes in the real source tree.
#
# These tests build artifacts from head and compares their contents.

if [ ! -e "build/make/core/Makefile" ]; then
  echo "$0 must be run from the top of the Android source tree."
  exit 1
fi

############
# Test Setup
############

OUTPUT_DIR="$(mktemp -d $(pwd)/tmp.XXXXXX)"
SOONG_OUTPUT_DIR="$OUTPUT_DIR/soong"
BAZEL_OUTPUT_DIR="$OUTPUT_DIR/bazel"

export TARGET_PRODUCT="module_arm"
[ "$#" -eq 1 ] && export TARGET_PRODUCT="$1"

function call_bazel() {
  build/bazel/bin/bazel --output_base="$BAZEL_OUTPUT_DIR" $@
}

function cleanup {
  # call bazel clean because some bazel outputs don't have w bits.
  call_bazel clean
  rm -rf "${OUTPUT_DIR}"
}

function deapexer() {
  DEBUGFS_PATH="$(realpath $(call_bazel cquery --config=bp2build --config=linux_x86_64 --config=ci --output=files //external/e2fsprogs/debugfs))"
  call_bazel run --config=bp2build //system/apex/tools:deapexer -- --debugfs_path=$DEBUGFS_PATH $@
}

trap cleanup EXIT

###########
# Run Soong
###########
export UNBUNDLED_BUILD_SDKS_FROM_SOURCE=true # don't rely on prebuilts
export TARGET_BUILD_APPS="com.android.adbd com.android.tzdata build.bazel.examples.apex.minimal"
packages/modules/common/build/build_unbundled_mainline_module.sh \
  --product "$TARGET_PRODUCT" \
  --dist_dir "$SOONG_OUTPUT_DIR"

######################
# Run bp2build / Bazel
######################
build/soong/soong_ui.bash --make-mode BP2BUILD_VERBOSE=1 --skip-soong-tests bp2build

BAZEL_OUT="$(call_bazel info --config=bp2build output_path)"

call_bazel build --config=bp2build --config=ci --config=android \
  //packages/modules/adb/apex:com.android.adbd \
  //system/timezone/apex:com.android.tzdata \
  //build/bazel/examples/apex/minimal:build.bazel.examples.apex.minimal
BAZEL_ADBD="$(realpath $(call_bazel cquery --config=bp2build --config=android --config=ci --output=files //packages/modules/adb/apex:com.android.adbd))"
BAZEL_TZDATA="$(realpath $(call_bazel cquery --config=bp2build --config=android --config=ci --output=files //system/timezone/apex:com.android.tzdata))"
BAZEL_MINIMAL="$(realpath $(call_bazel cquery --config=bp2build --config=android --config=ci --output=files //build/bazel/examples/apex/minimal:build.bazel.examples.apex.minimal))"

# # Build debugfs separately, as it's not a dep of apexer, but needs to be an explicit arg.
call_bazel build --config=bp2build --config=linux_x86_64 //external/e2fsprogs/debugfs

#######
# Tests
#######

function compare_deapexer_list() {
  local BAZEL_APEX=$1; shift
  local APEX=$1; shift

  # Compare the outputs of `deapexer list`, which lists the contents of the apex filesystem image.
  local SOONG_APEX="$SOONG_OUTPUT_DIR/$APEX"

  local SOONG_LIST="$OUTPUT_DIR/soong.list"
  local BAZEL_LIST="$OUTPUT_DIR/bazel.list"

  deapexer list "$SOONG_APEX" > "$SOONG_LIST"
  deapexer list "$BAZEL_APEX" > "$BAZEL_LIST"

  if cmp -s "$SOONG_LIST" "$BAZEL_LIST"
  then
    echo "ok: $APEX"
  else
    echo "contents of $APEX are different between Soong and Bazel:"
    echo
    echo expected
    echo
    cat "$SOONG_LIST"
    echo
    echo got
    echo
    cat "$BAZEL_LIST"
    exit 1
  fi
}

compare_deapexer_list "${BAZEL_ADBD}" com.android.adbd.apex
compare_deapexer_list "${BAZEL_TZDATA}" com.android.tzdata.apex
compare_deapexer_list "${BAZEL_MINIMAL}" build.bazel.examples.apex.minimal.apex