bp2build_bazel_test.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #!/bin/bash -eu
  2. set -o pipefail
  3. # Test that bp2build and Bazel can play nicely together
  4. source "$(dirname "$0")/lib.sh"
  5. readonly GENERATED_BUILD_FILE_NAME="BUILD.bazel"
  6. function test_bp2build_null_build() {
  7. setup
  8. run_soong bp2build
  9. local output_mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker)
  10. run_soong bp2build
  11. local output_mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker)
  12. if [[ "$output_mtime1" != "$output_mtime2" ]]; then
  13. fail "Output bp2build marker file changed on null build"
  14. fi
  15. }
  16. test_bp2build_null_build
  17. function test_bp2build_null_build_with_globs() {
  18. setup
  19. mkdir -p foo/bar
  20. cat > foo/bar/Android.bp <<'EOF'
  21. filegroup {
  22. name: "globs",
  23. srcs: ["*.txt"],
  24. }
  25. EOF
  26. touch foo/bar/a.txt foo/bar/b.txt
  27. run_soong bp2build
  28. local output_mtime1=$(stat -c "%y" out/soong/bp2build_workspace_marker)
  29. run_soong bp2build
  30. local output_mtime2=$(stat -c "%y" out/soong/bp2build_workspace_marker)
  31. if [[ "$output_mtime1" != "$output_mtime2" ]]; then
  32. fail "Output bp2build marker file changed on null build"
  33. fi
  34. }
  35. test_bp2build_null_build_with_globs
  36. function test_bp2build_generates_all_buildfiles {
  37. setup
  38. create_mock_bazel
  39. mkdir -p foo/convertible_soong_module
  40. cat > foo/convertible_soong_module/Android.bp <<'EOF'
  41. genrule {
  42. name: "the_answer",
  43. cmd: "echo '42' > $(out)",
  44. out: [
  45. "the_answer.txt",
  46. ],
  47. bazel_module: {
  48. bp2build_available: true,
  49. },
  50. }
  51. EOF
  52. mkdir -p foo/unconvertible_soong_module
  53. cat > foo/unconvertible_soong_module/Android.bp <<'EOF'
  54. genrule {
  55. name: "not_the_answer",
  56. cmd: "echo '43' > $(out)",
  57. out: [
  58. "not_the_answer.txt",
  59. ],
  60. bazel_module: {
  61. bp2build_available: false,
  62. },
  63. }
  64. EOF
  65. run_soong bp2build
  66. if [[ ! -f "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
  67. fail "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
  68. fi
  69. if [[ ! -f "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}" ]]; then
  70. fail "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME} was not generated"
  71. fi
  72. if ! grep "the_answer" "./out/soong/workspace/foo/convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
  73. fail "missing BUILD target the_answer in convertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
  74. fi
  75. if grep "not_the_answer" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
  76. fail "found unexpected BUILD target not_the_answer in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
  77. fi
  78. if ! grep "filegroup" "./out/soong/workspace/foo/unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"; then
  79. fail "missing filegroup in unconvertible_soong_module/${GENERATED_BUILD_FILE_NAME}"
  80. fi
  81. # NOTE: We don't actually use the extra BUILD file for anything here
  82. run_bazel build --package_path=out/soong/workspace //foo/...
  83. local the_answer_file="bazel-out/android_target-fastbuild/bin/foo/convertible_soong_module/the_answer.txt"
  84. if [[ ! -f "${the_answer_file}" ]]; then
  85. fail "Expected '${the_answer_file}' to be generated, but was missing"
  86. fi
  87. if ! grep 42 "${the_answer_file}"; then
  88. fail "Expected to find 42 in '${the_answer_file}'"
  89. fi
  90. }
  91. test_bp2build_generates_all_buildfiles
  92. function test_cc_correctness {
  93. setup
  94. create_mock_bazel
  95. mkdir -p a
  96. cat > a/Android.bp <<EOF
  97. cc_object {
  98. name: "qq",
  99. srcs: ["qq.cc"],
  100. bazel_module: {
  101. bp2build_available: true,
  102. },
  103. stl: "none",
  104. system_shared_libs: [],
  105. }
  106. EOF
  107. cat > a/qq.cc <<EOF
  108. #include "qq.h"
  109. int qq() {
  110. return QQ;
  111. }
  112. EOF
  113. cat > a/qq.h <<EOF
  114. #define QQ 1
  115. EOF
  116. run_soong bp2build
  117. run_bazel build --package_path=out/soong/workspace //a:qq
  118. local output_mtime1=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
  119. run_bazel build --package_path=out/soong/workspace //a:qq
  120. local output_mtime2=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
  121. if [[ "$output_mtime1" != "$output_mtime2" ]]; then
  122. fail "output changed on null build"
  123. fi
  124. cat > a/qq.h <<EOF
  125. #define QQ 2
  126. EOF
  127. run_bazel build --package_path=out/soong/workspace //a:qq
  128. local output_mtime3=$(stat -c "%y" bazel-bin/a/_objs/qq/qq.o)
  129. if [[ "$output_mtime1" == "$output_mtime3" ]]; then
  130. fail "output not changed when included header changed"
  131. fi
  132. }
  133. test_cc_correctness