changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > demo / ffi/build.py

changeset 18: a1137af05c8d
parent: 315fedf35bc7
author: ellis <ellis@rwest.io>
date: Mon, 29 May 2023 21:46:21 -0400
permissions: -rw-r--r--
description: removed fig, use sexprs instead
1 try:
2  from cffi import FFI
3 except ImportError:
4  print("pip install cffi, included with PyPy")
5 
6 import os
7 import re
8 import pathlib
9 
10 
11 def parse_header(header):
12  h = open(header, "r").read().lstrip()
13  cdef = re.sub(
14  r"^(#|\s*\/*\*|extern).*[\r\n]|.*\"C\"$|^(?:[\t ]*(?:\r?\n|\r))+",
15  "",
16  h,
17  flags=re.MULTILINE,
18  )
19  return cdef
20 
21 
22 def init_ffi(cdef):
23  ffi = FFI()
24  ffi.set_source(
25  "_demo",
26  """
27  #include "demo.h"
28  """,
29 # libraries=["demo"],
30  library_dirs=["."],
31  include_dirs=["."],
32  )
33 
34  ffi.cdef(cdef)
35 
36  return ffi
37 
38 
39 def compile(ffi, lib_dir, v):
40  os.environ["LD_RUN_PATH"] = os.path.abspath(lib_dir)
41  ffi.compile(verbose=v)
42 
43 
44 if __name__ == "__main__":
45  build_dir = pathlib.Path(__file__).parent
46  cdef = parse_header(build_dir / "demo.h")
47  print(cdef)
48  compile(init_ffi(cdef), build_dir, True)