From f1298215cb6d9b9d7e665fc87a01efc39d5a05ce Mon Sep 17 00:00:00 2001 From: Cocoa Date: Tue, 27 Feb 2024 15:10:48 +0800 Subject: [PATCH] added python support (#44) --- .github/workflows/ci.yml | 24 ++++++++++++++++++++++++ .gitignore | 5 +++++ README.md | 31 +++++++++++++++++++++++++++++++ pyproject.toml | 25 +++++++++++++++++++++++++ ruapu-py.c | 37 +++++++++++++++++++++++++++++++++++++ setup.py | 18 ++++++++++++++++++ 6 files changed, 140 insertions(+) create mode 100644 .gitignore create mode 100644 pyproject.toml create mode 100644 ruapu-py.c create mode 100644 setup.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f252589..d70e3a5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,6 +31,12 @@ jobs: run: | clang main.c -o ruapu-clang ./ruapu-clang + - name: build-test-python + run: | + python3 -m pip install pip -U + python3 -m pip install setuptools -U + pip3 install . + python3 -c 'import ruapu; print(ruapu.supports("neon")); print(ruapu.supports(isa="avx"))' macos: runs-on: macos-latest @@ -40,6 +46,12 @@ jobs: run: | clang main.c -o ruapu ./ruapu + - name: build-test-python + run: | + python3 -m pip install pip -U + python3 -m pip install setuptools -U + pip3 install . + python3 -c 'import ruapu; print(ruapu.supports("neon")); print(ruapu.supports(isa="avx"))' macos-arm64: runs-on: macos-14 @@ -49,6 +61,12 @@ jobs: run: | clang main.c -o ruapu ./ruapu + - name: build-test-python + run: | + python3 -m pip install pip -U + python3 -m pip install setuptools -U + pip3 install . + python3 -c 'import ruapu; print(ruapu.supports("neon")); print(ruapu.supports(isa="avx"))' windows: runs-on: windows-latest @@ -64,6 +82,12 @@ jobs: run: | gcc main.c -o ruapu-mingw.exe ./ruapu-mingw.exe + - name: build-test-python + run: | + python3 -m pip install pip -U + python3 -m pip install setuptools -U + pip3 install . + python3 -c 'import ruapu; print(ruapu.supports("neon")); print(ruapu.supports(isa="avx"))' qemu: runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b07d6d1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# Python files +build/ +dist/ +*.egg-info/ +*.pyc diff --git a/README.md b/README.md index 72c8f2a..6a705e3 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ int main() ## Let's ruapu +### ruapu with C +
@@ -77,6 +79,35 @@ xop = 0
+### ruapu with Python + + + + + +
+ +Compile and install ruapu library + +```shell +# from source code +pip3 install . +``` + + +Use ruapu in python + +```shell +$ python3 +>>> import ruapu +>>> ruapu.supports("avx2") +True +>>> ruapu.supports(isa="avx2") +True +... +``` +
+
Github-hosted runner result (Linux) diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..482069a --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,25 @@ +[build-system] +requires = ["setuptools", "cython"] +build-backend = "setuptools.build_meta" + +[project] +name = "ruapu" +authors = [ + {name = "nihui", email = "shuizhuyuanluo@126.com"}, + {name = "cocoa-xu", email = "i@uwucocoa.moe"}, +] +description = "The Python binding of ruapu." +readme = "README.md" +requires-python = ">=3.5" +keywords = ["ruapu", "cpu", "isa", "detect"] +license = {"text" = "MIT"} +version = "0.1.0" + +[project.urls] +"Homepage" = "https://github.com/nihui/ruapu" + +[tool.setuptools] +package-dir = {"" = "."} + +[tool.setuptools.packages.find] +where = ["."] diff --git a/ruapu-py.c b/ruapu-py.c new file mode 100644 index 0000000..c6ca524 --- /dev/null +++ b/ruapu-py.c @@ -0,0 +1,37 @@ +#define PY_SSIZE_T_CLEAN +#include + +#define RUAPU_IMPLEMENTATION +#include "ruapu.h" + +static PyObject *ruapu_supports_py(PyObject *self, PyObject *args, PyObject *kwargs) +{ + static char *kwlist[] = {"isa", NULL}; + const char *isa; + if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &isa)) + return NULL; + if (ruapu_supports(isa)) + Py_RETURN_TRUE; + Py_RETURN_FALSE; +} + +static PyMethodDef ruapu_methods[] = +{ + {"supports", ruapu_supports_py, METH_VARARGS | METH_KEYWORDS, "Check if the CPU supports an instruction set"}, + {NULL, NULL, 0, NULL} +}; + +static struct PyModuleDef ruapu_module = +{ + PyModuleDef_HEAD_INIT, + "ruapu", + "ruapu module", + -1, + ruapu_methods +}; + +PyMODINIT_FUNC PyInit_ruapu(void) +{ + ruapu_init(); + return PyModule_Create(&ruapu_module); +} diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..fb88143 --- /dev/null +++ b/setup.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from setuptools import setup, find_packages, Extension + +ext = Extension( + name = 'ruapu', + sources = ['ruapu-py.c'], + py_limited_api = True +) + +setup_args = dict( + packages = find_packages(where="."), + package_dir = {"": "."}, + ext_modules = [ext], +) + +setup(**setup_args)