xref: /libCEED/python/build_ceed_cffi.py (revision 9ba83ac0e4b1fca39d6fa6737a318a9f0cbc172d)
1# Copyright (c) 2017-2026, 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# Checks to see if a c line is part of the lines we have to exclude (macros)
17def is_valid_line(line):
18    if (line.startswith("#") and not line.startswith("#include")):
19        return False
20    if (line.startswith("#include \"deprecated.h\"")):
21        return False
22    if (line.startswith("  CEED_QFUNCTION_ATTR")):
23        return False
24    if (line.startswith("  static const char")):
25        return False
26    if (line.endswith('\\\n')):
27        return False
28    if ("CeedErrorImpl" in line):
29        return False
30    if (r'const char *, ...);' in line):
31        return False
32    if (line.startswith("CEED_EXTERN const char *const")):
33        return False
34    if (ceed_version_ge.match(line)):
35        return False
36    return True
37
38
39def get_ceed_dirs():
40    here = os.path.dirname(os.path.abspath(__file__))
41    prefix = os.path.dirname(here)
42    # make install links the installed library in build/lbiceed.so Sadly, it
43    # does not seem possible to obtain the bath that libceed_build_ext has/will
44    # install to.
45    libdir = os.path.join(prefix, "build")
46    return prefix, libdir
47
48
49ceed_dir, ceed_libdir = get_ceed_dirs()
50
51# ------------------------------------------------------------------------------
52# Provide C definitions to CFFI
53# ------------------------------------------------------------------------------
54lines = []
55for header_path in ["include/ceed/types.h", "include/ceed/ceed.h"]:
56    with open(os.path.abspath(header_path)) as f:
57        lines += [line.strip() for line in f if is_valid_line(line)]
58lines = [line.replace("CEED_EXTERN", "extern") for line in lines]
59
60# Find scalar type inclusion line and insert definitions
61for line in lines:
62    if re.search("ceed-f32.h", line) is not None:
63        insert_index = lines.index(line) + 1
64        extra_lines = ['typedef float CeedScalar;']
65        extra_lines.append('static const int CEED_SCALAR_TYPE;')
66        extra_lines.append('static const double CEED_EPSILON;')
67    elif re.search("ceed-f64.h", line) is not None:
68        insert_index = lines.index(line) + 1
69        extra_lines = ['typedef double CeedScalar;']
70        extra_lines.append('static const int CEED_SCALAR_TYPE;')
71        extra_lines.append('static const double CEED_EPSILON;')
72lines[insert_index: insert_index] = extra_lines
73
74# Remove all include statements now that scalar type has been dealt with
75lines = [line for line in lines if not line.startswith("#include")]
76
77# Build header from lines
78header = '\n'.join(lines)
79header = header.split("static inline CeedInt CeedIntPow", 1)[0]
80header += '\nextern int CeedVectorGetState(CeedVector, uint64_t*);'
81header += '\nextern int CeedElemRestrictionGetLLayout(CeedElemRestriction, CeedInt layout[3]);'
82header += '\nextern int CeedElemRestrictionGetELayout(CeedElemRestriction, CeedInt layout[3]);'
83
84# Note: cffi cannot handle vargs
85header = re.sub("va_list", "const char *", header)
86
87ffibuilder.cdef(header)
88
89ffibuilder.set_source("_ceed_cffi",
90                      """
91  #define va_list const char *
92  #include <ceed.h>   // the C header of the library
93  #include <ceed/backend.h> // declarations for the backend functions above
94  """,
95                      include_dirs=[
96                          os.path.join(ceed_dir, "include")],  # include path
97                      libraries=["ceed"],   # library name, for the linker
98                      # library path, for the linker
99                      library_dirs=[ceed_libdir],
100                      # use libceed.so as installed
101                      runtime_library_dirs=['$ORIGIN/libceed/lib']
102                      )
103
104# ------------------------------------------------------------------------------
105# Builder
106# ------------------------------------------------------------------------------
107if __name__ == "__main__":
108    ffibuilder.compile(verbose=True)
109
110# ------------------------------------------------------------------------------
111