migrate_cpp.sh 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/bin/bash -eux
  2. #
  3. # Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  4. # Exceptions. See /LICENSE for license information.
  5. # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. #
  7. # Runs an example migration of woff2 C++ code.
  8. # cd to the carbon-lang root.
  9. cd "$(dirname "$0")/../../.."
  10. EXAMPLE="${PWD}/third_party/examples/woff2"
  11. # Remove any previous conversion. Each time this is run, it should demonstrate
  12. # on a fresh copy of woff2.
  13. rm -rf "${EXAMPLE}/carbon/"
  14. # Initialize the carbon directory with C++ code only.
  15. mkdir -p "${EXAMPLE}/carbon/"
  16. for x in LICENSE include src; do
  17. cp -R "${EXAMPLE}/original/${x}" "${EXAMPLE}/carbon/${x}"
  18. done
  19. # Copy files into the carbon directory to simplify the setup.
  20. cp "${EXAMPLE}/BUILD.original" \
  21. "${EXAMPLE}/carbon/BUILD"
  22. cp "${EXAMPLE}/WORKSPACE.original" \
  23. "${EXAMPLE}/carbon/WORKSPACE"
  24. # Kludge for adding LLVM include paths into the compile flags.
  25. # TODO: Find better solution.
  26. COMPILE_FLAGS=($(cat "${EXAMPLE}/compile_flags.carbon.txt" | sed 's/"/\\"/g'))
  27. for x in $(
  28. clang++ -Wp,-v -xc++ -stdlib=libc++ - -fsyntax-only < /dev/null 2>&1 |
  29. grep /llvm/); do
  30. COMPILE_FLAGS+=("-isystem")
  31. COMPILE_FLAGS+=("${x}")
  32. done
  33. # Construct a compilation database for use by run-clang-tidy.py.
  34. COMPDB="${EXAMPLE}/carbon/compile_commands.json"
  35. echo "[" > "${COMPDB}"
  36. for f in $(find "${EXAMPLE}/carbon" -regex ".*\.\(cc\|h\)"); do
  37. echo "{ \"file\": \"$(realpath --relative-to "${EXAMPLE}/carbon" ${f})\"," >> "${COMPDB}"
  38. echo " \"directory\": \"${EXAMPLE}/carbon\"," >> "${COMPDB}"
  39. echo " \"arguments\": [" >> "${COMPDB}"
  40. echo " \"clang++\"," >> "${COMPDB}"
  41. for index in ${!COMPILE_FLAGS[@]}; do
  42. echo " \"${COMPILE_FLAGS[$index]}\"," >> "${COMPDB}"
  43. done
  44. echo " \"${f}\"" >> "${COMPDB}"
  45. echo " ]" >> "${COMPDB}"
  46. echo "}," >> "${COMPDB}"
  47. done
  48. # Remove the last comma, for JSON syntax correctness.
  49. sed -i '$ s/,$//' "${COMPDB}"
  50. echo "]" >> "${EXAMPLE}/carbon/compile_commands.json"
  51. # Run the migration tool.
  52. bazel build -c opt //migrate_cpp
  53. # Not sure why, but execution of cpp_refactoring fails while saving refactorings
  54. # if not in the directory. Ideally shouldn't be required, passing the path to
  55. # migrate_cpp should work.
  56. pushd "${EXAMPLE}/carbon"
  57. ../../../../bazel-bin/migrate_cpp/migrate_cpp .
  58. popd
  59. # Don't save the compile flags.
  60. rm "${EXAMPLE}/carbon/compile_commands.json"