summaryrefslogtreecommitdiff
path: root/rtems_waf/config/base.py
blob: e789a1e022c8e6520efe1e92599ae98049c01247 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
try:
	from configparser import ConfigParser, NoOptionError
except ImportError:
	from ConfigParser import ConfigParser, NoOptionError

from os.path import exists
#from __init__ import options_map, Default, features_list, config_list
from rtems_waf.compat import add_metaclass #2to3
from sys import version_info

Default = None			# Default value.
Disable = "-DISABLED-"	# Disable this option
options_map = {}		# Global options map.
features_list = []		# Global features list.
config_list = []		# Global config list.


def fatal(str):
	print("FATAL: %s" % str)
	exit(1)


class Value(object):
	"""Holds an option value internally.  This acts in a similar fashion to a dictionary."""

	def __init__(self):
		self.__dict__["options"] = {}

	def __setattr__(self, key, value):
		"""Set a value, this either fetches the option or sets it if it already exists."""
		self._set_or_get(key, value)

	def __getitem__(self, key):
		"""Get an option from the internal table."""
		return self.options[key]

	def __getattr__(self, key):
		"""Allow an option to be fetched as an attribute."""
		return self.options[key]

	def _set_or_get(self, option, value):
		"""
			Set or get an option.

			:param option: Option name (string)
			:param value: Value to set option
		"""

		# Disabling an option removes it from the header completely irrespective of its type.
		if type(value) == str and value == "-DISABLED-":
			del self.options[option]
			return

		if option not in self.options:
			if option not in options_map:
				fatal("Missing default option: %s" % option)
			opt = options_map[option]	# Get option class
			i = opt(value)				# Create option with value
			self.options[i.name] = i	# Set in dictionary
		else:
			i = self.options[option]
			i.set(value)				# Set value

	def __str__(self):
		"""String representation of a value object."""
		return "Value object at %s: %s" % (hex(id(self)), self.value)

	def __iter__(self):
		"""Allow iteration."""
		return iter(self.options)



# Self register configs.
class ConfigMeta(type):
	"""Automatically register configs classes."""
	def __new__(cls, name, bases, dct):
		new = type.__new__(cls, name, bases, dct)
		if hasattr(new, "is_feature") and new.is_feature is True: # XXX: Find a better way to differentiate.
			features_list.append(new)
		elif hasattr(new, "name"):
			config_list.append(new)
		return new

@add_metaclass(ConfigMeta)
class Config(object):
	feature = () 							#: Feature list (XXX: Why is this required?)
	feature_default = ("gcc", "debug")		#: Default features.

	def __init__(self):
		self.base = False					#: Whether this is a base config or not.
		self.option_header = Value()		#: Internal header options
		self.option_build = Value()			#: Internal build options

		# Iterate over base classes in reverse order so the 'latest'
		# defined option is taken.
		for i in type(self).__mro__[::-1]:
			if hasattr(i, "header"):
				i.header(self, self.option_header)
			if hasattr(i, "build"):
				i.build(self, self.option_build)

		# Make sure features don't conflict with each other.
		processed = []
		feature_obj = [x for x in features_list if x.name in self.feature]
		for feature in feature_obj:
			conflicts = set(processed) & set(feature.conflicts)
			processed.append(feature.name)
			if conflicts:
				raise Exception("Feature %s conflicts with %s" % (feature.name, ", ".join(conflicts)))
			feature(self)

		self.feature += self.feature_default	# Features in this config


	def header(self, c):
		"""
			Header config options.

			:param c: `self.option_header`
		"""
		pass


	def build(self, c):
		"""
			Build config options.

			:param c: `self.build_header`
		"""
		pass

	# XXX: needs to merge both dictionaries and sort them.
	def config_get(self):
		"""Get the config.cfg (.ini) format for this config."""
		str = "[%s]\n" % self.name
		def add(d, str):
			for o in sorted(d):
				opt = d[o]
				str += "%s" % opt.config_get()
				str += "\n\n"
			return str

		str = add(self.option_header, str)
		str = add(self.option_build, str)
		return str




class Feature(Config):
	"""Build feature base class"""
	name = None			#: Name of feature
	description = None	#: Description
	exclude = None		#: BSPs to exclude
	include = None		#: BSPs to include
	conflicts = None	#: Other features this one conflicts with
	is_feature = True	#: Whether this is a feature or not

	def __init__(self, parent):
		self.build(parent.option_build)
		self.header(parent.option_header)

	def __str__(self):
		return "%s (%s)" % (self, self.name)



class cfg_general(Config):
	"""[general] block for `config.cfg.`"""
	name = "general"
	def build(self, c):
		c.BSP					= Default
		c.PREFIX				= Default
		c.PATH_TOOLS			= Default
		c.ENABLE_DEBUG			= Default
		c.CFLAGS				= Default
		c.LIBS					= Default
		c.LDFLAGS				= Default


class cfg_host(Config):
	"""[host] block for `config.cfg.`"""
	name = "host"
	def build(self, c):
		c.CFLAGS				= Default
		c.LIBS					= Default
		c.LDFLAGS				= Default


class cfg_bsp(Config):
	"""[bsp] block for `config.cfg` to hold BSP-specific settings"""
	name = "bsp"
	def build(self, c):
		c.ENABLE_DEBUG			= Default
		c.ENABLE_MP				= Default
		c.ENABLE_MULTILIB		= Default
		c.ENABLE_NETWORKING		= Default
		c.ENABLE_NEWLIB			= Default
		c.ENABLE_POSIX			= Default
		c.ENABLE_PTHREADS		= Default
		c.ENABLE_SERDBG			= Default
		c.ENABLE_SHELL			= Default
		c.ENABLE_SMP			= Default
		c.LINK_START			= Default
		c.LINK_END				= Default
		c.LINK_LINK				= Default





class BuildConfig(object):
	"""
		This class handles creating and loading `config.cfg`.
	"""
	file_config = "config.cfg"	#: Default config file name.

	def __init__(self, list_bsp=[]):
		self.cfg_default = [cfg_general(), cfg_host(), cfg_bsp()]	#: Default BSP configuration.
		self.cfg = list(self.cfg_default)							#: BSP config.
		self.list_bsp = []

		if list_bsp:
			self.list_bsp = sorted(list_bsp)
		elif not exists(self.file_config):
			fatal("Missing config.cfg")
		else:
			# Load on-disk config.
			self.cfg_user = ConfigParser()
			self.cfg_user.read(self.file_config)

			# Set BSP list.
			# XXX: Far too complicated due to chicken-and-egg issue.
			#      This will be refactored in the future.
			tmp = cfg_general()
			opt = tmp.option_build["BSP"]
			o = self.cfg_user.get("general", "BSP")
			if version_info < (3,) and type(o) is unicode: #2to3
				o = str(o)
			opt.set(o)
			self.list_bsp = opt.value

		# Parse BSPs
		self._parse_bsp(self.list_bsp)

		# Load user configuration
		if not list_bsp:
			self._cfg_user_load()

		# Make sure BSP= is always set.
		self.option_set("general", "BSP", " " .join(self.list_bsp))


	def _parse_bsp(self, list_bsp):
		"""
			Parse BSP config.

			:param: list_bsp: List of BSPs in this config.
		"""

		# Make this simplier
		bsp_map = {}
		for b in self.list_bsp:
			bsp = [x for x in config_list if x.name == b][0]
			bsp_arch = [x for x in config_list if x.name == bsp.arch][0]
			bsp_map.setdefault((bsp_arch.name, bsp_arch), []).append(bsp)

		# Save for usage in config_set
		self.bsp_map = bsp_map

		for bsp_name, bsp_arch in sorted(bsp_map):
			self.cfg.append(bsp_arch())
			for bsp in bsp_map[(bsp_name, bsp_arch)]:
				self.cfg.append(bsp())


	def save(self):
		"""Save config to disk."""
		with open(self.file_config, "w") as fp:
			fp.write(self._cfg_get())
			fp.write("\n\n") # Handy.


	def config_set(self, ctx, cfg_name, arch_name=None):
		"""
			Apply config internally to waf.

		:param ctx: waf Context.
		:param cfg_name: BSP config name (arch/bsp format).
		:param arch_name: Architecture name.
		"""
		cfg = None
		if arch_name:
#			self.config_set(ctx, arch_name)
			cfg_name = "%s/%s" % (arch_name, cfg_name)

		for c in self.cfg:
			if c.name == cfg_name:
				cfg = c
				break

		if not cfg:
			fatal("BuildConfig:config_set(): Invalid config: %s" % cfg_name)

		for option in cfg.option_build:
			opt = cfg.option_build[option]
#			self._set_cfg_user(cfg_name, opt)
			opt.set_config_build(ctx)

		for option in cfg.option_header:
			opt = cfg.option_header[option]
#			self._set_cfg_user(cfg_name, opt)
			opt.set_config_header(ctx)


	def bsp_get_detail(self, arch, bsp):
		cfg = None

		for c in config_list:
			if c.name == "%s/%s" % (arch, bsp):
				cfg = c
				break

		if cfg is None:
			return "MISSING" # XXX: Throw an exception if this is missing?

		return "."


	def option_set(self, cfg, option, value):
		"""
			Set an option within a config

			:param cfg: Config to set.
			:param option: Option name.
			:param value: Value to set.
		"""
		for config in self.cfg_default:
			if config.name == cfg:
				# Only allow build options to be set for now.
				for o in config.option_build:
					opt = config.option_build[o]
					if opt.name == option:
						opt.set(value)

	def _cfg_get(self):
		"""Get config text."""
		cfg = ""
		for bsp in self.cfg:
			cfg += "\n\n"
			cfg += bsp.config_get()
		return cfg.strip()


	#XXX: unused
	def _cfg_add(self, str):
		self.cfg += "\n"
		self.cfg += str


	def _cfg_user_load(self):
		"""Load user config from disk."""
		for cfg_bsp in self.cfg:
			section = cfg_bsp.name

			if not self.cfg_user.has_section(section):
				fatal("Missing section: [%s]" % section)

			for option in cfg_bsp.option_build:
				opt = cfg_bsp.option_build[option]

				o = self.cfg_user.get(section, opt.name)

				# configpaser does not convert values anymore.
				if o in ["True", "False"]:
					o = self.cfg_user.getboolean(section, opt.name)

				# We do not support unicode internally
				if version_info < (3,) and type(o) is unicode: #2to3
					o = str(o)

				self._set_cfg_user(section, opt)

#				opt.set(o)

			for option in cfg_bsp.option_header:
				opt = cfg_bsp.option_header[option]
				self._set_cfg_user(section, opt)
#				o = self.cfg_user.get(section, opt.name)
#				opt.set(o)


	def _set_cfg_user(self, section, opt):
		if not self.cfg_user.has_section(section):
			fatal("Missing section: [%s]" % section)

		o = self.cfg_user.get(section, opt.name)

		# configpaser does not convert values anymore.
		if o in ["True", "False"]:
			o = self.cfg_user.getboolean(section, opt.name)

		# We do not support unicode internally
		if version_info < (3,) and type(o) is unicode: #2to3
			o = str(o)

		opt.set(o)






# This needs to be here to avoid recursive deps, it's more convenient to
# have the features in a seperate file.
#import feature
#import rtems_waf.defaults.bsp