← 输入框|github.com|刷新
Skip to content

ccall: allow to use a header file #1613

Open

Description

@certik

Right now, every @ccall always generates a C forward declaration (in the C backend). In addition, we would also like to have a mode, something like:

@ccall(header="some_header.h")
def f(x: i32) -> f64:
    pass

@ccall(header="some_header.h")
def g(x: i32) -> f64:
    pass

# later
e = f(5)
h = g(5)

Which would generate the following code:

#include "some_header.h"

...
e = f(5)
h = g(5)

and it would not forward declare f and it would only include some_header.h once. So in particular, if f is actually a C macro declared in the some_header.h file, it would also just work.

Alternative syntax:

from ltypes import headers

h = headers("some_header.h")
@ccall(h)
def dummy():
    pass

@ccall
def f(x: i32) -> f64:
    pass

# later
e = f(5)

We might need to figure out the best design, but we need this feature.

Activity

  1. certik commented on Mar 23, 2023

    @certik
    ContributorAuthor

    To simplify, one idea might be:

    with ltypes.header("some_header.h"):
        def f(x: i32) -> f64:
            pass
    
        def g(x: i32) -> f64:
            pass

    Another idea:

    with ltypes.library(headers=["some_header.h"], lib="something"):
        def f(x: i32) -> f64:
            pass
    
        def g(x: i32) -> f64:
            pass

    And this would use the header file with the C backend, and it would use the lib (with the proper prefix and suffix that is platform dependent) with the pure Python mode as well as the LLVM mode.

  2. self-assigned this
    on Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions