070e51cff17ba487054e9ea9273e1d11adb48b51
[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
77                 self.name = name
78                 self.arch = arch
79                 self.gcc_march = gcc_march
80                 self.gcc_mcpu = gcc_mcpu or ''
81                 self.gcc_mfpu = gcc_mfpu or ''
82                 self.gcc_options = _make_list(gcc_options)
83                 self.gnu_host = gnu_host or ''
84                 self.flash_erase_size = flash_erase_size
85                 self.flash_pad_size = flash_pad_size
86                 self.flash_compression = flash_compression or ''
87                 self.ubifs_leb_size = ubifs_leb_size
88                 self.ubifs_max_leb_count = ubifs_max_leb_count
89                 self.ubifs_min_io_size = ubifs_min_io_size
90                 self.ubinize_config_vol_size = ubinize_config_vol_size
91
92                 config.boards[name] = self
93
94 def component(*args, **kwargs):
95         import components
96
97         c = components.Component(*args, **kwargs)
98         config.components[c.name] = c
99         return c
100
101 def provide_component(name):
102         c = config.components.get(name)
103         if not c:
104                 c = component(name)
105         c.cached = True
106         return c
107
108 def parse(pattern, require=True):
109         if isabs(pattern):
110                 dirs = [None]
111         else:
112                 config_dirs = config.config_dirs + config.initial_config_dirs
113                 dirs = [join(config.top_dir, dir) for dir in config_dirs]
114                 dirs.append(config.top_dir)
115
116         found = False
117
118         for dir in dirs:
119                 for path in glob(join(dir, pattern)):
120                         if exclude.match(basename(path)):
121                                 continue
122
123                         found = True
124
125                         if config.debug:
126                                 print 'Parsing', path
127
128                         execfile(path, actions.copy(), {})
129
130         if not found:
131                 msg = 'Config file(s) not found: %s' % pattern
132                 if require:
133                         raise Error(msg)
134                 elif config.debug:
135                         print msg
136
137 def set_flag(name):
138         config.flags.add(name)
139
140 def clear_flag(name):
141         config.flags.discard(name)
142
143 def use_board(name):
144         config.board = name
145
146 def cache_dir(path):
147         config.cache_dir = path
148
149 def sb2_target(name):
150         config.sb2_target = name
151
152 def sb2_compiler(path):
153         config.sb2_compiler = path
154
155 def sb2_init_options(args):
156         config.sb2_init_options = list(args)
157
158 def get_variables():
159         return config
160
161 def rootfs_root(path, target = None):
162         rootfs.path = path
163         rootfs.target = target
164
165 def include_paths(paths):
166         rootfs.include_paths.extend(paths)
167
168 def include_files(files):
169         rootfs.include_files.extend(files)
170
171 def exclude_paths(paths):
172         rootfs.exclude_paths.extend(paths)
173
174 def exclude_files(files):
175         rootfs.exclude_files.extend(files)
176
177 def exclude_expressions(expressions):
178         rootfs.exclude_expressions.extend(expressions)
179
180 def created_paths(paths):
181         rootfs.created_paths.extend(paths)
182
183 def file_modes(modes):
184         rootfs.file_modes.update(modes)
185
186 def directory_owners(owners):
187         rootfs.directory_owners.update(owners)
188
189 def device_nodes(devices):
190         rootfs.devices.update(devices)
191
192 def remove_account(uid=None, gid=None):
193         rootfs.remove_uid = uid
194         rootfs.remove_gid = gid
195
196 class RootFS(object):
197         def __init__(self):
198                 self.path = None
199                 self.target = None
200                 self.include_paths = []
201                 self.include_files = []
202                 self.exclude_paths = []
203                 self.exclude_files = []
204                 self.exclude_expressions = []
205                 self.created_paths = []
206                 self.devices = {}
207                 self.file_modes = {}
208                 self.directory_owners = {}
209                 self.remove_uid = None
210                 self.remove_gid = None
211
212 class Config(object):
213         def __init__(self):
214                 self.initial_config_dirs = []
215                 self.config_dirs = []
216
217                 self.top_dir = getenv('MATRIX_TOPDIR', '')
218                 __cache_dir = join(self.top_dir, 'build-cache')
219                 self.cache_dir = getenv('MATRIX_CACHEDIR', __cache_dir)
220                 self.script_dir = None
221
222                 self.boards = {}
223                 self.board = None
224                 self.components = {}
225                 self.roots = []
226                 self.flags = set()
227                 self.verbose = False
228                 self.debug = False
229                 self.protocol = False
230                 self.force = False
231                 self.keep_going = False
232                 self.pull_config_dirs = False
233                 self.jobs = 1
234                 self.make_jobs = 1
235
236                 self.sb2_target = None
237                 self.sb2_compiler = None
238                 self.sb2_init_options = []
239
240 actions = dict(
241         Board = Board,
242         CacheDir = cache_dir,
243         ClearFlag = clear_flag,
244         Component = component,
245         ConfigDir = config_dir,
246         GetVariables = get_variables,
247         Parse = parse,
248         ProvideComponent = provide_component,
249         PullConfigDirs = pull_config_dirs,
250         Root = root,
251         SB2Compiler = sb2_compiler,
252         SB2InitOptions = sb2_init_options,
253         SB2Target = sb2_target,
254         SetFlag = set_flag,
255         UseBoard = use_board,
256         RootFSRootPath = rootfs_root,
257         RootFSPaths = include_paths,
258         RootFSFiles = include_files,
259         RootFSExcludePaths = exclude_paths,
260         RootFSExcludeFiles = exclude_files,
261         RootFSExcludeExpressions = exclude_expressions,
262         RootFSCreatePaths = created_paths,
263         RootFSFileModes = file_modes,
264         RootFSDirectoryOwners = directory_owners,
265         RootFSDeviceNodes = device_nodes,
266         RootFSRemoveAccount = remove_account,
267 )
268
269 config = Config()
270 rootfs = RootFS()
271