Skip to content

Commit

Permalink
rework cffi build
Browse files Browse the repository at this point in the history
  • Loading branch information
slyon committed Jul 31, 2023
1 parent 13bbb01 commit b76bfdf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
11 changes: 8 additions & 3 deletions python-cffi/_build_cffi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import os
import sys

from cffi import FFI
ffibuilder = FFI()
Expand Down Expand Up @@ -51,6 +52,9 @@
NetplanBackend netplan_state_get_backend(const NetplanState* np_state);
""")

cffi_inc = os.getenv('CFFI_INC', sys.argv[1])
cffi_lib = os.getenv('CFFI_LIB', sys.argv[2])

# set_source() gives the name of the python extension module to
# produce, and some C source code as a string. This C code needs
# to make the declarated functions, types and globals available,
Expand All @@ -62,9 +66,10 @@
#include "parse.h"
#include "parse-nm.h"
""",
include_dirs=[os.getenv('CFFI_INC')],
library_dirs=[os.getenv('CFFI_LIB')],
include_dirs=[cffi_inc],
library_dirs=[cffi_lib],
libraries=['netplan', 'glib-2.0']) # library name, for the linker

if __name__ == "__main__":
ffibuilder.compile(verbose=False)
#ffibuilder.compile(verbose=False)
ffibuilder.distutils_extension('.')
32 changes: 23 additions & 9 deletions python-cffi/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,31 @@ python = pymod.find_installation(
'python3',
modules: ['cffi']
)
python_dep = python.dependency(required: true)

#https://mesonbuild.com/FAQ.html#but-i-really-want-to-use-wildcards
out = run_command(
'sh', '-c', 'python3 _build_cffi.py && find -name _libnetplan*.so',
env: [
'CFFI_INC=' + join_paths(meson.project_source_root(), 'include'),
'CFFI_LIB=' + join_paths(meson.current_build_dir(), 'src'),
cffi_srcs = configure_file(
command: [
python,
files('_build_cffi.py'),
join_paths(meson.project_source_root(), 'include'),
join_paths(meson.current_build_dir(), 'src'),
],
check: true,
output: '_libnetplan0.c',
)

cffi_pyext = python.extension_module(
'_libnetplan0',
link_whole: static_library(
'_libnetplan0',
cffi_srcs,
dependencies: [python_dep, glib],
include_directories: [inc],
),
dependencies: [python_dep],
include_directories: [include_directories(join_paths('..', 'include'))],
subdir: 'netplan',
install: true,
)
cffi_so = out.stdout().strip().split('\n')

bindings_sources = '''
__init__.py
Expand All @@ -22,5 +36,5 @@ bindings_sources = '''
'''.split()

bindings = python.install_sources(
[bindings_sources, cffi_so],
[bindings_sources],
subdir: 'netplan')

0 comments on commit b76bfdf

Please sign in to comment.