# Copyright (c) 2017-2022, Lawrence Livermore National Security, LLC and other CEED contributors
# All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
#
# SPDX-License-Identifier: BSD-2-Clause
#
# This file is part of CEED:  http://github.com/ceed

import os
import re
from cffi import FFI
ffibuilder = FFI()

ceed_version_ge = re.compile(r'\s+\(!?CEED_VERSION.*')

# ------------------------------------------------------------------------------
# Provide C definitions to CFFI
# ------------------------------------------------------------------------------
with open(os.path.abspath("include/ceed/ceed.h")) as f:
    lines = [line.strip() for line in f if
             not (line.startswith("#") and not line.startswith("#include")) and
             not line.startswith("  static") and
             "CeedErrorImpl" not in line and
             "const char *, ...);" not in line and
             not line.startswith("CEED_EXTERN const char *const") and
             not ceed_version_ge.match(line)]
    lines = [line.replace("CEED_EXTERN", "extern") for line in lines]
    # Find scalar type inclusion line and insert definitions
    for line in lines:
        if re.search("ceed-f32.h", line) is not None:
            insert_index = lines.index(line) + 1
            extra_lines = ['typedef float CeedScalar;']
            extra_lines.append('static const int CEED_SCALAR_TYPE;')
            extra_lines.append('static const double CEED_EPSILON;')
        elif re.search("ceed-f64.h", line) is not None:
            insert_index = lines.index(line) + 1
            extra_lines = ['typedef double CeedScalar;']
            extra_lines.append('static const int CEED_SCALAR_TYPE;')
            extra_lines.append('static const double CEED_EPSILON;')
    lines[insert_index: insert_index] = extra_lines
    # Remove all include statements now that scalar type has been dealt with
    lines = [line for line in lines if not line.startswith("#include")]
    # Build header from lines
    header = '\n'.join(lines)
    header = header.split("static inline CeedInt CeedIntPow", 1)[0]
    header += '\nextern int CeedVectorGetState(CeedVector, uint64_t*);'
    header += '\nextern int CeedElemRestrictionGetELayout(CeedElemRestriction, CeedInt *layout);'
    # Note: cffi cannot handle vargs
    header = re.sub("va_list", "const char *", header)
ffibuilder.cdef(header)

ffibuilder.set_source("_ceed_cffi",
                      """
  #define va_list const char *
  #include <ceed/ceed.h>   // the C header of the library
  """,
                      include_dirs=[
                          os.path.abspath("include")],  # include path
                      libraries=["ceed"],   # library name, for the linker
                      library_dirs=['./lib'],  # library path, for the linker
                      # use libceed.so as installed
                      runtime_library_dirs=['$ORIGIN/libceed/lib']
                      )

# ------------------------------------------------------------------------------
# Builder
# ------------------------------------------------------------------------------
if __name__ == "__main__":
    ffibuilder.compile(verbose=True)

# ------------------------------------------------------------------------------
