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