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