rewrite.sh 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/bin/bash
  2. # Copyright 2020 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. # IMPORTANT! Before running this script you have to perform these steps:
  6. # 1. Run `mkdir ~/scratch`
  7. # 2. Run `gn args out/{your_out_dir}` and set the following options:
  8. # use_goma = false
  9. # # this can be skipped if you do this in build/config/clang/clang.gni
  10. # clang_use_chrome_plugins = false
  11. #
  12. # (You can do these steps only once, as long as you don't delete the directories
  13. # between rewrites.)
  14. #
  15. # For more fine-grained instructions, see:
  16. # https://docs.google.com/document/d/1chTvr3fSofQNV_PDPEHRyUgcJCQBgTDOOBriW9gIm9M/edit?ts=5e9549a2#heading=h.fjdnrdg1gcty
  17. set -e # makes the script quit on any command failure
  18. OUT_DIR="out/rewrite"
  19. if [ "$1" != "" ]
  20. then
  21. OUT_DIR="$1"
  22. fi
  23. SCRIPT_PATH=$(realpath $0)
  24. REWRITER_SRC_DIR=$(dirname $SCRIPT_PATH)
  25. COMPILE_DIRS=.
  26. EDIT_DIRS=.
  27. # Save llvm-build as it is about to be overwritten.
  28. mv third_party/llvm-build third_party/llvm-build-upstream
  29. # Build and test the rewriter.
  30. echo "*** Building the rewriter ***"
  31. time tools/clang/scripts/build.py \
  32. --without-android \
  33. --without-fuchsia \
  34. --extra-tools rewrite_raw_ptr_fields
  35. tools/clang/rewrite_raw_ptr_fields/tests/run_all_tests.py
  36. # Build generated files that a successful compilation depends on.
  37. echo "*** Preparing targets ***"
  38. gn gen $OUT_DIR
  39. GEN_H_TARGETS=`ninja -C $OUT_DIR -t targets all | grep '^gen/.*\(\.h\|inc\|css_tokenizer_codepoints.cc\)' | cut -d : -f 1`
  40. time ninja -C $OUT_DIR $GEN_H_TARGETS
  41. if grep -qE '^\s*target_os\s*=\s*("win"|win)' $OUT_DIR/args.gn
  42. then
  43. TARGET_OS_OPTION="--target_os=win"
  44. fi
  45. # A preliminary rewriter run in a special mode that generates a list of fields
  46. # to ignore. These fields would likely lead to compiler errors if rewritten.
  47. echo "*** Generating the ignore list ***"
  48. time tools/clang/scripts/run_tool.py \
  49. $TARGET_OS_OPTION \
  50. --tool rewrite_raw_ptr_fields \
  51. --tool-arg=--exclude-paths=$REWRITER_SRC_DIR/manual-paths-to-ignore.txt \
  52. --generate-compdb \
  53. -p $OUT_DIR \
  54. $COMPILE_DIRS > ~/scratch/rewriter.out
  55. cat ~/scratch/rewriter.out \
  56. | sed '/^==== BEGIN FIELD FILTERS ====$/,/^==== END FIELD FILTERS ====$/{//!b};d' \
  57. | sort | uniq > ~/scratch/automated-fields-to-ignore.txt
  58. cat ~/scratch/automated-fields-to-ignore.txt \
  59. tools/clang/rewrite_raw_ptr_fields/manual-fields-to-ignore.txt \
  60. | grep -v "base::FileDescriptorWatcher::Controller::watcher_" \
  61. > ~/scratch/combined-fields-to-ignore.txt
  62. # Main rewrite.
  63. echo "*** Running the main rewrite phase ***"
  64. time tools/clang/scripts/run_tool.py \
  65. $TARGET_OS_OPTION \
  66. --tool rewrite_raw_ptr_fields \
  67. --tool-arg=--exclude-fields=$HOME/scratch/combined-fields-to-ignore.txt \
  68. --tool-arg=--exclude-paths=$REWRITER_SRC_DIR/manual-paths-to-ignore.txt \
  69. -p $OUT_DIR \
  70. $COMPILE_DIRS > ~/scratch/rewriter.main.out
  71. # Apply edits generated by the main rewrite.
  72. echo "*** Applying edits ***"
  73. cat ~/scratch/rewriter.main.out | \
  74. tools/clang/scripts/extract_edits.py | \
  75. tools/clang/scripts/apply_edits.py -p $OUT_DIR $EDIT_DIRS
  76. # Format sources, as many lines are likely over 80 chars now.
  77. echo "*** Formatting ***"
  78. time git cl format
  79. # Restore llvm-build. Without this, your future builds will be painfully slow.
  80. rm -r -f third_party/llvm-build
  81. mv third_party/llvm-build-upstream third_party/llvm-build