xref: /libCEED/python/build_ceed_cffi.py (revision 80a9ef0545a39c00cdcaab1ca26f8053604f3120)
1# Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
2# the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
3# reserved. See files LICENSE and NOTICE for details.
4#
5# This file is part of CEED, a collection of benchmarks, miniapps, software
6# libraries and APIs for efficient high-order finite element and spectral
7# element discretizations for exascale applications. For more information and
8# source code availability see http://github.com/ceed.
9#
10# The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11# a collaborative effort of two U.S. Department of Energy organizations (Office
12# of Science and the National Nuclear Security Administration) responsible for
13# the planning and preparation of a capable exascale ecosystem, including
14# software, applications, hardware, advanced system engineering and early
15# testbed platforms, in support of the nation's exascale computing imperative.
16
17import os
18import re
19from cffi import FFI
20ffibuilder = FFI()
21
22ceed_version_ge = re.compile(r'\s+\(!?CEED_VERSION.*')
23
24# ------------------------------------------------------------------------------
25# Provide C definitions to CFFI
26# ------------------------------------------------------------------------------
27with open(os.path.abspath("include/ceed/ceed.h")) as f:
28    lines = [line.strip() for line in f if
29             not (line.startswith("#") and not line.startswith("#include")) and
30             not line.startswith("  static") and
31             "CeedErrorImpl" not in line and
32             "const char *, ...);" not in line and
33             not line.startswith("CEED_EXTERN const char *const") and
34             not ceed_version_ge.match(line)]
35    lines = [line.replace("CEED_EXTERN", "extern") for line in lines]
36    # Find scalar type inclusion line and insert definitions
37    for line in lines:
38        if re.search("ceed-f32.h", line) is not None:
39            insert_index = lines.index(line) + 1
40            extra_lines = ['typedef float CeedScalar;']
41            extra_lines.append('static const int CEED_SCALAR_TYPE;')
42            extra_lines.append('static const double CEED_EPSILON;')
43        elif re.search("ceed-f64.h", line) is not None:
44            insert_index = lines.index(line) + 1
45            extra_lines = ['typedef double CeedScalar;']
46            extra_lines.append('static const int CEED_SCALAR_TYPE;')
47            extra_lines.append('static const double CEED_EPSILON;')
48    lines[insert_index: insert_index] = extra_lines
49    # Remove all include statements now that scalar type has been dealt with
50    lines = [line for line in lines if not line.startswith("#include")]
51    # Build header from lines
52    header = '\n'.join(lines)
53    header = header.split("static inline CeedInt CeedIntPow", 1)[0]
54    header += '\nextern int CeedVectorGetState(CeedVector, uint64_t*);'
55    header += '\nextern int CeedElemRestrictionGetELayout(CeedElemRestriction, CeedInt *layout);'
56    # Note: cffi cannot handle vargs
57    header = re.sub("va_list", "const char *", header)
58ffibuilder.cdef(header)
59
60ffibuilder.set_source("_ceed_cffi",
61                      """
62  #define va_list const char *
63  #include <ceed/ceed.h>   // the C header of the library
64  """,
65                      include_dirs=[
66                          os.path.abspath("include")],  # include path
67                      libraries=["ceed"],   # library name, for the linker
68                      library_dirs=['./lib'],  # library path, for the linker
69                      # use libceed.so as installed
70                      runtime_library_dirs=['$ORIGIN/libceed/lib']
71                      )
72
73# ------------------------------------------------------------------------------
74# Builder
75# ------------------------------------------------------------------------------
76if __name__ == "__main__":
77    ffibuilder.compile(verbose=True)
78
79# ------------------------------------------------------------------------------
80