diff_build_graphs.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/bin/bash -eu
  2. #
  3. # Copyright 2017 Google Inc. All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. set -e
  17. # This file makes it easy to confirm that a set of changes in source code don't result in any
  18. # changes to the generated ninja files. This is to reduce the effort required to be confident
  19. # in the correctness of refactorings
  20. function die() {
  21. echo "$@" >&2
  22. exit 1
  23. }
  24. function usage() {
  25. violation="$1"
  26. die "$violation
  27. Usage: diff_build_graphs.sh [--products=product1,product2...] <OLD_VERSIONS> <NEW_VERSIONS>
  28. This file builds and parses the build files (Android.mk, Android.bp, etc) for each requested
  29. product and for both sets of versions, and checks whether the ninja files (which implement
  30. the build graph) changed between the two versions.
  31. Example: diff_build_graphs.sh 'build/soong:work^ build/blueprint:work^' 'build/soong:work build/blueprint:work'
  32. Options:
  33. --products=PRODUCTS comma-separated list of products to check"
  34. }
  35. PRODUCTS_ARG=""
  36. OLD_VERSIONS=""
  37. NEW_VERSIONS=""
  38. function parse_args() {
  39. # parse optional arguments
  40. while true; do
  41. arg="${1-}"
  42. case "$arg" in
  43. --products=*) PRODUCTS_ARG="$arg";;
  44. *) break;;
  45. esac
  46. shift
  47. done
  48. # parse required arguments
  49. if [ "$#" != "2" ]; then
  50. usage ""
  51. fi
  52. #argument validation
  53. OLD_VERSIONS="$1"
  54. NEW_VERSIONS="$2"
  55. }
  56. parse_args "$@"
  57. # find some file paths
  58. cd "$(dirname $0)"
  59. SCRIPT_DIR="$PWD"
  60. cd ../../..
  61. CHECKOUT_ROOT="$PWD"
  62. OUT_DIR="${OUT_DIR-}"
  63. if [ -z "$OUT_DIR" ]; then
  64. OUT_DIR=out
  65. fi
  66. WORK_DIR="$OUT_DIR/diff"
  67. OUT_DIR_OLD="$WORK_DIR/out_old"
  68. OUT_DIR_NEW="$WORK_DIR/out_new"
  69. OUT_DIR_TEMP="$WORK_DIR/out_temp"
  70. function checkout() {
  71. versionSpecs="$1"
  72. for versionSpec in $versionSpecs; do
  73. project="$(echo $versionSpec | sed 's|\([^:]*\):\([^:]*\)|\1|')"
  74. ref="$(echo $versionSpec | sed 's|\([^:]*\):\([^:]*\)|\2|')"
  75. echo "checking out ref $ref in project $project"
  76. git -C "$project" checkout "$ref"
  77. done
  78. }
  79. function run_build() {
  80. echo
  81. echo "Starting build"
  82. # rebuild multiproduct_kati, in case it was missing before,
  83. # or in case it is affected by some of the changes we're testing
  84. make blueprint_tools
  85. # find multiproduct_kati and have it build the ninja files for each product
  86. builder="$(echo $OUT_DIR/host/*/bin/multiproduct_kati)"
  87. BUILD_NUMBER=sample "$builder" $PRODUCTS_ARG --keep --out "$OUT_DIR_TEMP" || true
  88. echo
  89. }
  90. function diffProduct() {
  91. product="$1"
  92. zip1="$OUT_DIR_OLD/${product}.zip"
  93. unzipped1="$OUT_DIR_OLD/$product"
  94. zip2="$OUT_DIR_NEW/${product}.zip"
  95. unzipped2="$OUT_DIR_NEW/$product"
  96. unzip -qq "$zip1" -d "$unzipped1"
  97. unzip -qq "$zip2" -d "$unzipped2"
  98. #do a diff of the ninja files
  99. diffFile="$WORK_DIR/diff.txt"
  100. diff -r "$unzipped1" "$unzipped2" -x build_date.txt -x build_number.txt -x '\.*' -x '*.log' -x build_fingerprint.txt -x build.ninja.d -x '*.zip' > $diffFile || true
  101. if [[ -s "$diffFile" ]]; then
  102. # outputs are different, so remove the unzipped versions but keep the zipped versions
  103. echo "First few differences (total diff linecount=$(wc -l $diffFile)) for product $product:"
  104. cat "$diffFile" | head -n 10
  105. echo "End of differences for product $product"
  106. rm -rf "$unzipped1" "$unzipped2"
  107. else
  108. # outputs are the same, so remove all of the outputs
  109. rm -rf "$zip1" "$unzipped1" "$zip2" "$unzipped2"
  110. fi
  111. }
  112. function do_builds() {
  113. #reset work dir
  114. rm -rf "$WORK_DIR"
  115. mkdir "$WORK_DIR"
  116. #build new code
  117. checkout "$NEW_VERSIONS"
  118. run_build
  119. mv "$OUT_DIR_TEMP" "$OUT_DIR_NEW"
  120. #build old code
  121. #TODO do we want to cache old results? Maybe by the time we care to cache old results this will
  122. #be running on a remote server somewhere and be completely different
  123. checkout "$OLD_VERSIONS"
  124. run_build
  125. mv "$OUT_DIR_TEMP" "$OUT_DIR_OLD"
  126. #cleanup
  127. echo created "$OUT_DIR_OLD" and "$OUT_DIR_NEW"
  128. }
  129. function main() {
  130. do_builds
  131. checkout "$NEW_VERSIONS"
  132. #find all products
  133. productsFile="$WORK_DIR/all_products.txt"
  134. find $OUT_DIR_OLD $OUT_DIR_NEW -mindepth 1 -maxdepth 1 -name "*.zip" | sed "s|^$OUT_DIR_OLD/||" | sed "s|^$OUT_DIR_NEW/||" | sed "s|\.zip$||" | sort | uniq > "$productsFile"
  135. echo Diffing products
  136. for product in $(cat $productsFile); do
  137. diffProduct "$product"
  138. done
  139. echo Done diffing products
  140. echo "Any differing outputs can be seen at $OUT_DIR_OLD/*.zip and $OUT_DIR_NEW/*.zip"
  141. echo "See $WORK_DIR/diff.txt for the full list of differences for the latest product checked"
  142. }
  143. main