xref: /libCEED/python/build_ceed_cffi.py (revision 0d627ac128dbf360e78b18bad1cfd2089ff3d6d8)
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# ------------------------------------------------------------------------------
18lines = []
19for header_path in ["include/ceed/types.h", "include/ceed/ceed.h"]:
20    with open(os.path.abspath(header_path)) as f:
21        lines += [line.strip() for line in f if
22                  not (line.startswith("#") and not line.startswith("#include")) and
23                  not line.startswith("  static") and
24                  "CeedErrorImpl" not in line and
25                  "const char *, ...);" not in line and
26                  not line.startswith("CEED_EXTERN const char *const") and
27                  not ceed_version_ge.match(line)]
28lines = [line.replace("CEED_EXTERN", "extern") for line in lines]
29
30# Find scalar type inclusion line and insert definitions
31for line in lines:
32    if re.search("ceed-f32.h", line) is not None:
33        insert_index = lines.index(line) + 1
34        extra_lines = ['typedef float CeedScalar;']
35        extra_lines.append('static const int CEED_SCALAR_TYPE;')
36        extra_lines.append('static const double CEED_EPSILON;')
37    elif re.search("ceed-f64.h", line) is not None:
38        insert_index = lines.index(line) + 1
39        extra_lines = ['typedef double CeedScalar;']
40        extra_lines.append('static const int CEED_SCALAR_TYPE;')
41        extra_lines.append('static const double CEED_EPSILON;')
42lines[insert_index: insert_index] = extra_lines
43
44# Remove all include statements now that scalar type has been dealt with
45lines = [line for line in lines if not line.startswith("#include")]
46
47# Build header from lines
48header = '\n'.join(lines)
49header = header.split("static inline CeedInt CeedIntPow", 1)[0]
50header += '\nextern int CeedVectorGetState(CeedVector, uint64_t*);'
51header += '\nextern int CeedElemRestrictionGetELayout(CeedElemRestriction, CeedInt *layout);'
52
53# Note: cffi cannot handle vargs
54header = re.sub("va_list", "const char *", header)
55
56ffibuilder.cdef(header)
57
58ffibuilder.set_source("_ceed_cffi",
59                      """
60  #define va_list const char *
61  #include <ceed/ceed.h>   // the C header of the library
62  """,
63                      include_dirs=[
64                          os.path.abspath("include")],  # include path
65                      libraries=["ceed"],   # library name, for the linker
66                      library_dirs=['./lib'],  # library path, for the linker
67                      # use libceed.so as installed
68                      runtime_library_dirs=['$ORIGIN/libceed/lib']
69                      )
70
71# ------------------------------------------------------------------------------
72# Builder
73# ------------------------------------------------------------------------------
74if __name__ == "__main__":
75    ffibuilder.compile(verbose=True)
76
77# ------------------------------------------------------------------------------
78