Commit 4b83dc91 authored by Mark Peek's avatar Mark Peek

Allow conditional compilation environment in distutils

This commit allows you to pass in variables for the conditional compilation
feature from distutils. This allows you to do fairly complex autoconf-type
checks from setup.py and pass the results in to the compiler.

This is an update to the change in ticket #323 here:
http://trac.cython.org/cython_trac/ticket/323
parent 262bc9fb
...@@ -686,4 +686,5 @@ default_options = dict( ...@@ -686,4 +686,5 @@ default_options = dict(
c_line_in_traceback = True, c_line_in_traceback = True,
language_level = 2, language_level = 2,
gdb_debug = False, gdb_debug = False,
compile_time_env = None,
) )
...@@ -67,6 +67,9 @@ class CompileTimeScope(object): ...@@ -67,6 +67,9 @@ class CompileTimeScope(object):
def declare(self, name, value): def declare(self, name, value):
self.entries[name] = value self.entries[name] = value
def update(self, other):
self.entries.update(other)
def lookup_here(self, name): def lookup_here(self, name):
return self.entries[name] return self.entries[name]
...@@ -286,6 +289,9 @@ class PyrexScanner(Scanner): ...@@ -286,6 +289,9 @@ class PyrexScanner(Scanner):
self.compile_time_env = initial_compile_time_env() self.compile_time_env = initial_compile_time_env()
self.compile_time_eval = 1 self.compile_time_eval = 1
self.compile_time_expr = 0 self.compile_time_expr = 0
if hasattr(context.options, 'compile_time_env') and \
context.options.compile_time_env is not None:
self.compile_time_env.update(context.options.compile_time_env)
self.parse_comments = parse_comments self.parse_comments = parse_comments
self.source_encoding = source_encoding self.source_encoding = source_encoding
if filename.is_python_file(): if filename.is_python_file():
......
...@@ -83,6 +83,8 @@ class build_ext(_build_ext.build_ext): ...@@ -83,6 +83,8 @@ class build_ext(_build_ext.build_ext):
"compiler directive overrides"), "compiler directive overrides"),
('pyrex-gdb', None, ('pyrex-gdb', None,
"generate debug information for cygdb"), "generate debug information for cygdb"),
('pyrex-compile-time-env', None,
"pyrex compile time environment"),
]) ])
boolean_options.extend([ boolean_options.extend([
...@@ -101,6 +103,7 @@ class build_ext(_build_ext.build_ext): ...@@ -101,6 +103,7 @@ class build_ext(_build_ext.build_ext):
self.pyrex_gen_pxi = 0 self.pyrex_gen_pxi = 0
self.pyrex_gdb = False self.pyrex_gdb = False
self.no_c_in_traceback = 0 self.no_c_in_traceback = 0
self.pyrex_compile_time_env = None
def finalize_options (self): def finalize_options (self):
_build_ext.build_ext.finalize_options(self) _build_ext.build_ext.finalize_options(self)
...@@ -182,6 +185,9 @@ class build_ext(_build_ext.build_ext): ...@@ -182,6 +185,9 @@ class build_ext(_build_ext.build_ext):
(extension.language and extension.language.lower() == 'c++') (extension.language and extension.language.lower() == 'c++')
pyrex_gen_pxi = self.pyrex_gen_pxi or getattr(extension, 'pyrex_gen_pxi', 0) pyrex_gen_pxi = self.pyrex_gen_pxi or getattr(extension, 'pyrex_gen_pxi', 0)
pyrex_gdb = self.pyrex_gdb or getattr(extension, 'pyrex_gdb', False) pyrex_gdb = self.pyrex_gdb or getattr(extension, 'pyrex_gdb', False)
pyrex_compile_time_env = self.pyrex_compile_time_env or \
getattr(extension, 'pyrex_compile_time_env', None)
# Set up the include_path for the Cython compiler: # Set up the include_path for the Cython compiler:
# 1. Start with the command line option. # 1. Start with the command line option.
# 2. Add in any (unique) paths from the extension # 2. Add in any (unique) paths from the extension
...@@ -270,7 +276,8 @@ class build_ext(_build_ext.build_ext): ...@@ -270,7 +276,8 @@ class build_ext(_build_ext.build_ext):
c_line_in_traceback = not no_c_in_traceback, c_line_in_traceback = not no_c_in_traceback,
generate_pxi = pyrex_gen_pxi, generate_pxi = pyrex_gen_pxi,
output_dir = output_dir, output_dir = output_dir,
gdb_debug = pyrex_gdb) gdb_debug = pyrex_gdb,
compile_time_env = pyrex_compile_time_env)
result = cython_compile(source, options=options, result = cython_compile(source, options=options,
full_module_name=module_name) full_module_name=module_name)
else: else:
......
...@@ -63,6 +63,7 @@ class Extension(_Extension.Extension): ...@@ -63,6 +63,7 @@ class Extension(_Extension.Extension):
pyrex_gen_pxi = 0, pyrex_gen_pxi = 0,
pyrex_gdb = False, pyrex_gdb = False,
no_c_in_traceback = False, no_c_in_traceback = False,
pyrex_compile_time_env = None,
**kw): **kw):
_Extension.Extension.__init__(self, name, sources, _Extension.Extension.__init__(self, name, sources,
...@@ -90,6 +91,7 @@ class Extension(_Extension.Extension): ...@@ -90,6 +91,7 @@ class Extension(_Extension.Extension):
self.pyrex_gen_pxi = pyrex_gen_pxi self.pyrex_gen_pxi = pyrex_gen_pxi
self.pyrex_gdb = pyrex_gdb self.pyrex_gdb = pyrex_gdb
self.no_c_in_traceback = no_c_in_traceback self.no_c_in_traceback = no_c_in_traceback
self.pyrex_compile_time_env = pyrex_compile_time_env
# class Extension # class Extension
......
PYTHON setup.py build_ext --inplace
PYTHON -c "import a; import sys; sys.exit(a.compile_env_test())"
######## setup.py ########
from distutils.core import setup
from Cython.Distutils.extension import Extension
from Cython.Distutils import build_ext
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension(
"a", ["a.pyx"],
pyrex_compile_time_env = {'TEST': True},
)],
)
######## a.pyx ########
def compile_env_test():
IF TEST:
return 0
ELSE:
return 1
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment