Skip to content

keystone-engine: add build-keystone-engine.yml for riscv64 wheels - #1775

Merged
luhenry merged 7 commits into
mainfrom
keystone-engine
Sep 12, 2026
Merged

keystone-engine: add build-keystone-engine.yml for riscv64 wheels#1775
luhenry merged 7 commits into
mainfrom
keystone-engine

Conversation

@luhenry

@luhenry luhenry commented Sep 11, 2026

Copy link
Copy Markdown
Member

Compiles keystone's vendored LLVM 3.9 MC-layer/codegen fork (Arm, Arm64, Hexagon, Mips, PowerPC, Sparc, SystemZ, X86, EVM) via CMake and packages the resulting libkeystone.so behind the ctypes bindings in bindings/python. Upstream publishes no riscv64 wheel.

Mirrors upstream's python-publish.yml.

Differs from upstream

  • Patches llvm/cmake/config-ix.cmake to recognise riscv64 as a host arch - this LLVM 3.9-era snapshot predates RISC-V support in LLVM itself by years.
  • Installs cmake via dnf in CIBW_BEFORE_ALL_LINUX - setup.py shells out to it directly and it is not a declared build dependency; pypi.riseproject.dev lags PyPI's own latest cmake.
  • Replaces the vendored llvm/cmake/config.guess (stamped 2011) with automake's copy, and sets CMAKE_POLICY_VERSION_MINIMUM=3.5 - both needed for cmake compatibility on this arch/toolchain.

Matrix: manylinux only, matching upstream's own Linux matrix exactly (python-publish.yml never builds musllinux either). setup.py's bdist_wheel --plat-name insertion hardcodes manylinux1_ + platform.machine() regardless of actual libc - a bug capstone's near-identical bindings/python/setup.py already fixed upstream (capstone#2445) - so auditwheel refuses to repair that GLIBC-tagged wheel into a musllinux one.

Testing

  • Upstream's CI runs no tests at all. bindings/python/sample.py is the closest thing to a test upstream ships, but one of its own calls (b"add eax, 15h" under the default non-RADIX16 syntax) raises KS_ERR_ASM_INVALIDOPERAND independent of any target this build produces - a numeric-literal parsing quirk in the demo script itself, not riscv64-specific. CIBW_TEST_COMMAND inlines the rest of sample.py's known-good calls instead, exercising ks.asm() across every architecture keystone supports (x86, arm, arm64, hexagon, mips, ppc, sparc, systemz).

License: OK

Built on cp312-manylinux_riscv64; all eight architecture asm() calls passed.

Ports keystone-engine 0.9.2's Python bindings, following upstream's own
python-publish.yml. setup.py builds the vendored LLVM 3.9 MC-layer/codegen
fork (Arm, Arm64, Hexagon, Mips, PowerPC, Sparc, SystemZ, X86 + EVM) via
CMake (make-share.sh) and loads the resulting libkeystone.so via ctypes;
no ext_modules and bdist_wheel universal=1 collapse the build to one
cp312 job per libc (manylinux + musllinux).
CIBW_TEST_SOURCES copies bindings/python/sample.py to the test dir
preserving its project-relative path, not flattened to the test dir
root, so the test command needs the same bindings/python/ prefix.
Both manylinux and musllinux riscv64 images carry a newer cmake (>= 4)
ahead on PATH by default, which hard-rejects the vendored
CMakeLists.txt's cmake_policy(SET CMP0051 OLD) call outright. Install
cmake from the image's own package manager (< 4, still honours it) and
put it first on PATH.

Also replace the vendored llvm/cmake/config.guess (stamped 2011, predates
riscv64) with automake's copy, which already recognises it - otherwise
CMake's GetHostTriple.cmake fails with 'unable to guess system type'.
config-ix.cmake's LLVM_NATIVE_ARCH table predates RISC-V's addition to
upstream LLVM and has no riscv64 case, so cmake dies with 'Unknown
architecture riscv64' before target selection. Add a riscv64 branch
mapping to an inert placeholder, matching how any host arch absent
from keystone's own 8-target build already behaves (native JIT stays
unavailable, which keystone's own code never uses).
…arget

llvm-build (utils/llvm-build/llvmbuild/main.py) validates --native-target
against real LLVMBuild target components independently of config-ix.cmake's
own LLVM_TARGETS_TO_BUILD check, so the placeholder from the previous patch
('RISCV', not a real component in this tree) failed there with 'invalid
native target: RISCV (not in project)'. llvm-build already special-cases
the literal string 'Unknown' to mean no native target; use that instead.
Prepending /usr/bin onto PATH to win over the pre-baked >= 4 cmake also
shadowed musllinux's system python, which broke cibuildwheel's own
'python on PATH must match its managed interpreter' check. Shadow only
cmake via a dedicated one-binary PATH entry instead.

Also replace the CIBW_TEST_COMMAND: the manylinux leg got far enough to
run it and hit KS_ERR_ASM_INVALIDOPERAND on sample.py's own
b"add eax, 15h" call (default non-RADIX16 syntax rejects the bare hex
suffix - a numeric-literal parsing quirk in the demo script itself, not
riscv64-specific). Inline the rest of sample.py's known-good calls
instead of running the script as-is.
The manylinux leg built, tested, and repaired cleanly. musllinux fails
at the auditwheel-repair step: setup.py's bdist_wheel --plat-name
insertion hardcodes 'manylinux1_' + platform.machine() regardless of
actual libc (a bug capstone's near-identical bindings/python setup.py
already fixed upstream, capstone#2445, by dropping the hardcoded
prefix), so auditwheel refuses to repair a GLIBC-tagged wheel into a
musllinux one. Upstream's own python-publish.yml never builds
musllinux either - dropping it here isn't a reduced port, it matches
upstream's actual Linux matrix exactly.
@luhenry
luhenry merged commit 2c3f8c8 into main Sep 12, 2026
7 checks passed
@luhenry
luhenry deleted the keystone-engine branch September 12, 2026 19:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant