xref: /petsc/src/binding/petsc4py/conf/cythonize.py (revision 6dd63270497ad23dcf16ae500a87ff2b2a0b7474)
1#!/usr/bin/env python3
2"""Run Cython with custom options."""
3
4import os
5import sys
6
7appdir = os.path.dirname(os.path.abspath(__file__))
8sys.path.insert(0, appdir)
9
10import cyautodoc  # noqa: F401,E402
11from Cython.Compiler.Main import main as cython_main  # noqa: E402
12
13
14def cythonize(args=None):
15    """Run `cython --3str --cleanup 3 <args>...`."""
16    if args is None:
17        argv = sys.argv[:]
18    else:
19        argv = [os.path.abspath(__file__)] + list(args)
20
21    if '--cleanup' not in argv:
22        argv[1:1] = ['--cleanup', '3']
23    if '--3str' not in argv:
24        argv[1:1] = ['--3str']
25
26    cwd = os.getcwd()
27    sys_argv = sys.argv[:]
28    try:
29        sys.argv[:] = argv
30        cython_main(command_line=1)
31        return 0
32    except SystemExit as exc:
33        return exc.code
34    finally:
35        os.chdir(cwd)
36        sys.argv[:] = sys_argv
37
38
39def main():
40    """Entry-point to run Cython with custom options."""
41    args = sys.argv[1:]
42    if not args:
43        topdir = os.path.dirname(appdir)
44        srcdir = os.path.join(topdir, 'src')
45        source = os.path.join('petsc4py', 'PETSc.pyx')
46        target = os.path.join('petsc4py', 'PETSc.c')
47        args += ['--working', srcdir]
48        args += [source, '--output-file', target]
49    sys.exit(cythonize(args))
50
51
52if __name__ == '__main__':
53    main()
54