3
# Scott Newton, 2005 (scottn)
4
# Thomas Nagy, 2006-2010 (ita)
7
Support for waf command-line options
9
Provides default command-line options,
10
as well as custom ones, used by the ``options`` wscript function.
14
import os, tempfile, optparse, sys, re
15
from waflib import Logs, Utils, Context
17
cmds = 'distclean configure build install clean uninstall check dist distcheck'.split()
19
Constant representing the default waf commands displayed in::
27
A dictionary representing the command-line options::
35
List of commands to execute extracted from the command-line. This list is consumed during the execution, see :py:func:`waflib.Scripting.run_commands`.
38
lockfile = os.environ.get('WAFLOCK', '.lock-waf_%s_build' % sys.platform)
39
try: cache_global = os.path.abspath(os.environ['WAFCACHE'])
40
except KeyError: cache_global = ''
41
platform = Utils.unversioned_sys_platform()
44
class opt_parser(optparse.OptionParser):
46
Command-line options parser.
48
def __init__(self, ctx):
49
optparse.OptionParser.__init__(self, conflict_handler="resolve", version='waf %s (%s)' % (Context.WAFVERSION, Context.WAFREVISION))
51
self.formatter.width = Logs.get_term_cols()
56
p('-j', '--jobs', dest='jobs', default=jobs, type='int', help='amount of parallel jobs (%r)' % jobs)
57
p('-k', '--keep', dest='keep', default=0, action='count', help='keep running happily even if errors are found')
58
p('-v', '--verbose', dest='verbose', default=0, action='count', help='verbosity level -v -vv or -vvv [default: 0]')
59
p('--nocache', dest='nocache', default=False, action='store_true', help='ignore the WAFCACHE (if set)')
60
p('--zones', dest='zones', default='', action='store', help='debugging zones (task_gen, deps, tasks, etc)')
62
gr = optparse.OptionGroup(self, 'configure options')
63
self.add_option_group(gr)
65
gr.add_option('-o', '--out', action='store', default='', help='build dir for the project', dest='out')
66
gr.add_option('-t', '--top', action='store', default='', help='src dir for the project', dest='top')
68
default_prefix = os.environ.get('PREFIX')
69
if not default_prefix:
70
if platform == 'win32':
71
d = tempfile.gettempdir()
72
default_prefix = d[0].upper() + d[1:]
73
# win32 preserves the case, but gettempdir does not
75
default_prefix = '/usr/local/'
76
gr.add_option('--prefix', dest='prefix', default=default_prefix, help='installation prefix [default: %r]' % default_prefix)
77
gr.add_option('--download', dest='download', default=False, action='store_true', help='try to download the tools if missing')
80
gr = optparse.OptionGroup(self, 'build and install options')
81
self.add_option_group(gr)
83
gr.add_option('-p', '--progress', dest='progress_bar', default=0, action='count', help= '-p: progress bar; -pp: ide output')
84
gr.add_option('--targets', dest='targets', default='', action='store', help='task generators, e.g. "target1,target2"')
86
gr = optparse.OptionGroup(self, 'step options')
87
self.add_option_group(gr)
88
gr.add_option('--files', dest='files', default='', action='store', help='files to process, by regexp, e.g. "*/main.c,*/test/main.o"')
90
default_destdir = os.environ.get('DESTDIR', '')
91
gr = optparse.OptionGroup(self, 'install/uninstall options')
92
self.add_option_group(gr)
93
gr.add_option('--destdir', help='installation root [default: %r]' % default_destdir, default=default_destdir, dest='destdir')
94
gr.add_option('-f', '--force', dest='force', default=False, action='store_true', help='force file installation')
98
Return the message to print on ``waf --help``
101
for cls in Context.classes:
102
if not cls.cmd or cls.cmd == 'options':
105
s = cls.__doc__ or ''
106
cmds_str[cls.cmd] = s
109
for (k, v) in Context.g_module.__dict__.items():
110
if k in ['options', 'init', 'shutdown']:
113
if type(v) is type(Context.create_context):
114
if v.__doc__ and not k.startswith('_'):
115
cmds_str[k] = v.__doc__
119
just = max(just, len(k))
121
lst = [' %s: %s' % (k.ljust(just), v) for (k, v) in cmds_str.items()]
125
return '''waf [commands] [options]
127
Main commands (example: ./waf build -j4)
132
class OptionsContext(Context.Context):
134
Collect custom options from wscript files and parses the command line.
135
Set the global :py:const:`waflib.Options.commands` and :py:const:`waflib.Options.options` values.
141
def __init__(self, **kw):
142
super(OptionsContext, self).__init__(**kw)
144
self.parser = opt_parser(self)
145
"""Instance of :py:class:`waflib.Options.opt_parser`"""
147
self.option_groups = {}
151
Find the amount of cpu cores to set the default amount of tasks executed in parallel. At
152
runtime the options can be obtained from :py:const:`waflib.Options.options` ::
154
from waflib.Options import options
157
:return: the amount of cpu cores
160
count = int(os.environ.get('JOBS', 0))
162
if 'NUMBER_OF_PROCESSORS' in os.environ:
163
# on Windows, use the NUMBER_OF_PROCESSORS environment variable
164
count = int(os.environ.get('NUMBER_OF_PROCESSORS', 1))
166
# on everything else, first try the POSIX sysconf values
167
if hasattr(os, 'sysconf_names'):
168
if 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
169
count = int(os.sysconf('SC_NPROCESSORS_ONLN'))
170
elif 'SC_NPROCESSORS_CONF' in os.sysconf_names:
171
count = int(os.sysconf('SC_NPROCESSORS_CONF'))
172
if not count and os.name not in ('nt', 'java'):
174
tmp = self.cmd_and_log(['sysctl', '-n', 'hw.ncpu'], quiet=0)
178
if re.match('^[0-9]+$', tmp):
186
def add_option(self, *k, **kw):
188
Wrapper for optparse.add_option::
191
ctx.add_option('-u', '--use', dest='use', default=False, action='store_true',
192
help='a boolean option')
194
self.parser.add_option(*k, **kw)
196
def add_option_group(self, *k, **kw):
198
Wrapper for optparse.add_option_group::
201
ctx.add_option_group('some options')
202
gr.add_option('-u', '--use', dest='use', default=False, action='store_true')
205
gr = self.option_groups[k[0]]
207
gr = self.parser.add_option_group(*k, **kw)
208
self.option_groups[k[0]] = gr
211
def get_option_group(self, opt_str):
213
Wrapper for optparse.get_option_group::
216
gr = ctx.get_option_group('configure options')
217
gr.add_option('-o', '--out', action='store', default='',
218
help='build dir for the project', dest='out')
222
return self.option_groups[opt_str]
224
for group in self.parser.option_groups:
225
if group.title == opt_str:
229
def parse_args(self, _args=None):
231
Parse arguments from a list (not bound to the command-line).
233
:param _args: arguments
234
:type _args: list of strings
236
global options, commands
237
(options, leftover_args) = self.parser.parse_args(args=_args)
238
commands = leftover_args
241
options.destdir = os.path.abspath(os.path.expanduser(options.destdir))
243
if options.verbose >= 1:
244
self.load('errcheck')
248
See :py:func:`waflib.Context.Context.execute`
250
super(OptionsContext, self).execute()