Skip to content

Commit

Permalink
Add support for cibuildwheel (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
kendryte747 authored Oct 10, 2024
1 parent 53c212d commit ae39ba3
Show file tree
Hide file tree
Showing 12 changed files with 230 additions and 95 deletions.
48 changes: 46 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ concurrency:
cancel-in-progress: true

jobs:
build-python:
name: build-python-${{matrix.config.name}}
build-wheel:
name: build-wheel-${{matrix.config.name}}
runs-on: ${{matrix.config.os}}
strategy:
matrix:
Expand All @@ -38,3 +38,47 @@ jobs:
- name: Build wheel
run: python -m cibuildwheel --output-dir wheelhouse

- name: Upload k230_flash-python Build Artifact
uses: actions/upload-artifact@v4
with:
name: k230_flash-python-${{matrix.config.name}}
path: ${{github.workspace}}/wheelhouse
if-no-files-found: error

pypi-publish:
name: Upload release to PyPI
runs-on: ubuntu-latest
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags')
needs: ["build-wheel"]
environment:
name: pypi
url: https://pypi.org/p/k230_flash
permissions:
id-token: write
steps:
- uses: actions/checkout@v4

- uses: actions/download-artifact@v4
with:
name: k230_flash-python-macos
path: ${{github.workspace}}/dist

- uses: actions/download-artifact@v4
with:
name: k230_flash-python-linux
path: ${{github.workspace}}/dist

- uses: actions/download-artifact@v4
with:
name: k230_flash-python-windows
path: ${{github.workspace}}/dist

- name: List dist
run: |
ls -alh ${{github.workspace}}/dist
- name: Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
verbose: true
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
__py_cache__
__pycache__
build
install
dist
wheelhouse

.pytest_cache
*.egg-info
*.spec
11 changes: 7 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
cmake_minimum_required(VERSION 3.18)

if(NOT DEFINED VERSION_INFO)
set(VERSION_INFO "0.0.1")
endif()
set(K230_FLASH_VERSION_MAJOR 0)
set(K230_FLASH_VERSION_MINOR 0)
set(K230_FLASH_VERSION_PATCH 2)

project(kburn VERSION ${VERSION_INFO})
set(K230_FLASH_VERSION_STRING ${K230_FLASH_VERSION_MAJOR}.${K230_FLASH_VERSION_MINOR}.${K230_FLASH_VERSION_PATCH})
message(STATUS "K230_FLASH_VERSION_STRING = ${K230_FLASH_VERSION_STRING}")

project(kburn_top)

# libkburn
add_subdirectory(src/kburn)
Expand Down
26 changes: 25 additions & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1 +1,25 @@
TDB
Copyright (c) 2022, Canaan Bright Sight Co., Ltd

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

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.

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.
25 changes: 2 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
# K230 Flash Python
# K230 Flash

## Requirements

```bash
sudo apt install gcc python3-venv pybind11-dev

# create virtual env, only first time
python3 -m vevn ~/.venv

# active virtual env, every time
source ~/.venv/bin/activate

pip3 install pybind11 wheel setuptools
```

## Build

```bash
# active virtual env, every time
source ~/.venv/bin/activate

python3 setup.py bdist_wheel
```
K230 Flash is a python tools to program Kendryte K230 and K230D Chips, Supports program firmware to `EMMC`, `SDCARD`, `SPI-NOR`, `SPI-NAND` and `OTP`

## Usage

Expand Down
13 changes: 9 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ classifiers = [
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: 3.13",
"License :: OSI Approved :: MIT License",
"License :: OSI Approved :: BSD License",
"Operating System :: OS Independent",
"Topic :: Utilities",
]
keywords = ["kendryte", "kflash", "k230"]
dependencies = []

[project.urls]
homepage = "https://github.com/kendryte747/k230_flash_py"
homepage = "https://github.com/kendryte/k230_flash"

[project.scripts]
k230_flash = "kburn.k230_flash:main"
Expand All @@ -35,17 +35,22 @@ requires = ["setuptools>=42", "wheel", "cmake>=3.18", "pybind11"]
build-backend = "setuptools.build_meta"

[tool.cibuildwheel]
build = ["cp38*", "cp39*", "cp310*", "cp311*", "cp312*", "cp313"]
build = ["cp38*", "cp39*", "cp310*", "cp311*", "cp312*", "cp313*"]
skip = "*musllinux*"
test-requires = "pytest"
test-command = [
"pytest {project}/tests/import"
]

[tool.cibuildwheel.windows]
archs = ["AMD64", "x86"]
archs = ["AMD64"]

[tool.cibuildwheel.linux]
archs = ["x86_64"]
before-all = [
"yum install -y libudev-devel",
]
repair-wheel-command = "LD_LIBRARY_PATH=/lib/kburn auditwheel repair -w {dest_dir} {wheel}"

[tool.cibuildwheel.macos]
archs = ["x86_64"]
Expand Down
26 changes: 23 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import io
import os
import re
import subprocess
Expand Down Expand Up @@ -30,6 +31,10 @@ def run(self):
self.build_cmake(ext)
super().run()

def is_in_cibuildwheel(self):
# maybe other env
return ('AUDITWHEEL_PLAT' in os.environ)

def build_cmake(self, ext: Extension):
"""The steps required to build the extension"""
self.announce("Preparing the build environment", level=3)
Expand All @@ -47,8 +52,10 @@ def build_cmake(self, ext: Extension):
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))

cmake_args = [f"-DVERSION_INFO={self.distribution.get_version()}"]
cmake_args = []
# cmake_args += [f"-DVERSION_INFO={self.distribution.get_version()}"]
cmake_args += ['-DPython3_ROOT_DIR=' + os.path.dirname(sys.executable)]
cmake_args += ['-DIS_CIBUILDWHEEL=' + ('ON' if self.is_in_cibuildwheel() else "OFF")]
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
Expand Down Expand Up @@ -106,9 +113,22 @@ def run(self):
"""Outfiles are the libraries that were built using cmake"""
self.outfiles = self.distribution.data_files

def find_version():
with io.open("CMakeLists.txt", encoding="utf8") as f:
version_file = f.read()

version_major = re.findall(r"K230_FLASH_VERSION_MAJOR (.+?)", version_file)
version_minor = re.findall(r"K230_FLASH_VERSION_MINOR (.+?)", version_file)
version_patch = re.findall(r"K230_FLASH_VERSION_PATCH (.+?)", version_file)

if version_major and version_minor and version_patch:
return version_major[0] + "." + version_minor[0] + "." + version_patch[0]

raise RuntimeError("Unable to find version string.")

setup(
name="k230_flash",
version="0.0.1",
version=find_version(),
author="kendryte747",
author_email="kendryte747@gmail.com",
description="K230 Burning Tool",
Expand All @@ -118,8 +138,8 @@ def run(self):
ext_modules=[CMakeExtension("_kburn")],
cmdclass={
'build_ext': BuildCMakeExt,
'install_lib': InstallCMakeLibs,
'install_data': InstallCMakeLibsData,
'install_lib': InstallCMakeLibs
},
entry_points={
'console_scripts': [
Expand Down
Loading

0 comments on commit ae39ba3

Please sign in to comment.