| 36 | | from setuptools import setup, Extension, Feature |
|---|
| | 34 | from distutils.core import setup, Command |
|---|
| | 35 | from distutils.core import Distribution as _Distribution |
|---|
| | 36 | from distutils.core import Extension as _Extension |
|---|
| | 37 | from distutils.command.build_ext import build_ext as _build_ext |
|---|
| | 38 | |
|---|
| | 39 | try: |
|---|
| | 40 | from Pyrex.Distutils import Extension as _Extension |
|---|
| | 41 | from Pyrex.Distutils import build_ext as _build_ext |
|---|
| | 42 | with_pyrex = True |
|---|
| | 43 | except ImportError: |
|---|
| | 44 | with_pyrex = False |
|---|
| | 45 | |
|---|
| | 46 | import sys, os.path |
|---|
| | 47 | |
|---|
| | 48 | |
|---|
| | 49 | class Distribution(_Distribution): |
|---|
| | 50 | |
|---|
| | 51 | def __init__(self, attrs=None): |
|---|
| | 52 | _Distribution.__init__(self, attrs) |
|---|
| | 53 | if not self.ext_modules: |
|---|
| | 54 | return |
|---|
| | 55 | for ext in reversed(self.ext_modules): |
|---|
| | 56 | if not isinstance(ext, Extension): |
|---|
| | 57 | continue |
|---|
| | 58 | setattr(self, ext.attr_name, None) |
|---|
| | 59 | self.global_options = [ |
|---|
| | 60 | (ext.option_name, None, |
|---|
| | 61 | "include %s" % ext.feature_description), |
|---|
| | 62 | (ext.neg_option_name, None, |
|---|
| | 63 | "exclude %s (default)" % ext.feature_description), |
|---|
| | 64 | ] + self.global_options |
|---|
| | 65 | self.negative_opt = self.negative_opt.copy() |
|---|
| | 66 | self.negative_opt[ext.neg_option_name] = ext.option_name |
|---|
| | 67 | |
|---|
| | 68 | |
|---|
| | 69 | class Extension(_Extension): |
|---|
| | 70 | |
|---|
| | 71 | def __init__(self, name, sources, feature_name, feature_description, **kwds): |
|---|
| | 72 | if not with_pyrex: |
|---|
| | 73 | for filename in sources[:]: |
|---|
| | 74 | base, ext = os.path.splitext(filename) |
|---|
| | 75 | if ext == 'pyx': |
|---|
| | 76 | sources.replace(filename, '%s.c' % base) |
|---|
| | 77 | _Extension.__init__(self, name, sources, **kwds) |
|---|
| | 78 | self.feature_name = feature_name |
|---|
| | 79 | self.feature_description = feature_description |
|---|
| | 80 | self.attr_name = 'with_' + feature_name.replace('-', '_') |
|---|
| | 81 | self.option_name = 'with-' + feature_name |
|---|
| | 82 | self.neg_option_name = 'without-' + feature_name |
|---|
| | 83 | |
|---|
| | 84 | |
|---|
| | 85 | class build_ext(_build_ext): |
|---|
| | 86 | |
|---|
| | 87 | def get_source_files(self): |
|---|
| | 88 | self.check_extensions_list(self.extensions) |
|---|
| | 89 | filenames = [] |
|---|
| | 90 | for ext in self.extensions: |
|---|
| | 91 | if with_pyrex: |
|---|
| | 92 | self.pyrex_sources(ext.sources, ext) |
|---|
| | 93 | for filename in ext.sources: |
|---|
| | 94 | filenames.append(filename) |
|---|
| | 95 | base = os.path.splitext(filename)[0] |
|---|
| | 96 | for ext in ['c', 'h', 'pyx', 'pxd']: |
|---|
| | 97 | filename = '%s.%s' % (base, ext) |
|---|
| | 98 | if filename not in filenames and os.path.isfile(filename): |
|---|
| | 99 | filenames.append(filename) |
|---|
| | 100 | return filenames |
|---|
| | 101 | |
|---|
| | 102 | def build_extensions(self): |
|---|
| | 103 | self.check_extensions_list(self.extensions) |
|---|
| | 104 | for ext in self.extensions: |
|---|
| | 105 | if isinstance(ext, Extension): |
|---|
| | 106 | if not getattr(self.distribution, ext.attr_name): |
|---|
| | 107 | continue |
|---|
| | 108 | if with_pyrex: |
|---|
| | 109 | ext.sources = self.pyrex_sources(ext.sources, ext) |
|---|
| | 110 | self.build_extension(ext) |
|---|
| | 111 | |
|---|
| | 112 | |
|---|
| | 113 | class test(Command): |
|---|
| | 114 | |
|---|
| | 115 | user_options = [] |
|---|
| | 116 | |
|---|
| | 117 | def initialize_options(self): |
|---|
| | 118 | pass |
|---|
| | 119 | |
|---|
| | 120 | def finalize_options(self): |
|---|
| | 121 | pass |
|---|
| | 122 | |
|---|
| | 123 | def run(self): |
|---|
| | 124 | build_cmd = self.get_finalized_command('build') |
|---|
| | 125 | build_cmd.run() |
|---|
| | 126 | sys.path.insert(0, build_cmd.build_lib) |
|---|
| | 127 | sys.path.insert(0, 'tests') |
|---|
| | 128 | import test_all |
|---|
| | 129 | test_all.main() |
|---|
| | 130 | |
|---|