| 1 |
# The distutils.ccompiler module doesn't allow the runtime addition of compiler |
|---|
| 2 |
# classes that live outside the distutils package, and of course it doesn't |
|---|
| 3 |
# recognize the D language and its associated compilers by default. |
|---|
| 4 |
# |
|---|
| 5 |
# This module hot-patches distutils.ccompiler to overcome those limitations. |
|---|
| 6 |
# |
|---|
| 7 |
# To apply these changes, the setup.py file for extension modules written in D |
|---|
| 8 |
# should execute the following import statement: |
|---|
| 9 |
# from celerid import patch_distutils |
|---|
| 10 |
|
|---|
| 11 |
from distutils import ccompiler as cc |
|---|
| 12 |
from distutils.command import build |
|---|
| 13 |
|
|---|
| 14 |
from celerid import dcompiler |
|---|
| 15 |
|
|---|
| 16 |
cc.CCompiler.language_map['.d'] = 'd' |
|---|
| 17 |
cc.CCompiler.language_order.insert(0, 'd') |
|---|
| 18 |
|
|---|
| 19 |
cc.compiler_class['dmd'] = ('celerid.dcompiler', 'DMDDCompiler', 'Digital Mars D') |
|---|
| 20 |
cc.compiler_class['gdc'] = ('celerid.dcompiler', 'GDCDCompiler', 'GCC D Compiler') |
|---|
| 21 |
|
|---|
| 22 |
_old_new_compiler = cc.new_compiler |
|---|
| 23 |
|
|---|
| 24 |
def new_compiler(compiler=None, dry_run=0, force=0, **kwargs): |
|---|
| 25 |
if compiler is not None: |
|---|
| 26 |
compiler = compiler.lower() |
|---|
| 27 |
|
|---|
| 28 |
if compiler is None: |
|---|
| 29 |
if dcompiler._isPlatWin: |
|---|
| 30 |
compiler = 'dmd' |
|---|
| 31 |
else: |
|---|
| 32 |
compiler = 'gdc' |
|---|
| 33 |
|
|---|
| 34 |
if compiler not in ('dmd', 'gdc'): |
|---|
| 35 |
return _old_new_compiler(compiler=compiler, |
|---|
| 36 |
dry_run=dry_run, force=force, **kwargs |
|---|
| 37 |
) |
|---|
| 38 |
elif compiler == 'dmd': |
|---|
| 39 |
return dcompiler.DMDDCompiler(None, dry_run, force) |
|---|
| 40 |
elif compiler == 'gdc': |
|---|
| 41 |
return dcompiler.GDCDCompiler(None, dry_run, force) |
|---|
| 42 |
else: |
|---|
| 43 |
raise RuntimeError, "Couldn't get a compiler..." |
|---|
| 44 |
|
|---|
| 45 |
cc.new_compiler = new_compiler |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
# A user's setup.py wouldn't have imported this module unless it intended to |
|---|
| 49 |
# compile D code, so override the default compiler setting to point to DMD. |
|---|
| 50 |
# This allows a user to compile a D extension with the command line |
|---|
| 51 |
# python setup.py build |
|---|
| 52 |
# instead of needing |
|---|
| 53 |
# python setup.py build --compiler=dmd |
|---|
| 54 |
def get_default_compiler(*args, **kwargs): |
|---|
| 55 |
if dcompiler._isPlatWin: |
|---|
| 56 |
return 'dmd' |
|---|
| 57 |
else: |
|---|
| 58 |
return 'gdc' |
|---|
| 59 |
|
|---|
| 60 |
cc.get_default_compiler = get_default_compiler |
|---|
| 61 |
|
|---|
| 62 |
# Force the distutils build command to recognize the '--optimize' or '-O' |
|---|
| 63 |
# command-line option. |
|---|
| 64 |
build.build.user_options.append( |
|---|
| 65 |
('optimize', 'O', |
|---|
| 66 |
'Ask the D compiler to optimize the generated code, at the expense of' |
|---|
| 67 |
' safety features such as array bounds checks.'), |
|---|
| 68 |
) |
|---|
| 69 |
build.build.boolean_options.append('optimize') |
|---|
| 70 |
|
|---|
| 71 |
_old_initialize_options = build.build.initialize_options |
|---|
| 72 |
def _new_initialize_options(self): |
|---|
| 73 |
_old_initialize_options(self) |
|---|
| 74 |
self.optimize = 0 |
|---|
| 75 |
build.build.initialize_options = _new_initialize_options |
|---|