1 """Build a Pyrex file from .pyx source to .so loadable module using
2 the installed distutils infrastructure. Call:
4 out_fname = pyx_to_dll("foo.pyx")
10 from distutils.dist import Distribution
11 from distutils.errors import DistutilsArgError, DistutilsError, CCompilerError
12 from distutils.extension import Extension
13 from distutils.util import grok_environment_error
15 from Cython.Distutils import build_ext
22 def pyx_to_dll(filename, ext = None, force_rebuild = 0,
23 build_in_temp=False, pyxbuild_dir=None):
24 """Compile a PYX file to a DLL and return the name of the generated .so
26 assert os.path.exists(filename), "Could not find %s" % os.path.abspath(filename)
28 path, name = os.path.split(filename)
31 modname, extension = os.path.splitext(name)
32 assert extension in (".pyx", ".py"), extension
34 filename = filename[:-len(extension)] + '.c'
35 ext = Extension(name=modname, sources=[filename])
38 pyxbuild_dir = os.path.join(path, "_pyxbld")
44 args = [quiet, "build_ext"]
46 args.append("--force")
47 if HAS_CYTHON and build_in_temp:
48 args.append("--pyrex-c-in-temp")
49 dist = Distribution({"script_name": None, "script_args": args})
50 if not dist.ext_modules:
52 dist.ext_modules.append(ext)
54 dist.cmdclass = {'build_ext': build_ext}
55 build = dist.get_command_obj('build')
56 build.build_base = pyxbuild_dir
58 config_files = dist.find_config_files()
59 try: config_files.remove('setup.cfg')
60 except ValueError: pass
61 dist.parse_config_files(config_files)
64 ok = dist.parse_command_line()
65 except DistutilsArgError:
69 print("options (after parsing command line):")
70 dist.dump_option_dicts()
76 return dist.get_command_obj("build_ext").get_outputs()[0]
77 except KeyboardInterrupt:
79 except (IOError, os.error):
80 exc = sys.exc_info()[1]
81 error = grok_environment_error(exc)
84 sys.stderr.write(error + "\n")
87 raise RuntimeError(error)
89 except (DistutilsError, CCompilerError):
93 exc = sys.exc_info()[1]
94 raise RuntimeError(repr(exc))
96 if __name__=="__main__":
97 pyx_to_dll("dummy.pyx")