xref: /libCEED/python/build_ceed_cffi.py (revision 3d8e882215d238700cdceb37404f76ca7fa24eaa)
1# Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors
2# All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# This file is part of CEED:  http://github.com/ceed
7
8import os
9import re
10from cffi import FFI
11ffibuilder = FFI()
12
13ceed_version_ge = re.compile(r'\s+\(!?CEED_VERSION.*')
14
15# ------------------------------------------------------------------------------
16# Provide C definitions to CFFI
17# ------------------------------------------------------------------------------
18with open(os.path.abspath("include/ceed/ceed.h")) as f:
19    lines = [line.strip() for line in f if
20             not (line.startswith("#") and not line.startswith("#include")) and
21             not line.startswith("  static") and
22             "CeedErrorImpl" not in line and
23             "const char *, ...);" not in line and
24             not line.startswith("CEED_EXTERN const char *const") and
25             not ceed_version_ge.match(line)]
26    lines = [line.replace("CEED_EXTERN", "extern") for line in lines]
27    # Find scalar type inclusion line and insert definitions
28    for line in lines:
29        if re.search("ceed-f32.h", line) is not None:
30            insert_index = lines.index(line) + 1
31            extra_lines = ['typedef float CeedScalar;']
32            extra_lines.append('static const int CEED_SCALAR_TYPE;')
33            extra_lines.append('static const double CEED_EPSILON;')
34        elif re.search("ceed-f64.h", line) is not None:
35            insert_index = lines.index(line) + 1
36            extra_lines = ['typedef double CeedScalar;']
37            extra_lines.append('static const int CEED_SCALAR_TYPE;')
38            extra_lines.append('static const double CEED_EPSILON;')
39    lines[insert_index: insert_index] = extra_lines
40    # Remove all include statements now that scalar type has been dealt with
41    lines = [line for line in lines if not line.startswith("#include")]
42    # Build header from lines
43    header = '\n'.join(lines)
44    header = header.split("static inline CeedInt CeedIntPow", 1)[0]
45    header += '\nextern int CeedVectorGetState(CeedVector, uint64_t*);'
46    header += '\nextern int CeedElemRestrictionGetELayout(CeedElemRestriction, CeedInt *layout);'
47    # Note: cffi cannot handle vargs
48    header = re.sub("va_list", "const char *", header)
49ffibuilder.cdef(header)
50
51ffibuilder.set_source("_ceed_cffi",
52                      """
53  #define va_list const char *
54  #include <ceed/ceed.h>   // the C header of the library
55  """,
56                      include_dirs=[
57                          os.path.abspath("include")],  # include path
58                      libraries=["ceed"],   # library name, for the linker
59                      library_dirs=['./lib'],  # library path, for the linker
60                      # use libceed.so as installed
61                      runtime_library_dirs=['$ORIGIN/libceed/lib']
62                      )
63
64# ------------------------------------------------------------------------------
65# Builder
66# ------------------------------------------------------------------------------
67if __name__ == "__main__":
68    ffibuilder.compile(verbose=True)
69
70# ------------------------------------------------------------------------------
71