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