lib.sh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/bin/bash -eu
  2. set -o pipefail
  3. HARDWIRED_MOCK_TOP=
  4. # Uncomment this to be able to view the source tree after a test is run
  5. # HARDWIRED_MOCK_TOP=/tmp/td
  6. REAL_TOP="$(readlink -f "$(dirname "$0")"/../../..)"
  7. if [[ ! -z "$HARDWIRED_MOCK_TOP" ]]; then
  8. MOCK_TOP="$HARDWIRED_MOCK_TOP"
  9. else
  10. MOCK_TOP=$(mktemp -t -d st.XXXXX)
  11. trap cleanup_mock_top EXIT
  12. fi
  13. WARMED_UP_MOCK_TOP=$(mktemp -t soong_integration_tests_warmup.XXXXXX.tar.gz)
  14. trap 'rm -f "$WARMED_UP_MOCK_TOP"' EXIT
  15. function warmup_mock_top {
  16. info "Warming up mock top ..."
  17. info "Mock top warmup archive: $WARMED_UP_MOCK_TOP"
  18. cleanup_mock_top
  19. mkdir -p "$MOCK_TOP"
  20. cd "$MOCK_TOP"
  21. create_mock_soong
  22. run_soong
  23. tar czf "$WARMED_UP_MOCK_TOP" *
  24. }
  25. function cleanup_mock_top {
  26. cd /
  27. rm -fr "$MOCK_TOP"
  28. }
  29. function info {
  30. echo -e "\e[92;1m[TEST HARNESS INFO]\e[0m" $*
  31. }
  32. function fail {
  33. echo -e "\e[91;1mFAILED:\e[0m" $*
  34. exit 1
  35. }
  36. function copy_directory() {
  37. local dir="$1"
  38. local parent="$(dirname "$dir")"
  39. mkdir -p "$MOCK_TOP/$parent"
  40. cp -R "$REAL_TOP/$dir" "$MOCK_TOP/$parent"
  41. }
  42. function symlink_file() {
  43. local file="$1"
  44. mkdir -p "$MOCK_TOP/$(dirname "$file")"
  45. ln -s "$REAL_TOP/$file" "$MOCK_TOP/$file"
  46. }
  47. function symlink_directory() {
  48. local dir="$1"
  49. mkdir -p "$MOCK_TOP/$dir"
  50. # We need to symlink the contents of the directory individually instead of
  51. # using one symlink for the whole directory because finder.go doesn't follow
  52. # symlinks when looking for Android.bp files
  53. for i in $(ls "$REAL_TOP/$dir"); do
  54. local target="$MOCK_TOP/$dir/$i"
  55. local source="$REAL_TOP/$dir/$i"
  56. if [[ -e "$target" ]]; then
  57. if [[ ! -d "$source" || ! -d "$target" ]]; then
  58. fail "Trying to symlink $dir twice"
  59. fi
  60. else
  61. ln -s "$REAL_TOP/$dir/$i" "$MOCK_TOP/$dir/$i";
  62. fi
  63. done
  64. }
  65. function create_mock_soong {
  66. copy_directory build/blueprint
  67. copy_directory build/soong
  68. symlink_directory prebuilts/go
  69. symlink_directory prebuilts/build-tools
  70. symlink_directory external/go-cmp
  71. symlink_directory external/golang-protobuf
  72. touch "$MOCK_TOP/Android.bp"
  73. }
  74. function setup() {
  75. cleanup_mock_top
  76. mkdir -p "$MOCK_TOP"
  77. echo
  78. echo ----------------------------------------------------------------------------
  79. info "Running test case \e[96;1m${FUNCNAME[1]}\e[0m"
  80. cd "$MOCK_TOP"
  81. tar xzf "$WARMED_UP_MOCK_TOP"
  82. }
  83. function run_soong() {
  84. build/soong/soong_ui.bash --make-mode --skip-ninja --skip-config --soong-only --skip-soong-tests "$@"
  85. }
  86. function create_mock_bazel() {
  87. copy_directory build/bazel
  88. symlink_directory prebuilts/bazel
  89. symlink_directory prebuilts/jdk
  90. symlink_directory external/bazel-skylib
  91. symlink_file WORKSPACE
  92. symlink_file BUILD
  93. symlink_file tools/bazel
  94. }
  95. run_bazel() {
  96. tools/bazel "$@"
  97. }
  98. run_ninja() {
  99. build/soong/soong_ui.bash --make-mode --skip-config --soong-only --skip-soong-tests "$@"
  100. }
  101. info "Starting Soong integration test suite $(basename $0)"
  102. info "Mock top: $MOCK_TOP"
  103. export ALLOW_MISSING_DEPENDENCIES=true
  104. warmup_mock_top