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