3
# Scott Newton, 2005 (scottn)
4
# Thomas Nagy, 2006 (ita)
6
"Custom command-line options"
8
import os, sys, imp, types, tempfile, optparse
10
from Constants import *
12
cmds = 'distclean configure build install clean uninstall check dist distcheck'.split()
14
# TODO remove in waf 1.6 the following two
22
lockfile = os.environ.get('WAFLOCK', '.lock-wscript')
23
try: cache_global = os.path.abspath(os.environ['WAFCACHE'])
24
except KeyError: cache_global = ''
25
platform = Utils.unversioned_sys_platform()
26
conf_file = 'conf-runs-%s-%d.pickle' % (platform, ABI)
28
remote_repo = ['http://waf.googlecode.com/svn/']
29
"""remote directory for the plugins"""
32
# Such a command-line should work: JOBS=4 PREFIX=/opt/ DESTDIR=/tmp/ahoj/ waf configure
33
default_prefix = os.environ.get('PREFIX')
34
if not default_prefix:
35
if platform == 'win32': default_prefix = tempfile.gettempdir()
36
else: default_prefix = '/usr/local/'
38
default_jobs = os.environ.get('JOBS', -1)
41
if 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
42
default_jobs = os.sysconf('SC_NPROCESSORS_ONLN')
44
default_jobs = int(Utils.cmd_output(['sysctl', '-n', 'hw.ncpu']))
46
if os.name == 'java': # platform.system() == 'Java'
47
from java.lang import Runtime
48
default_jobs = Runtime.getRuntime().availableProcessors()
50
# environment var defined on win32
51
default_jobs = int(os.environ.get('NUMBER_OF_PROCESSORS', 1))
53
default_destdir = os.environ.get('DESTDIR', '')
57
module = Utils.g_module
59
# create the help messages for commands
61
keys = list(tbl.keys())
65
if not module.build.__doc__:
66
module.build.__doc__ = 'builds the project'
67
if 'configure' in tbl:
68
if not module.configure.__doc__:
69
module.configure.__doc__ = 'configures the project'
71
ban = ['set_options', 'init', 'shutdown']
73
optlst = [x for x in keys if not x in ban
74
and type(tbl[x]) is type(parse_args_impl)
76
and not x.startswith('_')]
78
just = max([len(x) for x in optlst])
81
cmds_str.append(' %s: %s' % (x.ljust(just), tbl[x].__doc__))
82
ret = '\n'.join(cmds_str)
85
return '''waf [command] [options]
87
Main commands (example: ./waf build -j4)
92
setattr(optparse.OptionParser, 'get_usage', get_usage)
94
def create_parser(module=None):
95
Logs.debug('options: create_parser is called')
96
parser = optparse.OptionParser(conflict_handler="resolve", version = 'waf %s (%s)' % (WAFVERSION, WAFREVISION))
98
parser.formatter.width = Utils.get_term_cols()
103
default = default_jobs,
104
help = 'amount of parallel jobs (%r)' % default_jobs,
108
action = 'store_true',
110
help = 'keep running happily on independent task groups',
116
help = 'verbosity level -v -vv or -vvv [default: 0]',
120
action = 'store_true',
122
help = 'ignore the WAFCACHE (if set)',
128
help = 'debugging zones (task_gen, deps, tasks, etc)',
131
p('-p', '--progress',
134
help = '-p: progress bar; -pp: ide output',
135
dest = 'progress_bar')
140
help = 'build given task generators, e.g. "target1,target2"',
141
dest = 'compile_targets')
143
gr = optparse.OptionGroup(parser, 'configuration options')
144
parser.add_option_group(gr)
145
gr.add_option('-b', '--blddir',
148
help = 'build dir for the project (configuration)',
150
gr.add_option('-s', '--srcdir',
153
help = 'src dir for the project (configuration)',
155
gr.add_option('--prefix',
156
help = 'installation prefix (configuration) [default: %r]' % default_prefix,
157
default = default_prefix,
160
gr = optparse.OptionGroup(parser, 'installation options')
161
parser.add_option_group(gr)
162
gr.add_option('--destdir',
163
help = 'installation root [default: %r]' % default_destdir,
164
default = default_destdir,
166
gr.add_option('-f', '--force',
167
action = 'store_true',
169
help = 'force file installation',
174
def parse_args_impl(parser, _args=None):
175
global options, commands, arg_line
176
(options, args) = parser.parse_args(args=_args)
179
#arg_line = args[:] # copy
181
# By default, 'waf' is equivalent to 'waf build'
183
for var in cmds: commands[var] = 0
185
commands['build'] = 1
188
# Parse the command arguments
192
# the check thing depends on the build
194
idx = args.index('check')
196
bidx = args.index('build')
198
raise ValueError('build before check')
199
except ValueError, e:
200
args.insert(idx, 'build')
202
if args[0] != 'init':
203
args.insert(0, 'init')
206
if options.keep: options.jobs = 1
207
if options.jobs < 1: options.jobs = 1
209
if 'install' in sys.argv or 'uninstall' in sys.argv:
210
# absolute path only if set
211
options.destdir = options.destdir and os.path.abspath(os.path.expanduser(options.destdir))
213
Logs.verbose = options.verbose
217
Logs.zones = options.zones.split(',')
218
if not Logs.verbose: Logs.verbose = 1
219
elif Logs.verbose > 0:
220
Logs.zones = ['runner']
225
# 1. rename the class to OptionsContext
226
# 2. instead of a class attribute, use a module (static 'parser')
227
# 3. parse_args_impl was made in times when we did not know about binding new methods to classes
229
class Handler(Utils.Context):
230
"""loads wscript modules in folders for adding options
231
This class should be named 'OptionsContext'
232
A method named 'recurse' is bound when used by the module Scripting"""
235
# make it possible to access the reference, like Build.bld
237
def __init__(self, module=None):
238
self.parser = create_parser(module)
239
self.cwd = os.getcwd()
240
Handler.parser = self
242
def add_option(self, *k, **kw):
243
self.parser.add_option(*k, **kw)
245
def add_option_group(self, *k, **kw):
246
return self.parser.add_option_group(*k, **kw)
248
def get_option_group(self, opt_str):
249
return self.parser.get_option_group(opt_str)
251
def sub_options(self, *k, **kw):
252
if not k: raise Utils.WscriptError('folder expected')
253
self.recurse(k[0], name='set_options')
255
def tool_options(self, *k, **kw):
256
Utils.python_24_guard()
259
raise Utils.WscriptError('invalid tool_options call %r %r' % (k, kw))
260
tools = Utils.to_list(k[0])
262
# TODO waf 1.6 remove the global variable tooldir
263
path = Utils.to_list(kw.get('tdir', kw.get('tooldir', tooldir)))
266
tool = tool.replace('++', 'xx')
267
if tool == 'java': tool = 'javaw'
268
if tool.lower() == 'unittest': tool = 'unittestw'
269
module = Utils.load_tool(tool, path)
271
fun = module.set_options
272
except AttributeError:
275
fun(kw.get('option_group', self))
277
def parse_args(self, args=None):
278
parse_args_impl(self.parser, args)