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