configurable config file lookup path
[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 class Board(object):
15         def __init__(self, name, arch, gcc_march,
16                      gcc_mcpu=None,
17                      gcc_mfpu=None,
18                      gcc_options=None,
19                      gnu_host=None,
20                      flash_erase_size=65536,
21                      flash_pad_size=0):
22                 self.name = name
23                 self.arch = arch
24                 self.gcc_march = gcc_march
25                 self.gcc_mcpu = gcc_mcpu or ''
26                 self.gcc_mfpu = gcc_mfpu or ''
27                 self.gcc_options = gcc_options or ''
28                 self.gnu_host = gnu_host or ''
29                 self.flash_erase_size = flash_erase_size
30                 self.flash_pad_size = flash_pad_size
31
32                 config.boards[name] = self
33
34 def Component(*args, **kwargs):
35         import components
36
37         c = components.Component(*args, **kwargs)
38         config.components[c.name] = c
39         return c
40
41 def PlatformProvidedComponent(*args, **kwargs):
42         import components
43
44         c = components.PlatformProvidedComponent(*args, **kwargs)
45         config.components[c.name] = c
46         return c
47
48 def parse(pattern, required=False):
49         if isabs(pattern):
50                 dirs = [None]
51         else:
52                 dirs = [join(config.top_dir, dir) for dir in config.confdirs]
53                 dirs.insert(0, config.top_dir)
54
55         paths = {}
56         for dir in dirs:
57                 for path in glob(join(dir, pattern)):
58                         name = basename(path)
59                         if not exclude.match(name):
60                                 paths[name] = path
61
62         names = paths.keys()
63         names.sort()
64         for name in names:
65                 path = paths[name]
66
67                 if config.debug:
68                         print 'Parsing', path
69
70                 execfile(path, actions, config.__dict__)
71
72         if not paths:
73                 msg = 'Config file(s) not found: %s' % pattern
74                 if required:
75                         raise Error(msg)
76                 elif config.debug:
77                         print msg
78
79 class Config(object):
80         def __init__(self):
81                 self.confdirs = []
82
83                 self.top_dir = getenv('MATRIX_TOPDIR', '')
84                 __cache_dir = join(self.top_dir, 'build-cache')
85                 self.cache_dir = getenv('MATRIX_CACHEDIR', __cache_dir)
86                 self.script_dir = None
87
88                 self.boards = {}
89                 self.board = None
90                 self.components = {}
91                 self.roots = []
92                 self.flags = []
93                 self.libc = 'glibc'
94                 self.verbose = False
95                 self.debug = False
96                 self.force = False
97                 self.keep_going = False
98                 self.jobs = 1
99                 self.make_jobs = 1
100
101 actions = dict(
102         Board = Board,
103         Component = Component,
104         PlatformProvidedComponent = PlatformProvidedComponent,
105         parse = parse,
106 )
107
108 config = Config()