File: build_w.py

package info (click to toggle)
pyopengl 2.0.1.08-5.1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 19,484 kB
  • ctags: 9,036
  • sloc: pascal: 64,950; xml: 28,088; ansic: 20,696; python: 19,761; tcl: 668; makefile: 240; sh: 25
file content (302 lines) | stat: -rwxr-xr-x 9,649 bytes parent folder | download
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
#!/usr/bin/env python

import sys, os, os.path, re, string
from distutils.filelist import FileList
from util import *
import distutils.cmd

import traceback

import time, string

# re for CVS keywords
CVS_keyword = re.compile(r'[$][A-Za-z]+:\s+([^$]+?)\s+[$]')


def check_swig_version(swig_name):
	try:
		if hasattr( os, 'popen3'):
			sin,sout,stderr = os.popen3("%s -version" % swig_name)
			data = stderr.read()
		else:
			data = os.popen( "%s -version" % swig_name).read()
		if string.find(data,"1.3.13") == -1:
			return 0
		else:
			return 1
	except:
		if __debug__:
			traceback.print_exc(file = sys.stderr)
		return 0
	
def handle_wrong_swig_version():
	print "WARNING!!! wrong swig version.  Need 1.3.13, continuing anyway."
	time.sleep(3)


class build_w(distutils.cmd.Command):

	description = '"build" Python wrappers using SWIG.'

	user_options = [
		('force', 'f', "forcibly build everything (ignore file timestamps)"),
		]

	boolean_options = ['force']


	def initialize_options (self):
		self.force = None


	def finalize_options (self):
		self.set_undefined_options('build', ('force', 'force'))


	def run (self):
		# try to run swig
		self.swig_name = None
		
		for swig_name in ('swig', 'swig1.3'):
			try:
				# should do a version check
				self.spawn([swig_name, '-version'])
				self.swig_name = swig_name
				if not check_swig_version(swig_name):
					handle_wrong_swig_version()
					self.swig_name = None
				break
			except:
				pass

		# if brain dead spawn doesn't work...
		if self.swig_name is None:
			for swig_name in ('swig', 'swig1.3'):
				try:
					r = os.system("%s -version" % swig_name)
				except:
					r == 99999999
					if r == 256:
						self.swig_name = swig_name

						if not check_swig_version(swig_name):
							handle_wrong_swig_version()

						# found the swig name, stop checking.
						break
			

		if self.swig_name is None:
			self.warn("Can't find SWIG, will just have to do with the existing wrapper source.")
		else:
			self.mkpath('src/interface')
			self.mkpath('src/shadow')
	
			interfaces = []
			f = FileList()
			f.findall('interface')
			f.process_template_line('global-include *.i')
			f.files.sort()
			for file in f.files:
				interfaces.append(string.join(string.split(os.path.splitext(file)[0], os.sep)[1:], '.'))
			interfaces.sort()
	
			for full_module_name in interfaces:
				self.swig(full_module_name)


	def make_version_selector(self, path, config_path, pre_text, version_text, post_text, BUILD):
		module_name = os.path.splitext(os.path.basename(path))[0]
		f = open(path, 'w')
		# not really sure if we need to propagate the BUILD, specifically the libs value, to the C file
		# build_ext doesn't need it but build_py may.  If distutils supported preprocessing better than
		# we wouldn't need to link in in build_py, hence wouldn't need to to propagate the BUILD info.
		f.write('/*\n')
		for key, item in BUILD.items():
			# write the build info, but don't include stuff that setup.py doesn't need
			if key not in ('shadow', 'macro_template', 'api_versions', 'headers'):
				f.write('# BUILD %s %s\n' % (key, repr(item)))
		f.write('*/\n')
		# the headers including any needed by the module
		f.write('#include "%s"\n' % config_path)
		for header in BUILD['headers']:
			f.write('#include <%s>\n' % header)
		f.write('\n')

		f.write(pre_text)
		if BUILD.has_key('macro_template'):
			for i in range(len(BUILD['api_versions'])):
				# api_version_underscore should be a matter of convenience, but SWIG's preprocessor is dysfunctional
				api_version_underscore = '%d_%d' % ((BUILD['api_versions'][i] & 0xff00) >> 8, BUILD['api_versions'][i] & 0xff)
				# setup the testing macro
				macro = BUILD['macro_template'] % {'api_version':BUILD['api_versions'][i], 'api_version_underscore':api_version_underscore}
				if i == 0:
					f.write('#if ' + macro + '\n')
				elif not BUILD['api_version_check'] and i+1 == len(BUILD['api_versions']):
					f.write('#else\n')
				else:
					f.write('#elif ' + macro + '\n')
				f.write(version_text % BUILD['api_versions'][i])
			if BUILD['api_version_check']:
				f.write('#else\n')
				f.write('#error "Do not know how to compile %s for API version < 0x%04x"\n' % (module_name, BUILD['api_versions'][-1]))
			f.write('#endif\n')
		elif len(BUILD['api_versions']) > 1:
			print 'Multiple API versions specified for %s but no macro template provided!' % module_name
			sys.exit(1)
		else:
			f.write(version_text % BUILD['api_versions'][0])
		f.write(post_text)
		f.close()


	def make_wrappers(self, swig_cmd, module_name, source_path, shadow_path):
		self.swig_just_ran = 1
		try:
			# this shouldn't be req, but SWIG seems to like it
			os.remove(source_path)
		except:
			pass

		try:
			self.spawn(swig_cmd)

			if not os.path.exists(source_path):
				sys.exit(1)

			if shadow_path:
				# copy the shadow script
				t = module_name + '.py'
				# SWIG isn't very consistent in its placement of the shadow script
				for f in [t, os.path.join('src', t), os.path.join('src', 'interface', t)]:
					if os.path.exists(f):
						# does this screw up things on the Mac?
						x = open(f).read()
						x = string.replace(x, module_name + 'c', module_name + '_')
						open(shadow_path, 'w').write(x)
						os.remove(f)
						break

			f = open(source_path)
			x = f.read()
			f.close()
			# replace CVS keywords
			x = CVS_keyword.sub(r'\1', x)
			x = string.replace(x, module_name + 'c', module_name + '_')
			# find all the docstrings and put them into the function table
			for method in re.findall('static char _doc[_]([^[\s]+)', x):
				x = re.sub(
					r'[{]\s*\(\s*char\s*\*\s*\)\s*"%s"\s*,\s*([^ ",]+)\s*,\s*([^,"}]+)[}]' % method,
					r'{ (char *)"%s", \1, \2, _doc_%s}' % (method, method),
					x
				)
			f = open(source_path, 'w')
			f.write(x)
			f.close()
		except:
			if os.path.exists(source_path):
				os.remove(source_path)
			sys.exit(1)

		
	def swig(self, full_module_name):
		global CVS_keyword

		self.announce('Building wrappers for %s' % full_module_name)

		# split the full module name into the short name and the path
		m = string.split(full_module_name, '.')
		module_name = m[-1]
		if len(m) == 1:
			module_path = ''
		else:
			module_path = apply(os.path.join, m[:-1])

		# the swig cmd line
		swig_cmd = [self.swig_name, '-python', '-Iinterface']

		# create the req paths and figure out the interface name	
		base_path = apply(os.path.join, m)
		self.mkpath(string.join(['OpenGL'] + m[:-1], '/'))
		interface_path = os.path.join('interface', base_path + '.i')

		# retrieve the build info
		BUILD = get_build_info(interface_path)

		if BUILD['shadow']:
			mkdir(os.path.join('src', 'shadow'))
			# if this is a shadow interface then append a '_' to the end of the c module name
			c_full_module_name = full_module_name + '_'
			c_module_name = module_name + '_'
			c_base_path = base_path + '_'
			# insert the shadow option
			swig_cmd.append('-shadow')
			# figure out the script names
			shadow_version_selector_path = os.path.join('src', 'shadow', full_module_name + '.c')
			self.make_file([interface_path],
						   shadow_version_selector_path,
						   self.make_version_selector,
						   (shadow_version_selector_path,
							'src/config.h',
							'#include <stdio.h>\n\nint main(int argc, char **argv)\n{\n\tFILE *f = fopen("api_version", "w");\n',
							'\tfprintf(f, "%d");\n',
							'\tfclose(f);\n\treturn 0;\n}\n',
							BUILD),
						   exec_msg = 'Generating shadow version selector',
						   skip_msg = "Shadow version selector doesn't need updating")
		else:
			# no modification of the module name needed
			c_full_module_name = full_module_name
			c_module_name = module_name
			c_base_path = base_path

		# fiqure out the interface source names
		src_wrapper_path = os.path.join('src', 'interface', c_full_module_name + '.c')

		c_version_selector_path = os.path.join('src', 'interface', c_full_module_name + '.c')
		self.make_file([interface_path],
					   c_version_selector_path,
					   self.make_version_selector,
					   (c_version_selector_path,
						'../config.h',
						'',
						'#include "%s.%%04x.inc"\n' % c_full_module_name,
						'',
						BUILD),
					   exec_msg = 'Generating C version selector',
					   skip_msg = "C version selector doesn't need updating")


		source_path_template = os.path.join('src', 'interface', c_full_module_name + '.%04x.inc')
		shadow_path_template = os.path.join('src', 'shadow', full_module_name + '.%04x.py')

		for api_version in BUILD['api_versions']:
			# fiqure out the paths
			source_path = source_path_template % api_version
			if BUILD['shadow']:
				shadow_path = shadow_path_template % api_version
				outfiles = (source_path, shadow_path)
			else:
				shadow_path = ''
				outfiles = (source_path,)

		
			this_swig_cmd = swig_cmd + ['-DAPI_VERSION=%d' % api_version, '-o', source_path, interface_path]

			self.swig_just_ran = 0
			args = (this_swig_cmd, module_name, source_path, shadow_path)
			exec_msg = 'Generating wrappers for API version 0x%04x' % api_version
			skip_msg = "Wrappers for API version 0x%04x don't need updating" % api_version

			for outfile in outfiles:
				if self.swig_just_ran:
					break
				self.make_file([interface_path],
							   outfile,
							   self.make_wrappers,
							   args,
							   exec_msg = exec_msg,
							   skip_msg = skip_msg)

		self.announce('')