2011-09-10 11:13:51 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
# Scott Newton, 2005 (scottn)
|
2018-01-01 20:53:49 +01:00
|
|
|
# Thomas Nagy, 2006-2018 (ita)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
Support for waf command-line options
|
|
|
|
|
2016-06-25 13:55:13 +02:00
|
|
|
Provides default and command-line options, as well the command
|
|
|
|
that reads the ``options`` wscript function.
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
import os, tempfile, optparse, sys, re
|
2016-05-14 12:15:15 +02:00
|
|
|
from waflib import Logs, Utils, Context, Errors
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2017-09-10 19:16:02 +02:00
|
|
|
options = optparse.Values()
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
2016-06-25 14:49:27 +02:00
|
|
|
A global dictionary representing user-provided command-line options::
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
$ waf --foo=bar
|
|
|
|
"""
|
|
|
|
|
|
|
|
commands = []
|
|
|
|
"""
|
2016-06-25 13:55:13 +02:00
|
|
|
List of commands to execute extracted from the command-line. This list
|
|
|
|
is consumed during the execution by :py:func:`waflib.Scripting.run_commands`.
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
|
2014-01-25 22:28:07 +01:00
|
|
|
envvars = []
|
|
|
|
"""
|
|
|
|
List of environment variable declarations placed after the Waf executable name.
|
2016-06-25 13:55:13 +02:00
|
|
|
These are detected by searching for "=" in the remaining arguments.
|
|
|
|
You probably do not want to use this.
|
2014-01-25 22:28:07 +01:00
|
|
|
"""
|
|
|
|
|
2011-10-01 19:04:26 +02:00
|
|
|
lockfile = os.environ.get('WAFLOCK', '.lock-waf_%s_build' % sys.platform)
|
2016-06-25 13:55:13 +02:00
|
|
|
"""
|
|
|
|
Name of the lock file that marks a project as configured
|
|
|
|
"""
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
class opt_parser(optparse.OptionParser):
|
|
|
|
"""
|
|
|
|
Command-line options parser.
|
|
|
|
"""
|
2017-09-05 10:13:25 +02:00
|
|
|
def __init__(self, ctx, allow_unknown=False):
|
2017-09-10 19:16:02 +02:00
|
|
|
optparse.OptionParser.__init__(self, conflict_handler='resolve', add_help_option=False,
|
2019-10-14 07:59:40 +02:00
|
|
|
version='%s %s (%s)' % (Context.WAFNAME, Context.WAFVERSION, Context.WAFREVISION))
|
2011-09-10 11:13:51 +02:00
|
|
|
self.formatter.width = Logs.get_term_cols()
|
|
|
|
self.ctx = ctx
|
2017-09-05 10:13:25 +02:00
|
|
|
self.allow_unknown = allow_unknown
|
|
|
|
|
|
|
|
def _process_args(self, largs, rargs, values):
|
|
|
|
"""
|
|
|
|
Custom _process_args to allow unknown options according to the allow_unknown status
|
|
|
|
"""
|
|
|
|
while rargs:
|
|
|
|
try:
|
|
|
|
optparse.OptionParser._process_args(self,largs,rargs,values)
|
2017-09-10 20:34:15 +02:00
|
|
|
except (optparse.BadOptionError, optparse.AmbiguousOptionError) as e:
|
2017-09-05 10:13:25 +02:00
|
|
|
if self.allow_unknown:
|
2017-09-10 20:34:15 +02:00
|
|
|
largs.append(e.opt_str)
|
2017-09-05 10:13:25 +02:00
|
|
|
else:
|
2017-09-10 20:34:15 +02:00
|
|
|
self.error(str(e))
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2020-03-04 23:14:40 +01:00
|
|
|
def _process_long_opt(self, rargs, values):
|
|
|
|
# --custom-option=-ftxyz is interpreted as -f -t... see #2280
|
|
|
|
if self.allow_unknown:
|
|
|
|
back = [] + rargs
|
|
|
|
try:
|
|
|
|
optparse.OptionParser._process_long_opt(self, rargs, values)
|
|
|
|
except optparse.BadOptionError:
|
|
|
|
while rargs:
|
|
|
|
rargs.pop()
|
|
|
|
rargs.extend(back)
|
|
|
|
rargs.pop(0)
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
optparse.OptionParser._process_long_opt(self, rargs, values)
|
|
|
|
|
2014-08-10 21:38:08 +02:00
|
|
|
def print_usage(self, file=None):
|
|
|
|
return self.print_help(file)
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
def get_usage(self):
|
|
|
|
"""
|
2016-06-25 14:49:27 +02:00
|
|
|
Builds the message to print on ``waf --help``
|
|
|
|
|
|
|
|
:rtype: string
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
cmds_str = {}
|
|
|
|
for cls in Context.classes:
|
2013-11-12 21:25:09 +01:00
|
|
|
if not cls.cmd or cls.cmd == 'options' or cls.cmd.startswith( '_' ):
|
2011-09-10 11:13:51 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
s = cls.__doc__ or ''
|
|
|
|
cmds_str[cls.cmd] = s
|
|
|
|
|
|
|
|
if Context.g_module:
|
|
|
|
for (k, v) in Context.g_module.__dict__.items():
|
2014-01-05 22:27:06 +01:00
|
|
|
if k in ('options', 'init', 'shutdown'):
|
2011-09-10 11:13:51 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
if type(v) is type(Context.create_context):
|
|
|
|
if v.__doc__ and not k.startswith('_'):
|
|
|
|
cmds_str[k] = v.__doc__
|
|
|
|
|
|
|
|
just = 0
|
|
|
|
for k in cmds_str:
|
|
|
|
just = max(just, len(k))
|
|
|
|
|
|
|
|
lst = [' %s: %s' % (k.ljust(just), v) for (k, v) in cmds_str.items()]
|
|
|
|
lst.sort()
|
|
|
|
ret = '\n'.join(lst)
|
|
|
|
|
2019-10-14 07:59:40 +02:00
|
|
|
return '''%s [commands] [options]
|
2011-09-10 11:13:51 +02:00
|
|
|
|
2019-10-14 07:59:40 +02:00
|
|
|
Main commands (example: ./%s build -j4)
|
2011-09-10 11:13:51 +02:00
|
|
|
%s
|
2019-10-14 07:59:40 +02:00
|
|
|
''' % (Context.WAFNAME, Context.WAFNAME, ret)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
|
|
|
|
class OptionsContext(Context.Context):
|
|
|
|
"""
|
2016-06-25 14:49:27 +02:00
|
|
|
Collects custom options from wscript files and parses the command line.
|
|
|
|
Sets the global :py:const:`waflib.Options.commands` and :py:const:`waflib.Options.options` values.
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
cmd = 'options'
|
|
|
|
fun = 'options'
|
|
|
|
|
|
|
|
def __init__(self, **kw):
|
|
|
|
super(OptionsContext, self).__init__(**kw)
|
|
|
|
|
|
|
|
self.parser = opt_parser(self)
|
|
|
|
"""Instance of :py:class:`waflib.Options.opt_parser`"""
|
|
|
|
|
|
|
|
self.option_groups = {}
|
|
|
|
|
2014-01-25 18:28:14 +01:00
|
|
|
jobs = self.jobs()
|
|
|
|
p = self.add_option
|
|
|
|
color = os.environ.get('NOCOLOR', '') and 'no' or 'auto'
|
2017-03-17 13:21:12 +01:00
|
|
|
if os.environ.get('CLICOLOR', '') == '0':
|
|
|
|
color = 'no'
|
|
|
|
elif os.environ.get('CLICOLOR_FORCE', '') == '1':
|
|
|
|
color = 'yes'
|
2014-01-25 18:28:14 +01:00
|
|
|
p('-c', '--color', dest='colors', default=color, action='store', help='whether to use colors (yes/no/auto) [default: auto]', choices=('yes', 'no', 'auto'))
|
2016-03-25 14:20:57 +01:00
|
|
|
p('-j', '--jobs', dest='jobs', default=jobs, type='int', help='amount of parallel jobs (%r)' % jobs)
|
2014-04-12 18:16:44 +02:00
|
|
|
p('-k', '--keep', dest='keep', default=0, action='count', help='continue despite errors (-kk to try harder)')
|
2014-01-25 18:28:14 +01:00
|
|
|
p('-v', '--verbose', dest='verbose', default=0, action='count', help='verbosity level -v -vv or -vvv [default: 0]')
|
|
|
|
p('--zones', dest='zones', default='', action='store', help='debugging zones (task_gen, deps, tasks, etc)')
|
2017-09-30 21:31:07 +02:00
|
|
|
p('--profile', dest='profile', default=0, action='store_true', help=optparse.SUPPRESS_HELP)
|
|
|
|
p('--pdb', dest='pdb', default=0, action='store_true', help=optparse.SUPPRESS_HELP)
|
2017-09-10 19:16:02 +02:00
|
|
|
p('-h', '--help', dest='whelp', default=0, action='store_true', help="show this help message and exit")
|
2014-01-25 18:28:14 +01:00
|
|
|
|
|
|
|
gr = self.add_option_group('Configuration options')
|
2014-10-07 18:43:44 +02:00
|
|
|
self.option_groups['configure options'] = gr
|
2014-01-25 18:28:14 +01:00
|
|
|
|
|
|
|
gr.add_option('-o', '--out', action='store', default='', help='build dir for the project', dest='out')
|
|
|
|
gr.add_option('-t', '--top', action='store', default='', help='src dir for the project', dest='top')
|
|
|
|
|
2019-11-28 18:13:36 +01:00
|
|
|
gr.add_option('--no-lock-in-run', action='store_true', default=os.environ.get('NO_LOCK_IN_RUN', ''), help=optparse.SUPPRESS_HELP, dest='no_lock_in_run')
|
|
|
|
gr.add_option('--no-lock-in-out', action='store_true', default=os.environ.get('NO_LOCK_IN_OUT', ''), help=optparse.SUPPRESS_HELP, dest='no_lock_in_out')
|
|
|
|
gr.add_option('--no-lock-in-top', action='store_true', default=os.environ.get('NO_LOCK_IN_TOP', ''), help=optparse.SUPPRESS_HELP, dest='no_lock_in_top')
|
2015-05-02 17:13:00 +02:00
|
|
|
|
2014-10-01 01:11:05 +02:00
|
|
|
default_prefix = getattr(Context.g_module, 'default_prefix', os.environ.get('PREFIX'))
|
2014-01-25 18:28:14 +01:00
|
|
|
if not default_prefix:
|
2016-06-25 13:55:13 +02:00
|
|
|
if Utils.unversioned_sys_platform() == 'win32':
|
2014-01-25 18:28:14 +01:00
|
|
|
d = tempfile.gettempdir()
|
|
|
|
default_prefix = d[0].upper() + d[1:]
|
|
|
|
# win32 preserves the case, but gettempdir does not
|
|
|
|
else:
|
|
|
|
default_prefix = '/usr/local/'
|
|
|
|
gr.add_option('--prefix', dest='prefix', default=default_prefix, help='installation prefix [default: %r]' % default_prefix)
|
2015-02-19 13:46:18 +01:00
|
|
|
gr.add_option('--bindir', dest='bindir', help='bindir')
|
|
|
|
gr.add_option('--libdir', dest='libdir', help='libdir')
|
2014-01-25 18:28:14 +01:00
|
|
|
|
|
|
|
gr = self.add_option_group('Build and installation options')
|
2014-10-07 18:43:44 +02:00
|
|
|
self.option_groups['build and install options'] = gr
|
2014-01-25 18:28:14 +01:00
|
|
|
gr.add_option('-p', '--progress', dest='progress_bar', default=0, action='count', help= '-p: progress bar; -pp: ide output')
|
|
|
|
gr.add_option('--targets', dest='targets', default='', action='store', help='task generators, e.g. "target1,target2"')
|
|
|
|
|
|
|
|
gr = self.add_option_group('Step options')
|
2014-10-07 18:43:44 +02:00
|
|
|
self.option_groups['step options'] = gr
|
2014-01-25 18:28:14 +01:00
|
|
|
gr.add_option('--files', dest='files', default='', action='store', help='files to process, by regexp, e.g. "*/main.c,*/test/main.o"')
|
|
|
|
|
|
|
|
default_destdir = os.environ.get('DESTDIR', '')
|
2014-10-07 18:43:44 +02:00
|
|
|
|
2014-01-25 18:28:14 +01:00
|
|
|
gr = self.add_option_group('Installation and uninstallation options')
|
2014-10-07 18:43:44 +02:00
|
|
|
self.option_groups['install/uninstall options'] = gr
|
2014-01-25 18:28:14 +01:00
|
|
|
gr.add_option('--destdir', help='installation root [default: %r]' % default_destdir, default=default_destdir, dest='destdir')
|
|
|
|
gr.add_option('-f', '--force', dest='force', default=False, action='store_true', help='force file installation')
|
|
|
|
gr.add_option('--distcheck-args', metavar='ARGS', help='arguments to pass to distcheck', default=None, action='store')
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
def jobs(self):
|
|
|
|
"""
|
2016-06-25 14:49:27 +02:00
|
|
|
Finds the optimal amount of cpu cores to use for parallel jobs.
|
|
|
|
At runtime the options can be obtained from :py:const:`waflib.Options.options` ::
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
from waflib.Options import options
|
|
|
|
njobs = options.jobs
|
|
|
|
|
|
|
|
:return: the amount of cpu cores
|
|
|
|
:rtype: int
|
|
|
|
"""
|
|
|
|
count = int(os.environ.get('JOBS', 0))
|
|
|
|
if count < 1:
|
|
|
|
if 'NUMBER_OF_PROCESSORS' in os.environ:
|
|
|
|
# on Windows, use the NUMBER_OF_PROCESSORS environment variable
|
|
|
|
count = int(os.environ.get('NUMBER_OF_PROCESSORS', 1))
|
|
|
|
else:
|
|
|
|
# on everything else, first try the POSIX sysconf values
|
|
|
|
if hasattr(os, 'sysconf_names'):
|
|
|
|
if 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
|
|
|
|
count = int(os.sysconf('SC_NPROCESSORS_ONLN'))
|
|
|
|
elif 'SC_NPROCESSORS_CONF' in os.sysconf_names:
|
|
|
|
count = int(os.sysconf('SC_NPROCESSORS_CONF'))
|
2011-09-30 16:45:11 +02:00
|
|
|
if not count and os.name not in ('nt', 'java'):
|
2011-10-09 22:34:01 +02:00
|
|
|
try:
|
2011-10-10 18:56:10 +02:00
|
|
|
tmp = self.cmd_and_log(['sysctl', '-n', 'hw.ncpu'], quiet=0)
|
2016-05-14 12:15:15 +02:00
|
|
|
except Errors.WafError:
|
2011-10-09 22:34:01 +02:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if re.match('^[0-9]+$', tmp):
|
|
|
|
count = int(tmp)
|
2011-09-10 11:13:51 +02:00
|
|
|
if count < 1:
|
|
|
|
count = 1
|
|
|
|
elif count > 1024:
|
|
|
|
count = 1024
|
|
|
|
return count
|
|
|
|
|
|
|
|
def add_option(self, *k, **kw):
|
|
|
|
"""
|
2016-06-25 14:49:27 +02:00
|
|
|
Wraps ``optparse.add_option``::
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def options(ctx):
|
2016-06-25 14:49:27 +02:00
|
|
|
ctx.add_option('-u', '--use', dest='use', default=False,
|
|
|
|
action='store_true', help='a boolean option')
|
|
|
|
|
|
|
|
:rtype: optparse option object
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
2012-05-17 14:57:58 +02:00
|
|
|
return self.parser.add_option(*k, **kw)
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def add_option_group(self, *k, **kw):
|
|
|
|
"""
|
2016-06-25 14:49:27 +02:00
|
|
|
Wraps ``optparse.add_option_group``::
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def options(ctx):
|
2012-04-12 19:55:08 +02:00
|
|
|
gr = ctx.add_option_group('some options')
|
2011-09-10 11:13:51 +02:00
|
|
|
gr.add_option('-u', '--use', dest='use', default=False, action='store_true')
|
2016-06-25 14:49:27 +02:00
|
|
|
|
|
|
|
:rtype: optparse option group object
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
gr = self.option_groups[k[0]]
|
2012-02-11 13:43:47 +01:00
|
|
|
except KeyError:
|
2011-09-10 11:13:51 +02:00
|
|
|
gr = self.parser.add_option_group(*k, **kw)
|
|
|
|
self.option_groups[k[0]] = gr
|
|
|
|
return gr
|
|
|
|
|
|
|
|
def get_option_group(self, opt_str):
|
|
|
|
"""
|
2016-06-25 14:49:27 +02:00
|
|
|
Wraps ``optparse.get_option_group``::
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
def options(ctx):
|
|
|
|
gr = ctx.get_option_group('configure options')
|
|
|
|
gr.add_option('-o', '--out', action='store', default='',
|
|
|
|
help='build dir for the project', dest='out')
|
|
|
|
|
2016-06-25 14:49:27 +02:00
|
|
|
:rtype: optparse option group object
|
2011-09-10 11:13:51 +02:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return self.option_groups[opt_str]
|
|
|
|
except KeyError:
|
|
|
|
for group in self.parser.option_groups:
|
|
|
|
if group.title == opt_str:
|
|
|
|
return group
|
|
|
|
return None
|
|
|
|
|
2017-09-10 19:16:02 +02:00
|
|
|
def sanitize_path(self, path, cwd=None):
|
|
|
|
if not cwd:
|
|
|
|
cwd = Context.launch_dir
|
|
|
|
p = os.path.expanduser(path)
|
|
|
|
p = os.path.join(cwd, p)
|
|
|
|
p = os.path.normpath(p)
|
|
|
|
p = os.path.abspath(p)
|
|
|
|
return p
|
|
|
|
|
|
|
|
def parse_cmd_args(self, _args=None, cwd=None, allow_unknown=False):
|
2017-09-05 10:13:25 +02:00
|
|
|
"""
|
2017-09-10 19:16:02 +02:00
|
|
|
Just parse the arguments
|
2017-09-05 10:13:25 +02:00
|
|
|
"""
|
2017-09-10 19:16:02 +02:00
|
|
|
self.parser.allow_unknown = allow_unknown
|
2017-09-05 10:13:25 +02:00
|
|
|
(options, leftover_args) = self.parser.parse_args(args=_args)
|
2017-09-10 19:16:02 +02:00
|
|
|
envvars = []
|
|
|
|
commands = []
|
|
|
|
for arg in leftover_args:
|
|
|
|
if '=' in arg:
|
|
|
|
envvars.append(arg)
|
|
|
|
elif arg != 'options':
|
|
|
|
commands.append(arg)
|
|
|
|
|
2019-09-26 21:50:18 +02:00
|
|
|
if options.jobs < 1:
|
|
|
|
options.jobs = 1
|
2017-09-10 19:16:02 +02:00
|
|
|
for name in 'top out destdir prefix bindir libdir'.split():
|
|
|
|
# those paths are usually expanded from Context.launch_dir
|
|
|
|
if getattr(options, name, None):
|
|
|
|
path = self.sanitize_path(getattr(options, name), cwd)
|
|
|
|
setattr(options, name, path)
|
|
|
|
return options, commands, envvars
|
2017-09-05 10:13:25 +02:00
|
|
|
|
2017-09-10 19:16:02 +02:00
|
|
|
def init_module_vars(self, arg_options, arg_commands, arg_envvars):
|
|
|
|
options.__dict__.clear()
|
|
|
|
del commands[:]
|
|
|
|
del envvars[:]
|
2017-09-05 10:13:25 +02:00
|
|
|
|
2017-09-10 19:16:02 +02:00
|
|
|
options.__dict__.update(arg_options.__dict__)
|
|
|
|
commands.extend(arg_commands)
|
|
|
|
envvars.extend(arg_envvars)
|
2017-09-05 10:13:25 +02:00
|
|
|
|
2017-09-10 19:16:02 +02:00
|
|
|
for var in envvars:
|
|
|
|
(name, value) = var.split('=', 1)
|
|
|
|
os.environ[name.strip()] = value
|
2017-09-05 10:13:25 +02:00
|
|
|
|
2017-09-10 19:16:02 +02:00
|
|
|
def init_logs(self, options, commands, envvars):
|
2017-09-19 14:14:20 +02:00
|
|
|
Logs.verbose = options.verbose
|
2017-09-05 10:13:25 +02:00
|
|
|
if options.verbose >= 1:
|
|
|
|
self.load('errcheck')
|
|
|
|
|
|
|
|
colors = {'yes' : 2, 'auto' : 1, 'no' : 0}[options.colors]
|
|
|
|
Logs.enable_colors(colors)
|
|
|
|
|
|
|
|
if options.zones:
|
|
|
|
Logs.zones = options.zones.split(',')
|
2017-09-10 19:16:02 +02:00
|
|
|
if not Logs.verbose:
|
|
|
|
Logs.verbose = 1
|
2017-09-05 10:13:25 +02:00
|
|
|
elif Logs.verbose > 0:
|
|
|
|
Logs.zones = ['runner']
|
|
|
|
if Logs.verbose > 2:
|
|
|
|
Logs.zones = ['*']
|
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
def parse_args(self, _args=None):
|
|
|
|
"""
|
2017-02-16 07:03:43 +01:00
|
|
|
Parses arguments from a list which is not necessarily the command-line.
|
2017-09-10 19:16:02 +02:00
|
|
|
Initializes the module variables options, commands and envvars
|
|
|
|
If help is requested, prints it and exit the application
|
2011-09-10 11:13:51 +02:00
|
|
|
|
|
|
|
:param _args: arguments
|
|
|
|
:type _args: list of strings
|
|
|
|
"""
|
2017-09-10 19:16:02 +02:00
|
|
|
options, commands, envvars = self.parse_cmd_args()
|
|
|
|
self.init_logs(options, commands, envvars)
|
|
|
|
self.init_module_vars(options, commands, envvars)
|
2014-01-05 01:39:32 +01:00
|
|
|
|
2011-09-10 11:13:51 +02:00
|
|
|
def execute(self):
|
|
|
|
"""
|
|
|
|
See :py:func:`waflib.Context.Context.execute`
|
|
|
|
"""
|
|
|
|
super(OptionsContext, self).execute()
|
|
|
|
self.parse_args()
|
2016-02-28 00:12:50 +01:00
|
|
|
Utils.alloc_process_pool(options.jobs)
|
2017-09-10 19:16:02 +02:00
|
|
|
|