Options.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. # Scott Newton, 2005 (scottn)
  4. # Thomas Nagy, 2006-2018 (ita)
  5. """
  6. Support for waf command-line options
  7. Provides default and command-line options, as well the command
  8. that reads the ``options`` wscript function.
  9. """
  10. import os, tempfile, optparse, sys, re
  11. from waflib import Logs, Utils, Context, Errors
  12. options = optparse.Values()
  13. """
  14. A global dictionary representing user-provided command-line options::
  15. $ waf --foo=bar
  16. """
  17. commands = []
  18. """
  19. List of commands to execute extracted from the command-line. This list
  20. is consumed during the execution by :py:func:`waflib.Scripting.run_commands`.
  21. """
  22. envvars = []
  23. """
  24. List of environment variable declarations placed after the Waf executable name.
  25. These are detected by searching for "=" in the remaining arguments.
  26. You probably do not want to use this.
  27. """
  28. lockfile = os.environ.get('WAFLOCK', '.lock-waf_%s_build' % sys.platform)
  29. """
  30. Name of the lock file that marks a project as configured
  31. """
  32. class opt_parser(optparse.OptionParser):
  33. """
  34. Command-line options parser.
  35. """
  36. def __init__(self, ctx, allow_unknown=False):
  37. optparse.OptionParser.__init__(self, conflict_handler='resolve', add_help_option=False,
  38. version='waf %s (%s)' % (Context.WAFVERSION, Context.WAFREVISION))
  39. self.formatter.width = Logs.get_term_cols()
  40. self.ctx = ctx
  41. self.allow_unknown = allow_unknown
  42. def _process_args(self, largs, rargs, values):
  43. """
  44. Custom _process_args to allow unknown options according to the allow_unknown status
  45. """
  46. while rargs:
  47. try:
  48. optparse.OptionParser._process_args(self,largs,rargs,values)
  49. except (optparse.BadOptionError, optparse.AmbiguousOptionError) as e:
  50. if self.allow_unknown:
  51. largs.append(e.opt_str)
  52. else:
  53. self.error(str(e))
  54. def print_usage(self, file=None):
  55. return self.print_help(file)
  56. def get_usage(self):
  57. """
  58. Builds the message to print on ``waf --help``
  59. :rtype: string
  60. """
  61. cmds_str = {}
  62. for cls in Context.classes:
  63. if not cls.cmd or cls.cmd == 'options' or cls.cmd.startswith( '_' ):
  64. continue
  65. s = cls.__doc__ or ''
  66. cmds_str[cls.cmd] = s
  67. if Context.g_module:
  68. for (k, v) in Context.g_module.__dict__.items():
  69. if k in ('options', 'init', 'shutdown'):
  70. continue
  71. if type(v) is type(Context.create_context):
  72. if v.__doc__ and not k.startswith('_'):
  73. cmds_str[k] = v.__doc__
  74. just = 0
  75. for k in cmds_str:
  76. just = max(just, len(k))
  77. lst = [' %s: %s' % (k.ljust(just), v) for (k, v) in cmds_str.items()]
  78. lst.sort()
  79. ret = '\n'.join(lst)
  80. return '''waf [commands] [options]
  81. Main commands (example: ./waf build -j4)
  82. %s
  83. ''' % ret
  84. class OptionsContext(Context.Context):
  85. """
  86. Collects custom options from wscript files and parses the command line.
  87. Sets the global :py:const:`waflib.Options.commands` and :py:const:`waflib.Options.options` values.
  88. """
  89. cmd = 'options'
  90. fun = 'options'
  91. def __init__(self, **kw):
  92. super(OptionsContext, self).__init__(**kw)
  93. self.parser = opt_parser(self)
  94. """Instance of :py:class:`waflib.Options.opt_parser`"""
  95. self.option_groups = {}
  96. jobs = self.jobs()
  97. p = self.add_option
  98. color = os.environ.get('NOCOLOR', '') and 'no' or 'auto'
  99. if os.environ.get('CLICOLOR', '') == '0':
  100. color = 'no'
  101. elif os.environ.get('CLICOLOR_FORCE', '') == '1':
  102. color = 'yes'
  103. p('-c', '--color', dest='colors', default=color, action='store', help='whether to use colors (yes/no/auto) [default: auto]', choices=('yes', 'no', 'auto'))
  104. p('-j', '--jobs', dest='jobs', default=jobs, type='int', help='amount of parallel jobs (%r)' % jobs)
  105. p('-k', '--keep', dest='keep', default=0, action='count', help='continue despite errors (-kk to try harder)')
  106. p('-v', '--verbose', dest='verbose', default=0, action='count', help='verbosity level -v -vv or -vvv [default: 0]')
  107. p('--zones', dest='zones', default='', action='store', help='debugging zones (task_gen, deps, tasks, etc)')
  108. p('--profile', dest='profile', default=0, action='store_true', help=optparse.SUPPRESS_HELP)
  109. p('--pdb', dest='pdb', default=0, action='store_true', help=optparse.SUPPRESS_HELP)
  110. p('-h', '--help', dest='whelp', default=0, action='store_true', help="show this help message and exit")
  111. gr = self.add_option_group('Configuration options')
  112. self.option_groups['configure options'] = gr
  113. gr.add_option('-o', '--out', action='store', default='', help='build dir for the project', dest='out')
  114. gr.add_option('-t', '--top', action='store', default='', help='src dir for the project', dest='top')
  115. gr.add_option('--no-lock-in-run', action='store_true', default='', help=optparse.SUPPRESS_HELP, dest='no_lock_in_run')
  116. gr.add_option('--no-lock-in-out', action='store_true', default='', help=optparse.SUPPRESS_HELP, dest='no_lock_in_out')
  117. gr.add_option('--no-lock-in-top', action='store_true', default='', help=optparse.SUPPRESS_HELP, dest='no_lock_in_top')
  118. default_prefix = getattr(Context.g_module, 'default_prefix', os.environ.get('PREFIX'))
  119. if not default_prefix:
  120. if Utils.unversioned_sys_platform() == 'win32':
  121. d = tempfile.gettempdir()
  122. default_prefix = d[0].upper() + d[1:]
  123. # win32 preserves the case, but gettempdir does not
  124. else:
  125. default_prefix = '/usr/local/'
  126. gr.add_option('--prefix', dest='prefix', default=default_prefix, help='installation prefix [default: %r]' % default_prefix)
  127. gr.add_option('--bindir', dest='bindir', help='bindir')
  128. gr.add_option('--libdir', dest='libdir', help='libdir')
  129. gr = self.add_option_group('Build and installation options')
  130. self.option_groups['build and install options'] = gr
  131. gr.add_option('-p', '--progress', dest='progress_bar', default=0, action='count', help= '-p: progress bar; -pp: ide output')
  132. gr.add_option('--targets', dest='targets', default='', action='store', help='task generators, e.g. "target1,target2"')
  133. gr = self.add_option_group('Step options')
  134. self.option_groups['step options'] = gr
  135. gr.add_option('--files', dest='files', default='', action='store', help='files to process, by regexp, e.g. "*/main.c,*/test/main.o"')
  136. default_destdir = os.environ.get('DESTDIR', '')
  137. gr = self.add_option_group('Installation and uninstallation options')
  138. self.option_groups['install/uninstall options'] = gr
  139. gr.add_option('--destdir', help='installation root [default: %r]' % default_destdir, default=default_destdir, dest='destdir')
  140. gr.add_option('-f', '--force', dest='force', default=False, action='store_true', help='force file installation')
  141. gr.add_option('--distcheck-args', metavar='ARGS', help='arguments to pass to distcheck', default=None, action='store')
  142. def jobs(self):
  143. """
  144. Finds the optimal amount of cpu cores to use for parallel jobs.
  145. At runtime the options can be obtained from :py:const:`waflib.Options.options` ::
  146. from waflib.Options import options
  147. njobs = options.jobs
  148. :return: the amount of cpu cores
  149. :rtype: int
  150. """
  151. count = int(os.environ.get('JOBS', 0))
  152. if count < 1:
  153. if 'NUMBER_OF_PROCESSORS' in os.environ:
  154. # on Windows, use the NUMBER_OF_PROCESSORS environment variable
  155. count = int(os.environ.get('NUMBER_OF_PROCESSORS', 1))
  156. else:
  157. # on everything else, first try the POSIX sysconf values
  158. if hasattr(os, 'sysconf_names'):
  159. if 'SC_NPROCESSORS_ONLN' in os.sysconf_names:
  160. count = int(os.sysconf('SC_NPROCESSORS_ONLN'))
  161. elif 'SC_NPROCESSORS_CONF' in os.sysconf_names:
  162. count = int(os.sysconf('SC_NPROCESSORS_CONF'))
  163. if not count and os.name not in ('nt', 'java'):
  164. try:
  165. tmp = self.cmd_and_log(['sysctl', '-n', 'hw.ncpu'], quiet=0)
  166. except Errors.WafError:
  167. pass
  168. else:
  169. if re.match('^[0-9]+$', tmp):
  170. count = int(tmp)
  171. if count < 1:
  172. count = 1
  173. elif count > 1024:
  174. count = 1024
  175. return count
  176. def add_option(self, *k, **kw):
  177. """
  178. Wraps ``optparse.add_option``::
  179. def options(ctx):
  180. ctx.add_option('-u', '--use', dest='use', default=False,
  181. action='store_true', help='a boolean option')
  182. :rtype: optparse option object
  183. """
  184. return self.parser.add_option(*k, **kw)
  185. def add_option_group(self, *k, **kw):
  186. """
  187. Wraps ``optparse.add_option_group``::
  188. def options(ctx):
  189. gr = ctx.add_option_group('some options')
  190. gr.add_option('-u', '--use', dest='use', default=False, action='store_true')
  191. :rtype: optparse option group object
  192. """
  193. try:
  194. gr = self.option_groups[k[0]]
  195. except KeyError:
  196. gr = self.parser.add_option_group(*k, **kw)
  197. self.option_groups[k[0]] = gr
  198. return gr
  199. def get_option_group(self, opt_str):
  200. """
  201. Wraps ``optparse.get_option_group``::
  202. def options(ctx):
  203. gr = ctx.get_option_group('configure options')
  204. gr.add_option('-o', '--out', action='store', default='',
  205. help='build dir for the project', dest='out')
  206. :rtype: optparse option group object
  207. """
  208. try:
  209. return self.option_groups[opt_str]
  210. except KeyError:
  211. for group in self.parser.option_groups:
  212. if group.title == opt_str:
  213. return group
  214. return None
  215. def sanitize_path(self, path, cwd=None):
  216. if not cwd:
  217. cwd = Context.launch_dir
  218. p = os.path.expanduser(path)
  219. p = os.path.join(cwd, p)
  220. p = os.path.normpath(p)
  221. p = os.path.abspath(p)
  222. return p
  223. def parse_cmd_args(self, _args=None, cwd=None, allow_unknown=False):
  224. """
  225. Just parse the arguments
  226. """
  227. self.parser.allow_unknown = allow_unknown
  228. (options, leftover_args) = self.parser.parse_args(args=_args)
  229. envvars = []
  230. commands = []
  231. for arg in leftover_args:
  232. if '=' in arg:
  233. envvars.append(arg)
  234. elif arg != 'options':
  235. commands.append(arg)
  236. for name in 'top out destdir prefix bindir libdir'.split():
  237. # those paths are usually expanded from Context.launch_dir
  238. if getattr(options, name, None):
  239. path = self.sanitize_path(getattr(options, name), cwd)
  240. setattr(options, name, path)
  241. return options, commands, envvars
  242. def init_module_vars(self, arg_options, arg_commands, arg_envvars):
  243. options.__dict__.clear()
  244. del commands[:]
  245. del envvars[:]
  246. options.__dict__.update(arg_options.__dict__)
  247. commands.extend(arg_commands)
  248. envvars.extend(arg_envvars)
  249. for var in envvars:
  250. (name, value) = var.split('=', 1)
  251. os.environ[name.strip()] = value
  252. def init_logs(self, options, commands, envvars):
  253. Logs.verbose = options.verbose
  254. if options.verbose >= 1:
  255. self.load('errcheck')
  256. colors = {'yes' : 2, 'auto' : 1, 'no' : 0}[options.colors]
  257. Logs.enable_colors(colors)
  258. if options.zones:
  259. Logs.zones = options.zones.split(',')
  260. if not Logs.verbose:
  261. Logs.verbose = 1
  262. elif Logs.verbose > 0:
  263. Logs.zones = ['runner']
  264. if Logs.verbose > 2:
  265. Logs.zones = ['*']
  266. def parse_args(self, _args=None):
  267. """
  268. Parses arguments from a list which is not necessarily the command-line.
  269. Initializes the module variables options, commands and envvars
  270. If help is requested, prints it and exit the application
  271. :param _args: arguments
  272. :type _args: list of strings
  273. """
  274. options, commands, envvars = self.parse_cmd_args()
  275. self.init_logs(options, commands, envvars)
  276. self.init_module_vars(options, commands, envvars)
  277. def execute(self):
  278. """
  279. See :py:func:`waflib.Context.Context.execute`
  280. """
  281. super(OptionsContext, self).execute()
  282. self.parse_args()
  283. Utils.alloc_process_pool(options.jobs)