xref: /libCEED/python/build_ceed_cffi.py (revision ae718e2f2476441c07fb6c037655a83f28d5f429)
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
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    header = '\n'.join(lines)
37    header = header.split("static inline CeedInt CeedIntPow", 1)[0]
38    header += '\nextern int CeedVectorGetState(CeedVector, uint64_t*);'
39    header += '\nextern int CeedElemRestrictionGetELayout(CeedElemRestriction, CeedInt *layout);'
40    # Note: cffi cannot handle vargs
41    header = re.sub("va_list", "const char *", header)
42ffibuilder.cdef(header)
43
44ffibuilder.set_source("_ceed_cffi",
45                      """
46  #define va_list const char *
47  #include <ceed.h>   // the C header of the library
48  """,
49                      include_dirs=[
50                          os.path.abspath("include")],  # include path
51                      libraries=["ceed"],   # library name, for the linker
52                      library_dirs=['./lib'],  # library path, for the linker
53                      # use libceed.so as installed
54                      runtime_library_dirs=['$ORIGIN/libceed/lib']
55                      )
56
57# ------------------------------------------------------------------------------
58# Builder
59# ------------------------------------------------------------------------------
60if __name__ == "__main__":
61    ffibuilder.compile(verbose=True)
62
63# ------------------------------------------------------------------------------
64