Created
March 22, 2022 20:12
-
-
Save p7g/62d0a31701d374068406d1aab0f839eb to your computer and use it in GitHub Desktop.
Generates the HPy example C file and setup.py https://docs.hpyproject.org/en/latest/api.html#a-simple-example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import textwrap | |
from csnake import ( | |
AddressOf, | |
CodeWriter, | |
Function, | |
TextModifier, | |
Variable, | |
) | |
cw = CodeWriter() | |
cw.include("<stddef.h>") | |
cw.include("<hpy.h>") | |
# Macros | |
HPyDef_METH = Function( | |
"HPyDef_METH", | |
arguments=[ | |
("name", "ident"), | |
("name_str", "char *"), | |
("impl_name", "ident"), | |
("sig", "int"), | |
], | |
) | |
HPy_MODINIT = Function("HPy_MODINIT", arguments=[("name", "ident")]) | |
# Functions, declared externally | |
HPy_Absolute = Function( | |
"HPy_Absolute", "HPy", arguments=[("ctx", "HPyContext*"), ("arg", "HPy")] | |
) | |
HPyModule_Create = Function( | |
"HPyModule_Create", | |
"HPy", | |
arguments=[ | |
("ctx", "HPyContext*"), | |
("moddef", "HPyModuleDef*"), | |
], | |
) | |
cw.add_line( | |
HPyDef_METH.generate_call( | |
"myabs", | |
'"myabs"', | |
"myabs_impl", | |
"HPyFunc_O", | |
) | |
) | |
myabs_impl = Function( | |
"myabs_impl", | |
"HPy", | |
["static"], | |
[("ctx", "HPyContext*"), ("self", "HPy"), ("arg", "HPy")], | |
) | |
fcw = CodeWriter() | |
fcw.add_line("return " + HPy_Absolute.generate_call("ctx", "arg") + ";") | |
myabs_impl.add_code(fcw) | |
cw.add_function_definition(myabs_impl) | |
cw.add_variable_initialization( | |
Variable( | |
"SimpleMethods", | |
"HPyDef*", | |
qualifiers=["static"], | |
value=[ | |
AddressOf(TextModifier("myabs")), | |
TextModifier("NULL"), | |
], | |
) | |
) | |
cw.add_variable_initialization( | |
Variable( | |
"simple", | |
"HPyModuleDef", | |
qualifiers=["static"], | |
value={ | |
"m_name": "simple", | |
"m_doc": "HPy Example", | |
"m_size": -1, | |
"defines": TextModifier("SimpleMethods"), | |
"legacy_methods": TextModifier("NULL"), | |
}, | |
) | |
) | |
cw.add_line(HPy_MODINIT.generate_call("simple")) | |
init_simple_impl = Function( | |
"init_simple_impl", | |
"HPy", | |
arguments=[ | |
("ctx", "HPyContext*"), | |
], | |
) | |
mod_create_call = HPyModule_Create.generate_call( | |
"ctx", AddressOf(TextModifier("simple")) | |
) | |
init_simple_impl.add_code(f"return {mod_create_call};") | |
cw.add_function_definition(init_simple_impl) | |
cw.write_to_file("simple.c") | |
with open("setup.py", "w") as f: | |
f.write( | |
textwrap.dedent( | |
""" | |
from setuptools import setup, Extension | |
from os import path | |
setup( | |
name="simple", | |
hpy_ext_modules=[ | |
Extension( | |
"simple", | |
sources=[path.join(path.dirname(__file__), "simple.c")], | |
), | |
], | |
setup_requires=["hpy"], | |
) | |
""" | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment