1ffe3c632Sopenharmony_ci#!/bin/bash
2ffe3c632Sopenharmony_ci
3ffe3c632Sopenharmony_ci# This script verifies that BUILD files and cmake files are in sync with src/Makefile.am
4ffe3c632Sopenharmony_ci
5ffe3c632Sopenharmony_ciset -eo pipefail
6ffe3c632Sopenharmony_ci
7ffe3c632Sopenharmony_ciif [ "$(uname)" != "Linux" ]; then
8ffe3c632Sopenharmony_ci  echo "build_files_updated_unittest only supported on Linux. Skipping..."
9ffe3c632Sopenharmony_ci  exit 0
10ffe3c632Sopenharmony_cifi
11ffe3c632Sopenharmony_ci
12ffe3c632Sopenharmony_ci# Keep in sync with files needed by update_file_lists.sh
13ffe3c632Sopenharmony_cigenerated_files=(
14ffe3c632Sopenharmony_ci  "BUILD"
15ffe3c632Sopenharmony_ci  "cmake/extract_includes.bat.in"
16ffe3c632Sopenharmony_ci  "cmake/libprotobuf-lite.cmake"
17ffe3c632Sopenharmony_ci  "cmake/libprotobuf.cmake"
18ffe3c632Sopenharmony_ci  "cmake/libprotoc.cmake"
19ffe3c632Sopenharmony_ci  "cmake/tests.cmake"
20ffe3c632Sopenharmony_ci  "src/Makefile.am"
21ffe3c632Sopenharmony_ci)
22ffe3c632Sopenharmony_ci
23ffe3c632Sopenharmony_ci# If we're running in Bazel, use the Bazel-provided temp-dir.
24ffe3c632Sopenharmony_ciif [ -n "${TEST_TMPDIR}" ]; then
25ffe3c632Sopenharmony_ci  # Env-var TEST_TMPDIR is set, assume that this is Bazel.
26ffe3c632Sopenharmony_ci  # Bazel may have opinions whether we are allowed to delete TEST_TMPDIR.
27ffe3c632Sopenharmony_ci  test_root="${TEST_TMPDIR}/build_files_updated_unittest"
28ffe3c632Sopenharmony_ci  mkdir "${test_root}"
29ffe3c632Sopenharmony_cielse
30ffe3c632Sopenharmony_ci  # Seems like we're not executed by Bazel.
31ffe3c632Sopenharmony_ci  test_root=$(mktemp -d)
32ffe3c632Sopenharmony_cifi
33ffe3c632Sopenharmony_ci
34ffe3c632Sopenharmony_ci# From now on, fail if there are any unbound variables.
35ffe3c632Sopenharmony_ciset -u
36ffe3c632Sopenharmony_ci
37ffe3c632Sopenharmony_ci# Remove artifacts after test is finished.
38ffe3c632Sopenharmony_cifunction cleanup {
39ffe3c632Sopenharmony_ci  rm -rf "${test_root}"
40ffe3c632Sopenharmony_ci}
41ffe3c632Sopenharmony_citrap cleanup EXIT
42ffe3c632Sopenharmony_ci
43ffe3c632Sopenharmony_ci# Create golden dir and add snapshot of current state.
44ffe3c632Sopenharmony_cigolden_dir="${test_root}/golden"
45ffe3c632Sopenharmony_cimkdir -p "${golden_dir}/cmake" "${golden_dir}/src"
46ffe3c632Sopenharmony_cifor file in ${generated_files[@]}; do
47ffe3c632Sopenharmony_ci  cp "${file}" "${golden_dir}/${file}"
48ffe3c632Sopenharmony_cidone
49ffe3c632Sopenharmony_ci
50ffe3c632Sopenharmony_ci# Create test dir, copy current state into it, and execute update script.
51ffe3c632Sopenharmony_citest_dir="${test_root}/test"
52ffe3c632Sopenharmony_cicp -R "${golden_dir}" "${test_dir}"
53ffe3c632Sopenharmony_ci
54ffe3c632Sopenharmony_cicp "update_file_lists.sh" "${test_dir}/update_file_lists.sh"
55ffe3c632Sopenharmony_cichmod +x "${test_dir}/update_file_lists.sh"
56ffe3c632Sopenharmony_cicd "${test_root}/test"
57ffe3c632Sopenharmony_cibash "${test_dir}/update_file_lists.sh"
58ffe3c632Sopenharmony_ci
59ffe3c632Sopenharmony_ci# Test whether there are any differences
60ffe3c632Sopenharmony_cifor file in ${generated_files[@]}; do
61ffe3c632Sopenharmony_ci  diff "${golden_dir}/${file}" "${test_dir}/${file}"
62ffe3c632Sopenharmony_cidone
63