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