OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 import os | 6 import os |
7 import os.path | 7 import os.path |
| 8 import re |
8 | 9 |
9 | 10 |
10 def GetNinjaOutputDirectory(chrome_root, configuration=None): | 11 def GetNinjaOutputDirectory(chrome_root, configuration=None): |
11 """Returns <chrome_root>/<output_dir>/(Release|Debug). | 12 """Returns <chrome_root>/<output_dir>/(Release|Debug). |
12 | 13 |
| 14 The output_dir is detected in the following ways, in order of precedence: |
| 15 1. CHROMIUM_OUT_DIR environment variable. |
| 16 2. GYP_GENERATOR_FLAGS environment variable output_dir property. |
| 17 3. Symlink target, if src/out is a symlink. |
| 18 4. Most recently modified (e.g. built) directory called out or out_*. |
| 19 |
13 The configuration chosen is the one most recently generated/built, but can be | 20 The configuration chosen is the one most recently generated/built, but can be |
14 overriden via the <configuration> parameter. Detects a custom output_dir | 21 overriden via the <configuration> parameter.""" |
15 specified by GYP_GENERATOR_FLAGS.""" | |
16 | 22 |
17 output_dir = 'out' | 23 output_dirs = [] |
18 generator_flags = os.getenv('GYP_GENERATOR_FLAGS', '').split(' ') | 24 if ('CHROMIUM_OUT_DIR' in os.environ and |
19 for flag in generator_flags: | 25 os.path.isdir(os.path.join(chrome_root, os.environ['CHROMIUM_OUT_DIR']))): |
20 name_value = flag.split('=', 1) | 26 output_dirs = [os.environ['CHROMIUM_OUT_DIR']] |
21 if len(name_value) == 2 and name_value[0] == 'output_dir': | 27 if not output_dirs: |
22 output_dir = name_value[1] | 28 generator_flags = os.getenv('GYP_GENERATOR_FLAGS', '').split(' ') |
| 29 for flag in generator_flags: |
| 30 name_value = flag.split('=', 1) |
| 31 if (len(name_value) == 2 and name_value[0] == 'output_dir' and |
| 32 os.path.isdir(os.path.join(chrome_root, name_value[1]))): |
| 33 output_dirs = [name_value[1]] |
| 34 if not output_dirs: |
| 35 out = os.path.join(chrome_root, 'out') |
| 36 if os.path.islink(out): |
| 37 out_target = os.path.join(os.path.dirname(out), os.readlink(out)) |
| 38 if os.path.exists(out_target): |
| 39 output_dirs = [out_target] |
| 40 if not output_dirs: |
| 41 for f in os.listdir(chrome_root): |
| 42 if (re.match('out(?:$|_)', f) and |
| 43 os.path.isdir(os.path.join(chrome_root, f))): |
| 44 output_dirs.append(f) |
23 | 45 |
24 root = os.path.join(chrome_root, output_dir) | 46 configs = [configuration] if configuration else ['Debug', 'Release'] |
25 if configuration: | 47 output_paths = [os.path.join(chrome_root, out_dir, config) |
26 return os.path.join(root, configuration) | 48 for out_dir in output_dirs for config in configs] |
27 | 49 |
28 debug_path = os.path.join(root, 'Debug') | 50 def approx_directory_mtime(path): |
29 release_path = os.path.join(root, 'Release') | 51 if not os.path.exists(path): |
| 52 return -1 |
| 53 # This is a heuristic; don't recurse into subdirectories. |
| 54 paths = [path] + [os.path.join(path, f) for f in os.listdir(path)] |
| 55 return max(os.path.getmtime(p) for p in paths) |
30 | 56 |
31 def is_release_15s_newer(test_path): | 57 return max(output_paths, key=approx_directory_mtime) |
32 try: | |
33 debug_mtime = os.path.getmtime(os.path.join(debug_path, test_path)) | |
34 except os.error: | |
35 debug_mtime = 0 | |
36 try: | |
37 rel_mtime = os.path.getmtime(os.path.join(release_path, test_path)) | |
38 except os.error: | |
39 rel_mtime = 0 | |
40 return rel_mtime - debug_mtime >= 15 | |
41 | |
42 if is_release_15s_newer('build.ninja') or is_release_15s_newer('protoc'): | |
43 return release_path | |
44 return debug_path | |
OLD | NEW |