rewrite-multiple-platforms.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #!/bin/bash
  2. # Copyright 2021 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 run
  6. # `rm -r ~/scratch && mkdir ~/scratch` first
  7. #
  8. #
  9. # For more fine-grained instructions, see:
  10. # https://docs.google.com/document/d/1chTvr3fSofQNV_PDPEHRyUgcJCQBgTDOOBriW9gIm9M/edit?ts=5e9549a2#heading=h.fjdnrdg1gcty
  11. set -e # makes the script quit on any command failure
  12. PLATFORMS="win,android"
  13. if [ "$1" != "" ]
  14. then
  15. PLATFORMS="$1"
  16. fi
  17. SCRIPT_PATH=$(realpath $0)
  18. REWRITER_SRC_DIR=$(dirname $SCRIPT_PATH)
  19. COMPILE_DIRS=.
  20. EDIT_DIRS=.
  21. # Save llvm-build as it is about to be overwritten.
  22. mv third_party/llvm-build third_party/llvm-build-upstream
  23. # Build and test the rewriter.
  24. echo "*** Building the rewriter ***"
  25. time tools/clang/scripts/build.py \
  26. --without-android \
  27. --without-fuchsia \
  28. --extra-tools rewrite_raw_ptr_fields
  29. tools/clang/rewrite_raw_ptr_fields/tests/run_all_tests.py
  30. args_for_platform() {
  31. case "$1" in
  32. android)
  33. cat <<EOF
  34. target_os = "android"
  35. clang_use_chrome_plugins = false
  36. is_chrome_branded = true
  37. is_debug = false
  38. dcheck_always_on = true
  39. is_official_build = true
  40. symbol_level = 1
  41. use_goma = false
  42. enable_remoting = true
  43. enable_webview_bundles = true
  44. ffmpeg_branding = "Chrome"
  45. proprietary_codecs = true
  46. EOF
  47. ;;
  48. win)
  49. cat <<EOF
  50. target_os = "win"
  51. clang_use_chrome_plugins = false
  52. enable_precompiled_headers = false
  53. is_chrome_branded = true
  54. is_debug = false
  55. dcheck_always_on = true
  56. is_official_build = true
  57. symbol_level = 1
  58. use_goma = false
  59. chrome_pgo_phase = 0
  60. EOF
  61. ;;
  62. linux)
  63. cat <<EOF
  64. target_os = "linux"
  65. dcheck_always_on = true
  66. is_chrome_branded = true
  67. is_debug = false
  68. is_official_build = true
  69. use_goma = false
  70. chrome_pgo_phase = 0
  71. EOF
  72. ;;
  73. cros)
  74. cat <<EOF
  75. target_os = "chromeos"
  76. chromeos_is_browser_only = true
  77. dcheck_always_on = true
  78. is_chrome_branded = true
  79. is_debug = false
  80. is_official_build = true
  81. use_goma = false
  82. chrome_pgo_phase = 0
  83. EOF
  84. ;;
  85. mac)
  86. cat <<EOF
  87. target_os = "mac"
  88. dcheck_always_on = true
  89. is_chrome_branded = true
  90. is_debug = false
  91. is_official_build = true
  92. use_goma = false
  93. chrome_pgo_phase = 0
  94. symbol_level = 1
  95. EOF
  96. ;;
  97. *)
  98. echo "unknown platform"
  99. exit 1
  100. ;;
  101. esac
  102. }
  103. pre_process() {
  104. PLATFORM="$1"
  105. OUT_DIR="out/rewrite-$PLATFORM"
  106. mkdir -p "$OUT_DIR"
  107. args_for_platform "$PLATFORM" > "$OUT_DIR/args.gn"
  108. # Build generated files that a successful compilation depends on.
  109. echo "*** Preparing targets for $PLATFORM ***"
  110. gn gen $OUT_DIR
  111. time ninja -C $OUT_DIR -t targets all \
  112. | grep '^gen/.*\(\.h\|inc\|css_tokenizer_codepoints.cc\)' \
  113. | cut -d : -f 1 \
  114. | xargs -s $(expr $(getconf ARG_MAX) - 256) ninja -C $OUT_DIR
  115. TARGET_OS_OPTION=""
  116. if [ $PLATFORM = "win" ]; then
  117. TARGET_OS_OPTION="--target_os=win"
  118. fi
  119. # A preliminary rewriter run in a special mode that generates a list of fields
  120. # to ignore. These fields would likely lead to compiler errors if rewritten.
  121. echo "*** Generating the ignore list for $PLATFORM ***"
  122. time tools/clang/scripts/run_tool.py \
  123. $TARGET_OS_OPTION \
  124. --tool rewrite_raw_ptr_fields \
  125. --tool-arg=--exclude-paths=$REWRITER_SRC_DIR/manual-paths-to-ignore.txt \
  126. --generate-compdb \
  127. -p $OUT_DIR \
  128. $COMPILE_DIRS > ~/scratch/rewriter-$PLATFORM.out
  129. cat ~/scratch/rewriter-$PLATFORM.out >> ~/scratch/rewriter.out
  130. }
  131. main_rewrite() {
  132. PLATFORM=$1
  133. OUT_DIR="out/rewrite-${PLATFORM}"
  134. TARGET_OS_OPTION=""
  135. if [ $PLATFORM = "win" ]; then
  136. TARGET_OS_OPTION="--target_os=win"
  137. fi
  138. # Main rewrite.
  139. echo "*** Running the main rewrite phase for $PLATFORM ***"
  140. time tools/clang/scripts/run_tool.py \
  141. $TARGET_OS_OPTION \
  142. --tool rewrite_raw_ptr_fields \
  143. --tool-arg=--exclude-fields="$HOME/scratch/combined-fields-to-ignore.txt" \
  144. --tool-arg=--exclude-paths=$REWRITER_SRC_DIR/manual-paths-to-ignore.txt \
  145. -p $OUT_DIR \
  146. $COMPILE_DIRS > ~/scratch/rewriter-$PLATFORM.main.out
  147. cat ~/scratch/rewriter-$PLATFORM.main.out >> ~/scratch/rewriter.main.out
  148. }
  149. for PLATFORM in ${PLATFORMS//,/ }
  150. do
  151. pre_process "$PLATFORM"
  152. done
  153. cat ~/scratch/rewriter.out \
  154. | sed '/^==== BEGIN FIELD FILTERS ====$/,/^==== END FIELD FILTERS ====$/{//!b};d' \
  155. | sort | uniq > ~/scratch/automated-fields-to-ignore.txt
  156. cat ~/scratch/automated-fields-to-ignore.txt \
  157. tools/clang/rewrite_raw_ptr_fields/manual-fields-to-ignore.txt \
  158. | grep -v "base::FileDescriptorWatcher::Controller::watcher_" \
  159. > ~/scratch/combined-fields-to-ignore.txt
  160. for PLATFORM in ${PLATFORMS//,/ }
  161. do
  162. main_rewrite "$PLATFORM"
  163. done
  164. # Apply edits generated by the main rewrite.
  165. echo "*** Applying edits ***"
  166. cat ~/scratch/rewriter.main.out | \
  167. tools/clang/scripts/extract_edits.py | \
  168. tools/clang/scripts/apply_edits.py -p out/rewrite-win $EDIT_DIRS
  169. # Format sources, as many lines are likely over 80 chars now.
  170. echo "*** Formatting ***"
  171. time git cl format
  172. # Restore llvm-build. Without this, your future builds will be painfully slow.
  173. rm -r -f third_party/llvm-build
  174. mv third_party/llvm-build-upstream third_party/llvm-build