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
3 # found in the LICENSE file.
13 def GetNinjaOutputDirectory(chrome_root
, configuration
=None):
14 """Returns <chrome_root>/<output_dir>/(Release|Debug).
16 If either of the following environment variables are set, their
17 value is used to determine the output directory:
18 1. CHROMIUM_OUT_DIR environment variable.
19 2. GYP_GENERATOR_FLAGS environment variable output_dir property.
21 Otherwise, all directories starting with the word out are examined.
23 The output directory must contain {configuration}/build.ninja (if
24 configuration is None, both Debug and Release will be checked).
26 The configuration chosen is the one most recently generated/built,
27 but can be overriden via the <configuration> parameter.
31 if ('CHROMIUM_OUT_DIR' in os
.environ
and
32 os
.path
.isdir(os
.path
.join(chrome_root
, os
.environ
['CHROMIUM_OUT_DIR']))):
33 output_dirs
= [os
.environ
['CHROMIUM_OUT_DIR']]
35 generator_flags
= os
.getenv('GYP_GENERATOR_FLAGS', '').split(' ')
36 for flag
in generator_flags
:
37 name_value
= flag
.split('=', 1)
38 if (len(name_value
) == 2 and name_value
[0] == 'output_dir' and
39 os
.path
.isdir(os
.path
.join(chrome_root
, name_value
[1]))):
40 output_dirs
= [name_value
[1]]
42 for f
in os
.listdir(chrome_root
):
43 if re
.match(r
'out\b', f
):
44 out
= os
.path
.realpath(os
.path
.join(chrome_root
, f
))
45 if os
.path
.isdir(out
):
46 output_dirs
.append(os
.path
.relpath(out
, start
= chrome_root
))
48 configs
= [configuration
] if configuration
else ['Debug', 'Release']
51 for out_dir
, config
in itertools
.product(output_dirs
, configs
):
52 path
= os
.path
.join(chrome_root
, out_dir
, config
)
53 if os
.path
.exists(os
.path
.join(path
, 'build.ninja')):
56 def approx_directory_mtime(path
):
57 # This is a heuristic; don't recurse into subdirectories.
58 paths
= [path
] + [os
.path
.join(path
, f
) for f
in os
.listdir(path
)]
59 return max(os
.path
.getmtime(p
) for p
in paths
)
62 return max(generate_paths(), key
=approx_directory_mtime
)
64 raise exceptions
.RuntimeError(
65 'Unable to find a valid ninja output directory.')
67 if __name__
== '__main__':
68 if len(sys
.argv
) != 2:
69 raise exceptions
.RuntimeError('Expected a single path argument.')
70 print GetNinjaOutputDirectory(sys
.argv
[1])