Add the ability to set board-specific flags
[matrix.git] / matrix / config.py
1 # Copyright (C) 2007-2009 Movial Creative Technologies Inc.
2 # Authors: Timo Savola
3 #          Kalle Vahlman <kalle.vahlman@movial.com>
4 #          Daniel Bainton <daniel.bainton@movial.com>
5
6 import re
7 from glob import glob
8 from os import getenv
9 from os.path import basename, exists, isabs, isdir, join
10 from sets import Set as set
11
12 Error = RuntimeError
13
14 exclude = re.compile(r'^[.#].*|.*[~#]$')
15
16 def _make_list(arg):
17         if arg is None:
18                 return []
19
20         if isinstance(arg, basestring):
21                 return arg.split()
22
23         return list(arg)
24
25 def config_dir(path=None, url=None, branch='master', optional=False):
26         from repositories import Repository
27
28         if not path:
29                 if not url:
30                         raise Error('No path or URL specified')
31
32                 path = basename(url)
33                 if path.endswith('.git'):
34                         path = path[:-4]
35
36         if optional and path not in config.initial_config_dirs:
37                 if config.debug:
38                         print 'Skipping', path
39                 return
40
41         if url:
42                 name = basename(path)
43                 repo = Repository(name=name, path=path, url=url, branch=branch)
44
45                 if repo.exists():
46                         if config.pull_config_dirs:
47                                 repo.pull()
48                 else:
49                         repo.clone()
50         else:
51                 if not isdir(path):
52                         raise Error('Config dir does not exist: %s' % path)
53
54         if path not in config.initial_config_dirs:
55                 config.config_dirs.append(path)
56
57 def pull_config_dirs(flag):
58         config.pull_config_dirs = flag
59
60 def root(url, branch='master'):
61         config.roots.append((url, branch))
62
63 class Board(object):
64         def __init__(self, name, arch, gcc_march,
65                      gcc_mcpu=None,
66                      gcc_mfpu=None,
67                      gcc_options=None,
68                      gnu_host=None,
69                      flash_erase_size=65536,
70                      flash_pad_size=0,
71                      flash_compression=None,
72                      ubifs_leb_size=0,
73                      ubifs_max_leb_count=0,
74                      ubifs_min_io_size=0,
75                      ubinize_config_vol_size=0,
76                      flags=[]):
77
78                 self.name = name
79                 self.arch = arch
80                 self.gcc_march = gcc_march
81                 self.gcc_mcpu = gcc_mcpu or ''
82                 self.gcc_mfpu = gcc_mfpu or ''
83                 self.gcc_options = _make_list(gcc_options)
84                 self.gnu_host = gnu_host or ''
85                 self.flash_erase_size = flash_erase_size
86                 self.flash_pad_size = flash_pad_size
87                 self.flash_compression = flash_compression or ''
88                 self.ubifs_leb_size = ubifs_leb_size
89                 self.ubifs_max_leb_count = ubifs_max_leb_count
90                 self.ubifs_min_io_size = ubifs_min_io_size
91                 self.ubinize_config_vol_size = ubinize_config_vol_size
92                 self.flags = flags
93
94                 config.boards[name] = self
95
96 def component(*args, **kwargs):
97         import components
98
99         c = components.Component(*args, **kwargs)
100         config.components[c.name] = c
101         return c
102
103 def provide_component(name):
104         c = config.components.get(name)
105         if not c:
106                 c = component(name)
107         c.cached = True
108         return c
109
110 def parse(pattern, require=True):
111         if isabs(pattern):
112                 dirs = [None]
113         else:
114                 config_dirs = config.config_dirs + config.initial_config_dirs
115                 dirs = [join(config.top_dir, dir) for dir in config_dirs]
116                 dirs.append(config.top_dir)
117
118         found = False
119
120         for dir in dirs:
121                 for path in glob(join(dir, pattern)):
122                         if exclude.match(basename(path)):
123                                 continue
124
125                         found = True
126
127                         if config.debug:
128                                 print 'Parsing', path
129
130                         execfile(path, actions.copy(), {})
131
132         if not found:
133                 msg = 'Config file(s) not found: %s' % pattern
134                 if require:
135                         raise Error(msg)
136                 elif config.debug:
137                         print msg
138
139 def set_flag(name):
140         config.flags.add(name)
141
142 def clear_flag(name):
143         config.flags.discard(name)
144
145 def use_board(name):
146         config.board = name
147
148 def cache_dir(path):
149         config.cache_dir = path
150
151 def sb2_target(name):
152         config.sb2_target = name
153
154 def sb2_compiler(path):
155         config.sb2_compiler = path
156
157 def sb2_init_options(args):
158         config.sb2_init_options = list(args)
159
160 def get_variables():
161         return config
162
163 def rootfs_root(path, target = None):
164         rootfs.path = path
165         rootfs.target = target
166
167 def include_paths(paths):
168         rootfs.include_paths.extend(paths)
169
170 def include_files(files):
171         rootfs.include_files.extend(files)
172
173 def exclude_paths(paths):
174         rootfs.exclude_paths.extend(paths)
175
176 def exclude_files(files):
177         rootfs.exclude_files.extend(files)
178
179 def exclude_expressions(expressions):
180         rootfs.exclude_expressions.extend(expressions)
181
182 def created_paths(paths):
183         rootfs.created_paths.extend(paths)
184
185 def file_modes(modes):
186         rootfs.file_modes.update(modes)
187
188 def directory_owners(owners):
189         rootfs.directory_owners.update(owners)
190
191 def device_nodes(devices):
192         rootfs.devices.update(devices)
193
194 def remove_account(uid=None, gid=None):
195         rootfs.remove_uid = uid
196         rootfs.remove_gid = gid
197
198 class RootFS(object):
199         def __init__(self):
200                 self.path = None
201                 self.target = None
202                 self.include_paths = []
203                 self.include_files = []
204                 self.exclude_paths = []
205                 self.exclude_files = []
206                 self.exclude_expressions = []
207                 self.created_paths = []
208                 self.devices = {}
209                 self.file_modes = {}
210                 self.directory_owners = {}
211                 self.remove_uid = None
212                 self.remove_gid = None
213
214 class Config(object):
215         def __init__(self):
216                 self.initial_config_dirs = []
217                 self.config_dirs = []
218
219                 self.top_dir = getenv('MATRIX_TOPDIR', '')
220                 __cache_dir = join(self.top_dir, 'build-cache')
221                 self.cache_dir = getenv('MATRIX_CACHEDIR', __cache_dir)
222                 self.script_dir = None
223
224                 self.boards = {}
225                 self.board = None
226                 self.components = {}
227                 self.roots = []
228                 self.flags = set()
229                 self.verbose = False
230                 self.debug = False
231                 self.protocol = False
232                 self.force = False
233                 self.keep_going = False
234                 self.pull_config_dirs = False
235                 self.jobs = 1
236                 self.make_jobs = 1
237
238                 self.sb2_target = None
239                 self.sb2_compiler = None
240                 self.sb2_init_options = []
241
242 actions = dict(
243         Board = Board,
244         CacheDir = cache_dir,
245         ClearFlag = clear_flag,
246         Component = component,
247         ConfigDir = config_dir,
248         GetVariables = get_variables,
249         Parse = parse,
250         ProvideComponent = provide_component,
251         PullConfigDirs = pull_config_dirs,
252         Root = root,
253         SB2Compiler = sb2_compiler,
254         SB2InitOptions = sb2_init_options,
255         SB2Target = sb2_target,
256         SetFlag = set_flag,
257         UseBoard = use_board,
258         RootFSRootPath = rootfs_root,
259         RootFSPaths = include_paths,
260         RootFSFiles = include_files,
261         RootFSExcludePaths = exclude_paths,
262         RootFSExcludeFiles = exclude_files,
263         RootFSExcludeExpressions = exclude_expressions,
264         RootFSCreatePaths = created_paths,
265         RootFSFileModes = file_modes,
266         RootFSDirectoryOwners = directory_owners,
267         RootFSDeviceNodes = device_nodes,
268         RootFSRemoveAccount = remove_account,
269 )
270
271 config = Config()
272 rootfs = RootFS()
273