Skip to content

Commit

Permalink
meson: Skeleton for python3-netplan build
Browse files Browse the repository at this point in the history
  • Loading branch information
slyon committed Jul 26, 2023
1 parent 6ade72a commit 2000088
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 0 deletions.
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ subdir('include')
subdir('src')
subdir('dbus')
subdir('netplan')
subdir('python-cffi')
subdir('examples')
subdir('doc')

Expand Down
4 changes: 4 additions & 0 deletions python-cffi/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
_libnetplan*.c
_libnetplan*.o
_libnetplan*.so

17 changes: 17 additions & 0 deletions python-cffi/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (C) 2023 Canonical, Ltd.
# Author: Lukas Märdian <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Re-export submodules
from .state import *
49 changes: 49 additions & 0 deletions python-cffi/libnetplan_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3

# Copyright (C) 2023 Canonical, Ltd.
# Author: Lukas Märdian <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from cffi import FFI
ffibuilder = FFI()

# cdef() expects a single string declaring the C types, functions and
# globals needed to use the shared object. It must be in valid C syntax.
ffibuilder.cdef("""
typedef struct netplan_state NetplanState;
typedef enum {
NETPLAN_BACKEND_NONE,
NETPLAN_BACKEND_NETWORKD,
NETPLAN_BACKEND_NM,
NETPLAN_BACKEND_OVS,
NETPLAN_BACKEND_MAX_,
} NetplanBackend;
NetplanState* netplan_state_new();
void netplan_state_reset(NetplanState* np_state);
void netplan_state_clear(NetplanState** np_state);
NetplanBackend netplan_state_get_backend(const NetplanState* np_state);
""")

# 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,
# so it is often just the "#include".
ffibuilder.set_source_pkgconfig("_libnetplan0", ['netplan', 'glib-2.0'],
"""
#include "netplan/netplan.h" // the C header of the library
""",
libraries=['netplan', 'glib-2.0']) # library name, for the linker

if __name__ == "__main__":
ffibuilder.compile(verbose=False)
18 changes: 18 additions & 0 deletions python-cffi/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
pymod = import('python')
python = pymod.find_installation('python3')



#https://mesonbuild.com/FAQ.html#but-i-really-want-to-use-wildcards
out = run_command('sh', '-c', 'python3 libnetplan_build.py && find -name _libnetplan*.so', check: true)
cffi_so = out.stdout().strip().split('\n')
message(cffi_so)

bindings_sources = '''
__init__.py
state.py
'''.split()

bindings = python.install_sources(
[bindings_sources, cffi_so],
subdir: 'netplan')
21 changes: 21 additions & 0 deletions python-cffi/state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2023 Canonical, Ltd.
# Author: Lukas Märdian <[email protected]>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from ._libnetplan import ffi, lib

class State():
def __init__(self):
state = lib.netplan_state_new()
print(lib.netplan_state_get_backend(state))

0 comments on commit 2000088

Please sign in to comment.