Cython has moved to github.

cython

view Cython/Compiler/CmdLine.py @ 1149:7b8970eb4837

Option to emit #line directives, ticket #53
author Robert Bradshaw <robertwb@math.washington.edu>
date Sat Sep 13 12:48:45 2008 -0700 (3 years ago)
parents 2c9e037c8174
children a782d5979ad2
line source
1 #
2 # Cython - Command Line Parsing
3 #
5 import sys
6 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.
12 Usage: cython [options] sourcefile.pyx ...
14 Options:
15 -V, --version Display version number of cython compiler
16 -l, --create-listing Write error messages to a listing file
17 -I, --include-dir <directory> Search for include files in named directory
18 (multiply include directories are allowed).
19 -o, --output-file <filename> Specify name of generated C file
20 -r, --recursive Recursively find and compile dependencies
21 -t, --timestamps Only compile newer source files (implied with -r)
22 -f, --force Compile all source files (overrides implied -t)
23 -q, --quiet Don't print module names in recursive mode
24 -v, --verbose Be verbose, print file names on multiple compilation
25 -p, --embed-positions If specified, the positions in Cython files of each
26 function definition is embedded in its docstring.
27 -z, --pre-import <module> If specified, assume undeclared names in this
28 module. Emulates the behavior of putting
29 "from <module> import *" at the top of the file.
30 --incref-local-binop Force local an extra incref on local variables before
31 performing any binary operations.
32 --cleanup <level> Release interned objects on python exit, for memory debugging.
33 Level indicates aggressiveness, default 0 releases nothing.
34 -w, --working <directory> Sets the working directory for Cython (the directory modules
35 are searched from)
37 -D, --no-docstrings Remove docstrings.
38 -a, --annotate Produce a colorized HTML version of the source.
39 --line-directives Produce #line directives pointing to the .pyx source
40 --cplus Output a c++ rather than c file.
41 -X, --directive <name>=<value>[,<name=value,...] Overrides a compiler directive
42 """
43 #The following experimental options are supported only on MacOSX:
44 # -C, --compile Compile generated .c file to .o file
45 # -X, --link Link .o file to produce extension module (implies -C)
46 # -+, --cplus Use C++ compiler for compiling and linking
47 # Additional .o files to link may be supplied when using -X."""
49 def bad_usage():
50 sys.stderr.write(usage)
51 sys.exit(1)
53 def parse_command_line(args):
55 from Cython.Compiler.Main import \
56 CompilationOptions, default_options
58 def pop_arg():
59 if args:
60 return args.pop(0)
61 else:
62 bad_usage()
64 def get_param(option):
65 tail = option[2:]
66 if tail:
67 return tail
68 else:
69 return pop_arg()
71 options = CompilationOptions(default_options)
72 sources = []
73 while args:
74 if args[0].startswith("-"):
75 option = pop_arg()
76 if option in ("-V", "--version"):
77 options.show_version = 1
78 elif option in ("-l", "--create-listing"):
79 options.use_listing_file = 1
80 elif option in ("-C", "--compile"):
81 options.c_only = 0
82 elif option in ("-X", "--link"):
83 options.c_only = 0
84 options.obj_only = 0
85 elif option in ("-+", "--cplus"):
86 options.cplus = 1
87 elif option.startswith("-I"):
88 options.include_path.append(get_param(option))
89 elif option == "--include-dir":
90 options.include_path.append(pop_arg())
91 elif option in ("-w", "--working"):
92 options.working_path = pop_arg()
93 elif option in ("-o", "--output-file"):
94 options.output_file = pop_arg()
95 elif option in ("-r", "--recursive"):
96 options.recursive = 1
97 elif option in ("-t", "--timestamps"):
98 options.timestamps = 1
99 elif option in ("-f", "--force"):
100 options.timestamps = 0
101 elif option in ("-v", "--verbose"):
102 options.verbose += 1
103 elif option in ("-p", "--embed-positions"):
104 Options.embed_pos_in_docstring = 1
105 elif option in ("-z", "--pre-import"):
106 Options.pre_import = pop_arg()
107 elif option == "--incref-local-binop":
108 Options.incref_local_binop = 1
109 elif option == "--cleanup":
110 Options.generate_cleanup_code = int(pop_arg())
111 elif option in ("-D", "--no-docstrings"):
112 Options.docstrings = False
113 elif option in ("-a", "--annotate"):
114 Options.annotate = True
115 elif option == "--convert-range":
116 Options.convert_range = True
117 elif option == "--line-directives":
118 options.emit_linenums = True
119 elif option in ("-X", "--directive"):
120 try:
121 options.pragma_overrides = Options.parse_option_list(pop_arg())
122 except ValueError, e:
123 sys.stderr.write("Error in compiler directive: %s\n" % e.message)
124 sys.exit(1)
125 else:
126 bad_usage()
127 else:
128 arg = pop_arg()
129 if arg.endswith(".pyx"):
130 sources.append(arg)
131 elif arg.endswith(".py"):
132 # maybe do some other stuff, but this should work for now
133 sources.append(arg)
134 elif arg.endswith(".o"):
135 options.objects.append(arg)
136 else:
137 sys.stderr.write(
138 "cython: %s: Unknown filename suffix\n" % arg)
139 if options.objects and len(sources) > 1:
140 sys.stderr.write(
141 "cython: Only one source file allowed together with .o files\n")
142 if options.use_listing_file and len(sources) > 1:
143 sys.stderr.write(
144 "cython: Only one source file allowed when using -o\n")
145 sys.exit(1)
146 if len(sources) == 0 and not options.show_version:
147 bad_usage()
148 return options, sources