config: move config variables behind GetVariables()
[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 def root(url, branch='master'):
47         config.roots.append((url, branch))
48
49 class Board(object):
50         def __init__(self, name, arch, gcc_march,
51                      gcc_mcpu=None,
52                      gcc_mfpu=None,
53                      gcc_options=None,
54                      gnu_host=None,
55                      flash_erase_size=65536,
56                      flash_pad_size=0,
57                      flash_compression=None,
58                      ubifs_leb_size=0,
59                      ubifs_max_leb_count=0,
60                      ubifs_min_io_size=0,
61                      ubinize_config_vol_size=0):
62
63                 self.name = name
64                 self.arch = arch
65                 self.gcc_march = gcc_march
66                 self.gcc_mcpu = gcc_mcpu or ''
67                 self.gcc_mfpu = gcc_mfpu or ''
68                 self.gcc_options = _make_list(gcc_options)
69                 self.gnu_host = gnu_host or ''
70                 self.flash_erase_size = flash_erase_size
71                 self.flash_pad_size = flash_pad_size
72                 self.flash_compression = flash_compression or ''
73                 self.ubifs_leb_size = ubifs_leb_size
74                 self.ubifs_max_leb_count = ubifs_max_leb_count
75                 self.ubifs_min_io_size = ubifs_min_io_size
76                 self.ubinize_config_vol_size = ubinize_config_vol_size
77
78                 config.boards[name] = self
79
80 def component(*args, **kwargs):
81         import components
82
83         c = components.Component(*args, **kwargs)
84         config.components[c.name] = c
85         return c
86
87 def provide_component(name):
88         c = config.components.get(name)
89         if not c:
90                 c = component(name)
91         c.cached = True
92         return c
93
94 def parse(pattern, require=True):
95         if isabs(pattern):
96                 dirs = [None]
97         else:
98                 config_dirs = config.config_dirs + config.initial_config_dirs
99                 dirs = [join(config.top_dir, dir) for dir in config_dirs]
100                 dirs.append(config.top_dir)
101
102         found = False
103
104         for dir in dirs:
105                 for path in glob(join(dir, pattern)):
106                         if exclude.match(basename(path)):
107                                 continue
108
109                         found = True
110
111                         if config.debug:
112                                 print 'Parsing', path
113
114                         execfile(path, actions.copy(), {})
115
116         if not found:
117                 msg = 'Config file(s) not found: %s' % pattern
118                 if require:
119                         raise Error(msg)
120                 elif config.debug:
121                         print msg
122
123 def get_variables():
124         return config
125
126 class Config(object):
127         def __init__(self):
128                 self.initial_config_dirs = []
129                 self.config_dirs = []
130
131                 self.top_dir = getenv('MATRIX_TOPDIR', '')
132                 __cache_dir = join(self.top_dir, 'build-cache')
133                 self.cache_dir = getenv('MATRIX_CACHEDIR', __cache_dir)
134                 self.script_dir = None
135
136                 self.boards = {}
137                 self.board = None
138                 self.components = {}
139                 self.roots = []
140                 self.flags = set()
141                 self.verbose = False
142                 self.debug = False
143                 self.force = False
144                 self.keep_going = False
145                 self.jobs = 1
146                 self.make_jobs = 1
147
148 actions = dict(
149         Board = Board,
150         Component = component,
151         ConfigDir = config_dir,
152         GetVariables = get_variables,
153         Parse = parse,
154         ProvideComponent = provide_component,
155         Root = root,
156 )
157
158 config = Config()