config: unify 'confdirs' and 'roots' semantics
[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, require=True):
69         if isabs(pattern):
70                 dirs = [None]
71         else:
72                 all_confdirs = config.confdirs + config.initial_confdirs
73                 dirs = [join(config.top_dir, dir) for dir in all_confdirs]
74                 dirs.append(config.top_dir)
75
76         found = False
77
78         for dir in dirs:
79                 for path in glob(join(dir, pattern)):
80                         if exclude.match(basename(path)):
81                                 continue
82
83                         found = True
84
85                         if config.debug:
86                                 print 'Parsing', path
87
88                         execfile(path, actions, config.__dict__)
89
90         if not found:
91                 msg = 'Config file(s) not found: %s' % pattern
92                 if require:
93                         raise Error(msg)
94                 elif config.debug:
95                         print msg
96
97 class Config(object):
98         def __init__(self):
99                 self.initial_confdirs = []
100                 self.confdirs = []
101
102                 self.top_dir = getenv('MATRIX_TOPDIR', '')
103                 __cache_dir = join(self.top_dir, 'build-cache')
104                 self.cache_dir = getenv('MATRIX_CACHEDIR', __cache_dir)
105                 self.script_dir = None
106
107                 self.boards = {}
108                 self.board = None
109                 self.components = {}
110                 self.roots = []
111                 self.flags = []
112                 self.libc = 'glibc'
113                 self.verbose = False
114                 self.debug = False
115                 self.force = False
116                 self.keep_going = False
117                 self.jobs = 1
118                 self.make_jobs = 1
119
120 actions = dict(
121         Board = Board,
122         Component = component,
123         ProvideComponent = provide_component,
124         Parse = parse,
125 )
126
127 config = Config()