From patchwork Wed Nov 8 15:52:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Phil Howard X-Patchwork-Id: 742419 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0978DC4167D for ; Wed, 8 Nov 2023 15:52:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232410AbjKHPwL (ORCPT ); Wed, 8 Nov 2023 10:52:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50610 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232249AbjKHPwK (ORCPT ); Wed, 8 Nov 2023 10:52:10 -0500 Received: from mail-lf1-x164.google.com (mail-lf1-x164.google.com [IPv6:2a00:1450:4864:20::164]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 40E9A1BC3 for ; Wed, 8 Nov 2023 07:52:08 -0800 (PST) Received: by mail-lf1-x164.google.com with SMTP id 2adb3069b0e04-50930f126b1so8787695e87.3 for ; Wed, 08 Nov 2023 07:52:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gadgetoid.com; s=google; t=1699458726; x=1700063526; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=OsysV5eruwPdrtc0oefirnLVr+6DpwR5v34oQmz7Bl4=; b=eLbiwmdQA/ydjG/Kt+BLiftRCmQtn3EOJ4iJz2N6qKAVElVlPw5xgMtzmt/BUPH91U qAIbbppHd4you6EZv6P+qbAa8/ORa1EIJvqlFn10n9TTxO/1dLXoV+YahJklSxIcpxdl +dj3uiX35FkGdlQH0kyJoF4WQ9qcLMEPryQIOsFbaOytnPEA9MGgk3kNL2TmbV8J2Xge zrtsYqrCMjBouk5wLvvfQJzDNTK1gzi6+1bEik7gxnB/Y/aKd6dd89yVH6OhvYQhJ2fe vscrB5SNqpQNT4dcmG1RSv1s/meWzTKJBhdIXKNx1QsAZwIbaXhL8EjXVQi/FMv6svMk 3urw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1699458726; x=1700063526; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=OsysV5eruwPdrtc0oefirnLVr+6DpwR5v34oQmz7Bl4=; b=Rai9LZBYEYnIthh35OZRI0qv03U7vWCZ05AjR9Fm3bQWHBCLhzsgS8VcpUNo2dXY3u vO0fuC3L3k9vvA5ez1UMOkP4Dh+qPghXADrEoA9HoLII6Ww6TB8guHdBSVmDKwuX84Xk Q/Lr2uwpfKZeW1oEjjyaNOGjUDfGDuYoM9y9FY/IMQedmX/Q5YsvW5jKW3MGQvrspQTy UBLz4YH9ONIZP84IWB5ttKEoRG+I3bxoTu7bA8sW7yR5+17SLKLYWpf+fkzJL6XQUtiB WNZwH2CYju6eNC2QfyaJ570p5pYC2tCC/vpgHpoLUr5G4ynkFMtJRm5ZCZdXXsWpgkMR gTQA== X-Gm-Message-State: AOJu0YyGcFBc4/JPZPLIdnwGkaYZ73t1AGscDTsj8h4hMz6yK+rPaBjR 63RzjwKXPG0TiJAx46xfZ7W8Rd792cpDvuUSRllE9i8ZQ19K/g== X-Google-Smtp-Source: AGHT+IEk5owFtks/FCUjpbdxSjr6MS/VD52Rf/oneHRot23FvY6r+fA7jKDEnPkVZ8xIXGlXdzj/hhh1MWRP X-Received: by 2002:a05:6512:2246:b0:504:30eb:f2ac with SMTP id i6-20020a056512224600b0050430ebf2acmr2004059lfu.68.1699458726243; Wed, 08 Nov 2023 07:52:06 -0800 (PST) Received: from pop-os.. (cpc91242-cmbg18-2-0-cust972.5-4.cable.virginm.net. [82.8.131.205]) by smtp-relay.gmail.com with ESMTPS id f19-20020a05651232d300b005094a2523b3sm1052844lfg.29.2023.11.08.07.52.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Nov 2023 07:52:06 -0800 (PST) X-Relaying-Domain: gadgetoid.com From: Phil Howard To: Linus Walleij , Andy Shevchenko , Kent Gibson , Bartosz Golaszewski Cc: linux-gpio@vger.kernel.org, Phil Howard Subject: [libgpiod][PATCH v2 1/1] bindings: python: standalone build tooling for tests Date: Wed, 8 Nov 2023 15:52:00 +0000 Message-Id: <20231108155200.3050417-2-phil@gadgetoid.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231108155200.3050417-1-phil@gadgetoid.com> References: <20231108155200.3050417-1-phil@gadgetoid.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org Move extension definitions and tooling for building tests into `build_tests.py` and update Makefile.am to call it with appropriate path prefixes. `build_tests.py` will perform a standalone build of the text extensions, keeping any build noise in a temporary directory and copying the final built modules automatically out to `tests/gpiosim` and `tests/procname`. Add "python-tests-run" to Makefile.am so it's clear how to run the tests. Add .so object files generated by build_test.py to Makefile.am's clean-local. Signed-off-by: Phil Howard --- bindings/python/Makefile.am | 33 +++++++++--- bindings/python/build_tests.py | 95 ++++++++++++++++++++++++++++++++++ bindings/python/setup.py | 28 +--------- 3 files changed, 121 insertions(+), 35 deletions(-) create mode 100644 bindings/python/build_tests.py diff --git a/bindings/python/Makefile.am b/bindings/python/Makefile.am index 079ceb1..389da9d 100644 --- a/bindings/python/Makefile.am +++ b/bindings/python/Makefile.am @@ -7,19 +7,36 @@ EXTRA_DIST = \ if WITH_TESTS -BUILD_TESTS = 1 +python-tests: + TOP_SRCDIR=$(abs_top_builddir) \ + TOP_BUILDDIR=$(abs_top_builddir) \ + $(PYTHON) build_tests.py + +python-tests-run: + PYTHONPATH=$(abs_top_srcdir)/bindings/python + LD_LIBRARY_PATH=$(abs_top_builddir)/lib/.libs/:\ +$(abs_top_builddir)/tests/gpiosim/.libs/ \ + python3 -B -m tests + +else + +python-tests: endif -all-local: - GPIOD_WITH_TESTS=$(BUILD_TESTS) \ - $(PYTHON) setup.py build_ext --inplace \ - --include-dirs=$(top_srcdir)/include/:$(top_srcdir)/tests/gpiosim/ \ - --library-dirs=$(top_builddir)/lib/.libs/:$(top_srcdir)/tests/gpiosim/.libs/ +clean-local: + rm -rf dist + rm -f gpiod/_ext.*.so + rm -f tests/*/_ext.*.so + +all-local: python-tests + CFLAGS="-I$(abs_top_srcdir)/include/ -I$(abs_top_srcdir)/tests/gpiosim \ + -L$(abs_top_builddir)/lib/.libs/ -L$(abs_top_builddir)/tests/gpiosim/.libs/" \ + $(PYTHON) -m build install-exec-local: - GPIOD_WITH_TESTS= \ - $(PYTHON) setup.py install --prefix=$(DESTDIR)$(prefix) + $(PYTHON) -m pip install dist/*.whl \ + --prefix=$(DESTDIR)$(prefix) SUBDIRS = gpiod diff --git a/bindings/python/build_tests.py b/bindings/python/build_tests.py new file mode 100644 index 0000000..1760257 --- /dev/null +++ b/bindings/python/build_tests.py @@ -0,0 +1,95 @@ +# SPDX-License-Identifier: GPL-2.0-or-later +# SPDX-FileCopyrightText: 2023 Phil Howard + +""" +Bring up just enough of setuptools/distutils in order to build the gpiod +test module C extensions. + +Set "build_temp" and "build_lib" so that our source directory is not +polluted with artefacts in build/ + +Builds: + + tests/gpiosim/_ext..so + tests/procname/_ext..so + +""" + +import glob +import tempfile +from os import getenv, path + +from setuptools import Distribution, Extension +from setuptools.command.build_ext import build_ext + +TOP_SRCDIR = getenv("TOP_SRCDIR", "../../") +TOP_BUILDDIR = getenv("TOP_BUILDDIR", "../../") + +# __version__ +with open("gpiod/version.py", "r") as fd: + exec(fd.read()) + +# The tests are run in-place with PYTHONPATH set to bindings/python +# so we need the gpiod extension module too. +gpiod_ext = Extension( + "gpiod._ext", + sources=glob.glob("gpiod/ext/*.c"), + define_macros=[("_GNU_SOURCE", "1")], + libraries=["gpiod"], + extra_compile_args=["-Wall", "-Wextra"], + include_dirs=[ + path.join(TOP_SRCDIR, "include"), + ], + library_dirs=[ + path.join(TOP_BUILDDIR, "lib/.libs"), + ], +) + +gpiosim_ext = Extension( + "tests.gpiosim._ext", + sources=["tests/gpiosim/ext.c"], + define_macros=[("_GNU_SOURCE", "1")], + libraries=["gpiosim"], + extra_compile_args=["-Wall", "-Wextra"], + include_dirs=[ + path.join(TOP_SRCDIR, "include"), + path.join(TOP_SRCDIR, "tests/gpiosim"), + ], + library_dirs=[ + path.join(TOP_BUILDDIR, "lib/.libs"), + path.join(TOP_BUILDDIR, "tests/gpiosim/.libs"), + ], +) + +procname_ext = Extension( + "tests.procname._ext", + sources=["tests/procname/ext.c"], + define_macros=[("_GNU_SOURCE", "1")], + extra_compile_args=["-Wall", "-Wextra"], +) + +dist = Distribution( + { + "name": "gpiod", + "ext_modules": [gpiosim_ext, procname_ext, gpiod_ext], + "version": __version__, + "platforms": ["linux"], + } +) + +try: + from setuptools.logging import configure + + configure() +except ImportError: + from distutils.log import DEBUG, set_verbosity + + set_verbosity(DEBUG) + +with tempfile.TemporaryDirectory(prefix="libgpiod-") as temp_dir: + command = build_ext(dist) + command.inplace = True + command.build_temp = temp_dir + command.build_lib = temp_dir + command.finalize_options() + command.run() diff --git a/bindings/python/setup.py b/bindings/python/setup.py index e8704d5..9607a28 100644 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -13,7 +13,6 @@ from setuptools.errors import BaseError LINK_SYSTEM_LIBGPIOD = getenv("LINK_SYSTEM_LIBGPIOD") == "1" LIBGPIOD_MINIMUM_VERSION = "2.1" LIBGPIOD_VERSION = getenv("LIBGPIOD_VERSION") -GPIOD_WITH_TESTS = getenv("GPIOD_WITH_TESTS") == "1" SRC_BASE_URL = "https://mirrors.edge.kernel.org/pub/software/libs/libgpiod/" TAR_FILENAME = "libgpiod-{version}.tar.gz" ASC_FILENAME = "sha256sums.asc" @@ -189,11 +188,6 @@ class build_ext(orig_build_ext): super().run() - # We don't ever want the module tests directory in our package - # since this might include gpiosim._ext or procname._ext from a - # previous dirty build tree. - rmtree(path.join(self.build_lib, "tests"), ignore_errors=True) - class sdist(orig_sdist): """ @@ -226,32 +220,12 @@ gpiod_ext = Extension( extra_compile_args=["-Wall", "-Wextra"], ) -gpiosim_ext = Extension( - "tests.gpiosim._ext", - sources=["tests/gpiosim/ext.c"], - define_macros=[("_GNU_SOURCE", "1")], - libraries=["gpiosim"], - extra_compile_args=["-Wall", "-Wextra"], -) - -procname_ext = Extension( - "tests.procname._ext", - sources=["tests/procname/ext.c"], - define_macros=[("_GNU_SOURCE", "1")], - extra_compile_args=["-Wall", "-Wextra"], -) - -extensions = [gpiod_ext] -if GPIOD_WITH_TESTS: - extensions.append(gpiosim_ext) - extensions.append(procname_ext) - setup( name="gpiod", url="https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git", packages=find_packages(exclude=["tests", "tests.*"]), python_requires=">=3.9.0", - ext_modules=extensions, + ext_modules=[gpiod_ext], cmdclass={"build_ext": build_ext, "sdist": sdist}, version=__version__, author="Bartosz Golaszewski",