clobber.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python
  2. # Copyright 2015 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. """This script provides methods for clobbering build directories."""
  6. import argparse
  7. import os
  8. import shutil
  9. import subprocess
  10. import sys
  11. def extract_gn_build_commands(build_ninja_file):
  12. """Extracts from a build.ninja the commands to run GN.
  13. The commands to run GN are the gn rule and build.ninja build step at the
  14. top of the build.ninja file. We want to keep these when deleting GN builds
  15. since we want to preserve the command-line flags to GN.
  16. On error, returns the empty string."""
  17. result = ""
  18. with open(build_ninja_file, 'r') as f:
  19. # Read until the third blank line. The first thing GN writes to the file
  20. # is "ninja_required_version = x.y.z", then the "rule gn" and the third
  21. # is the section for "build build.ninja", separated by blank lines.
  22. num_blank_lines = 0
  23. while num_blank_lines < 3:
  24. line = f.readline()
  25. if len(line) == 0:
  26. return '' # Unexpected EOF.
  27. result += line
  28. if line[0] == '\n':
  29. num_blank_lines = num_blank_lines + 1
  30. return result
  31. def delete_dir(build_dir):
  32. if os.path.islink(build_dir):
  33. return
  34. # For unknown reasons (anti-virus?) rmtree of Chromium build directories
  35. # often fails on Windows.
  36. if sys.platform.startswith('win'):
  37. subprocess.check_call(['rmdir', '/s', '/q', build_dir], shell=True)
  38. else:
  39. shutil.rmtree(build_dir)
  40. def delete_build_dir(build_dir):
  41. # GN writes a build.ninja.d file. Note that not all GN builds have args.gn.
  42. build_ninja_d_file = os.path.join(build_dir, 'build.ninja.d')
  43. if not os.path.exists(build_ninja_d_file):
  44. delete_dir(build_dir)
  45. return
  46. # GN builds aren't automatically regenerated when you sync. To avoid
  47. # messing with the GN workflow, erase everything but the args file, and
  48. # write a dummy build.ninja file that will automatically rerun GN the next
  49. # time Ninja is run.
  50. build_ninja_file = os.path.join(build_dir, 'build.ninja')
  51. build_commands = extract_gn_build_commands(build_ninja_file)
  52. try:
  53. gn_args_file = os.path.join(build_dir, 'args.gn')
  54. with open(gn_args_file, 'r') as f:
  55. args_contents = f.read()
  56. except IOError:
  57. args_contents = ''
  58. e = None
  59. try:
  60. # delete_dir and os.mkdir() may fail, such as when chrome.exe is running,
  61. # and we still want to restore args.gn/build.ninja/build.ninja.d, so catch
  62. # the exception and rethrow it later.
  63. delete_dir(build_dir)
  64. os.mkdir(build_dir)
  65. except Exception as e:
  66. pass
  67. # Put back the args file (if any).
  68. if args_contents != '':
  69. with open(gn_args_file, 'w') as f:
  70. f.write(args_contents)
  71. # Write the build.ninja file sufficiently to regenerate itself.
  72. with open(os.path.join(build_dir, 'build.ninja'), 'w') as f:
  73. if build_commands != '':
  74. f.write(build_commands)
  75. else:
  76. # Couldn't parse the build.ninja file, write a default thing.
  77. f.write('''ninja_required_version = 1.7.2
  78. rule gn
  79. command = gn -q gen //out/%s/
  80. description = Regenerating ninja files
  81. build build.ninja: gn
  82. generator = 1
  83. depfile = build.ninja.d
  84. ''' % (os.path.split(build_dir)[1]))
  85. # Write a .d file for the build which references a nonexistant file. This
  86. # will make Ninja always mark the build as dirty.
  87. with open(build_ninja_d_file, 'w') as f:
  88. f.write('build.ninja: nonexistant_file.gn\n')
  89. if e:
  90. # Rethrow the exception we caught earlier.
  91. raise e
  92. def clobber(out_dir):
  93. """Clobber contents of build directory.
  94. Don't delete the directory itself: some checkouts have the build directory
  95. mounted."""
  96. for f in os.listdir(out_dir):
  97. path = os.path.join(out_dir, f)
  98. if os.path.isfile(path):
  99. os.unlink(path)
  100. elif os.path.isdir(path):
  101. delete_build_dir(path)
  102. def main():
  103. parser = argparse.ArgumentParser()
  104. parser.add_argument('out_dir', help='The output directory to clobber')
  105. args = parser.parse_args()
  106. clobber(args.out_dir)
  107. return 0
  108. if __name__ == '__main__':
  109. sys.exit(main())