2 # Copyright (c) 2011 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.
6 '''Tool to determine inputs and outputs of a grit file.
14 from grit
import grd_reader
18 def Outputs(filename
, defines
):
19 grd
= grd_reader
.Parse(
20 filename
, defines
=defines
, tags_to_ignore
=set(['messages']))
24 # Add all explicitly-specified output files
25 for output
in grd
.GetOutputFiles():
26 path
= output
.GetFilename()
29 if path
.endswith('.h'):
30 path
, filename
= os
.path
.split(path
)
31 if output
.attrs
['lang']:
32 lang_folders
[output
.attrs
['lang']] = os
.path
.dirname(path
)
34 # Add all generated files, once for each output language.
36 if node
.name
== 'structure':
37 # TODO(joi) Should remove the "if sconsdep is true" thing as it is a
38 # hack - see grit/node/structure.py
39 if node
.HasFileForLanguage() and node
.attrs
['sconsdep'] == 'true':
40 for lang
in lang_folders
:
41 path
= node
.FileForLanguage(lang
, lang_folders
[lang
],
43 return_if_not_generated
=False)
47 return [t
.replace('\\', '/') for t
in target
]
50 def Inputs(filename
, defines
):
51 grd
= grd_reader
.Parse(
52 filename
, debug
=False, defines
=defines
, tags_to_ignore
=set(['messages']))
55 if (node
.name
== 'structure' or node
.name
== 'skeleton' or
56 (node
.name
== 'file' and node
.parent
and
57 node
.parent
.name
== 'translations')):
58 files
.append(node
.GetFilePath())
59 elif node
.name
== 'include':
60 # Only include files that we actually plan on using.
61 if node
.SatisfiesOutputCondition():
62 files
.append(node
.FilenameToOpen())
63 # If it's a flattened node, grab inlined resources too.
64 if node
.attrs
['flattenhtml'] == 'true':
65 files
.extend(node
.GetHtmlResourceFilenames())
67 # Add in the grit source files. If one of these change, we want to re-run
69 grit_root_dir
= os
.path
.dirname(__file__
)
70 for root
, dirs
, filenames
in os
.walk(grit_root_dir
):
71 grit_src
= [os
.path
.join(root
, f
) for f
in filenames
72 if f
.endswith('.py') or f
== 'resource_ids']
73 files
.extend(grit_src
)
75 return [f
.replace('\\', '/') for f
in files
]
79 print 'USAGE: ./grit_info.py --inputs [-D foo] <grd-files>..'
80 print ' ./grit_info.py --outputs [-D foo] <out-prefix> <grd-files>..'
84 parser
= optparse
.OptionParser()
85 parser
.add_option("--inputs", action
="store_true", dest
="inputs")
86 parser
.add_option("--outputs", action
="store_true", dest
="outputs")
87 parser
.add_option("-D", action
="append", dest
="defines", default
=[])
88 # grit build also supports '-E KEY=VALUE', support that to share command
90 parser
.add_option("-E", action
="append", dest
="build_env", default
=[])
91 parser
.add_option("-w", action
="append", dest
="whitelist_files", default
=[])
93 options
, args
= parser
.parse_args(argv
)
99 for define
in options
.defines
:
103 for filename
in args
:
104 inputs
= Inputs(filename
, defines
)
105 # Include grd file as second input (works around gyp expecting it).
106 inputs
= [inputs
[0], filename
] + inputs
[1:]
107 if options
.whitelist_files
:
108 inputs
.extend(options
.whitelist_files
)
109 return '\n'.join(inputs
)
110 elif options
.outputs
:
115 outputs
= [posixpath
.join(args
[0], f
) for f
in Outputs(f
, defines
)]
116 return '\n'.join(outputs
)
122 result
= DoMain(argv
[1:])
130 if __name__
== '__main__':
131 sys
.exit(main(sys
.argv
))