blob: 01b47607604242510400e167c39a09f64073dff5 [file] [log] [blame]
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -04001#!/bin/bash -eu
2
3set -o pipefail
4
5# Test that bp2build and Bazel can play nicely together
6
7source "$(dirname "$0")/lib.sh"
8
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -04009readonly GENERATED_BUILD_FILE_NAME="BUILD.bazel"
10
Jingwen Chen53dfa402021-08-12 09:37:14 +000011function test_bp2build_null_build() {
12 setup
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020013 run_soong bp2build
Lukacs T. Berki90b43342021-11-02 14:42:04 +010014 local output_mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Jingwen Chen53dfa402021-08-12 09:37:14 +000015
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020016 run_soong bp2build
Lukacs T. Berki90b43342021-11-02 14:42:04 +010017 local output_mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Jingwen Chen53dfa402021-08-12 09:37:14 +000018
19 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
20 fail "Output bp2build marker file changed on null build"
21 fi
22}
23
24test_bp2build_null_build
25
26function test_bp2build_null_build_with_globs() {
27 setup
28
29 mkdir -p foo/bar
30 cat > foo/bar/Android.bp <<'EOF'
31filegroup {
32 name: "globs",
33 srcs: ["*.txt"],
34 }
35EOF
36 touch foo/bar/a.txt foo/bar/b.txt
37
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020038 run_soong bp2build
Lukacs T. Berki90b43342021-11-02 14:42:04 +010039 local output_mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Jingwen Chen53dfa402021-08-12 09:37:14 +000040
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020041 run_soong bp2build
Lukacs T. Berki90b43342021-11-02 14:42:04 +010042 local output_mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker)
Jingwen Chen53dfa402021-08-12 09:37:14 +000043
44 if [[ "$output_mtime1" != "$output_mtime2" ]]; then
45 fail "Output bp2build marker file changed on null build"
46 fi
47}
48
49test_bp2build_null_build_with_globs
50
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040051function test_bp2build_generates_all_buildfiles {
52 setup
53 create_mock_bazel
54
55 mkdir -p foo/convertible_soong_module
56 cat > foo/convertible_soong_module/Android.bp <<'EOF'
57genrule {
58 name: "the_answer",
59 cmd: "echo '42' > $(out)",
60 out: [
61 "the_answer.txt",
62 ],
63 bazel_module: {
64 bp2build_available: true,
65 },
66 }
67EOF
68
69 mkdir -p foo/unconvertible_soong_module
70 cat > foo/unconvertible_soong_module/Android.bp <<'EOF'
71genrule {
72 name: "not_the_answer",
73 cmd: "echo '43' > $(out)",
74 out: [
75 "not_the_answer.txt",
76 ],
77 bazel_module: {
78 bp2build_available: false,
79 },
80 }
81EOF
82
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020083 run_soong bp2build
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040084
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040085 if [[ ! -f "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
86 fail "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040087 fi
88
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040089 if [[ ! -f "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
90 fail "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040091 fi
92
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040093 if ! grep "the_answer" "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
94 fail "missing BUILD target the_answer in convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040095 fi
96
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -040097 if grep "not_the_answer" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
98 fail "found unexpected BUILD target not_the_answer in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -040099 fi
100
Rupert Shuttleworth413a7a92021-05-18 07:47:15 -0400101 if ! grep "filegroup" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
102 fail "missing filegroup in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400103 fi
104
105 # NOTE: We don't actually use the extra BUILD file for anything here
106 run_bazel build --package_path=out/soong/workspace //foo/...
107
Jingwen Chen3c5083c2021-10-13 14:25:41 +0000108 local the_answer_file="bazel-out/k8-fastbuild/bin/foo/convertible_soong_module/the_answer.txt"
Rupert Shuttleworth2a4fc3e2021-04-21 07:10:09 -0400109 if [[ ! -f "${the_answer_file}" ]]; then
110 fail "Expected '${the_answer_file}' to be generated, but was missing"
111 fi
112 if ! grep 42 "${the_answer_file}"; then
113 fail "Expected to find 42 in '${the_answer_file}'"
114 fi
115}
116
117test_bp2build_generates_all_buildfiles