1 | # |
---|
2 | # Copyright (c) 2015-2016 Chris Johns <chrisj@rtems.org>. All rights reserved. |
---|
3 | # |
---|
4 | # Copyright (c) 2009, 2017 embedded brains GmbH. All rights reserved. |
---|
5 | # |
---|
6 | # embedded brains GmbH |
---|
7 | # Dornierstr. 4 |
---|
8 | # 82178 Puchheim |
---|
9 | # Germany |
---|
10 | # <info@embedded-brains.de> |
---|
11 | # |
---|
12 | # Copyright (c) 2012 OAR Corporation. All rights reserved. |
---|
13 | # |
---|
14 | # Redistribution and use in source and binary forms, with or without |
---|
15 | # modification, are permitted provided that the following conditions |
---|
16 | # are met: |
---|
17 | # 1. Redistributions of source code must retain the above copyright |
---|
18 | # notice, this list of conditions and the following disclaimer. |
---|
19 | # 2. Redistributions in binary form must reproduce the above copyright |
---|
20 | # notice, this list of conditions and the following disclaimer in the |
---|
21 | # documentation and/or other materials provided with the distribution. |
---|
22 | # |
---|
23 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
24 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
25 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
26 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
27 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
28 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
29 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
30 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
31 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
32 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
33 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
34 | |
---|
35 | # FreeBSD: http://svn.freebsd.org/base/releng/8.2/sys (revision 222485) |
---|
36 | |
---|
37 | from __future__ import print_function |
---|
38 | |
---|
39 | import shutil |
---|
40 | import os |
---|
41 | import re |
---|
42 | import sys |
---|
43 | import getopt |
---|
44 | import filecmp |
---|
45 | import difflib |
---|
46 | import codecs |
---|
47 | import copy |
---|
48 | |
---|
49 | # |
---|
50 | # Global controls. |
---|
51 | # |
---|
52 | LIBBSD_DIR = "." |
---|
53 | FreeBSD_DIR = "freebsd-org" |
---|
54 | verboseLevel = 0 |
---|
55 | isDryRun = False |
---|
56 | isDiffMode = False |
---|
57 | filesProcessedCount = 0 |
---|
58 | filesProcessed = [] |
---|
59 | filesTotal = 0 |
---|
60 | filesTotalLines = 0 |
---|
61 | filesTotalInserts = 0 |
---|
62 | filesTotalDeletes = 0 |
---|
63 | diffDetails = { } |
---|
64 | |
---|
65 | verboseInfo = 1 |
---|
66 | verboseDetail = 2 |
---|
67 | verboseMoreDetail = 3 |
---|
68 | verboseDebug = 4 |
---|
69 | |
---|
70 | def _cflagsIncludes(cflags, includes): |
---|
71 | if type(cflags) is not list: |
---|
72 | if cflags is not None: |
---|
73 | _cflags = cflags.split(' ') |
---|
74 | else: |
---|
75 | _cflags = [None] |
---|
76 | else: |
---|
77 | _cflags = cflags |
---|
78 | if type(includes) is not list: |
---|
79 | _includes = [includes] |
---|
80 | else: |
---|
81 | _includes = includes |
---|
82 | return _cflags, _includes |
---|
83 | |
---|
84 | def verbose(level = verboseInfo): |
---|
85 | return verboseLevel >= level |
---|
86 | |
---|
87 | def changedFileSummary(statsReport = False): |
---|
88 | |
---|
89 | global filesTotal, filesTotalLines, filesTotalInserts, filesTotalDeletes |
---|
90 | |
---|
91 | if isDiffMode == False: |
---|
92 | if verbose(): |
---|
93 | print('%d file(s) were changed:' % (filesProcessedCount)) |
---|
94 | for f in sorted(filesProcessed): |
---|
95 | print(' %s' % (f)) |
---|
96 | else: |
---|
97 | print('%d file(s) were changed.' % (filesProcessedCount)) |
---|
98 | if statsReport: |
---|
99 | print('Stats Report:') |
---|
100 | transparent = filesTotal - len(diffDetails) |
---|
101 | changes = filesTotalInserts + filesTotalDeletes |
---|
102 | opacity = (float(changes) / (filesTotalLines + changes)) * 100.0 |
---|
103 | print(' Total File(s):%d Unchanged:%d (%.1f%%) Changed:%d' \ |
---|
104 | ' Opacity:%5.1f%% Lines:%d Edits:%d (+):%d (-):%d' % \ |
---|
105 | (filesTotal, transparent, (float(transparent) / filesTotal) * 100.0, len(diffDetails), \ |
---|
106 | opacity, filesTotalLines, changes, filesTotalInserts, filesTotalDeletes)) |
---|
107 | # |
---|
108 | # Sort by opacity. |
---|
109 | # |
---|
110 | ordered_diffs = sorted(diffDetails.items(), key = lambda diff: diff[1].opacity, reverse = True) |
---|
111 | for f in ordered_diffs: |
---|
112 | print(' %s' % (diffDetails[f[0]].status())) |
---|
113 | |
---|
114 | def readFile(name): |
---|
115 | try: |
---|
116 | contents = codecs.open(name, mode = 'r', encoding = 'utf-8', errors = 'ignore').read() |
---|
117 | except UnicodeDecodeError as ude: |
---|
118 | print('error: reading: %s: %s' % (name, ude)) |
---|
119 | sys.exit(1) |
---|
120 | return contents |
---|
121 | |
---|
122 | def writeFile(name, contents): |
---|
123 | path = os.path.dirname(name) |
---|
124 | if not os.path.exists(path): |
---|
125 | try: |
---|
126 | os.makedirs(path) |
---|
127 | except OSError as oe: |
---|
128 | print('error: cannot create directory: %s: %s' % (path, oe)) |
---|
129 | sys.exit(1) |
---|
130 | try: |
---|
131 | codecs.open(name, mode = 'w', encoding = 'utf-8', errors = 'ignore').write(contents) |
---|
132 | except UnicodeDecodeError as ude: |
---|
133 | print('error: write: %s: %s' % (name, ude)) |
---|
134 | sys.exit(1) |
---|
135 | |
---|
136 | # |
---|
137 | # A builder error. |
---|
138 | # |
---|
139 | class error(Exception): |
---|
140 | """Base class for exceptions.""" |
---|
141 | def __init__(self, msg): |
---|
142 | self.msg = 'error: %s' % (msg) |
---|
143 | def set_output(self, msg): |
---|
144 | self.msg = msg |
---|
145 | def __str__(self): |
---|
146 | return self.msg |
---|
147 | |
---|
148 | # |
---|
149 | # Diff Record |
---|
150 | # |
---|
151 | class diffRecord: |
---|
152 | def __init__(self, src, dst, orig, diff, inserts, deletes): |
---|
153 | self.src = src |
---|
154 | self.dst = dst |
---|
155 | self.orig = orig |
---|
156 | self.diff = diff |
---|
157 | self.lines = len(orig) |
---|
158 | self.inserts = inserts |
---|
159 | self.deletes = deletes |
---|
160 | self.changes = inserts + deletes |
---|
161 | self.opacity = (float(self.changes) / (self.lines + self.changes)) * 100.0 |
---|
162 | |
---|
163 | def __repr__(self): |
---|
164 | return self.src |
---|
165 | |
---|
166 | def status(self): |
---|
167 | return 'opacity:%5.1f%% edits:%4d (+):%-4d (-):%-4d %s' % \ |
---|
168 | (self.opacity, self.changes, self.inserts, self.deletes, self.src) |
---|
169 | |
---|
170 | # |
---|
171 | # This stuff needs to move to libbsd.py. |
---|
172 | # |
---|
173 | |
---|
174 | # Move target dependent files under a machine directory |
---|
175 | def mapCPUDependentPath(path): |
---|
176 | return path.replace("include/", "include/machine/") |
---|
177 | |
---|
178 | def fixIncludes(data): |
---|
179 | data = re.sub('#include <sys/resource.h>', '#include <rtems/bsd/sys/resource.h>', data) |
---|
180 | data = re.sub('#include <sys/unistd.h>', '#include <rtems/bsd/sys/unistd.h>', data) |
---|
181 | return data |
---|
182 | |
---|
183 | # revert fixing the include paths inside a C or .h file |
---|
184 | def revertFixIncludes(data): |
---|
185 | data = re.sub('#include <rtems/bsd/', '#include <', data) |
---|
186 | data = re.sub('#include <util.h>', '#include <rtems/bsd/util.h>', data) |
---|
187 | data = re.sub('#include <bsd.h>', '#include <rtems/bsd/bsd.h>', data) |
---|
188 | data = re.sub('#include <zerocopy.h>', '#include <rtems/bsd/zerocopy.h>', data) |
---|
189 | return data |
---|
190 | |
---|
191 | # fix include paths inside a C or .h file |
---|
192 | def fixLocalIncludes(data): |
---|
193 | data = re.sub('#include "opt_([^"]*)"', '#include <rtems/bsd/local/opt_\\1>', data) |
---|
194 | data = re.sub('#include "([^"]*)_if.h"', '#include <rtems/bsd/local/\\1_if.h>', data) |
---|
195 | data = re.sub('#include "miidevs([^"]*)"', '#include <rtems/bsd/local/miidevs\\1>', data) |
---|
196 | data = re.sub('#include "usbdevs([^"]*)"', '#include <rtems/bsd/local/usbdevs\\1>', data) |
---|
197 | return data |
---|
198 | |
---|
199 | # revert fixing the include paths inside a C or .h file |
---|
200 | def revertFixLocalIncludes(data): |
---|
201 | data = re.sub('#include <rtems/bsd/local/([^>]*)>', '#include "\\1"', data) |
---|
202 | return data |
---|
203 | |
---|
204 | def assertHeaderFile(path): |
---|
205 | if path[-2] != '.' or path[-1] != 'h': |
---|
206 | print("*** " + path + " does not end in .h") |
---|
207 | print("*** Move it to a C source file list") |
---|
208 | sys.exit(2) |
---|
209 | |
---|
210 | def assertSourceFile(path): |
---|
211 | if path[-2] != '.' or (path[-1] != 'c' and path[-1] != 'S'): |
---|
212 | print("*** " + path + " does not end in .c") |
---|
213 | print("*** Move it to a header file list") |
---|
214 | sys.exit(2) |
---|
215 | |
---|
216 | def diffSource(dstLines, srcLines, src, dst): |
---|
217 | global filesTotal, filesTotalLines, filesTotalInserts, filesTotalDeletes |
---|
218 | # |
---|
219 | # Diff, note there is no line termination on each string. Expand the |
---|
220 | # generator to list because the generator is not reusable. |
---|
221 | # |
---|
222 | diff = list(difflib.unified_diff(dstLines, |
---|
223 | srcLines, |
---|
224 | fromfile = src, |
---|
225 | tofile = dst, |
---|
226 | n = 5, |
---|
227 | lineterm = '')) |
---|
228 | inserts = 0 |
---|
229 | deletes = 0 |
---|
230 | if len(diff) > 0: |
---|
231 | if src in diffDetails and \ |
---|
232 | diffDetails[src].dst != dst and diffDetails[src].diff != diff: |
---|
233 | raise error('repeated diff of file different: src:%s dst:%s' % (src, dst)) |
---|
234 | for l in diff: |
---|
235 | if l[0] == '-': |
---|
236 | deletes += 1 |
---|
237 | elif l[0] == '+': |
---|
238 | inserts += 1 |
---|
239 | diffDetails[src] = diffRecord(src, dst, srcLines, diff, inserts, deletes) |
---|
240 | |
---|
241 | # |
---|
242 | # Count the total files, lines and the level of changes. |
---|
243 | # |
---|
244 | filesTotal += 1 |
---|
245 | filesTotalLines += len(srcLines) |
---|
246 | filesTotalInserts += inserts |
---|
247 | filesTotalDeletes += deletes |
---|
248 | |
---|
249 | return diff |
---|
250 | |
---|
251 | # |
---|
252 | # Converters provide a way to alter the various types of code. The conversion |
---|
253 | # process filters a file as it is copies from the source path to the |
---|
254 | # destination path. Specialised versions are provided for different types of |
---|
255 | # source. |
---|
256 | # |
---|
257 | class Converter(object): |
---|
258 | |
---|
259 | def convert(self, src, dst, hasSource = True, sourceFilter = None, srcContents = None): |
---|
260 | |
---|
261 | global filesProcessed, filesProcessedCount |
---|
262 | |
---|
263 | if verbose(verboseDebug): |
---|
264 | print("convert: filter:%s: %s -> %s" % \ |
---|
265 | (['yes', 'no'][sourceFilter is None], src, dst)) |
---|
266 | |
---|
267 | # |
---|
268 | # If there is no source raise an error if we expect source else print a |
---|
269 | # warning and do not try and convert. |
---|
270 | # |
---|
271 | if srcContents is None: |
---|
272 | if not os.path.exists(src): |
---|
273 | if hasSource: |
---|
274 | raise error('source not found: %s' % (src)) |
---|
275 | else: |
---|
276 | print('warning: no source: %s' % (src)) |
---|
277 | return |
---|
278 | |
---|
279 | # |
---|
280 | # Files read as a single string if not passed in. |
---|
281 | # |
---|
282 | srcContents = readFile(src) |
---|
283 | |
---|
284 | if os.path.exists(dst): |
---|
285 | dstContents = readFile(dst) |
---|
286 | else: |
---|
287 | print('warning: no destination: %s' % (dst)) |
---|
288 | dstContents = '' |
---|
289 | |
---|
290 | # |
---|
291 | # Filter the source. |
---|
292 | # |
---|
293 | if sourceFilter is not None: |
---|
294 | srcContents = sourceFilter(srcContents) |
---|
295 | |
---|
296 | # |
---|
297 | # Split into a list of lines. |
---|
298 | # |
---|
299 | srcLines = srcContents.split(os.linesep) |
---|
300 | dstLines = dstContents.split(os.linesep) |
---|
301 | |
---|
302 | if verbose(verboseDebug): |
---|
303 | print('Unified diff: %s (lines:%d)' % (src, len(srcLines))) |
---|
304 | |
---|
305 | # |
---|
306 | # Diff, note there is no line termination on each string. |
---|
307 | # |
---|
308 | diff = diffSource(dstLines, srcLines, src, dst) |
---|
309 | |
---|
310 | # |
---|
311 | # The diff list is empty if the files are the same. |
---|
312 | # |
---|
313 | if len(diff) > 0: |
---|
314 | |
---|
315 | if verbose(verboseDebug): |
---|
316 | print('Unified diff length: %d' % len(diff)) |
---|
317 | |
---|
318 | filesProcessed += [dst] |
---|
319 | filesProcessedCount += 1 |
---|
320 | if isDiffMode == False: |
---|
321 | if verbose(verboseDetail): |
---|
322 | print("UPDATE: %s -> %s" % (src, dst)) |
---|
323 | if isDryRun == False: |
---|
324 | writeFile(dst, srcContents) |
---|
325 | else: |
---|
326 | print("diff -u %s %s" % (src, dst)) |
---|
327 | for l in diff: |
---|
328 | print(l) |
---|
329 | |
---|
330 | class NoConverter(Converter): |
---|
331 | def convert(self, src, dst, hasSource = True, sourceFilter = None): |
---|
332 | return '/* EMPTY */\n' |
---|
333 | |
---|
334 | class FromFreeBSDToRTEMSHeaderConverter(Converter): |
---|
335 | def sourceFilter(self, data): |
---|
336 | data = fixLocalIncludes(data) |
---|
337 | data = fixIncludes(data) |
---|
338 | return data |
---|
339 | |
---|
340 | def convert(self, src, dst): |
---|
341 | sconverter = super(FromFreeBSDToRTEMSHeaderConverter, self) |
---|
342 | sconverter.convert(src, dst, sourceFilter = self.sourceFilter) |
---|
343 | |
---|
344 | class FromFreeBSDToRTEMSUserSpaceHeaderConverter(Converter): |
---|
345 | def sourceFilter(self, data): |
---|
346 | data = fixIncludes(data) |
---|
347 | return data |
---|
348 | |
---|
349 | def convert(self, src, dst): |
---|
350 | sconverter = super(FromFreeBSDToRTEMSUserSpaceHeaderConverter, self) |
---|
351 | sconverter.convert(src, dst, sourceFilter = self.sourceFilter) |
---|
352 | |
---|
353 | class FromFreeBSDToRTEMSSourceConverter(Converter): |
---|
354 | def sourceFilter(self, data): |
---|
355 | data = fixLocalIncludes(data) |
---|
356 | data = fixIncludes(data) |
---|
357 | data = '#include <machine/rtems-bsd-kernel-space.h>\n\n' + data |
---|
358 | return data |
---|
359 | |
---|
360 | def convert(self, src, dst): |
---|
361 | sconverter = super(FromFreeBSDToRTEMSSourceConverter, self) |
---|
362 | sconverter.convert(src, dst, sourceFilter = self.sourceFilter) |
---|
363 | |
---|
364 | class FromFreeBSDToRTEMSUserSpaceSourceConverter(Converter): |
---|
365 | def sourceFilter(self, data): |
---|
366 | data = fixIncludes(data) |
---|
367 | data = '#include <machine/rtems-bsd-user-space.h>\n\n' + data |
---|
368 | return data |
---|
369 | |
---|
370 | def convert(self, src, dst): |
---|
371 | sconverter = super(FromFreeBSDToRTEMSUserSpaceSourceConverter, self) |
---|
372 | sconverter.convert(src, dst, sourceFilter = self.sourceFilter) |
---|
373 | |
---|
374 | class FromRTEMSToFreeBSDHeaderConverter(Converter): |
---|
375 | def sourceFilter(self, data): |
---|
376 | data = revertFixLocalIncludes(data) |
---|
377 | data = revertFixIncludes(data) |
---|
378 | return data |
---|
379 | |
---|
380 | def convert(self, src, dst): |
---|
381 | sconverter = super(FromRTEMSToFreeBSDHeaderConverter, self) |
---|
382 | sconverter.convert(src, dst, hasSource = False, sourceFilter = self.sourceFilter) |
---|
383 | |
---|
384 | class FromRTEMSToFreeBSDSourceConverter(Converter): |
---|
385 | def sourceFilter(self, data): |
---|
386 | data = re.sub('#include <machine/rtems-bsd-kernel-space.h>\n\n', '', data) |
---|
387 | data = re.sub('#include <machine/rtems-bsd-user-space.h>\n\n', '', data) |
---|
388 | data = revertFixLocalIncludes(data) |
---|
389 | data = revertFixIncludes(data) |
---|
390 | return data |
---|
391 | |
---|
392 | def convert(self, src, dst): |
---|
393 | sconverter = super(FromRTEMSToFreeBSDSourceConverter, self) |
---|
394 | sconverter.convert(src, dst, hasSource = False, sourceFilter = self.sourceFilter) |
---|
395 | |
---|
396 | # |
---|
397 | # Compose a path based for the various parts of the source tree. |
---|
398 | # |
---|
399 | class PathComposer(object): |
---|
400 | def composeOriginPath(self, path): |
---|
401 | return path |
---|
402 | |
---|
403 | def composeLibBSDPath(self, path, prefix): |
---|
404 | return os.path.join(prefix, path) |
---|
405 | |
---|
406 | class FreeBSDPathComposer(PathComposer): |
---|
407 | def composeOriginPath(self, path): |
---|
408 | return os.path.join(FreeBSD_DIR, path) |
---|
409 | |
---|
410 | def composeLibBSDPath(self, path, prefix): |
---|
411 | return os.path.join(prefix, 'freebsd', path) |
---|
412 | |
---|
413 | class RTEMSPathComposer(PathComposer): |
---|
414 | def composeOriginPath(self, path): |
---|
415 | return path |
---|
416 | |
---|
417 | def composeLibBSDPath(self, path, prefix): |
---|
418 | return os.path.join(prefix, 'rtemsbsd', path) |
---|
419 | |
---|
420 | class LinuxPathComposer(PathComposer): |
---|
421 | def composeOriginPath(self, path): |
---|
422 | return path |
---|
423 | |
---|
424 | def composeLibBSDPath(self, path, prefix): |
---|
425 | return os.path.join(prefix, 'linux', path) |
---|
426 | |
---|
427 | class CPUDependentFreeBSDPathComposer(FreeBSDPathComposer): |
---|
428 | def composeLibBSDPath(self, path, prefix): |
---|
429 | path = super(CPUDependentFreeBSDPathComposer, self).composeLibBSDPath(path, prefix) |
---|
430 | path = mapCPUDependentPath(path) |
---|
431 | return path |
---|
432 | |
---|
433 | class CPUDependentRTEMSPathComposer(RTEMSPathComposer): |
---|
434 | def composeLibBSDPath(self, path, prefix): |
---|
435 | path = super(CPUDependentRTEMSPathComposer, self).composeLibBSDPath(path, prefix) |
---|
436 | path = mapCPUDependentPath(path) |
---|
437 | return path |
---|
438 | |
---|
439 | class CPUDependentLinuxPathComposer(LinuxPathComposer): |
---|
440 | def composeLibBSDPath(self, path, prefix): |
---|
441 | path = super(CPUDependentLinuxPathComposer, self).composeLibBSDPath(path, prefix) |
---|
442 | path = mapCPUDependentPath(path) |
---|
443 | return path |
---|
444 | |
---|
445 | class TargetSourceCPUDependentPathComposer(CPUDependentFreeBSDPathComposer): |
---|
446 | def __init__(self, targetCPU, sourceCPU): |
---|
447 | self.targetCPU = targetCPU |
---|
448 | self.sourceCPU = sourceCPU |
---|
449 | |
---|
450 | def composeLibBSDPath(self, path, prefix): |
---|
451 | path = super(TargetSourceCPUDependentPathComposer, self).composeLibBSDPath(path, prefix) |
---|
452 | path = path.replace(self.sourceCPU, self.targetCPU) |
---|
453 | return path |
---|
454 | |
---|
455 | class BuildSystemFragmentComposer(object): |
---|
456 | def __init__(self, includes = None): |
---|
457 | if type(includes) is not list: |
---|
458 | self.includes = [includes] |
---|
459 | else: |
---|
460 | self.includes = includes |
---|
461 | |
---|
462 | def compose(self, path): |
---|
463 | return '' |
---|
464 | |
---|
465 | class SourceFileFragmentComposer(BuildSystemFragmentComposer): |
---|
466 | |
---|
467 | def __init__(self, cflags = "default", includes = None): |
---|
468 | self.cflags, self.includes = _cflagsIncludes(cflags, includes) |
---|
469 | |
---|
470 | def compose(self, path): |
---|
471 | if None in self.includes: |
---|
472 | flags = self.cflags |
---|
473 | else: |
---|
474 | flags = self.cflags + self.includes |
---|
475 | return ['sources', flags, ('default', None)], [path], self.cflags, self.includes |
---|
476 | |
---|
477 | class SourceFileIfHeaderComposer(SourceFileFragmentComposer): |
---|
478 | |
---|
479 | def __init__(self, headers, cflags = "default", includes = None): |
---|
480 | if headers is not list: |
---|
481 | headers = [headers] |
---|
482 | self.headers = headers |
---|
483 | super(SourceFileIfHeaderComposer, self).__init__(cflags = cflags, includes = includes) |
---|
484 | |
---|
485 | def compose(self, path): |
---|
486 | r = SourceFileFragmentComposer.compose(self, path) |
---|
487 | define_keys = '' |
---|
488 | for h in self.headers: |
---|
489 | h = h.upper() |
---|
490 | for c in '\/-.': |
---|
491 | h = h.replace(c, '_') |
---|
492 | define_keys += ' ' + h |
---|
493 | r[0][2] = (define_keys.strip(), self.headers) |
---|
494 | return r |
---|
495 | |
---|
496 | class TestFragementComposer(BuildSystemFragmentComposer): |
---|
497 | |
---|
498 | def __init__(self, testName, fileFragments, runTest = True, netTest = False): |
---|
499 | self.testName = testName |
---|
500 | self.fileFragments = fileFragments |
---|
501 | self.runTest = runTest |
---|
502 | self.netTest = netTest |
---|
503 | |
---|
504 | def compose(self, path): |
---|
505 | return ['tests', self.testName, ('default', None)], { 'files': self.fileFragments, |
---|
506 | 'run': self.runTest, |
---|
507 | 'net': self.netTest } |
---|
508 | |
---|
509 | class TestIfHeaderComposer(TestFragementComposer): |
---|
510 | |
---|
511 | def __init__(self, testName, headers, fileFragments, runTest = True, netTest = False): |
---|
512 | if headers is not list: |
---|
513 | headers = [headers] |
---|
514 | self.headers = headers |
---|
515 | super(TestIfHeaderComposer, self).__init__(testName, fileFragments, |
---|
516 | runTest = runTest, netTest = netTest) |
---|
517 | |
---|
518 | def compose(self, path): |
---|
519 | r = TestFragementComposer.compose(self, path) |
---|
520 | define_keys = '' |
---|
521 | for h in self.headers: |
---|
522 | h = h.upper() |
---|
523 | for c in '\/-.': |
---|
524 | h = h.replace(c, '_') |
---|
525 | define_keys += ' ' + h |
---|
526 | r[0][2] = (define_keys.strip(), self.headers) |
---|
527 | return r |
---|
528 | |
---|
529 | class KVMSymbolsFragmentComposer(BuildSystemFragmentComposer): |
---|
530 | |
---|
531 | def compose(self, path): |
---|
532 | return ['KVMSymbols', 'files', ('default', None)], [path], self.includes |
---|
533 | |
---|
534 | class RPCGENFragmentComposer(BuildSystemFragmentComposer): |
---|
535 | |
---|
536 | def compose(self, path): |
---|
537 | return ['RPCGen', 'files', ('default', None)], [path] |
---|
538 | |
---|
539 | class RouteKeywordsFragmentComposer(BuildSystemFragmentComposer): |
---|
540 | |
---|
541 | def compose(self, path): |
---|
542 | return ['RouteKeywords', 'files', ('default', None)], [path] |
---|
543 | |
---|
544 | class LexFragmentComposer(BuildSystemFragmentComposer): |
---|
545 | |
---|
546 | def __init__(self, sym, dep, cflags = None, includes = None): |
---|
547 | self.sym = sym |
---|
548 | self.dep = dep |
---|
549 | self.cflags, self.includes = _cflagsIncludes(cflags, includes) |
---|
550 | |
---|
551 | def compose(self, path): |
---|
552 | d = { 'file': path, |
---|
553 | 'sym': self.sym, |
---|
554 | 'dep': self.dep } |
---|
555 | if None not in self.cflags: |
---|
556 | d['cflags'] = self.cflags |
---|
557 | if None not in self.includes: |
---|
558 | d['includes'] = self.includes |
---|
559 | return ['lex', path, ('default', None)], d |
---|
560 | |
---|
561 | class YaccFragmentComposer(BuildSystemFragmentComposer): |
---|
562 | |
---|
563 | def __init__(self, sym, header, cflags = None, includes = None): |
---|
564 | self.sym = sym |
---|
565 | self.header = header |
---|
566 | self.cflags, self.includes = _cflagsIncludes(cflags, includes) |
---|
567 | |
---|
568 | def compose(self, path): |
---|
569 | d = { 'file': path, |
---|
570 | 'sym': self.sym, |
---|
571 | 'header': self.header } |
---|
572 | if None not in self.cflags: |
---|
573 | d['cflags'] = self.cflags |
---|
574 | if None not in self.includes: |
---|
575 | d['includes'] = self.includes |
---|
576 | return ['yacc', path, ('default', None)], d |
---|
577 | |
---|
578 | # |
---|
579 | # File - a file in the source we move backwards and forwards. |
---|
580 | # |
---|
581 | class File(object): |
---|
582 | def __init__(self, path, pathComposer, |
---|
583 | forwardConverter, reverseConverter, buildSystemComposer): |
---|
584 | if verbose(verboseMoreDetail): |
---|
585 | print("FILE: %-50s F:%-45s R:%-45s" % \ |
---|
586 | (path, |
---|
587 | forwardConverter.__class__.__name__, |
---|
588 | reverseConverter.__class__.__name__)) |
---|
589 | self.path = path |
---|
590 | self.pathComposer = pathComposer |
---|
591 | self.originPath = self.pathComposer.composeOriginPath(self.path) |
---|
592 | self.libbsdPath = self.pathComposer.composeLibBSDPath(self.path, LIBBSD_DIR) |
---|
593 | self.forwardConverter = forwardConverter |
---|
594 | self.reverseConverter = reverseConverter |
---|
595 | self.buildSystemComposer = buildSystemComposer |
---|
596 | |
---|
597 | def processSource(self, forward): |
---|
598 | if forward: |
---|
599 | if verbose(verboseDetail): |
---|
600 | print("process source: %s => %s" % (self.originPath, self.libbsdPath)) |
---|
601 | self.forwardConverter.convert(self.originPath, self.libbsdPath) |
---|
602 | else: |
---|
603 | if verbose(verboseDetail): |
---|
604 | print("process source: %s => %s converter:%s" % \ |
---|
605 | (self.libbsdPath, self.originPath, self.reverseConverter.__class__.__name__)) |
---|
606 | self.reverseConverter.convert(self.libbsdPath, self.originPath) |
---|
607 | |
---|
608 | def getFragment(self): |
---|
609 | return self.buildSystemComposer.compose(self.pathComposer.composeLibBSDPath(self.path, '')) |
---|
610 | |
---|
611 | # |
---|
612 | # Module - logical group of related files we can perform actions on |
---|
613 | # |
---|
614 | class Module(object): |
---|
615 | def __init__(self, manager, name, enabled = True): |
---|
616 | self.manager = manager |
---|
617 | self.name = name |
---|
618 | self.conditionalOn = "none" |
---|
619 | self.files = [] |
---|
620 | self.cpuDependentSourceFiles = {} |
---|
621 | self.dependencies = [] |
---|
622 | |
---|
623 | def initCPUDependencies(self, cpu): |
---|
624 | if cpu not in self.cpuDependentSourceFiles: |
---|
625 | self.cpuDependentSourceFiles[cpu] = [] |
---|
626 | |
---|
627 | def processSource(self, direction): |
---|
628 | if verbose(verboseDetail): |
---|
629 | print("process module: %s" % (self.name)) |
---|
630 | for f in self.files: |
---|
631 | f.processSource(direction) |
---|
632 | for cpu, files in self.cpuDependentSourceFiles.items(): |
---|
633 | for f in files: |
---|
634 | f.processSource(direction) |
---|
635 | |
---|
636 | def addFiles(self, newFiles, buildSystemComposer = BuildSystemFragmentComposer()): |
---|
637 | files = [] |
---|
638 | for newFile in newFiles: |
---|
639 | assertFile(newFile) |
---|
640 | files += [File(newFile, composers, buildSystemComposer)] |
---|
641 | return files |
---|
642 | |
---|
643 | def addFile(self, f): |
---|
644 | self.files += [f] |
---|
645 | |
---|
646 | def addFiles(self, newFiles, |
---|
647 | pathComposer, forwardConverter, reverseConverter, |
---|
648 | assertFile, buildSystemComposer = BuildSystemFragmentComposer()): |
---|
649 | files = [] |
---|
650 | for newFile in newFiles: |
---|
651 | assertFile(newFile) |
---|
652 | files += [File(newFile, pathComposer, forwardConverter, |
---|
653 | reverseConverter, buildSystemComposer)] |
---|
654 | return files |
---|
655 | |
---|
656 | def addKernelSpaceHeaderFiles(self, files): |
---|
657 | self.files += self.addFiles(files, |
---|
658 | FreeBSDPathComposer(), FromFreeBSDToRTEMSHeaderConverter(), |
---|
659 | FromRTEMSToFreeBSDHeaderConverter(), assertHeaderFile) |
---|
660 | |
---|
661 | def addUserSpaceHeaderFiles(self, files): |
---|
662 | self.files += self.addFiles(files, |
---|
663 | FreeBSDPathComposer(), FromFreeBSDToRTEMSUserSpaceHeaderConverter(), |
---|
664 | FromRTEMSToFreeBSDHeaderConverter(), assertHeaderFile) |
---|
665 | |
---|
666 | def addRTEMSHeaderFiles(self, files): |
---|
667 | self.files += self.addFiles(files, RTEMSPathComposer(), |
---|
668 | NoConverter(), NoConverter(), assertHeaderFile) |
---|
669 | |
---|
670 | def addLinuxHeaderFiles(self, files): |
---|
671 | self.files += self.addFiles(files, |
---|
672 | PathComposer(), NoConverter(), |
---|
673 | NoConverter(), assertHeaderFile) |
---|
674 | |
---|
675 | def addCPUDependentFreeBSDHeaderFiles(self, files): |
---|
676 | self.files += self.addFiles(files, |
---|
677 | CPUDependentFreeBSDPathComposer(), FromFreeBSDToRTEMSHeaderConverter(), |
---|
678 | FromRTEMSToFreeBSDHeaderConverter(), assertHeaderFile) |
---|
679 | |
---|
680 | def addCPUDependentLinuxHeaderFiles(self, files): |
---|
681 | self.files += self.addFiles(files, |
---|
682 | CPUDependentLinuxPathComposer(), NoConverter(), |
---|
683 | NoConverter(), assertHeaderFile) |
---|
684 | |
---|
685 | def addTargetSourceCPUDependentHeaderFiles(self, targetCPUs, sourceCPU, files): |
---|
686 | for cpu in targetCPUs: |
---|
687 | self.files += self.addFiles(files, |
---|
688 | TargetSourceCPUDependentPathComposer(cpu, sourceCPU), |
---|
689 | FromFreeBSDToRTEMSHeaderConverter(), |
---|
690 | NoConverter(), assertHeaderFile) |
---|
691 | |
---|
692 | def addSourceFiles(self, files, sourceFileFragmentComposer): |
---|
693 | self.files += self.addFiles(files, |
---|
694 | PathComposer(), NoConverter(), NoConverter(), assertSourceFile, |
---|
695 | sourceFileFragmentComposer) |
---|
696 | |
---|
697 | def addKernelSpaceSourceFiles(self, files, sourceFileFragmentComposer): |
---|
698 | self.files += self.addFiles(files, |
---|
699 | FreeBSDPathComposer(), FromFreeBSDToRTEMSSourceConverter(), |
---|
700 | FromRTEMSToFreeBSDSourceConverter(), assertSourceFile, |
---|
701 | sourceFileFragmentComposer) |
---|
702 | |
---|
703 | def addUserSpaceSourceFiles(self, files, sourceFileFragmentComposer): |
---|
704 | self.files += self.addFiles(files, |
---|
705 | FreeBSDPathComposer(), |
---|
706 | FromFreeBSDToRTEMSUserSpaceSourceConverter(), |
---|
707 | FromRTEMSToFreeBSDSourceConverter(), assertSourceFile, |
---|
708 | sourceFileFragmentComposer) |
---|
709 | |
---|
710 | def addRTEMSSourceFiles(self, files, sourceFileFragmentComposer): |
---|
711 | self.files += self.addFiles(files, |
---|
712 | RTEMSPathComposer(), NoConverter(), NoConverter(), |
---|
713 | assertSourceFile, sourceFileFragmentComposer) |
---|
714 | |
---|
715 | def addLinuxSourceFiles(self, files, sourceFileFragmentComposer): |
---|
716 | self.files += self.addFiles(files, |
---|
717 | PathComposer(), NoConverter(), |
---|
718 | NoConverter(), assertSourceFile, |
---|
719 | sourceFileFragmentComposer) |
---|
720 | |
---|
721 | def addCPUDependentFreeBSDSourceFiles(self, cpus, files, sourceFileFragmentComposer): |
---|
722 | for cpu in cpus: |
---|
723 | self.initCPUDependencies(cpu) |
---|
724 | self.cpuDependentSourceFiles[cpu] += \ |
---|
725 | self.addFiles(files, |
---|
726 | CPUDependentFreeBSDPathComposer(), FromFreeBSDToRTEMSSourceConverter(), |
---|
727 | FromRTEMSToFreeBSDSourceConverter(), assertSourceFile, |
---|
728 | sourceFileFragmentComposer) |
---|
729 | |
---|
730 | def addCPUDependentRTEMSSourceFiles(self, cpus, files, sourceFileFragmentComposer): |
---|
731 | for cpu in cpus: |
---|
732 | self.initCPUDependencies(cpu) |
---|
733 | self.cpuDependentSourceFiles[cpu] += \ |
---|
734 | self.addFiles(files, |
---|
735 | CPUDependentRTEMSPathComposer(), NoConverter(), |
---|
736 | NoConverter(), assertSourceFile, |
---|
737 | sourceFileFragmentComposer) |
---|
738 | |
---|
739 | def addCPUDependentLinuxSourceFiles(self, cpus, files, sourceFileFragmentComposer): |
---|
740 | for cpu in cpus: |
---|
741 | self.initCPUDependencies(cpu) |
---|
742 | self.cpuDependentSourceFiles[cpu] += \ |
---|
743 | self.addFiles(files, |
---|
744 | CPUDependentLinuxPathComposer(), NoConverter(), |
---|
745 | NoConverter(), assertSourceFile, |
---|
746 | sourceFileFragmentComposer) |
---|
747 | |
---|
748 | def addTest(self, testFragementComposer): |
---|
749 | self.files += [File(testFragementComposer.testName, |
---|
750 | PathComposer(), NoConverter(), NoConverter(), |
---|
751 | testFragementComposer)] |
---|
752 | |
---|
753 | def addDependency(self, dep): |
---|
754 | self.dependencies += [dep] |
---|
755 | |
---|
756 | # |
---|
757 | # Manager - a collection of modules. |
---|
758 | # |
---|
759 | class ModuleManager(object): |
---|
760 | def __init__(self): |
---|
761 | self.modules = {} |
---|
762 | self.generator = {} |
---|
763 | self.configuration = {} |
---|
764 | self.setGenerators() |
---|
765 | |
---|
766 | def __getitem__(self, key): |
---|
767 | if key not in self.modules: |
---|
768 | raise KeyError('module %s not found' % (key)) |
---|
769 | return self.modules[key] |
---|
770 | |
---|
771 | def getAllModules(self): |
---|
772 | if 'modules' in self.configuration: |
---|
773 | return self.configuration['modules'] |
---|
774 | return [] |
---|
775 | |
---|
776 | def getEnabledModules(self): |
---|
777 | if 'modules-enabled' in self.configuration: |
---|
778 | return self.configuration['modules-enabled'] |
---|
779 | return [] |
---|
780 | |
---|
781 | def addModule(self, module): |
---|
782 | self.modules[module.name] = module |
---|
783 | |
---|
784 | def processSource(self, direction): |
---|
785 | if verbose(verboseDetail): |
---|
786 | print("process modules:") |
---|
787 | for m in sorted(self.modules): |
---|
788 | self.modules[m].processSource(direction) |
---|
789 | |
---|
790 | def setConfiguration(self, config): |
---|
791 | self.configuration = copy.deepcopy(config) |
---|
792 | |
---|
793 | def getConfiguration(self): |
---|
794 | return self.configuration |
---|
795 | |
---|
796 | def updateConfiguration(self, config): |
---|
797 | self.configuration.update(config) |
---|
798 | |
---|
799 | def setModuleConfigiuration(self): |
---|
800 | mods = sorted(self.modules.keys()) |
---|
801 | self.configuration['modules'] = mods |
---|
802 | # Enabled modules are overwritten by config file. Default to all. |
---|
803 | self.configuration['modules-enabled'] = mods |
---|
804 | |
---|
805 | def generateBuild(self, only_enabled=True): |
---|
806 | modules_to_process = self.getEnabledModules() |
---|
807 | # Used for copy between FreeBSD and RTEMS |
---|
808 | if only_enabled == False: |
---|
809 | modules_to_process = self.getAllModules() |
---|
810 | for m in modules_to_process: |
---|
811 | self.modules[m].generate() |
---|
812 | |
---|
813 | def setGenerators(self): |
---|
814 | self.generator['convert'] = Converter |
---|
815 | self.generator['no-convert'] = NoConverter |
---|
816 | self.generator['from-FreeBSD-to-RTEMS-UserSpaceSourceConverter'] = FromFreeBSDToRTEMSUserSpaceSourceConverter |
---|
817 | self.generator['from-RTEMS-To-FreeBSD-SourceConverter'] = FromRTEMSToFreeBSDSourceConverter |
---|
818 | self.generator['buildSystemFragmentComposer'] = BuildSystemFragmentComposer |
---|
819 | |
---|
820 | self.generator['file'] = File |
---|
821 | |
---|
822 | self.generator['path'] = PathComposer |
---|
823 | self.generator['freebsd-path'] = FreeBSDPathComposer |
---|
824 | self.generator['rtems-path'] = RTEMSPathComposer |
---|
825 | self.generator['cpu-path'] = CPUDependentFreeBSDPathComposer |
---|
826 | self.generator['target-src-cpu--path'] = TargetSourceCPUDependentPathComposer |
---|
827 | |
---|
828 | self.generator['source'] = SourceFileFragmentComposer |
---|
829 | self.generator['test'] = TestFragementComposer |
---|
830 | self.generator['kvm-symbols'] = KVMSymbolsFragmentComposer |
---|
831 | self.generator['rpc-gen'] = RPCGENFragmentComposer |
---|
832 | self.generator['route-keywords'] = RouteKeywordsFragmentComposer |
---|
833 | self.generator['lex'] = LexFragmentComposer |
---|
834 | self.generator['yacc'] = YaccFragmentComposer |
---|
835 | |
---|
836 | self.generator['source-if-header'] = SourceFileIfHeaderComposer |
---|
837 | self.generator['test-if-header'] = TestIfHeaderComposer |
---|