summaryrefslogtreecommitdiff
path: root/waf-light
blob: 1bae1d58e8434b3c02407a52f303d10fe2627acd (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
#! /usr/bin/env python
# encoding: utf-8
# Thomas Nagy, 2005, 2006, 2007 (ita)

"""
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

3. The name of the author may not be used to endorse or promote products
   derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
"""

import os, sys
if 'PSYCOWAF' in os.environ:
	try:
		import psyco
		psyco.full()
	except:
		pass

VERSION="1.2.0"
REVISION="x"
INSTALL="x"
cwd = os.getcwd()

def decodeAscii85(s):
	out=[]
	app=out.append
	s=''.join(s.split()).replace('z','!!!!!')
	p1,p2=divmod(len(s), 5)
	stop=5*p1
	p3,p4=s[0:stop],s[stop:]
	for i in range(p1):
		off=i*5
		a=ord(p3[off])-33
		b=ord(p3[off+1])-33
		c=ord(p3[off+2])-33
		d=ord(p3[off+3])-33
		e=ord(p3[off+4])-33
		num=(52200625L*a)+(614125*b)+(7225*c)+(85*d)+e
		x,p=divmod(num,256)
		x,o=divmod(x,256)
		m,n=divmod(x,256)
		app(chr(m)+chr(n)+chr(o)+chr(p))
	if p2:
		while len(p4)<5: p4=p4+'!'
		a=ord(p4[0])-33
		b=ord(p4[1])-33
		c=ord(p4[2])-33
		d=ord(p4[3])-33
		e=ord(p4[4])-33
		num=(52200625L*a)+(614125*b)+(7225*c)+(85*d)+e
		x,p=divmod(num,256)
		x,o=divmod(x,256)
		m,n=divmod(x, 256)
		if p2==2: app(chr(m))
		elif p2==3: app(chr(m)+chr(n))
		elif p2==4: app(chr(m)+chr(n)+chr(o))
	return ''.join(out)

# wafdir is needed to parse the command-line arguments or print the version number
wafdir=None # SPECIAL LINE

def uncompress_wafdir(newdir):
	file = open(sys.argv[0], 'rb')
	while 1:
		line = file.readline()
		if not line:
			print "This is a stripped-down waf, there is no wafadmin directory available"
			print "Please set WAFDIR to a directory containing a directory named wafadmin"
			print "Or use the full waf version available freely at http://freehackers.org/~tnagy/bksys.html"
			print "\033[91mNo wafadmin: cannot execute anything (error)\033[0m"
			sys.exit(1)
		line=line.rstrip()
		if line=='# ===>BEGIN WOOF<===':
			cnt = file.readline()
			if not cnt:
				print "Corrupted waf (1)"
				sys.exit(1)

			line = file.readline().rstrip()
			if line!='# ===>END WOOF<===':
				print "Corrupted waf (2)"
				sys.exit(1)
			break
	if not cnt:
		print "Corrupted waf (3)"
		sys.exit(1)

	cnt = decodeAscii85(cnt[1:])

	# create wafadmin
	import shutil
	try: shutil.rmtree(newdir)
	except OSError: pass
	try: os.makedirs(newdir)
	except OSError:
		print "Could uncompress waf-local into %s"%newdir
		print "Please install waf system-wide or move waf in a writeable directory"
		sys.exit(1)

	os.chdir(newdir)
	file = open('wafadmin.tar.bz2', 'wb')
	file.write(cnt)
	file.close()

	# now we have the tar file to open
	import tarfile
	tar = tarfile.open('wafadmin.tar.bz2')
	for tarinfo in tar:
		tar.extract(tarinfo)
	tar.close()

	# cleanup the tarfile and chdir to the previous directory
	os.chmod('wafadmin', 0755)
	os.chmod('wafadmin'+os.sep+'Tools', 0755)
	os.unlink('wafadmin.tar.bz2')
	os.chdir(cwd)

	global wafdir
	wafdir = newdir

def try_wafdir(dir):
	global wafdir
	if wafdir: return
	try:
		os.stat(os.path.join(dir, 'wafadmin'))
		wafdir = os.path.abspath(dir)
	except OSError:
		pass

def find_wafadmin():
	global wafdir
	name = sys.argv[0]

	# wafadmin may be in $WAFDIR (developers)
	if 'WAFDIR' in os.environ:
		try_wafdir(os.environ['WAFDIR'])
		if wafdir: return

	# waf-light is a special beast
	if name[-5:] == 'light':
		try_wafdir(os.path.dirname(os.path.abspath(name)))
		if wafdir: return
		print "\033[91mwaf-light in use, wafadmin not found -> export WAFDIR=/folder\033[0m"
		sys.exit(1)

	if not wafdir:
		dir = "/lib/waf-%s-%s/" % (VERSION, REVISION)
		for i in [INSTALL, '/usr', '/usr/local', '/opt']:
			try_wafdir(i+dir)
			if wafdir: return

	# remove $HOME/.waf-version if asked to
	if wafdir:
		if "--nocache" in sys.argv:
			import shutil
			print "removing the local wafdir", wafdir
			try: shutil.rmtree(wafdir)
			except OSError: pass
		try: os.stat(wafdir)
		except OSError: wafdir=None

	if wafdir: return

	# look in the directory containing waf
	if sys.platform == 'win32': s='waf-%s-%s'
	else: s='.waf-%s-%s'
	dir = os.path.join(os.path.dirname(os.path.abspath(name)), s % (VERSION, REVISION))
	try_wafdir(dir)
	if wafdir: return

	# not found, uncompress
	wafdir = dir
	uncompress_wafdir(dir)

# run the test
find_wafadmin()
if "-vv" in sys.argv: print "wafdir is ", wafdir

# Update sys.path and import our modules
wafadmindir = os.path.join(wafdir, 'wafadmin')
tooldir = os.path.join(wafadmindir, 'Tools')
sys.path = [wafadmindir, tooldir] + sys.path

import Options, Params, Utils
from Params import fatal, warning

# Set the directory containing the tools
Params.g_tooldir = [tooldir]
Params.g_cwd_launch = cwd

if Params.g_version != VERSION:
	fatal('version mismatch waf %s <-> wafadmin %s (wafdir %s)' % (VERSION, Params.g_version, wafdir))

# some command-line options can be processed immediately
if '--version' in sys.argv:
	opt_obj = Options.Handler()
	opt_obj.parse_args()
	sys.exit(0)

# now find the wscript file
msg1 = 'Waf: *** Nothing to do! Please run waf from a directory containing a file named "wscript"'

# Some people want to configure their projects gcc-style:
# mkdir build && cd build && ../waf configure && ../waf
# check that this is really what is wanted
build_dir_override = None
candidate = None

lst = os.listdir(cwd)
xml = 0
#check if a wscript or a wscript_xml file is in current directory
if (not 'wscript' in lst) and (not 'wscript_xml' in lst):
	if 'configure' in sys.argv:
		#set the build directory with the current directory
		build_dir_override = cwd
	if 'wscript_build' in lst:
		#try to find the wscript root
		candidate = cwd
else:
	#wscript or wscript_xml is in current directory, use this directory as candidate
	candidate = cwd

try:
	#check the following dirs for wscript or wscript_xml
	search_for_candidate = True
	if not candidate:
		#check first the calldir if there is wscript or wscript_xml
		#for example: /usr/src/configure the calldir would be /usr/src
		calldir = os.path.abspath(os.path.dirname(sys.argv[0]))
		lst_calldir = os.listdir(calldir)
		if 'wscript'       in lst_calldir:
			candidate = calldir
			search_for_candidate = False
		if 'wscript_xml'   in lst_calldir:
			candidate = calldir
			xml = 1
			search_for_candidate = False
	if "--make-waf" in sys.argv and candidate:
		search_for_candidate = False

	#check all directories above current dir for wscript or wscript_xml if still not found
	while search_for_candidate:
		if len(cwd) <= 3:
			break # stop at / or c:
		dirlst = os.listdir(cwd)
		if 'wscript' in dirlst:
			candidate = cwd
			xml = 0
		if 'wscript_xml' in dirlst:
			candidate = cwd
			xml = 1
			break
		if 'configure' in sys.argv and candidate:
			break
		if Params.g_lockfile in dirlst:
			break
		cwd = cwd[:cwd.rfind(os.sep)] # climb up
except:
	fatal(msg1)

if not candidate:
	# check if the user only wanted to display the help
	if '-h' in sys.argv or '--help' in sys.argv:
		warning('No wscript file found: the help message may be incomplete')
		opt_obj = Options.Handler()
		opt_obj.parse_args()
		sys.exit(0)
	else:
		fatal(msg1)

# We have found wscript, but there is no guarantee that it is valid
os.chdir(candidate)

# xml -> jump to the parser
if xml:
	from XMLScripting import compile
	compile(candidate+os.sep+'wscript_xml')
else:
	# define the main module containing the functions init, shutdown, ..
	Utils.set_main_module(os.path.join(candidate, 'wscript'))

if build_dir_override:
	d = getattr(Utils.g_module, 'blddir', None)
	if d:
		# test if user has set the blddir in wscript.
		msg = 'Overriding build directory %s with %s' % (d, build_dir_override)
		Params.niceprint(msg, 'WARNING', 'waf')
	Utils.g_module.blddir = build_dir_override

# fetch the custom command-line options recursively and in a procedural way
opt_obj = Options.Handler()
opt_obj.sub_options('') # will look in wscript
opt_obj.parse_args()

# use the parser results
if Params.g_commands['dist']:
	# try to use the user-defined dist function first, fallback to the waf scheme
	fun = getattr(Utils.g_module, 'dist', None)
	if fun: fun(); sys.exit(0)

	appname = getattr(Utils.g_module, 'APPNAME', 'noname')

	get_version = getattr(Utils.g_module, 'get_version', None)
	if get_version: version = get_version()
	else: version = getattr(Utils.g_module, 'VERSION', None)
	if not version: version = '1.0'

	from Scripting import Dist
	Dist(appname, version)
	sys.exit(0)
elif Params.g_commands['distclean']:
	# try to use the user-defined distclean first, fallback to the waf scheme
	fun = getattr(Utils.g_module, 'distclean', None)
	if fun: fun(); sys.exit(0)
	from Scripting import DistClean
	DistClean()
	sys.exit(0)
elif Params.g_commands['distcheck']:
	# try to use the user-defined dist function first, fallback to the waf scheme
	fun = getattr(Utils.g_module, 'dist', None)
	if fun: fun(); sys.exit(0)

	appname = getattr(Utils.g_module, 'APPNAME', 'noname')

	get_version = getattr(Utils.g_module, 'get_version', None)
	if get_version: version = get_version()
	else: version = getattr(Utils.g_module, 'VERSION', None)
	if not version: version = '1.0'

	from Scripting import DistCheck
	DistCheck(appname, version)
	sys.exit(0)

fun=getattr(Utils.g_module, 'init', None)
if fun: fun()

import Scripting
try: Scripting.Main()
except KeyboardInterrupt: Params.fatal('build interrupted')
#import hotshot
#prof=hotshot.Profile("/tmp/proftest.txt")
#prof.runcall(Scripting.Main)
#prof.close()