From 97eb8f6d1a211a483f4708249db891f33ee899bf Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Sat, 20 Sep 2025 23:24:14 +0530 Subject: [PATCH] initialise with pylibbpf name Signed-off-by: varun-r-mallya --- .appveyor.yml | 27 ----- .github/workflows/conda.yml | 48 -------- .github/workflows/pip.yml | 35 ++++-- .github/workflows/wheels.yml | 81 +++++++++---- .gitignore | 3 + CMakeLists.txt | 8 +- LICENSE | 226 ++++++++++++++++++++++++++++++----- README.md | 23 +--- conda.recipe/meta.yaml | 38 ------ pybind11 | 2 +- pyproject.toml | 75 ++++++++++-- setup.py | 94 ++++++++------- src/main.cpp | 4 +- tests/test_basic.py | 3 +- 14 files changed, 406 insertions(+), 261 deletions(-) delete mode 100644 .appveyor.yml delete mode 100644 .github/workflows/conda.yml delete mode 100644 conda.recipe/meta.yaml diff --git a/.appveyor.yml b/.appveyor.yml deleted file mode 100644 index fa01973..0000000 --- a/.appveyor.yml +++ /dev/null @@ -1,27 +0,0 @@ -version: '{build}' -image: Visual Studio 2019 -platform: -- x86 -- x64 -environment: - global: - DISTUTILS_USE_SDK: 1 - PYTHONWARNINGS: ignore:DEPRECATION - MSSdk: 1 - matrix: - - PYTHON: 37 -install: -- cmd: '"%VS140COMNTOOLS%\..\..\VC\vcvarsall.bat" %PLATFORM%' -- ps: | - git submodule update -q --init --recursive - if ($env:PLATFORM -eq "x64") { $env:PYTHON = "$env:PYTHON-x64" } - $env:PATH = "C:\Python$env:PYTHON\;C:\Python$env:PYTHON\Scripts\;$env:PATH" - python -m pip install --disable-pip-version-check --upgrade --no-warn-script-location pip build pytest -build_script: -- ps: | - python -m build -s - cd dist - python -m pip install --verbose cmake_example-0.0.1.tar.gz - cd .. -test_script: -- ps: python -m pytest diff --git a/.github/workflows/conda.yml b/.github/workflows/conda.yml deleted file mode 100644 index ba42463..0000000 --- a/.github/workflows/conda.yml +++ /dev/null @@ -1,48 +0,0 @@ -name: Conda - -on: - workflow_dispatch: - push: - branches: - - master - pull_request: - -jobs: - build: - strategy: - fail-fast: false - matrix: - platform: [ubuntu-latest, windows-latest, macos-12] - python-version: ["3.8", "3.10"] - - runs-on: ${{ matrix.platform }} - - # The setup-miniconda action needs this to activate miniconda - defaults: - run: - shell: "bash -l {0}" - - steps: - - uses: actions/checkout@v4 - with: - submodules: true - - - name: Get conda - uses: conda-incubator/setup-miniconda@v3.0.4 - with: - python-version: ${{ matrix.python-version }} - channels: conda-forge - channel-priority: strict - - # Currently conda-build requires the dead package "toml" but doesn't declare it - - name: Prepare - run: conda install conda-build conda-verify pytest toml - - - name: Build - run: conda build conda.recipe - - - name: Install - run: conda install -c ${CONDA_PREFIX}/conda-bld/ cmake_example - - - name: Test - run: python -m pytest diff --git a/.github/workflows/pip.yml b/.github/workflows/pip.yml index f00f9c7..19836dd 100644 --- a/.github/workflows/pip.yml +++ b/.github/workflows/pip.yml @@ -6,31 +6,46 @@ on: push: branches: - master + - main jobs: build: strategy: fail-fast: false matrix: - platform: [windows-latest, macos-13, ubuntu-latest] - python-version: ["3.7", "3.11"] - + platform: [ubuntu-latest] + python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] + runs-on: ${{ matrix.platform }} - + steps: - uses: actions/checkout@v4 with: submodules: true - + - uses: actions/setup-python@v5 with: python-version: ${{ matrix.python-version }} - + + - name: Install system dependencies + run: | + sudo apt-get update + sudo apt-get install -y \ + libbpf-dev \ + linux-headers-generic \ + build-essential \ + cmake \ + ninja-build + - name: Add requirements - run: python -m pip install --upgrade wheel setuptools - + run: python -m pip install --upgrade pip wheel setuptools + - name: Build and install run: pip install --verbose .[test] - + + - name: Test import + run: python -c "import pylibbpf; print('Import successful')" + - name: Test - run: python -m pytest + run: python -m pytest -v + \ No newline at end of file diff --git a/.github/workflows/wheels.yml b/.github/workflows/wheels.yml index dd06035..0dcd081 100644 --- a/.github/workflows/wheels.yml +++ b/.github/workflows/wheels.yml @@ -1,11 +1,11 @@ name: Wheels - on: workflow_dispatch: pull_request: push: branches: - master + - main release: types: - published @@ -18,64 +18,93 @@ jobs: - uses: actions/checkout@v4 with: submodules: true - + - name: Build SDist run: pipx run build --sdist - + - name: Check metadata run: pipx run twine check dist/* - + - uses: actions/upload-artifact@v4 with: name: cibw-sdist path: dist/*.tar.gz - build_wheels: - name: Wheels on ${{ matrix.os }} - runs-on: ${{ matrix.os }} + name: Build wheels on Linux + runs-on: ubuntu-latest strategy: fail-fast: false matrix: - os: [ubuntu-latest, windows-latest, macos-13] - + # Build for both x86_64 and aarch64 + arch: [x86_64, aarch64] + steps: - uses: actions/checkout@v4 with: submodules: true - - - uses: pypa/cibuildwheel@v2.17 + + # Set up QEMU for aarch64 emulation + - name: Set up QEMU + if: matrix.arch == 'aarch64' + uses: docker/setup-qemu-action@v3 + with: + platforms: arm64 + + - name: Build wheels + uses: pypa/cibuildwheel@v2.17 env: - CIBW_ARCHS_MACOS: auto universal2 - + # Only build for Linux + CIBW_PLATFORM: linux + CIBW_ARCHS_LINUX: ${{ matrix.arch }} + + # Python versions to build for + CIBW_BUILD: "cp38-* cp39-* cp310-* cp311-* cp312-*" + CIBW_SKIP: "*-musllinux*" # Skip musl builds, focus on glibc + + # Install system dependencies before building + CIBW_BEFORE_ALL_LINUX: > + yum install -y epel-release && + yum install -y libbpf-devel kernel-headers || + (apt-get update && apt-get install -y libbpf-dev linux-headers-generic) + + # Test the built wheels + CIBW_TEST_COMMAND: "python -c 'import pylibbpf; print(f\"pylibbpf {pylibbpf.__version__} imported successfully\")'" + + # Skip testing on emulated architectures (too slow) + CIBW_TEST_SKIP: "*-linux_aarch64" + - name: Verify clean directory run: git diff --exit-code shell: bash - + - name: Upload wheels uses: actions/upload-artifact@v4 with: - name: cibw-wheels-${{ matrix.os }} + name: cibw-wheels-linux-${{ matrix.arch }} path: wheelhouse/*.whl - upload_all: - name: Upload if release + name: Upload to PyPI if release needs: [build_wheels, build_sdist] runs-on: ubuntu-latest if: github.event_name == 'release' && github.event.action == 'published' - + permissions: + # IMPORTANT: this permission is mandatory for trusted publishing + id-token: write + environment: + name: pypi + # url: https://pypi.org/project/pylibbpf/${{ github.event.release.name }} + steps: - - uses: actions/setup-python@v5 - with: - python-version: "3.x" - - - uses: actions/download-artifact@v4 + - name: Download all artifacts + uses: actions/download-artifact@v4 with: pattern: cibw-* path: dist merge-multiple: true - - - uses: pypa/gh-action-pypi-publish@release/v1 + + - name: Publish to PyPI + uses: pypa/gh-action-pypi-publish@release/v1 with: - password: ${{ secrets.pypi_password }} + packages-dir: dist/ diff --git a/.gitignore b/.gitignore index 635dc55..16e0c73 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ _generate/ *.py[cod] *.egg-info *env* +build/ +*venv/ +.idea/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 69122df..c9b7c8b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,10 +1,10 @@ -cmake_minimum_required(VERSION 3.4...3.18) -project(cmake_example) +cmake_minimum_required(VERSION 4.0) +project(pylibbpf) add_subdirectory(pybind11) -pybind11_add_module(cmake_example src/main.cpp) +pybind11_add_module(pylibbpf src/main.cpp) # EXAMPLE_VERSION_INFO is defined by setup.py and passed into the C++ code as a # define (VERSION_INFO) here. -target_compile_definitions(cmake_example +target_compile_definitions(pylibbpf PRIVATE VERSION_INFO=${EXAMPLE_VERSION_INFO}) diff --git a/LICENSE b/LICENSE index 99f6068..a5e2141 100644 --- a/LICENSE +++ b/LICENSE @@ -1,36 +1,202 @@ -Copyright (c) 2016 The Pybind Development Team, All rights reserved. -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ -1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION -2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. + 1. Definitions. -3. Neither the name of the copyright holder nor the names of its contributors - may be used to endorse or promote products derived from this software - without specific prior written permission. + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. -You are under no obligation whatsoever to provide any bug fixes, patches, or -upgrades to the features, functionality or performance of the source code -("Enhancements") to anyone; however, if you choose to make your Enhancements -available either publicly, or directly to the author of this software, without -imposing a separate written license agreement for such Enhancements, then you -hereby grant the following license: a non-exclusive, royalty-free perpetual -license to install, use, modify, prepare derivative works, incorporate into -other computer software, distribute, and sublicense such enhancements or -derivative works thereof, in binary and source code form. + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2025 Pragyansh Chaturvedi and Varun R Mallya + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md index 7e8e2d2..4143620 100644 --- a/README.md +++ b/README.md @@ -1,26 +1,5 @@ # cmake_example for pybind11 -[![Gitter][gitter-badge]][gitter-link] - -| CI | status | -|----------------------|--------| -| MSVC 2019 | [![AppVeyor][appveyor-badge]][appveyor-link] | -| conda.recipe | [![Conda Actions Status][actions-conda-badge]][actions-conda-link] | -| pip builds | [![Pip Actions Status][actions-pip-badge]][actions-pip-link] | -| [`cibuildwheel`][] | [![Wheels Actions Status][actions-wheels-badge]][actions-wheels-link] | - -[gitter-badge]: https://badges.gitter.im/pybind/Lobby.svg -[gitter-link]: https://gitter.im/pybind/Lobby -[actions-badge]: https://github.com/pybind/cmake_example/workflows/Tests/badge.svg -[actions-conda-link]: https://github.com/pybind/cmake_example/actions?query=workflow%3A%22Conda -[actions-conda-badge]: https://github.com/pybind/cmake_example/workflows/Conda/badge.svg -[actions-pip-link]: https://github.com/pybind/cmake_example/actions?query=workflow%3A%22Pip -[actions-pip-badge]: https://github.com/pybind/cmake_example/workflows/Pip/badge.svg -[actions-wheels-link]: https://github.com/pybind/cmake_example/actions?query=workflow%3AWheels -[actions-wheels-badge]: https://github.com/pybind/cmake_example/workflows/Wheels/badge.svg -[appveyor-link]: https://ci.appveyor.com/project/dean0x7d/cmake-example/branch/master -[appveyor-badge]: https://ci.appveyor.com/api/projects/status/57nnxfm4subeug43/branch/master?svg=true - An example [pybind11](https://github.com/pybind/pybind11) module built with a CMake-based build system. This is useful for C++ codebases that have an existing CMake project structure. This is being replaced by @@ -43,7 +22,7 @@ Problems vs. scikit-build-core based example: ## Prerequisites * A compiler with C++11 support -* Pip 10+ or CMake >= 3.4 (or 3.14+ on Windows, which was the first version to support VS 2019) +* Pip 10+ or CMake >= 4.1 * Ninja or Pip 10+ diff --git a/conda.recipe/meta.yaml b/conda.recipe/meta.yaml deleted file mode 100644 index 98a10fb..0000000 --- a/conda.recipe/meta.yaml +++ /dev/null @@ -1,38 +0,0 @@ -package: - name: cmake_example - version: 0.0.1 - -source: - path: .. - -build: - number: 0 - script: {{ PYTHON }} -m pip install . -vvv - -requirements: - build: - - "{{ compiler('cxx') }}" - - cmake - - ninja - - host: - - python - - pip !=22.1.0 - - run: - - python - - -test: - requires: - - pytest - imports: - - cmake_example - source_files: - - tests - commands: - - python -m pytest - -about: - summary: A CMake example project built with pybind11. - license_file: LICENSE diff --git a/pybind11 b/pybind11 index 914c06f..f5fbe86 160000 --- a/pybind11 +++ b/pybind11 @@ -1 +1 @@ -Subproject commit 914c06fb252b6cc3727d0eedab6736e88a3fcb01 +Subproject commit f5fbe867d2d26e4a0a9177a51f6e568868ad3dc8 diff --git a/pyproject.toml b/pyproject.toml index fd92373..6bf40dc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,13 +3,48 @@ requires = [ "setuptools>=42", "wheel", "ninja", - "cmake>=3.12", + "cmake>=4.0", ] build-backend = "setuptools.build_meta" +[project] +name = "pylibbpf" +version = "0.0.1" +description = "Python Bindings for Libbpf" +authors = [ + { name = "r41k0u", email = "pragyanshchaturvedi18@gmail.com" }, + { name = "varun-r-mallya", email = "varunrmallya@gmail.com" } +] +readme = "README.md" +license = { text = "Apache-2.0" } +requires-python = ">=3.8" +classifiers = [ + "Development Status :: 3 - Alpha", + "Intended Audience :: Developers", + "License :: OSI Approved :: Apache Software License", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: C++", + "Topic :: Software Development :: Libraries :: Python Modules", + "Topic :: System :: Operating System Kernels :: Linux", +] + +[project.optional-dependencies] +test = ["pytest>=6.0"] + +[project.urls] +Homepage = "https://github.com/varun-r-mallya/pylibbpf" +Repository = "https://github.com/varun-r-mallya/pylibbpf" +Issues = "https://github.com/varun-r-mallya/pylibbpf/issues" + [tool.mypy] files = "setup.py" -python_version = "3.7" +python_version = "3.8" strict = true show_error_codes = true enable_error_code = ["ignore-without-code", "redundant-expr", "truthy-bool"] @@ -19,7 +54,6 @@ warn_unreachable = true module = ["ninja"] ignore_missing_imports = true - [tool.pytest.ini_options] minversion = "6.0" addopts = ["-ra", "--showlocals", "--strict-markers", "--strict-config"] @@ -31,20 +65,39 @@ filterwarnings = [ testpaths = ["tests"] [tool.cibuildwheel] +build = ["cp38-*", "cp39-*", "cp310-*", "cp311-*", "cp312-*"] +skip = ["*-win32", "*-win_amd64", "*-macosx*", "pp*"] + +# Build for both x86_64 and aarch64 on Linux +archs = ["x86_64", "aarch64"] + +# Linux-specific settings +[tool.cibuildwheel.linux] +before-all = [ + "yum install -y epel-release || apt-get update", + "yum install -y libbpf-devel kernel-headers || apt-get install -y libbpf-dev linux-headers-generic", +] + +# Skip testing on emulated architectures (too slow) +test-skip = ["*_aarch64", "*_arm64"] + +# Test command for supported architectures test-command = "pytest {project}/tests" test-extras = ["test"] -test-skip = ["*universal2:arm64"] -# Setuptools bug causes collision between pypy and cpython artifacts + before-build = "rm -rf {project}/build" +environment = { CMAKE_BUILD_PARALLEL_LEVEL = "2" } + [tool.ruff] -target-version = "py37" +target-version = "py38" +line-length = 88 [tool.ruff.lint] extend-select = [ - "B", # flake8-bugbear - "I", # isort - "PGH", # pygrep-hooks - "RUF", # Ruff-specific - "UP", # pyupgrade + "B", # flake8-bugbear + "I", # isort + "PGH", # pygrep-hooks + "RUF", # Ruff-specific + "UP", # pyupgrade ] diff --git a/setup.py b/setup.py index 03f97fb..4a6e20f 100644 --- a/setup.py +++ b/setup.py @@ -50,20 +50,19 @@ class CMakeBuild(build_ext): f"-DCMAKE_BUILD_TYPE={cfg}", # not used on MSVC, but no harm ] build_args = [] + # Adding CMake arguments set as environment variable # (needed e.g. to build for ARM OSx on conda-forge) if "CMAKE_ARGS" in os.environ: cmake_args += [item for item in os.environ["CMAKE_ARGS"].split(" ") if item] - # In this example, we pass in the version to C++. You might not need to. - cmake_args += [f"-DEXAMPLE_VERSION_INFO={self.distribution.get_version()}"] + # Pass version to CMake + cmake_args += [f"-DPYLIBBPF_VERSION_INFO={self.distribution.get_version()}"] - if self.compiler.compiler_type != "msvc": + # Linux-specific build configuration + if sys.platform.startswith("linux"): # Using Ninja-build since it a) is available as a wheel and b) - # multithreads automatically. MSVC would require all variables be - # exported for Ninja to pick it up, which is a little tricky to do. - # Users can override the generator with CMAKE_GENERATOR in CMake - # 3.15+. + # multithreads automatically. if not cmake_generator or cmake_generator == "Ninja": try: import ninja @@ -76,31 +75,25 @@ class CMakeBuild(build_ext): except ImportError: pass - else: - # Single config generators are handled "normally" - single_config = any(x in cmake_generator for x in {"NMake", "Ninja"}) + # Handle cross-compilation for different architectures on Linux + # This is useful for building ARM binaries on x86 systems or vice versa + target_arch = os.environ.get("TARGET_ARCH", "") + if target_arch: + if target_arch in ["arm64", "aarch64"]: + cmake_args += [ + "-DCMAKE_SYSTEM_PROCESSOR=aarch64", + "-DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc", + "-DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++", + ] + elif target_arch in ["arm", "armv7l"]: + cmake_args += [ + "-DCMAKE_SYSTEM_PROCESSOR=arm", + "-DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc", + "-DCMAKE_CXX_COMPILER=arm-linux-gnueabihf-g++", + ] - # CMake allows an arch-in-generator style for backward compatibility - contains_arch = any(x in cmake_generator for x in {"ARM", "Win64"}) - - # Specify the arch if using MSVC generator, but only if it doesn't - # contain a backward-compatibility arch spec already in the - # generator name. - if not single_config and not contains_arch: - cmake_args += ["-A", PLAT_TO_CMAKE[self.plat_name]] - - # Multi-config generators have a different way to specify configs - if not single_config: - cmake_args += [ - f"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY_{cfg.upper()}={extdir}" - ] - build_args += ["--config", cfg] - - if sys.platform.startswith("darwin"): - # Cross-compile support for macOS - respect ARCHFLAGS if set - archs = re.findall(r"-arch (\S+)", os.environ.get("ARCHFLAGS", "")) - if archs: - cmake_args += ["-DCMAKE_OSX_ARCHITECTURES={}".format(";".join(archs))] + elif not sys.platform.startswith("linux"): + raise RuntimeError("pylibbpf is only supported on Linux platforms") # Set CMAKE_BUILD_PARALLEL_LEVEL to control the parallel build level # across all generators. @@ -123,18 +116,39 @@ class CMakeBuild(build_ext): ) -# The information here can also be placed in setup.cfg - better separation of -# logic and declaration, and simpler if you include description/version in a file. +# Read long description from README +readme_path = Path(__file__).parent / "README.md" +long_description = "" +if readme_path.exists(): + long_description = readme_path.read_text(encoding="utf-8") + setup( - name="cmake_example", + name="pylibbpf", version="0.0.1", - author="Dean Moldovan", - author_email="dean0x7d@gmail.com", - description="A test project using pybind11 and CMake", - long_description="", - ext_modules=[CMakeExtension("cmake_example")], + author="varun-r-mallya, r41k0u", + author_email="varunrmallyagmail.com", + description="Python Bindings for Libbpf", + long_description=long_description, + long_description_content_type="text/markdown", + url="https://github.com/varun-r-mallya/pylibbpf", + ext_modules=[CMakeExtension("pylibbpf")], cmdclass={"build_ext": CMakeBuild}, zip_safe=False, + classifiers=[ + "Development Status :: 3 - Alpha", + "Intended Audience :: Developers", + "License :: OSI Approved :: Apache Software License", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: C++", + "Topic :: Software Development :: Libraries :: Python Modules", + "Topic :: System :: Operating System Kernels :: Linux", + ], extras_require={"test": ["pytest>=6.0"]}, - python_requires=">=3.7", + python_requires=">=3.8", ) diff --git a/src/main.cpp b/src/main.cpp index d50bce7..1f992d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,12 +9,12 @@ int add(int i, int j) { namespace py = pybind11; -PYBIND11_MODULE(cmake_example, m) { +PYBIND11_MODULE(pylibbpf, m) { m.doc() = R"pbdoc( Pybind11 example plugin ----------------------- - .. currentmodule:: cmake_example + .. currentmodule:: pylibbpf .. autosummary:: :toctree: _generate diff --git a/tests/test_basic.py b/tests/test_basic.py index 070626e..f0417db 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1,5 +1,4 @@ -import cmake_example as m - +import pylibbpf as m def test_main(): assert m.__version__ == "0.0.1"