Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 1 | #!/bin/bash -eu |
| 2 | |
| 3 | set -o pipefail |
| 4 | |
| 5 | # Test that bp2build and Bazel can play nicely together |
| 6 | |
| 7 | source "$(dirname "$0")/lib.sh" |
| 8 | |
Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 9 | readonly GENERATED_BUILD_FILE_NAME="BUILD.bazel" |
| 10 | |
Jingwen Chen | 53dfa40 | 2021-08-12 09:37:14 +0000 | [diff] [blame] | 11 | function test_bp2build_null_build() { |
| 12 | setup |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 13 | run_soong bp2build |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 14 | local output_mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker) |
Jingwen Chen | 53dfa40 | 2021-08-12 09:37:14 +0000 | [diff] [blame] | 15 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 16 | run_soong bp2build |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 17 | local output_mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker) |
Jingwen Chen | 53dfa40 | 2021-08-12 09:37:14 +0000 | [diff] [blame] | 18 | |
| 19 | if [[ "$output_mtime1" != "$output_mtime2" ]]; then |
| 20 | fail "Output bp2build marker file changed on null build" |
| 21 | fi |
| 22 | } |
| 23 | |
| 24 | test_bp2build_null_build |
| 25 | |
| 26 | function test_bp2build_null_build_with_globs() { |
| 27 | setup |
| 28 | |
| 29 | mkdir -p foo/bar |
| 30 | cat > foo/bar/Android.bp <<'EOF' |
| 31 | filegroup { |
| 32 | name: "globs", |
| 33 | srcs: ["*.txt"], |
| 34 | } |
| 35 | EOF |
| 36 | touch foo/bar/a.txt foo/bar/b.txt |
| 37 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 38 | run_soong bp2build |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 39 | local output_mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker) |
Jingwen Chen | 53dfa40 | 2021-08-12 09:37:14 +0000 | [diff] [blame] | 40 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 41 | run_soong bp2build |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 42 | local output_mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker) |
Jingwen Chen | 53dfa40 | 2021-08-12 09:37:14 +0000 | [diff] [blame] | 43 | |
| 44 | if [[ "$output_mtime1" != "$output_mtime2" ]]; then |
| 45 | fail "Output bp2build marker file changed on null build" |
| 46 | fi |
| 47 | } |
| 48 | |
| 49 | test_bp2build_null_build_with_globs |
| 50 | |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 51 | function 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' |
| 57 | genrule { |
| 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 | } |
| 67 | EOF |
| 68 | |
| 69 | mkdir -p foo/unconvertible_soong_module |
| 70 | cat > foo/unconvertible_soong_module/Android.bp <<'EOF' |
| 71 | genrule { |
| 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 | } |
| 81 | EOF |
| 82 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 83 | run_soong bp2build |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 84 | |
Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 85 | 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 Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 87 | fi |
| 88 | |
Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 89 | 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 Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 91 | fi |
| 92 | |
Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 93 | 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 Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 95 | fi |
| 96 | |
Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 97 | 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 Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 99 | fi |
| 100 | |
Rupert Shuttleworth | 413a7a9 | 2021-05-18 07:47:15 -0400 | [diff] [blame] | 101 | 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 Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 103 | 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 Chen | ba6d4ac | 2021-11-09 11:55:36 +0000 | [diff] [blame] | 108 | local the_answer_file="bazel-out/android_target-fastbuild/bin/foo/convertible_soong_module/the_answer.txt" |
Rupert Shuttleworth | 2a4fc3e | 2021-04-21 07:10:09 -0400 | [diff] [blame] | 109 | 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 | |
| 117 | test_bp2build_generates_all_buildfiles |
Lukacs T. Berki | e3487c8 | 2022-05-02 10:13:19 +0200 | [diff] [blame^] | 118 | |
| 119 | function test_cc_correctness { |
| 120 | setup |
| 121 | create_mock_bazel |
| 122 | |
| 123 | mkdir -p a |
| 124 | cat > a/Android.bp <<EOF |
| 125 | cc_object { |
| 126 | name: "qq", |
| 127 | srcs: ["qq.cc"], |
| 128 | bazel_module: { |
| 129 | bp2build_available: true, |
| 130 | }, |
| 131 | stl: "none", |
| 132 | system_shared_libs: [], |
| 133 | } |
| 134 | EOF |
| 135 | |
| 136 | cat > a/qq.cc <<EOF |
| 137 | #include "qq.h" |
| 138 | int qq() { |
| 139 | return QQ; |
| 140 | } |
| 141 | EOF |
| 142 | |
| 143 | cat > a/qq.h <<EOF |
| 144 | #define QQ 1 |
| 145 | EOF |
| 146 | |
| 147 | run_soong bp2build |
| 148 | |
| 149 | run_bazel build --package_path=out/soong/workspace //a:qq |
| 150 | local output_mtime1=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o) |
| 151 | |
| 152 | run_bazel build --package_path=out/soong/workspace //a:qq |
| 153 | local output_mtime2=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o) |
| 154 | |
| 155 | if [[ "$output_mtime1" != "$output_mtime2" ]]; then |
| 156 | fail "output changed on null build" |
| 157 | fi |
| 158 | |
| 159 | cat > a/qq.h <<EOF |
| 160 | #define QQ 2 |
| 161 | EOF |
| 162 | |
| 163 | run_bazel build --package_path=out/soong/workspace //a:qq |
| 164 | local output_mtime3=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o) |
| 165 | |
| 166 | if [[ "$output_mtime1" == "$output_mtime3" ]]; then |
| 167 | fail "output not changed when included header changed" |
| 168 | fi |
| 169 | } |
| 170 | |
| 171 | test_cc_correctness |