CmdLine.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #
  2. # Cython - Command Line Parsing
  3. #
  4. from __future__ import absolute_import
  5. import os
  6. import sys
  7. from . import Options
  8. usage = """\
  9. Cython (http://cython.org) is a compiler for code written in the
  10. Cython language. Cython is based on Pyrex by Greg Ewing.
  11. Usage: cython [options] sourcefile.{pyx,py} ...
  12. Options:
  13. -V, --version Display version number of cython compiler
  14. -l, --create-listing Write error messages to a listing file
  15. -I, --include-dir <directory> Search for include files in named directory
  16. (multiple include directories are allowed).
  17. -o, --output-file <filename> Specify name of generated C file
  18. -t, --timestamps Only compile newer source files
  19. -f, --force Compile all source files (overrides implied -t)
  20. -v, --verbose Be verbose, print file names on multiple compilation
  21. -p, --embed-positions If specified, the positions in Cython files of each
  22. function definition is embedded in its docstring.
  23. --cleanup <level> Release interned objects on python exit, for memory debugging.
  24. Level indicates aggressiveness, default 0 releases nothing.
  25. -w, --working <directory> Sets the working directory for Cython (the directory modules
  26. are searched from)
  27. --gdb Output debug information for cygdb
  28. --gdb-outdir <directory> Specify gdb debug information output directory. Implies --gdb.
  29. -D, --no-docstrings Strip docstrings from the compiled module.
  30. -a, --annotate Produce a colorized HTML version of the source.
  31. --annotate-coverage <cov.xml> Annotate and include coverage information from cov.xml.
  32. --line-directives Produce #line directives pointing to the .pyx source
  33. --cplus Output a C++ rather than C file.
  34. --embed[=<method_name>] Generate a main() function that embeds the Python interpreter.
  35. -2 Compile based on Python-2 syntax and code semantics.
  36. -3 Compile based on Python-3 syntax and code semantics.
  37. --lenient Change some compile time errors to runtime errors to
  38. improve Python compatibility
  39. --capi-reexport-cincludes Add cincluded headers to any auto-generated header files.
  40. --fast-fail Abort the compilation on the first error
  41. --warning-errors, -Werror Make all warnings into errors
  42. --warning-extra, -Wextra Enable extra warnings
  43. -X, --directive <name>=<value>[,<name=value,...] Overrides a compiler directive
  44. """
  45. #The following experimental options are supported only on MacOSX:
  46. # -C, --compile Compile generated .c file to .o file
  47. # --link Link .o file to produce extension module (implies -C)
  48. # -+, --cplus Use C++ compiler for compiling and linking
  49. # Additional .o files to link may be supplied when using -X."""
  50. def bad_usage():
  51. sys.stderr.write(usage)
  52. sys.exit(1)
  53. def parse_command_line(args):
  54. from .Main import CompilationOptions, default_options
  55. pending_arg = []
  56. def pop_arg():
  57. if not args or pending_arg:
  58. bad_usage()
  59. if '=' in args[0] and args[0].startswith('--'): # allow "--long-option=xyz"
  60. name, value = args.pop(0).split('=', 1)
  61. pending_arg.append(value)
  62. return name
  63. return args.pop(0)
  64. def pop_value(default=None):
  65. if pending_arg:
  66. return pending_arg.pop()
  67. elif default is not None:
  68. return default
  69. elif not args:
  70. bad_usage()
  71. return args.pop(0)
  72. def get_param(option):
  73. tail = option[2:]
  74. if tail:
  75. return tail
  76. else:
  77. return pop_arg()
  78. options = CompilationOptions(default_options)
  79. sources = []
  80. while args:
  81. if args[0].startswith("-"):
  82. option = pop_arg()
  83. if option in ("-V", "--version"):
  84. options.show_version = 1
  85. elif option in ("-l", "--create-listing"):
  86. options.use_listing_file = 1
  87. elif option in ("-+", "--cplus"):
  88. options.cplus = 1
  89. elif option == "--embed":
  90. Options.embed = pop_value("main")
  91. elif option.startswith("-I"):
  92. options.include_path.append(get_param(option))
  93. elif option == "--include-dir":
  94. options.include_path.append(pop_value())
  95. elif option in ("-w", "--working"):
  96. options.working_path = pop_value()
  97. elif option in ("-o", "--output-file"):
  98. options.output_file = pop_value()
  99. elif option in ("-t", "--timestamps"):
  100. options.timestamps = 1
  101. elif option in ("-f", "--force"):
  102. options.timestamps = 0
  103. elif option in ("-v", "--verbose"):
  104. options.verbose += 1
  105. elif option in ("-p", "--embed-positions"):
  106. Options.embed_pos_in_docstring = 1
  107. elif option in ("-z", "--pre-import"):
  108. Options.pre_import = pop_value()
  109. elif option == "--cleanup":
  110. Options.generate_cleanup_code = int(pop_value())
  111. elif option in ("-D", "--no-docstrings"):
  112. Options.docstrings = False
  113. elif option in ("-a", "--annotate"):
  114. Options.annotate = True
  115. elif option == "--annotate-coverage":
  116. Options.annotate = True
  117. Options.annotate_coverage_xml = pop_value()
  118. elif option == "--convert-range":
  119. Options.convert_range = True
  120. elif option == "--line-directives":
  121. options.emit_linenums = True
  122. elif option == "--no-c-in-traceback":
  123. options.c_line_in_traceback = False
  124. elif option == "--gdb":
  125. options.gdb_debug = True
  126. options.output_dir = os.curdir
  127. elif option == "--gdb-outdir":
  128. options.gdb_debug = True
  129. options.output_dir = pop_value()
  130. elif option == "--lenient":
  131. Options.error_on_unknown_names = False
  132. Options.error_on_uninitialized = False
  133. elif option == '-2':
  134. options.language_level = 2
  135. elif option == '-3':
  136. options.language_level = 3
  137. elif option == "--capi-reexport-cincludes":
  138. options.capi_reexport_cincludes = True
  139. elif option == "--fast-fail":
  140. Options.fast_fail = True
  141. elif option == "--cimport-from-pyx":
  142. Options.cimport_from_pyx = True
  143. elif option in ('-Werror', '--warning-errors'):
  144. Options.warning_errors = True
  145. elif option in ('-Wextra', '--warning-extra'):
  146. options.compiler_directives.update(Options.extra_warnings)
  147. elif option == "--old-style-globals":
  148. Options.old_style_globals = True
  149. elif option == "--directive" or option.startswith('-X'):
  150. if option.startswith('-X') and option[2:].strip():
  151. x_args = option[2:]
  152. else:
  153. x_args = pop_value()
  154. try:
  155. options.compiler_directives = Options.parse_directive_list(
  156. x_args, relaxed_bool=True,
  157. current_settings=options.compiler_directives)
  158. except ValueError as e:
  159. sys.stderr.write("Error in compiler directive: %s\n" % e.args[0])
  160. sys.exit(1)
  161. elif option.startswith('--debug'):
  162. option = option[2:].replace('-', '_')
  163. from . import DebugFlags
  164. if option in dir(DebugFlags):
  165. setattr(DebugFlags, option, True)
  166. else:
  167. sys.stderr.write("Unknown debug flag: %s\n" % option)
  168. bad_usage()
  169. elif option in ('-h', '--help'):
  170. sys.stdout.write(usage)
  171. sys.exit(0)
  172. else:
  173. sys.stderr.write("Unknown compiler flag: %s\n" % option)
  174. sys.exit(1)
  175. else:
  176. sources.append(pop_arg())
  177. if pending_arg:
  178. bad_usage()
  179. if options.use_listing_file and len(sources) > 1:
  180. sys.stderr.write(
  181. "cython: Only one source file allowed when using -o\n")
  182. sys.exit(1)
  183. if len(sources) == 0 and not options.show_version:
  184. bad_usage()
  185. if Options.embed and len(sources) > 1:
  186. sys.stderr.write(
  187. "cython: Only one source file allowed when using -embed\n")
  188. sys.exit(1)
  189. return options, sources