File: d.py

package info (click to toggle)
xiphos 4.1.0.1%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,572 kB
  • sloc: ansic: 25,543; cpp: 13,223; python: 12,684; xml: 2,951; sh: 144; makefile: 42
file content (364 lines) | stat: -rw-r--r-- 10,898 bytes parent folder | download | duplicates (6)
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
#! /usr/bin/env python
# encoding: utf-8
import sys
if sys.hexversion < 0x020400f0: from sets import Set as set
import os,sys,re,optparse
import ccroot
import TaskGen,Utils,Task,Configure,Logs,Build
from Logs import debug,error
from TaskGen import taskgen,feature,after,before,extension
from Configure import conftest
EXT_D=['.d','.di','.D']
D_METHS=['apply_core','apply_vnum','apply_objdeps']
def filter_comments(filename):
	txt=Utils.readf(filename)
	buf=[]
	i=0
	max=len(txt)
	while i<max:
		c=txt[i]
		if c=='"':
			i+=1
			c=''
			while i<max:
				p=c
				c=txt[i]
				i+=1
				if i==max:return buf
				if c=='"':
					cnt=0
					while i<cnt and i<max:
						if txt[i-2-cnt]=='\\':cnt+=1
						else:break
					if(cnt%2)==0:break
			i+=1
		elif c=="'":
			i+=1
			if i==max:return buf
			c=txt[i]
			if c=='\\':
				i+=1
				if i==max:return buf
				c=txt[i]
				if c=='x':
					i+=2
				elif c=='u':
					i+=4
			i+=1
			if i==max:return buf
			c=txt[i]
			if c!='\'':error("uh-oh, invalid character")
		elif c=='/':
			if i==max:break
			c=txt[i+1]
			if c=='+':
				i+=1
				nesting=1
				prev=0
				while i<max:
					c=txt[i]
					if c=='+':
						prev=1
					elif c=='/':
						if prev:
							nesting-=1
							if nesting==0:break
						else:
							if i<max:
								i+=1
								c=txt[i]
								if c=='+':
									nesting+=1
							else:
								return buf
					else:
						prev=0
					i+=1
			elif c=='*':
				i+=1
				while i<max:
					c=txt[i]
					if c=='*':
						prev=1
					elif c=='/':
						if prev:break
					else:
						prev=0
					i+=1
			elif c=='/':
				i+=1
				c=txt[i]
				while i<max and c!='\n':
					i+=1
					c=txt[i]
		else:
			buf.append(c)
		i+=1
	return buf
class d_parser(object):
	def __init__(self,env,incpaths):
		self.allnames=[]
		self.re_module=re.compile("module\s+([^;]+)")
		self.re_import=re.compile("import\s+([^;]+)")
		self.re_import_bindings=re.compile("([^:]+):(.*)")
		self.re_import_alias=re.compile("[^=]+=(.+)")
		self.env=env
		self.nodes=[]
		self.names=[]
		self.incpaths=incpaths
	def tryfind(self,filename):
		found=0
		for n in self.incpaths:
			found=n.find_resource(filename.replace('.','/')+'.d')
			if found:
				self.nodes.append(found)
				self.waiting.append(found)
				break
		if not found:
			if not filename in self.names:
				self.names.append(filename)
	def get_strings(self,code):
		self.module=''
		lst=[]
		mod_name=self.re_module.search(code)
		if mod_name:
			self.module=re.sub('\s+','',mod_name.group(1))
		import_iterator=self.re_import.finditer(code)
		if import_iterator:
			for import_match in import_iterator:
				import_match_str=re.sub('\s+','',import_match.group(1))
				bindings_match=self.re_import_bindings.match(import_match_str)
				if bindings_match:
					import_match_str=bindings_match.group(1)
				matches=import_match_str.split(',')
				for match in matches:
					alias_match=self.re_import_alias.match(match)
					if alias_match:
						match=alias_match.group(1)
					lst.append(match)
		return lst
	def start(self,node):
		self.waiting=[node]
		while self.waiting:
			nd=self.waiting.pop(0)
			self.iter(nd)
	def iter(self,node):
		path=node.abspath(self.env)
		code="".join(filter_comments(path))
		names=self.get_strings(code)
		for x in names:
			if x in self.allnames:continue
			self.allnames.append(x)
			self.tryfind(x)
def scan(self):
	env=self.env
	gruik=d_parser(env,env['INC_PATHS'])
	gruik.start(self.inputs[0])
	if Logs.verbose:
		debug('deps: nodes found for %s: %s %s'%(str(self.inputs[0]),str(gruik.nodes),str(gruik.names)))
	return(gruik.nodes,gruik.names)
def get_target_name(self):
	v=self.env
	tp='program'
	for x in self.features:
		if x in['dshlib','dstaticlib']:
			tp=x.lstrip('d')
	return v['D_%s_PATTERN'%tp]%self.target
d_params={'dflags':'','importpaths':'','libs':'','libpaths':'','generate_headers':False,}
def init_d(self):
	for x in d_params:
		setattr(self,x,getattr(self,x,d_params[x]))
class d_taskgen(TaskGen.task_gen):
	def __init__(self,*k,**kw):
		TaskGen.task_gen.__init__(self,*k,**kw)
		if len(k)>1:
			self.features.append('d'+k[1])
TaskGen.bind_feature('d',D_METHS)
def init_d(self):
	Utils.def_attrs(self,dflags='',importpaths='',libs='',libpaths='',uselib='',uselib_local='',generate_headers=False,compiled_tasks=[],add_objects=[],link_task=None)
def apply_d_libs(self):
	env=self.env
	self.uselib=self.to_list(self.uselib)
	names=self.to_list(self.uselib_local)
	seen=set([])
	tmp=Utils.deque(names)
	while tmp:
		lib_name=tmp.popleft()
		if lib_name in seen:
			continue
		y=self.name_to_obj(lib_name)
		if not y:
			raise Utils.WafError('object %r was not found in uselib_local (required by %r)'%(lib_name,self.name))
		y.post()
		seen.add(lib_name)
		if getattr(y,'uselib_local',None):
			lst=y.to_list(y.uselib_local)
			if'dshlib'in y.features or'dprogram'in y.features:
				lst=[x for x in lst if not'dstaticlib'in self.name_to_obj(x).features]
			tmp.extend(lst)
		if getattr(y,'link_task',None):
			link_name=y.target[y.target.rfind(os.sep)+1:]
			if'dstaticlib'in y.features or'dshlib'in y.features:
				env.append_unique('DLINKFLAGS',env.DLIB_ST%link_name)
				env.append_unique('DLINKFLAGS',env.DLIBPATH_ST%y.link_task.outputs[0].parent.bldpath(env))
			self.link_task.set_run_after(y.link_task)
			dep_nodes=getattr(self.link_task,'dep_nodes',[])
			self.link_task.dep_nodes=dep_nodes+y.link_task.outputs
		for v in self.to_list(y.uselib):
			if not v in self.uselib:
				self.uselib.insert(0,v)
		if getattr(y,'export_incdirs',None):
			for x in self.to_list(y.export_incdirs):
				node=y.path.find_dir(x)
				if not node:
					raise Utils.WafError('object %r: invalid folder %r in export_incdirs'%(y.target,x))
				self.env.append_unique('INC_PATHS',node)
def apply_d_link(self):
	link=getattr(self,'link',None)
	if not link:
		if'dstaticlib'in self.features:link='static_link'
		else:link='d_link'
	outputs=[t.outputs[0]for t in self.compiled_tasks]
	self.link_task=self.create_task(link,outputs,self.path.find_or_declare(get_target_name(self)))
def apply_d_vars(self):
	env=self.env
	dpath_st=env['DPATH_ST']
	lib_st=env['DLIB_ST']
	libpath_st=env['DLIBPATH_ST']
	importpaths=self.to_list(self.importpaths)
	libpaths=[]
	libs=[]
	uselib=self.to_list(self.uselib)
	for i in uselib:
		if env['DFLAGS_'+i]:
			env.append_unique('DFLAGS',env['DFLAGS_'+i])
	for x in self.features:
		if not x in['dprogram','dstaticlib','dshlib']:
			continue
		x.lstrip('d')
		d_shlib_dflags=env['D_'+x+'_DFLAGS']
		if d_shlib_dflags:
			env.append_unique('DFLAGS',d_shlib_dflags)
	for i in uselib:
		if env['DPATH_'+i]:
			for entry in self.to_list(env['DPATH_'+i]):
				if not entry in importpaths:
					importpaths.append(entry)
	for path in importpaths:
		if os.path.isabs(path):
			env.append_unique('_DIMPORTFLAGS',dpath_st%path)
		else:
			node=self.path.find_dir(path)
			self.env.append_unique('INC_PATHS',node)
			env.append_unique('_DIMPORTFLAGS',dpath_st%node.srcpath(env))
			env.append_unique('_DIMPORTFLAGS',dpath_st%node.bldpath(env))
	for i in uselib:
		if env['LIBPATH_'+i]:
			for entry in self.to_list(env['LIBPATH_'+i]):
				if not entry in libpaths:
					libpaths.append(entry)
	libpaths=self.to_list(self.libpaths)+libpaths
	for path in libpaths:
		if not os.path.isabs(path):
			node=self.path.find_resource(path)
			if not node:
				raise Utils.WafError('could not find libpath %r from %r'%(path,self))
			path=node.abspath(self.env)
		env.append_unique('DLINKFLAGS',libpath_st%path)
	for i in uselib:
		if env['LIB_'+i]:
			for entry in self.to_list(env['LIB_'+i]):
				if not entry in libs:
					libs.append(entry)
	libs.extend(self.to_list(self.libs))
	for flag in self.to_list(self.dflags):
		env.append_unique('DFLAGS',flag)
	for lib in libs:
		env.append_unique('DLINKFLAGS',lib_st%lib)
	for i in uselib:
		dlinkflags=env['DLINKFLAGS_'+i]
		if dlinkflags:
			for linkflag in dlinkflags:
				env.append_unique('DLINKFLAGS',linkflag)
def add_shlib_d_flags(self):
	for linkflag in self.env['D_shlib_LINKFLAGS']:
		self.env.append_unique('DLINKFLAGS',linkflag)
def d_hook(self,node):
	task=self.create_task(self.generate_headers and'd_with_header'or'd')
	try:obj_ext=self.obj_ext
	except AttributeError:obj_ext='_%d.o'%self.idx
	task.inputs=[node]
	task.outputs=[node.change_ext(obj_ext)]
	self.compiled_tasks.append(task)
	if self.generate_headers:
		header_node=node.change_ext(self.env['DHEADER_ext'])
		task.outputs+=[header_node]
d_str='${D_COMPILER} ${DFLAGS} ${_DIMPORTFLAGS} ${D_SRC_F}${SRC} ${D_TGT_F}${TGT}'
d_with_header_str='${D_COMPILER} ${DFLAGS} ${_DIMPORTFLAGS} \
${D_HDR_F}${TGT[1].bldpath(env)} \
${D_SRC_F}${SRC} \
${D_TGT_F}${TGT[0].bldpath(env)}'
link_str='${D_LINKER} ${DLNK_SRC_F}${SRC} ${DLNK_TGT_F}${TGT} ${DLINKFLAGS}'
def override_exec(cls):
	old_exec=cls.exec_command
	def exec_command(self,*k,**kw):
		if isinstance(k[0],list):
			lst=k[0]
			for i in xrange(len(lst)):
				if lst[i]=='-of':
					del lst[i]
					lst[i]='-of'+lst[i]
					break
		return old_exec(self,*k,**kw)
	cls.exec_command=exec_command
cls=Task.simple_task_type('d',d_str,'GREEN',before='static_link d_link',shell=False)
cls.scan=scan
override_exec(cls)
cls=Task.simple_task_type('d_with_header',d_with_header_str,'GREEN',before='static_link d_link',shell=False)
override_exec(cls)
cls=Task.simple_task_type('d_link',link_str,color='YELLOW',shell=False)
override_exec(cls)
def generate_header(self,filename,install_path):
	if not hasattr(self,'header_lst'):self.header_lst=[]
	self.meths.append('process_header')
	self.header_lst.append([filename,install_path])
def process_header(self):
	env=self.env
	for i in getattr(self,'header_lst',[]):
		node=self.path.find_resource(i[0])
		if not node:
			raise Utils.WafError('file not found on d obj '+i[0])
		task=self.create_task('d_header')
		task.set_inputs(node)
		task.set_outputs(node.change_ext('.di'))
d_header_str='${D_COMPILER} ${D_HEADER} ${SRC}'
Task.simple_task_type('d_header',d_header_str,color='BLUE',shell=False)
def d_platform_flags(conf):
	v=conf.env
	binfmt=v.DEST_BINFMT or Utils.unversioned_sys_platform_to_binary_format(v.DEST_OS or Utils.unversioned_sys_platform())
	if binfmt=='pe':
		v['D_program_PATTERN']='%s.exe'
		v['D_shlib_PATTERN']='lib%s.dll'
		v['D_staticlib_PATTERN']='lib%s.a'
	else:
		v['D_program_PATTERN']='%s'
		v['D_shlib_PATTERN']='lib%s.so'
		v['D_staticlib_PATTERN']='lib%s.a'

feature('d')(init_d)
before('apply_type_vars')(init_d)
feature('d')(init_d)
before('apply_d_libs')(init_d)
feature('d')(apply_d_libs)
after('apply_d_link','init_d')(apply_d_libs)
before('apply_vnum')(apply_d_libs)
feature('dprogram','dshlib','dstaticlib')(apply_d_link)
after('apply_core')(apply_d_link)
feature('d')(apply_d_vars)
after('apply_core')(apply_d_vars)
feature('dshlib')(add_shlib_d_flags)
after('apply_d_vars')(add_shlib_d_flags)
extension(EXT_D)(d_hook)
taskgen(generate_header)
before('apply_core')(process_header)
conftest(d_platform_flags)