config: move top_dir as the last confdir
[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 PlatformProvidedComponent(*args, **kwargs):
62         import components
63
64         c = components.PlatformProvidedComponent(*args, **kwargs)
65         config.components[c.name] = c
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         paths = {}
76         for dir in dirs:
77                 for path in glob(join(dir, pattern)):
78                         name = basename(path)
79                         if not exclude.match(name):
80                                 paths[name] = path
81
82         names = paths.keys()
83         names.sort()
84         for name in names:
85                 path = paths[name]
86
87                 if config.debug:
88                         print 'Parsing', path
89
90                 execfile(path, actions, config.__dict__)
91
92         if not paths:
93                 msg = 'Config file(s) not found: %s' % pattern
94                 if required:
95                         raise Error(msg)
96                 elif config.debug:
97                         print msg
98
99 class Config(object):
100         def __init__(self):
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 = []
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         PlatformProvidedComponent = PlatformProvidedComponent,
125         parse = parse,
126 )
127
128 config = Config()