summaryrefslogtreecommitdiff
path: root/rtems_waf/waf.py
blob: 9f8a1112dbe41dce03bc435a97e5c2ba69727d68 (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
from waflib.Task import Task
from waflib.TaskGen import feature, before, after, extension, after_method
from waflib.Configure import conf
from waflib.Logs import pprint
#from waflib.Build import BuildContext, CleanContext, InstallContext, UninstallContext
#from waflib import Build, Scripting
#from waflib.Tools import c_preproc
#from rtems_waf import gccdeps
#from waflib import Logs
#import ConfigParser


#################
# Handle .S Files
#################
class casm(Task):
#	run_str = '${CC} ${ARCH_ST:ARCH} ${CFLAGS} ${CPPFLAGS} ${CPPPATH_ST:INCPATHS} ${DEFINES_ST:DEFINES} ${CC_SRC_F}${SRC} ${CC_TGT_F}${TGT}'
	run_str = '${CC} -DASM ${ARCH_ST:ARCH} ${CFLAGS} ${CPPFLAGS} ${FRAMEWORKPATH_ST:FRAMEWORKPATH} ${CPPPATH_ST:INCPATHS} ${DEFINES_ST:DEFINES} ${CC_SRC_F}${SRC} ${CC_TGT_F}${TGT}'
	ext_in  = ['.h']
	ext_out  = ['.o']
	color = 'BLUE'

@extension('.S')
def asm_hook(self, node):
	return self.create_compiled_task('casm', node)


##########
# Features
##########
@feature('bld_include')
@after_method('apply_incpaths')
def insert_blddir(self):
	self.env.prepend_value('INCPATHS', ['include'])

@feature('src_include')
@after_method('apply_incpaths', 'insert_blddir')
def insert_srcdir(self):
	path = self.bld.srcnode.abspath()
	self.env.append_value('INCPATHS', "%s/include" % path)

	if self.env.ENABLE_SMP:
		self.env.append_value('INCPATHS', "%s/cpukit/score/include/" % path)
		self.env.append_value('INCPATHS', "%s/cpukit/rtems/include/" % path)

@feature('src_include_rtems')
@after_method('apply_incpaths', 'insert_blddir')
def insert_srcdir_rtems(self):
	self.env.append_value('INCPATHS', "%s/include/rtems" % self.bld.srcnode.abspath())

@feature('src_include_networking')
@after_method('apply_incpaths', 'insert_blddir')
def insert_srcdir_networking(self):
	self.env.append_value('INCPATHS', "%s/cpukit/libnetworking" % self.bld.srcnode.abspath())

@feature('src_include_bsp')
@after_method('apply_incpaths', 'insert_blddir')
def insert_srcdir_bsp(self):
	self.env.append_value('INCPATHS', "%s/include/bsp" % self.bld.srcnode.abspath())

@feature('src_include_libcpu')
@after_method('apply_incpaths', 'insert_blddir')
def insert_srcdir_libcpu(self):
	self.env.append_value('INCPATHS', "%s/include/libcpu" % self.bld.srcnode.abspath())

@feature('src_include_libchip')
@after_method('apply_incpaths', 'insert_blddir')
def insert_srcdir_libchip(self):
	self.env.append_value('INCPATHS', "%s/include/libchip" % self.bld.srcnode.abspath())


###########
# Shortcuts
###########
def rtems_build(cmd, ctx, target_name, source, **kwarg):
	feature = "c bld_include"
	if "features" in kwarg:
		feature = "%s %s" % (kwarg["features"], feature)
		del kwarg["features"]

	cmd(
		source   = source,
		target   = target_name,
		features = feature,
		install_path = ctx.env.LIBDIR,
		**kwarg)

# There's probably a better way to do this.
@conf
def rtems_lib(ctx, target_name, source, **kwarg):
	rtems_build(ctx.stlib, ctx, target_name, source, **kwarg)

@conf
def rtems_obj(ctx, target_name, source, **kwarg):
	rtems_build(ctx, ctx, target_name, source, **kwarg)

@conf
def rtems_program(ctx, target_name, source, **kwarg):
	rtems_build(ctx.program, ctx, target_name, source, **kwarg)


@conf
def copy(ctx, source, target, name):
		ctx(
			rule='cp ${SRC} ${TGT}', # XXX: Make something that works on windows.
			source=source,
			target=target,
			name=name
		)

@conf
def copy_or_subst(ctx, source, target, name):
	if source.endswith(".in"):
		# This is required as not all 'linkcmd' files are named as such see the
		# bottom of c/wscript  It can be removed when the names are normalised
		# XXX: fix 'linkcmd' names.

		if target.endswith(".in"):
			target = target[:-3]

		ctx(
			features	= 'subst',
			source		= source,
			target		=  target,
			encoding	= 'ascii', # for python3.
			name		= name,
#			is_copy		= True
		)
	else:
		ctx.copy(source, target, name)


#################
# Configure Steps
#################
@conf
def check_func(ctx, func, mandatory=False):
	ctx.check_cc(
		mandatory   = mandatory,
		fragment    = "char %s();\n int main() { return %s(); return 0; }" % (func, func),
		define_name = "HAVE_%s" % func.upper(),
		execute     = False,
		msg         = "Checking for C library function %s" % func
	)


@conf
def check_size(ctx, field, mandatory=False, define_name=None):
	if define_name is None:
		define_name = "SIZEOF_%s" % field.upper()

	ctx.check_cc(
		mandatory   = mandatory,
		fragment    = """
			#include <sys/types.h>
			#include <stdio.h>
			main() {
			  printf("%%d", sizeof(%s));
			  return 0;
			}
			""" % field,
		execute     = True,
		define_ret  = True,
		define_name = define_name,
		quote       = False,
		msg         = "Checking size of %s" % field
	)


# XXX: It prints "yes" even if it doesn't exist.
@conf
def check_define(ctx, define, header, mandatory=False):
	ctx.check(
		mandatory   = mandatory,
		fragment    = '''#include <%s>\n int main () {\n #ifdef %s\n return 0;\n #endif\n return 1; }\n''' % (header, define),
		define_name = "HAVE_%s" % define.upper(),
		features    = "c cprogram",
		execute     = True,
		msg         = "Checking for define %s in %s" % (define, header)
	)


#################################################
# This writes objects to a file if there are > 25
# objects to avoid commandline arg limits for ar.
#################################################
def rtems_stlib_command(self, *k, **kw):
	# Following block borrowed from waflib/Tools/msvc.py
	bld = self.generator.bld

	try:
		if not kw.get('cwd', None):
			kw['cwd'] = bld.cwd
	except AttributeError:
		bld.cwd = kw['cwd'] = bld.variant_dir

	# Put the objects on the commandline if there aren't enough to
	# warrant writing to a file.
	if len(self.inputs) < 25:
		return self.generator.bld.exec_command(*k, **kw)

	file_obj = "%s_files" % self.outputs[0].abspath()
	with open(file_obj, "w") as fp:
		for f in self.inputs:
			fp.write("%s\n" % f.bldpath())

	pprint("YELLOW", "Wrote %d objects to %s" % (len(self.inputs), file_obj))
	cmd = self.env.AR + ["rc", self.outputs[0].bldpath(), "@%s_files" % self.outputs[0].bldpath()]

	# Task information for JSON build output.
	if self.env.BUILD_JSON:
		kw["json_task_self"] = self

	return self.generator.bld.exec_command(cmd, **kw)



# Tests
@feature('test_include')
@after_method('apply_incpaths')
def insert_test_include(self):
	self.env.prepend_value('INCPATHS', "%s/testsuites/support/include" % self.bld.srcnode.abspath())


from waflib.Tools.c import cprogram
from waflib.Tools.ccroot import USELIB_VARS

USELIB_VARS['test_cprogram'] = set(['STLIB', 'STLIBPATH', 'LDFLAGS'])

#from StringIO import StringIO
from os import fdopen, pipe, read, close
class test_cprogram(cprogram):
	run_str = '${LINK_CC} ${LDFLAGS} ${CFLAGS} ${CCLNK_SRC_F}${SRC} ${CCLNK_TGT_F}${TGT[0].abspath()} -specs gcc_spec -Wl,-Bstatic -Lc -Lcpukit -Wl,-start-group -lc -lgcc ${STLIBPATH_ST:STLIBPATH} ${STLIB_ST:STLIB}  -Wl,-end-group'

	def exec_command(self, cmd, **kw):
		r, w = pipe()
		rfd = fdopen(r, "rb", 0)
		kw["stderr"] = fdopen(w, "wb", 0)
		ret = cprogram.exec_command(self, cmd, **kw)
		kw["stderr"].close()

		if ret == 1:
			data = rfd.readlines()
			if " ".join(data).find("will not fit in region") != -1:
				file = self.outputs[0].abspath()
				with open(file, "w") as fp:
					fp.write("Target does not meet test memory constraints.\n")
				pprint("RED", "Target \"%s\" does not meet test memory constraints." % file)
				rfd.close()
				return 0
			print("".join(data))

		rfd.close()
		return ret



@conf
def rtems_test(ctx, target_name, source_name, **kwarg):
	features_merged = "c test_cprogram bld_include src_include"
	if "features" in kwarg:
		features_merged = "%s %s" % (kwarg["features"], features_merged)
		del kwarg["features"]

	use_merged = "rtemsbsp rtemscpu"
	if "use" in kwarg:
		use_merged = "%s %s" % (kwarg["use"], use_merged)
		del kwarg["use"]

	ctx(
		source			= source_name,
		target			= "test_%s" % target_name,
		features		= features_merged,
		use				= use_merged,
		install_path	= ctx.env.TESTDIR,
		**kwarg
	)


@conf
def rtems_doc(ctx, section):
	pprint("YELLOW", "See http://docs.rtems.org/%s/user/#%s (Not activated yet!)" % (ctx.env.RTEMS_VERSION, section))


@conf
def rtems_fatal(ctx, message, section):
	pprint("RED", message)
	ctx.rtems_doc(section)
	ctx.fatal("Fatal error")


@conf
def rtems_warn(ctx, message, section):
	pprint("YELLOW", message)
	ctx.rtems_doc(section)