diff mbox series

[libgpiod,1/3] bindings: python: optionally include module in sdist

Message ID 20231011121246.9467-2-phil@gadgetoid.com
State New
Headers show
Series bindings: python: optionally include module (...) | expand

Commit Message

Phil Howard Oct. 11, 2023, 12:12 p.m. UTC
Build gpiod into Python module.

Optional environment var USE_SYSTEM_GPIO=1 to
generate a module that depends upon system gpiod.

Signed-off-by: Phil Howard <phil@gadgetoid.com>
---
 bindings/python/MANIFEST.in |  4 +++
 bindings/python/include     |  1 +
 bindings/python/lib         |  1 +
 bindings/python/setup.py    | 57 ++++++++++++++++++++++++++++---------
 4 files changed, 50 insertions(+), 13 deletions(-)
 create mode 120000 bindings/python/include
 create mode 120000 bindings/python/lib

Comments

Andy Shevchenko Oct. 11, 2023, 5:11 p.m. UTC | #1
On Wed, Oct 11, 2023 at 01:12:44PM +0100, Phil Howard wrote:
> Build gpiod into Python module.
> 
> Optional environment var USE_SYSTEM_GPIO=1 to
> generate a module that depends upon system gpiod.

...

> --- /dev/null
> +++ b/bindings/python/include
> @@ -0,0 +1 @@
> +../../include

> \ No newline at end of file

These lines are bothering me, why the new line can't be added to all affected files?
Phil Howard Oct. 12, 2023, 9:03 a.m. UTC | #2
On Wed, 11 Oct 2023 at 18:11, Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Wed, Oct 11, 2023 at 01:12:44PM +0100, Phil Howard wrote:
> > Build gpiod into Python module.
> >
> > Optional environment var USE_SYSTEM_GPIO=1 to
> > generate a module that depends upon system gpiod.
>
> ...

*sigh*

>
> > --- /dev/null
> > +++ b/bindings/python/include
> > @@ -0,0 +1 @@
> > +../../include
>
> > \ No newline at end of file
>
> These lines are bothering me, why the new line can't be added to all affected files?

Is it convention for symlinks to include a newline, is it even possible?

I'm not super sure about the symlink approach, actually.

It's the path of least complexity but after some research into the
usage of "shutil.copytree"
to copy dependent and packaged files at build time I'm starting to
wonder if a more complex
setup.py isn't necessarily a bad idea.

>
> --
> With Best Regards,
> Andy Shevchenko
>
>
Bartosz Golaszewski Oct. 12, 2023, 7:18 p.m. UTC | #3
On Thu, Oct 12, 2023 at 11:03 AM Phil Howard <phil@gadgetoid.com> wrote:
>
> On Wed, 11 Oct 2023 at 18:11, Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> >
> > On Wed, Oct 11, 2023 at 01:12:44PM +0100, Phil Howard wrote:
> > > Build gpiod into Python module.
> > >
> > > Optional environment var USE_SYSTEM_GPIO=1 to
> > > generate a module that depends upon system gpiod.
> >
> > ...
>
> *sigh*
>
> >
> > > --- /dev/null
> > > +++ b/bindings/python/include
> > > @@ -0,0 +1 @@
> > > +../../include
> >
> > > \ No newline at end of file
> >
> > These lines are bothering me, why the new line can't be added to all affected files?
>
> Is it convention for symlinks to include a newline, is it even possible?
>
> I'm not super sure about the symlink approach, actually.
>
> It's the path of least complexity but after some research into the
> usage of "shutil.copytree"
> to copy dependent and packaged files at build time I'm starting to
> wonder if a more complex
> setup.py isn't necessarily a bad idea.
>

How about not having links in the repo but creating them as required
at build-time?

Bart

> >
> > --
> > With Best Regards,
> > Andy Shevchenko
> >
> >
>
>
> --
> Philip Howard
> Technology & Lifestyle Writer
> gadgetoid.com
>
> Gadgetoid gadg-et-oid [gaj-it-oid]
>
>                                      -adjective
>
> 1. having the characteristics or form of a gadget; resembling a
> mechanical contrivance or device.
Bartosz Golaszewski Oct. 12, 2023, 7:37 p.m. UTC | #4
On Wed, Oct 11, 2023 at 2:13 PM Phil Howard <phil@gadgetoid.com> wrote:
>
> Build gpiod into Python module.
>
> Optional environment var USE_SYSTEM_GPIO=1 to

Let's call it USE_SYSTEM_GPIOD or USE_SYSTEM_LIBGPIOD or even
LINK_SYSTEM_LIBGPIOD?

> generate a module that depends upon system gpiod.
>
> Signed-off-by: Phil Howard <phil@gadgetoid.com>
> ---
>  bindings/python/MANIFEST.in |  4 +++
>  bindings/python/include     |  1 +
>  bindings/python/lib         |  1 +
>  bindings/python/setup.py    | 57 ++++++++++++++++++++++++++++---------
>  4 files changed, 50 insertions(+), 13 deletions(-)
>  create mode 120000 bindings/python/include
>  create mode 120000 bindings/python/lib
>
> diff --git a/bindings/python/MANIFEST.in b/bindings/python/MANIFEST.in
> index c7124d4..eff8977 100644
> --- a/bindings/python/MANIFEST.in
> +++ b/bindings/python/MANIFEST.in
> @@ -11,3 +11,7 @@ recursive-include gpiod/ext *.h
>
>  recursive-include tests/gpiosim *.c
>  recursive-include tests/procname *.c
> +
> +recursive-include lib *.c
> +recursive-include lib *.h
> +recursive-include include *.h
> diff --git a/bindings/python/include b/bindings/python/include
> new file mode 120000
> index 0000000..fcffffb
> --- /dev/null
> +++ b/bindings/python/include
> @@ -0,0 +1 @@
> +../../include
> \ No newline at end of file

Addressing Andy's remark: I think this is just how git generates diffs
for links, nothing we can do about it.

I would prefer not to have links in the repo anyway, it would be great
if we could create them at build-time.

Bart

> diff --git a/bindings/python/lib b/bindings/python/lib
> new file mode 120000
> index 0000000..58677dd
> --- /dev/null
> +++ b/bindings/python/lib
> @@ -0,0 +1 @@
> +../../lib
> \ No newline at end of file
> diff --git a/bindings/python/setup.py b/bindings/python/setup.py
> index 66b7908..2e25981 100644
> --- a/bindings/python/setup.py
> +++ b/bindings/python/setup.py
> @@ -19,19 +19,53 @@ class build_ext(orig_build_ext):
>          rmtree(path.join(self.build_lib, "tests"), ignore_errors=True)
>
>
> +with open("gpiod/version.py", "r") as fd:
> +    exec(fd.read())
> +
> +
> +sources = [
> +    # gpiod Python bindings
> +    "gpiod/ext/chip.c",
> +    "gpiod/ext/common.c",
> +    "gpiod/ext/line-config.c",
> +    "gpiod/ext/line-settings.c",
> +    "gpiod/ext/module.c",
> +    "gpiod/ext/request.c",
> +]
> +
> +if "USE_SYSTEM_GPIOD" in environ and environ["USE_SYSTEM_GPIOD"] == "1":
> +    libraries = ["gpiod"]
> +    include_dirs = ["gpiod"]
> +else:
> +    sources += [
> +        # gpiod library
> +        "lib/chip.c",
> +        "lib/chip-info.c",
> +        "lib/edge-event.c",
> +        "lib/info-event.c",
> +        "lib/internal.c",
> +        "lib/line-config.c",
> +        "lib/line-info.c",
> +        "lib/line-request.c",
> +        "lib/line-settings.c",
> +        "lib/misc.c",
> +        "lib/request-config.c",
> +    ]
> +    libraries = []
> +    include_dirs = ["include", "lib", "gpiod/ext"]
> +
> +
>  gpiod_ext = Extension(
>      "gpiod._ext",
> -    sources=[
> -        "gpiod/ext/chip.c",
> -        "gpiod/ext/common.c",
> -        "gpiod/ext/line-config.c",
> -        "gpiod/ext/line-settings.c",
> -        "gpiod/ext/module.c",
> -        "gpiod/ext/request.c",
> -    ],
> +    libraries=libraries,
> +    sources=sources,
>      define_macros=[("_GNU_SOURCE", "1")],
> -    libraries=["gpiod"],
> -    extra_compile_args=["-Wall", "-Wextra"],
> +    include_dirs=include_dirs,
> +    extra_compile_args=[
> +        "-Wall",
> +        "-Wextra",
> +        '-DGPIOD_VERSION_STR="{}"'.format(__version__),
> +    ],
>  )
>
>  gpiosim_ext = Extension(
> @@ -54,9 +88,6 @@ if "GPIOD_WITH_TESTS" in environ and environ["GPIOD_WITH_TESTS"] == "1":
>      extensions.append(gpiosim_ext)
>      extensions.append(procname_ext)
>
> -with open("gpiod/version.py", "r") as fd:
> -    exec(fd.read())
> -
>  setup(
>      name="libgpiod",
>      packages=find_packages(exclude=["tests", "tests.*"]),
> --
> 2.34.1
>
diff mbox series

Patch

diff --git a/bindings/python/MANIFEST.in b/bindings/python/MANIFEST.in
index c7124d4..eff8977 100644
--- a/bindings/python/MANIFEST.in
+++ b/bindings/python/MANIFEST.in
@@ -11,3 +11,7 @@  recursive-include gpiod/ext *.h
 
 recursive-include tests/gpiosim *.c
 recursive-include tests/procname *.c
+
+recursive-include lib *.c
+recursive-include lib *.h
+recursive-include include *.h
diff --git a/bindings/python/include b/bindings/python/include
new file mode 120000
index 0000000..fcffffb
--- /dev/null
+++ b/bindings/python/include
@@ -0,0 +1 @@ 
+../../include
\ No newline at end of file
diff --git a/bindings/python/lib b/bindings/python/lib
new file mode 120000
index 0000000..58677dd
--- /dev/null
+++ b/bindings/python/lib
@@ -0,0 +1 @@ 
+../../lib
\ No newline at end of file
diff --git a/bindings/python/setup.py b/bindings/python/setup.py
index 66b7908..2e25981 100644
--- a/bindings/python/setup.py
+++ b/bindings/python/setup.py
@@ -19,19 +19,53 @@  class build_ext(orig_build_ext):
         rmtree(path.join(self.build_lib, "tests"), ignore_errors=True)
 
 
+with open("gpiod/version.py", "r") as fd:
+    exec(fd.read())
+
+
+sources = [
+    # gpiod Python bindings
+    "gpiod/ext/chip.c",
+    "gpiod/ext/common.c",
+    "gpiod/ext/line-config.c",
+    "gpiod/ext/line-settings.c",
+    "gpiod/ext/module.c",
+    "gpiod/ext/request.c",
+]
+
+if "USE_SYSTEM_GPIOD" in environ and environ["USE_SYSTEM_GPIOD"] == "1":
+    libraries = ["gpiod"]
+    include_dirs = ["gpiod"]
+else:
+    sources += [
+        # gpiod library
+        "lib/chip.c",
+        "lib/chip-info.c",
+        "lib/edge-event.c",
+        "lib/info-event.c",
+        "lib/internal.c",
+        "lib/line-config.c",
+        "lib/line-info.c",
+        "lib/line-request.c",
+        "lib/line-settings.c",
+        "lib/misc.c",
+        "lib/request-config.c",
+    ]
+    libraries = []
+    include_dirs = ["include", "lib", "gpiod/ext"]
+
+
 gpiod_ext = Extension(
     "gpiod._ext",
-    sources=[
-        "gpiod/ext/chip.c",
-        "gpiod/ext/common.c",
-        "gpiod/ext/line-config.c",
-        "gpiod/ext/line-settings.c",
-        "gpiod/ext/module.c",
-        "gpiod/ext/request.c",
-    ],
+    libraries=libraries,
+    sources=sources,
     define_macros=[("_GNU_SOURCE", "1")],
-    libraries=["gpiod"],
-    extra_compile_args=["-Wall", "-Wextra"],
+    include_dirs=include_dirs,
+    extra_compile_args=[
+        "-Wall",
+        "-Wextra",
+        '-DGPIOD_VERSION_STR="{}"'.format(__version__),
+    ],
 )
 
 gpiosim_ext = Extension(
@@ -54,9 +88,6 @@  if "GPIOD_WITH_TESTS" in environ and environ["GPIOD_WITH_TESTS"] == "1":
     extensions.append(gpiosim_ext)
     extensions.append(procname_ext)
 
-with open("gpiod/version.py", "r") as fd:
-    exec(fd.read())
-
 setup(
     name="libgpiod",
     packages=find_packages(exclude=["tests", "tests.*"]),