|
| 1 | +name: release wheels |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [master] |
| 6 | + |
| 7 | +jobs: |
| 8 | + build: |
| 9 | + name: build - ${{ matrix.runs-on }} |
| 10 | + runs-on: ${{ matrix.runs-on }} |
| 11 | + permissions: |
| 12 | + contents: write |
| 13 | + strategy: |
| 14 | + fail-fast: false |
| 15 | + matrix: |
| 16 | + runs-on: [ubuntu-latest, ubuntu-24.04-arm, macos-latest] |
| 17 | + steps: |
| 18 | + - uses: actions/checkout@v6 |
| 19 | + |
| 20 | + - uses: actions/setup-python@v6 |
| 21 | + with: |
| 22 | + python-version: "3.14" |
| 23 | + |
| 24 | + - name: build and release wheels |
| 25 | + env: |
| 26 | + GH_TOKEN: ${{ github.token }} |
| 27 | + run: | |
| 28 | + pip install build |
| 29 | +
|
| 30 | + for toml in */pyproject.toml; do |
| 31 | + [ -f "$toml" ] || continue |
| 32 | + pkg="$(dirname "$toml")" |
| 33 | + version=$(python3 -c "import tomllib; print(tomllib.load(open('$toml', 'rb'))['project']['version'])") |
| 34 | + tag="${pkg}/v${version}" |
| 35 | +
|
| 36 | + echo "=========================================" |
| 37 | + echo "Building: $pkg v$version" |
| 38 | + echo "=========================================" |
| 39 | + python -m build --wheel "$pkg" |
| 40 | +
|
| 41 | + whl=$(ls "$pkg"/dist/*.whl) |
| 42 | + echo "Uploading: $whl" |
| 43 | + if gh release view "$tag" --repo "$GITHUB_REPOSITORY" &>/dev/null; then |
| 44 | + gh release upload "$tag" "$whl" --repo "$GITHUB_REPOSITORY" --clobber |
| 45 | + else |
| 46 | + gh release create "$tag" "$whl" \ |
| 47 | + --repo "$GITHUB_REPOSITORY" \ |
| 48 | + --title "$pkg v$version" \ |
| 49 | + --notes "Platform wheels for $pkg $version" |
| 50 | + fi |
| 51 | + done |
| 52 | +
|
| 53 | + publish-shims: |
| 54 | + name: publish releases branch |
| 55 | + needs: build |
| 56 | + runs-on: ubuntu-latest |
| 57 | + permissions: |
| 58 | + contents: write |
| 59 | + steps: |
| 60 | + - uses: actions/checkout@v6 |
| 61 | + |
| 62 | + - name: build and push shim packages |
| 63 | + run: | |
| 64 | + REPO_URL="https://github.com/${GITHUB_REPOSITORY}" |
| 65 | +
|
| 66 | + # set up a temp dir for the releases branch |
| 67 | + tmp="$(mktemp -d)" |
| 68 | + trap 'rm -rf "$tmp"' EXIT |
| 69 | +
|
| 70 | + for toml in */pyproject.toml; do |
| 71 | + [ -f "$toml" ] || continue |
| 72 | + pkg="$(dirname "$toml")" |
| 73 | + version=$(python3 -c "import tomllib; print(tomllib.load(open('$toml', 'rb'))['project']['version'])") |
| 74 | + tag="${pkg}/v${version}" |
| 75 | + module="${pkg//-/_}" |
| 76 | +
|
| 77 | + echo "=========================================" |
| 78 | + echo "Creating shim: $pkg v$version" |
| 79 | + echo "=========================================" |
| 80 | +
|
| 81 | + pkg_dir="$tmp/$pkg" |
| 82 | + mod_dir="$pkg_dir/$module" |
| 83 | + mkdir -p "$mod_dir" |
| 84 | +
|
| 85 | + # copy __init__.py from source |
| 86 | + cp "$pkg/$module/__init__.py" "$mod_dir/" |
| 87 | +
|
| 88 | + # pyproject.toml — same metadata, no package-data (toolchain comes from wheel) |
| 89 | + cat > "$pkg_dir/pyproject.toml" << TOML |
| 90 | + [build-system] |
| 91 | + requires = ["setuptools>=64", "wheel"] |
| 92 | + build-backend = "setuptools.build_meta" |
| 93 | +
|
| 94 | + [project] |
| 95 | + name = "$pkg" |
| 96 | + version = "$version" |
| 97 | + description = "ARM GCC toolchain for bare-metal targets (pre-built)" |
| 98 | + requires-python = ">=3.8" |
| 99 | +
|
| 100 | + [project.scripts] |
| 101 | + $(grep -A100 '^\[project\.scripts\]' "$pkg/pyproject.toml" | tail -n +2 | sed '/^\[/q' | grep -v '^\[') |
| 102 | +
|
| 103 | + [tool.setuptools.packages.find] |
| 104 | + include = ["${module}*"] |
| 105 | +
|
| 106 | + [tool.setuptools.package-data] |
| 107 | + $module = ["toolchain/**/*"] |
| 108 | + TOML |
| 109 | +
|
| 110 | + # setup.py — downloads pre-built wheel from GH releases and extracts toolchain |
| 111 | + cat > "$pkg_dir/setup.py" << 'SETUP' |
| 112 | + import os |
| 113 | + import platform |
| 114 | + import zipfile |
| 115 | + from io import BytesIO |
| 116 | + from urllib.request import urlopen |
| 117 | +
|
| 118 | + from setuptools.command.build_py import build_py |
| 119 | +
|
| 120 | + REPO_URL = "REPO_URL_PLACEHOLDER" |
| 121 | + TAG = "TAG_PLACEHOLDER" |
| 122 | + VERSION = "VERSION_PLACEHOLDER" |
| 123 | + MODULE = "MODULE_PLACEHOLDER" |
| 124 | +
|
| 125 | + PLATFORM_MAP = { |
| 126 | + ("Linux", "x86_64"): "linux_x86_64", |
| 127 | + ("Linux", "aarch64"): "linux_aarch64", |
| 128 | + ("Darwin", "arm64"): "macosx_11_0_arm64", |
| 129 | + } |
| 130 | +
|
| 131 | +
|
| 132 | + class InstallPrebuilt(build_py): |
| 133 | + """Download pre-built wheel from GitHub Releases and extract the toolchain.""" |
| 134 | +
|
| 135 | + def run(self): |
| 136 | + pkg_dir = os.path.dirname(os.path.abspath(__file__)) |
| 137 | + toolchain_dir = os.path.join(pkg_dir, MODULE, "toolchain") |
| 138 | +
|
| 139 | + if not os.path.exists(os.path.join(toolchain_dir, "bin")): |
| 140 | + key = (platform.system(), platform.machine()) |
| 141 | + plat = PLATFORM_MAP.get(key) |
| 142 | + if plat is None: |
| 143 | + raise RuntimeError(f"unsupported platform: {key}") |
| 144 | +
|
| 145 | + whl_name = f"{MODULE}-{VERSION}-py3-none-{plat}.whl" |
| 146 | + url = f"{REPO_URL}/releases/download/{TAG}/{whl_name}" |
| 147 | +
|
| 148 | + print(f"Downloading {url} ...") |
| 149 | + data = urlopen(url).read() |
| 150 | +
|
| 151 | + print("Extracting toolchain ...") |
| 152 | + with zipfile.ZipFile(BytesIO(data)) as zf: |
| 153 | + prefix = f"{MODULE}/toolchain/" |
| 154 | + # also handle .data/purelib/ layout |
| 155 | + alt_prefix = f"{MODULE}-{VERSION}.data/purelib/{MODULE}/toolchain/" |
| 156 | + for info in zf.infolist(): |
| 157 | + for p in (prefix, alt_prefix): |
| 158 | + if info.filename.startswith(p): |
| 159 | + rel = info.filename[len(p):] |
| 160 | + if not rel: |
| 161 | + continue |
| 162 | + dest = os.path.join(toolchain_dir, rel) |
| 163 | + if info.is_dir(): |
| 164 | + os.makedirs(dest, exist_ok=True) |
| 165 | + else: |
| 166 | + os.makedirs(os.path.dirname(dest), exist_ok=True) |
| 167 | + with open(dest, "wb") as f: |
| 168 | + f.write(zf.read(info)) |
| 169 | + # preserve executable bit |
| 170 | + if info.external_attr >> 16 & 0o111: |
| 171 | + os.chmod(dest, 0o755) |
| 172 | + break |
| 173 | +
|
| 174 | + super().run() |
| 175 | +
|
| 176 | +
|
| 177 | + def setup(): |
| 178 | + from setuptools import setup as _setup |
| 179 | + _setup(cmdclass={"build_py": InstallPrebuilt}) |
| 180 | +
|
| 181 | +
|
| 182 | + if __name__ == "__main__": |
| 183 | + setup() |
| 184 | + SETUP |
| 185 | +
|
| 186 | + # template in the actual values |
| 187 | + sed -i "s|REPO_URL_PLACEHOLDER|${REPO_URL}|" "$pkg_dir/setup.py" |
| 188 | + sed -i "s|TAG_PLACEHOLDER|${tag}|" "$pkg_dir/setup.py" |
| 189 | + sed -i "s|VERSION_PLACEHOLDER|${version}|" "$pkg_dir/setup.py" |
| 190 | + sed -i "s|MODULE_PLACEHOLDER|${module}|" "$pkg_dir/setup.py" |
| 191 | + done |
| 192 | +
|
| 193 | + # push to releases branch |
| 194 | + cd "$tmp" |
| 195 | + git init |
| 196 | + git checkout -b releases |
| 197 | + git add . |
| 198 | + git config user.name "github-actions[bot]" |
| 199 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 200 | + git commit -m "update shim packages" |
| 201 | + git remote add origin "https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git" |
| 202 | + git push -f origin releases |
0 commit comments