added python support (#44)

This commit is contained in:
Cocoa 2024-02-27 15:10:48 +08:00 committed by GitHub
parent 8751606fc0
commit f1298215cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 140 additions and 0 deletions

View File

@ -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

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
# Python files
build/
dist/
*.egg-info/
*.pyc

View File

@ -37,6 +37,8 @@ int main()
## Let's ruapu
### ruapu with C
<table>
<tr><td>
@ -77,6 +79,35 @@ xop = 0
</td></tr>
</table>
### ruapu with Python
<table>
<tr><td>
Compile and install ruapu library
```shell
# from source code
pip3 install .
```
</td>
<td>
Use ruapu in python
```shell
$ python3
>>> import ruapu
>>> ruapu.supports("avx2")
True
>>> ruapu.supports(isa="avx2")
True
...
```
</td></tr>
</table>
<details>
<summary>Github-hosted runner result (Linux)</summary>

25
pyproject.toml Normal file
View File

@ -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 = ["."]

37
ruapu-py.c Normal file
View File

@ -0,0 +1,37 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#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);
}

18
setup.py Normal file
View File

@ -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)