summaryrefslogtreecommitdiff
path: root/py/waf/tools.py
blob: f08f3bcbc13b08ac2f45c715ba220a524d120ebb (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
from waflib.Logs import pprint
from os.path import exists, getmtime


def fatal(str):
	pprint('RED', str)
	exit(1)

def generate_rtems_config(ctx, file_in, file_out, devel=False):
	from os import fchmod
	from pprint import PrettyPrinter
	srcnode = ctx.srcnode.abspath()
	pp = PrettyPrinter(depth=4)
	bsps = {}


	for bsp in ctx.env.BSP:
		env = ctx.all_envs[bsp]
		bsps[bsp] = {
			 # XXX: This really needs to use features to get the paths.
			"cflags":	env.CFLAGS + env.CONFIG_CFLAGS + \
						[
							"-I%s/cpukit/include" % srcnode, "-I%s/cpukit/score/cpu/%s/include/" % (srcnode, env.RTEMS_ARCH),
							"-I%s/bsps/%s/%s/include/" % (srcnode, env.RTEMS_ARCH, env.BSP_SOURCE_DIR),
							"-I%s/bsps/include/" % srcnode,
							"-I%s/bsps/%s/include/" % (srcnode, env.RTEMS_ARCH)
						],
			"libs":		env.LIBS + ["-lrtemscpu -lrtemsbsp"] + env.CONFIG_LIBS,
			"ldflags":	env.LDFLAGS + env.CONFIG_LDFLAGS,
			"description": env.CONFIG_DESCRIPTION
		}

		if devel:
			path_bld = "%s/%s" % (ctx.bldnode.abspath(), bsp)

			include = []
			include.append("-I%s/include" % srcnode)
			include.append("-I%s/include" % path_bld)
			include.append("-I%s/include/rtems" % path_bld)
			bsps[bsp]["cflags"] = include + bsps[bsp]["cflags"]
#			bsps[bsp]["libs"] = ["%s/c/start.o" % path_bld] + bsps[bsp]["libs"]

			ldflags = []
			ldflags.append("-specs %s/gcc_spec" % path_bld)
			ldflags.append("-L%s/cpukit/" % path_bld)
			ldflags.append("-L%s/c/" % path_bld)
			ldflags.append("-L%s/bsps/arm/beagle/" % path_bld)
#			ldflags.append("-Wl,-T %s/c/linkcmds" % path_bld)
#			bsps[bsp]["ldflags"] = ldflags + bsps[bsp]["libs"]
			bsps[bsp]["ldflags"] += ldflags + ["-Wl,-start-group"] + bsps[bsp]["libs"] + ["-lc"] + ["-lgcc"] + ["-Wl,-end-group"]

		else:
			raise Exception("Doesn't work in install mode yet.")

	#XXX: file_in and file_out can be automatically calculated they don't need to be parms.
	with open(file_in, "r") as fp:
		config = fp.read()

	with open(file_out, "w") as fp:
		fp.write('#!%s\n'					% ctx.env.BIN_PYTHON[0]) # XXX: How does this work on Windows?
		fp.write('RTEMS_VERSION = "%s"\n'	% ctx.env.RTEMS_VERSION)
		fp.write('PREFIX="%s"\n'			% ctx.env.PREFIX)
		fp.write('BSP_LIST = %s\n'			% pp.pformat(bsps))
		fp.write(config)
		fchmod(fp.fileno(), 0o755)


# XXX: rewrite this.
def generate_gcc_spec_file(ctx, devel=False):

	path_bld = "%s/%s" % (ctx.bldnode.abspath(), ctx.variant)
	path_bld_linkcmds = "%s/bsps/" % path_bld #XXX temp for new build.
	path_bld_shared = "%s/bsps/%s/shared/" % (path_bld, ctx.env.RTEMS_ARCH) #XXX temp for new build.
	path_bld_bsp = "%s/bsps/%s/%s/" % (path_bld, ctx.env.RTEMS_ARCH, ctx.env.RTEMS_BSP) #XXX temp for new build.
	data = []

# /mnt/devel/rtems/commit/build/sparc/erc32/bsps/sparc/shared//linkcmds
# /mnt/devel/rtems/commit/build/sparc/erc32/bsps/linkcmds

	def expand_flags(ctx, obj_list):
#		path_bld = "%s/%s" % (ctx.bldnode.abspath(), ctx.variant)
		l = []
		for obj in obj_list:
			obj = obj.replace("${RTEMS_LINKCMDS}", "%s" % path_bld_linkcmds)
			obj = obj.replace("${RTEMS}", "%s" % path_bld_shared)
			obj = obj.replace("${RTEMS_BSP}", "%s" % path_bld_bsp)

			if obj.endswith('.o'):
				fmt = '%s%%s'
			else:
				fmt = '%s'
			l.append(fmt % obj)
		return " ".join(l)

	data.append("*startfile:")
	data.append(expand_flags(ctx, ctx.env.LINK_START))
	data.append("")
	data.append("*endfile:")
	data.append(expand_flags(ctx, ctx.env.LINK_END))
	data.append("")
	data.append("*link:")
	data.append(expand_flags(ctx, ctx.env.LINK_LINK))

	with open("%s/gcc_spec" % path_bld, "w") as fp:
		for line in data:
			fp.write(line)
			fp.write("\n")

	ctx.env.append_value('cfg_files', "%s/gcc_spec" % path_bld)

	return "%s/%s/gcc_spec" % (ctx.bldnode, ctx.variant)



# Get all the BSPs for a specific arch
def rtems_bsp_arch_all(arch):
	from py.config.bsp import map_bsp
	list_bsp = map_bsp() #XXX: temp

	if arch not in list_bsp:
		fatal("Incorrect arch for --bsp, must be in the form of arch or arch/name: \"%s\"" % arch)
	bsp_list = []
	for bsp in list_bsp[arch]:
		bsp_list += ['%s/%s' % (arch, bsp)]
	return bsp_list


# Get all the BSPs
def rtems_bsp_all():
	from py.config.bsp import map_bsp
	list_bsp = map_bsp() #XXX: temp

	bsp_list = []
	for arch in list_bsp:
		for bsp in list_bsp[arch]:
			bsp_list += ['%s/%s' % (arch, bsp)]
	return bsp_list


def rtems_bsp_wildcard(pattern):
	if '.' in pattern:
		pattern = pattern.replace('.', '\.')
	if '*' in pattern:
		pattern = pattern.replace('*', '.*')
	return '^' + pattern + '$'


def rtems_bsp_list(bsps):
	import re
	from py.config.bsp import map_bsp
	list_bsp = map_bsp() #XXX: temp

	bsp_list = [x.strip() for x in bsps.split(',')]

	verified_bsp_list = []

	for bsp in bsp_list:
		if '/' not in bsp:
			fatal("Incorrect value for --bsp must be in the form of arch/name: \"%s\"" % bsp)
		(arch, bsp) = bsp.split('/')
		pa = re.compile(rtems_bsp_wildcard(arch), re.IGNORECASE)
		pb = re.compile(rtems_bsp_wildcard(bsp), re.IGNORECASE)

		for arch in list_bsp:
			if pa.match(arch) is not None:
				for bsp in list_bsp[arch]:
					if pb.match(bsp) is not None:
						arch_bsp = '%s/%s' % (arch, bsp)
						verified_bsp_list += [arch_bsp]

	return sorted(verified_bsp_list)


def rtems_cmd_config(ctx):
	from py.config import BuildConfig, RTEMSConfig
	from py.config.bsp import get_option_class, get_config_class

	rc = RTEMSConfig(get_option_class(), get_config_class())

	if ctx.options.list is True:
		from py.config.bsp import map_bsp
		list_bsp = map_bsp() #XXX: temp

		from py.config import BuildConfig
#		cfg = BuildConfig(rc)

		for arch in sorted(list_bsp):
			print("")
			print(arch)

			for bsp in sorted(list_bsp[arch]):
#				descr = cfg.bsp_get_detail(arch, bsp)
				descr = "" #XXX: There is a bug here fix needs to be in config/base/BuildConfig::_parse_bsp
				print("  %-22s %s" % ("%s/%s" % (arch, bsp), descr))
		return




	if ctx.options.force is False and exists("config.cfg"):
		ctx.fatal("Please delete config.cfg before creating a new one.")

	if not ctx.options.bsps:
		ctx.fatal("You must specify a single or comma separated list of BSPs using --bsp")

	bsp_list = rtems_bsp_list(ctx.options.bsps)
	if not bsp_list:
		ctx.fatal("You must specify a single or comma separated list of BSPs using --bsp")


	cfg = BuildConfig(rc, bsp_list)
	cfg.option_set("general", "PATH_TOOLS", ctx.options.path_tools or "")
	cfg.option_set("general", "PREFIX", ctx.options.prefix or "")
	cfg.save()

	pprint("YELLOW", "Wrote config.cfg")
	archs = {}
	for bsp in bsp_list:
		pprint("YELLOW", "  - %s" % bsp)
		arch = bsp.split('/')[0]
		if arch not in archs:
			archs[arch] = 0
		archs[arch] += 1

	pprint("YELLOW", "Configured BSPS:")
	pprint("YELLOW", " Total    : %d" % len(bsp_list))
	arch_list = sorted(archs.keys())
	for arch in arch_list:
		pprint("YELLOW", "  %-8s: %d" % (arch, archs[arch]))


def rtems_cmd_bsp(ctx):
	ctx.fatal("Not implemented.")
	print("List of available BSPs")
	print("List of DISABLED BSPs")


# Get file mtime.
def get_file_mtime(file):
	return getmtime(file)


from subprocess import Popen, PIPE

def run(cmd):
	p =  Popen(cmd, stdout=PIPE, stderr=PIPE)
	stdout, stderr = p.communicate()
	return stdout[:-1], stderr[:-1], p.returncode


def get_options(bsp):
	pass

def get_config(bsp):
	pass