mirror of
https://github.com/sunnypilot/sunnypilot.git
synced 2026-02-19 07:43:57 +08:00
* Convert all text strings to f-strings Reformats all the text from the old "%-formatted" and .format(...) format to the newer f-string format, as defined in PEP 498. This requires Python 3.6+. Flynt 0.69 was used to reformat the strings. 120 f-strings were created in 51 files. F-strings are in general more readable, concise and performant. See also: https://www.python.org/dev/peps/pep-0498/#rationale * revert pyextra changes * revert ublox.py Co-authored-by: Willem Melching <willem.melching@gmail.com>
56 lines
1.3 KiB
Python
56 lines
1.3 KiB
Python
import os
|
|
import sys
|
|
import fcntl
|
|
import hashlib
|
|
import platform
|
|
from cffi import FFI
|
|
|
|
def suffix():
|
|
if platform.system() == "Darwin":
|
|
return ".dylib"
|
|
else:
|
|
return ".so"
|
|
|
|
def ffi_wrap(name, c_code, c_header, tmpdir="/tmp/ccache", cflags="", libraries=None):
|
|
if libraries is None:
|
|
libraries = []
|
|
|
|
cache = name + "_" + hashlib.sha1(c_code.encode('utf-8')).hexdigest()
|
|
try:
|
|
os.mkdir(tmpdir)
|
|
except OSError:
|
|
pass
|
|
|
|
fd = os.open(tmpdir, 0)
|
|
fcntl.flock(fd, fcntl.LOCK_EX)
|
|
try:
|
|
sys.path.append(tmpdir)
|
|
try:
|
|
mod = __import__(cache)
|
|
except Exception:
|
|
print(f"cache miss {cache}")
|
|
compile_code(cache, c_code, c_header, tmpdir, cflags, libraries)
|
|
mod = __import__(cache)
|
|
finally:
|
|
os.close(fd)
|
|
|
|
return mod.ffi, mod.lib
|
|
|
|
|
|
def compile_code(name, c_code, c_header, directory, cflags="", libraries=None):
|
|
if libraries is None:
|
|
libraries = []
|
|
|
|
ffibuilder = FFI()
|
|
ffibuilder.set_source(name, c_code, source_extension='.cpp', libraries=libraries)
|
|
ffibuilder.cdef(c_header)
|
|
os.environ['OPT'] = "-fwrapv -O2 -DNDEBUG -std=c++1z"
|
|
os.environ['CFLAGS'] = cflags
|
|
ffibuilder.compile(verbose=True, debug=False, tmpdir=directory)
|
|
|
|
|
|
def wrap_compiled(name, directory):
|
|
sys.path.append(directory)
|
|
mod = __import__(name)
|
|
return mod.ffi, mod.lib
|