config: rename parse as Parse
[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, join
9
10 Error = RuntimeError
11
12 exclude = sre.compile(r'^[.#].*|.*[~#]$')
13
14 def _make_list(arg):
15         if arg is None:
16                 return []
17
18         if isinstance(arg, basestring):
19                 return arg.split()
20
21         return list(arg)
22
23 class Board(object):
24         def __init__(self, name, arch, gcc_march,
25                      gcc_mcpu=None,
26                      gcc_mfpu=None,
27                      gcc_options=None,
28                      gnu_host=None,
29                      flash_erase_size=65536,
30                      flash_pad_size=0,
31                      flash_compression=None,
32                      ubifs_leb_size=0,
33                      ubifs_max_leb_count=0,
34                      ubifs_min_io_size=0,
35                      ubinize_config_vol_size=0):
36
37                 self.name = name
38                 self.arch = arch
39                 self.gcc_march = gcc_march
40                 self.gcc_mcpu = gcc_mcpu or ''
41                 self.gcc_mfpu = gcc_mfpu or ''
42                 self.gcc_options = _make_list(gcc_options)
43                 self.gnu_host = gnu_host or ''
44                 self.flash_erase_size = flash_erase_size
45                 self.flash_pad_size = flash_pad_size
46                 self.flash_compression = flash_compression or ''
47                 self.ubifs_leb_size = ubifs_leb_size
48                 self.ubifs_max_leb_count = ubifs_max_leb_count
49                 self.ubifs_min_io_size = ubifs_min_io_size
50                 self.ubinize_config_vol_size = ubinize_config_vol_size
51
52                 config.boards[name] = self
53
54 def component(*args, **kwargs):
55         import components
56
57         c = components.Component(*args, **kwargs)
58         config.components[c.name] = c
59         return c
60
61 def provide_component(name):
62         c = config.components.get(name)
63         if not c:
64                 c = component(name)
65         c.cached = True
66         return c
67
68 def parse(pattern, required=False):
69         if isabs(pattern):
70                 dirs = [None]
71         else:
72                 dirs = [join(config.top_dir, dir) for dir in config.confdirs]
73                 dirs.append(config.top_dir)
74
75         found = False
76
77         for dir in dirs:
78                 for path in glob(join(dir, pattern)):
79                         if exclude.match(basename(path)):
80                                 continue
81
82                         found = True
83
84                         if config.debug:
85                                 print 'Parsing', path
86
87                         execfile(path, actions, config.__dict__)
88
89         if not found:
90                 msg = 'Config file(s) not found: %s' % pattern
91                 if required:
92                         raise Error(msg)
93                 elif config.debug:
94                         print msg
95
96 class Config(object):
97         def __init__(self):
98                 self.confdirs = []
99
100                 self.top_dir = getenv('MATRIX_TOPDIR', '')
101                 __cache_dir = join(self.top_dir, 'build-cache')
102                 self.cache_dir = getenv('MATRIX_CACHEDIR', __cache_dir)
103                 self.script_dir = None
104
105                 self.boards = {}
106                 self.board = None
107                 self.components = {}
108                 self.roots = []
109                 self.flags = []
110                 self.libc = 'glibc'
111                 self.verbose = False
112                 self.debug = False
113                 self.force = False
114                 self.keep_going = False
115                 self.jobs = 1
116                 self.make_jobs = 1
117
118 actions = dict(
119         Board = Board,
120         Component = component,
121         ProvideComponent = provide_component,
122         Parse = parse,
123 )
124
125 config = Config()