1 | # |
---|
2 | # RTEMS Tools Project (http://www.rtems.org/) |
---|
3 | # Copyright 2014 Chris Johns (chrisj@rtems.org) |
---|
4 | # All rights reserved. |
---|
5 | # |
---|
6 | # This file is part of the RTEMS Tools package in 'rtems-tools'. |
---|
7 | # |
---|
8 | # Permission to use, copy, modify, and/or distribute this software for any |
---|
9 | # purpose with or without fee is hereby granted, provided that the above |
---|
10 | # copyright notice and this permission notice appear in all copies. |
---|
11 | # |
---|
12 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
---|
13 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
---|
14 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
---|
15 | # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
---|
16 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
---|
17 | # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
---|
18 | # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
---|
19 | |
---|
20 | # |
---|
21 | # Manage sources and patches |
---|
22 | # |
---|
23 | |
---|
24 | import log |
---|
25 | |
---|
26 | def _args(args): |
---|
27 | return [i for s in [ii.split() for ii in args] for i in s] |
---|
28 | |
---|
29 | def _make_key(label, index): |
---|
30 | return '%s%04d' % (label, index) |
---|
31 | |
---|
32 | def add(label, args, macros, error): |
---|
33 | args = _args(args) |
---|
34 | if len(args) < 2: |
---|
35 | error('%%%s requires at least 2 arguments' % (label)) |
---|
36 | _map = '%s-%s' % (label, args[0]) |
---|
37 | macros.create_map(_map) |
---|
38 | index = 0 |
---|
39 | while True: |
---|
40 | key = _make_key(label, index) |
---|
41 | if key not in macros.map_keys(_map): |
---|
42 | break |
---|
43 | index += 1 |
---|
44 | macros.set_write_map(_map) |
---|
45 | macros.define(key, ' '.join(args[1:])) |
---|
46 | macros.unset_write_map() |
---|
47 | return None |
---|
48 | |
---|
49 | def set(label, args, macros, error): |
---|
50 | args = _args(args) |
---|
51 | if len(args) < 2: |
---|
52 | error('%%%s requires at least 2 arguments' % (label)) |
---|
53 | return |
---|
54 | _map = '%s-%s' % (label, args[0]) |
---|
55 | macros.create_map(_map) |
---|
56 | key = _make_key(label, 0) |
---|
57 | if key not in macros.map_keys(_map): |
---|
58 | macros.set_write_map(_map) |
---|
59 | macros.define(key, ' '.join(args[1:])) |
---|
60 | macros.unset_write_map() |
---|
61 | return None |
---|
62 | |
---|
63 | def setup(label, args, macros, error): |
---|
64 | args = _args(args) |
---|
65 | if len(args) < 2: |
---|
66 | error('%%%s requires at least 2 arguments: %s' % (label, ' '.join(args))) |
---|
67 | ss = '%%setup %s %s' % (label, ' '.join(args)) |
---|
68 | _map = '%s-%s' % (label, args[0]) |
---|
69 | if 'setup' in macros.map_keys(_map): |
---|
70 | error('%%%s already setup source: %s' % (label, ' '.join(args))) |
---|
71 | return |
---|
72 | macros.set_write_map(_map) |
---|
73 | macros.define('setup', ss) |
---|
74 | macros.unset_write_map() |
---|
75 | return [ss] |
---|
76 | |
---|
77 | def process(label, args, macros, error): |
---|
78 | if label != 'source' and label != 'patch': |
---|
79 | error('invalid source type: %s' % (label)) |
---|
80 | args = _args(args) |
---|
81 | log.trace('sources: %s' % (' '.join(args))) |
---|
82 | if len(args) < 3: |
---|
83 | error('%%%s requires at least 3 arguments: %s' % (label, ' '.join(args))) |
---|
84 | return |
---|
85 | if args[0] == 'set': |
---|
86 | return set(label, args[1:], macros, error) |
---|
87 | elif args[0] == 'add': |
---|
88 | return add(label, args[1:], macros, error) |
---|
89 | elif args[0] == 'setup': |
---|
90 | return setup(label, args[1:], macros, error) |
---|
91 | error('invalid %%%s command: %s' % (label, args[0])) |
---|
92 | |
---|
93 | def hash(args, macros, error): |
---|
94 | args = _args(args) |
---|
95 | if len(args) != 3: |
---|
96 | error('invalid number of hash args') |
---|
97 | return |
---|
98 | _map = 'hashes' |
---|
99 | _file = macros.expand(args[1]) |
---|
100 | new_value = '%s %s' % (args[0], args[2]) |
---|
101 | existing_value = get_hash(_file, macros) |
---|
102 | if existing_value is not None: |
---|
103 | if existing_value != new_value: |
---|
104 | error('conflicting hash definitions for: %s' % (args[1])) |
---|
105 | return |
---|
106 | else: |
---|
107 | macros.create_map(_map) |
---|
108 | macros.set_write_map(_map) |
---|
109 | macros.define(_file, new_value) |
---|
110 | macros.unset_write_map() |
---|
111 | return None |
---|
112 | |
---|
113 | def get(label, name, macros, error): |
---|
114 | _map = '%s-%s' % (label, name) |
---|
115 | keys = macros.map_keys(_map) |
---|
116 | if len(keys) == 0: |
---|
117 | error('no %s set: %s (%s)' % (label, name, _map)) |
---|
118 | return |
---|
119 | srcs = [] |
---|
120 | for s in keys: |
---|
121 | sm = macros.get(s, globals = False, maps = _map) |
---|
122 | if sm is None: |
---|
123 | error('source macro not found: %s in %s (%s)' % (s, name, _map)) |
---|
124 | srcs += [sm[2]] |
---|
125 | return srcs |
---|
126 | |
---|
127 | def get_sources(name, macros, error): |
---|
128 | return get('source', name, macros, error) |
---|
129 | |
---|
130 | def get_patches(name, macros, error): |
---|
131 | return get('patch', name, macros, error) |
---|
132 | |
---|
133 | def get_keys(label, name, macros, error): |
---|
134 | _map = '%s-%s' % (label, name) |
---|
135 | return macros.map_keys(_map) |
---|
136 | |
---|
137 | def get_source_keys(name, macros, error): |
---|
138 | return get_keys('source', name, macros, error) |
---|
139 | |
---|
140 | def get_patch_keys(name, macros, error): |
---|
141 | return get_keys('patch', name, macros, error) |
---|
142 | |
---|
143 | def get_names(label, macros, error): |
---|
144 | names = [] |
---|
145 | for m in macros.maps(): |
---|
146 | if m.startswith('%s-' % (label)): |
---|
147 | names += [m[len('%s-' % (label)):]] |
---|
148 | return names |
---|
149 | |
---|
150 | def get_source_names(macros, error): |
---|
151 | return get_names('source', macros, error) |
---|
152 | |
---|
153 | def get_patch_names(macros, error): |
---|
154 | return get_names('patch', macros, error) |
---|
155 | |
---|
156 | def get_hash(name, macros): |
---|
157 | hash = None |
---|
158 | if name in macros.map_keys('hashes'): |
---|
159 | m1, m2, hash = macros.get(name, globals = False, maps = 'hashes') |
---|
160 | return hash |
---|