2 # Copyright (c) 2013 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.
22 # 'KEY' : ( <TYPE>, [Accepted Values], <Required?>)
24 'DISABLE': (bool, [True, False], False),
25 'SEL_LDR': (bool, [True, False], False),
26 # Disable this project from being included in the NaCl packaged app.
27 'DISABLE_PACKAGE': (bool, [True, False], False),
28 # Don't generate the additional files to allow this project to run as a
29 # packaged app (i.e. manifest.json, background.js, etc.).
30 'NO_PACKAGE_FILES': (bool, [True, False], False),
31 'TOOLS' : (list, VALID_TOOLCHAINS
, True),
32 'CONFIGS' : (list, ['Debug', 'Release'], False),
33 'PREREQ' : (list, '', False),
35 'NAME': (str, '', True),
37 # lib = library target
38 # so = shared object target, automatically added to NMF
39 # so-standalone = shared object target, not put into NMF
40 'TYPE': (str, ['main', 'lib', 'static-lib', 'so', 'so-standalone'],
42 'SOURCES': (list, '', True),
43 'CFLAGS': (list, '', False),
44 'CFLAGS_GCC': (list, '', False),
45 'CXXFLAGS': (list, '', False),
46 'DEFINES': (list, '', False),
47 'LDFLAGS': (list, '', False),
48 'INCLUDES': (list, '', False),
49 'LIBS' : (dict, VALID_TOOLCHAINS
, False),
50 'DEPS' : (list, '', False)
53 'FILES': (list, '', True),
54 'DEST': (str, '', True),
56 'SEARCH': (list, '', False),
57 'POST': (str, '', False),
58 'PRE': (str, '', False),
59 'DEST': (str, ['getting_started', 'examples/api',
60 'examples/demo', 'examples/tutorial',
61 'src', 'tests'], True),
62 'NAME': (str, '', False),
63 'DATA': (list, '', False),
64 'TITLE': (str, '', False),
65 'GROUP': (str, '', False),
66 'EXPERIMENTAL': (bool, [True, False], False),
67 'PERMISSIONS': (list, '', False),
68 'SOCKET_PERMISSIONS': (list, '', False),
69 'MULTI_PLATFORM': (bool, [True, False], False),
70 'MIN_CHROME_VERSION': (str, '', False),
74 class ValidationError(Exception):
78 def ValidateFormat(src
, dsc_format
):
79 # Verify all required keys are there
80 for key
in dsc_format
:
81 exp_type
, exp_value
, required
= dsc_format
[key
]
82 if required
and key
not in src
:
83 raise ValidationError('Missing required key %s.' % key
)
85 # For each provided key, verify it's valid
87 # Verify the key is known
88 if key
not in dsc_format
:
89 raise ValidationError('Unexpected key %s.' % key
)
91 exp_type
, exp_value
, required
= dsc_format
[key
]
94 # Verify the value is non-empty if required
95 if required
and not value
:
96 raise ValidationError('Expected non-empty value for %s.' % key
)
98 # If the expected type is a dict, but the provided type is a list
99 # then the list applies to all keys of the dictionary, so we reset
100 # the expected type and value.
102 if type(value
) is list:
106 # Verify the key is of the expected type
107 if exp_type
!= type(value
):
108 raise ValidationError('Key %s expects %s not %s.' % (
109 key
, exp_type
.__name
__.upper(), type(value
).__name
__.upper()))
111 # If it's a bool, the expected values are always True or False.
115 # If it's a string and there are expected values, make sure it matches
117 if type(exp_value
) is list and exp_value
:
118 if value
not in exp_value
:
119 raise ValidationError("Value '%s' not expected for %s." %
123 # if it's a list, then we need to validate the values
125 # If we expect a dictionary, then call this recursively
126 if type(exp_value
) is dict:
128 ValidateFormat(val
, exp_value
)
130 # If we expect a list of strings
131 if type(exp_value
) is str:
133 if type(val
) is not str:
134 raise ValidationError('Value %s in %s is not a string.' %
137 # if we expect a particular string
138 if type(exp_value
) is list:
140 if val
not in exp_value
:
141 raise ValidationError('Value %s not expected in %s.' %
145 # if we are expecting a dict, verify the keys are allowed
147 print "Expecting dict\n"
149 if sub
not in exp_value
:
150 raise ValidationError('Sub key %s not expected in %s.' %
154 # If we got this far, it's an unexpected type
155 raise ValidationError('Unexpected type %s for key %s.' %
156 (str(type(src
[key
])), key
))
159 def LoadProject(filename
):
160 with
open(filename
, 'r') as descfile
:
162 desc
= eval(descfile
.read(), {}, {})
163 except Exception as e
:
164 raise ValidationError(e
)
165 if desc
.get('DISABLE', False):
167 ValidateFormat(desc
, DSC_FORMAT
)
168 desc
['FILEPATH'] = os
.path
.abspath(filename
)
172 def LoadProjectTreeUnfiltered(srcpath
):
174 out
= collections
.defaultdict(list)
175 for root
, _
, files
in os
.walk(srcpath
):
176 for filename
in files
:
177 if fnmatch
.fnmatch(filename
, '*.dsc'):
178 filepath
= os
.path
.join(root
, filename
)
180 desc
= LoadProject(filepath
)
181 except ValidationError
as e
:
182 raise ValidationError("Failed to validate: %s: %s" % (filepath
, e
))
185 out
[key
].append(desc
)
189 def LoadProjectTree(srcpath
, include
, exclude
=None):
190 out
= LoadProjectTreeUnfiltered(srcpath
)
191 return FilterTree(out
, MakeDefaultFilterFn(include
, exclude
))
194 def GenerateProjects(tree
):
196 for val
in tree
[key
]:
200 def FilterTree(tree
, filter_fn
):
201 out
= collections
.defaultdict(list)
202 for branch
, desc
in GenerateProjects(tree
):
204 out
[branch
].append(desc
)
208 def MakeDefaultFilterFn(include
, exclude
):
209 def DefaultFilterFn(desc
):
210 matches_include
= not include
or DescMatchesFilter(desc
, include
)
211 matches_exclude
= exclude
and DescMatchesFilter(desc
, exclude
)
213 # Exclude list overrides include list.
216 return matches_include
218 return DefaultFilterFn
221 def DescMatchesFilter(desc
, filters
):
222 for key
, expected
in filters
.iteritems():
223 # For any filtered key which is unspecified, assumed False
224 value
= desc
.get(key
, False)
226 # If we provide an expected list, match at least one
227 if type(expected
) != list:
228 expected
= set([expected
])
229 if type(value
) != list:
232 if not set(expected
) & set(value
):
235 # If we fall through, then we matched the filters
239 def PrintProjectTree(tree
):
242 for val
in tree
[key
]:
243 print '\t' + val
['NAME']
247 parser
= optparse
.OptionParser(usage
='%prog [options] <dir>')
248 parser
.add_option('-e', '--experimental',
249 help='build experimental examples and libraries', action
='store_true')
250 parser
.add_option('-t', '--toolchain',
251 help='Build using toolchain. Can be passed more than once.',
254 options
, args
= parser
.parse_args(argv
[1:])
259 parser
.error('Expected 0 or 1 args, got %d.' % len(args
))
262 load_from_dir
= args
[0]
264 if options
.toolchain
:
265 filters
['TOOLS'] = options
.toolchain
267 if not options
.experimental
:
268 filters
['EXPERIMENTAL'] = False
271 tree
= LoadProjectTree(load_from_dir
, include
=filters
)
272 except ValidationError
as e
:
273 sys
.stderr
.write(str(e
) + '\n')
276 PrintProjectTree(tree
)
280 if __name__
== '__main__':
281 sys
.exit(main(sys
.argv
))