module WinMD::LibCFuns

Overview

Discovers the C functions that the installed Crystal's standard library declares in lib LibC for the target platform, so the generator can avoid redeclaring them (a fun with the same C name in another lib is a redefinition error unless the signatures are identical).

The list is read from the compiler found on PATH (or WINMD_CRYSTAL) at generation time rather than baked into winmd, so it always matches the Crystal that will compile the bindings. It covers every declaration under src/lib_c/<target>/ plus the platform's src/crystal/system/<os>/ files, not just what a minimal program's prelude happens to require: which stdlib files a user's program pulls in cannot be known here, so any name Crystal declares for the target is treated as taken.

Each declaration is parsed with Crystal's own parser so the generator also knows the parameter and return types LibC uses. With those it can emit a wrapper that forwards to the stdlib declaration instead of only commenting the function out (see WinMD::Function#libc_call).

Defined in:

winmd/libc_funs.cr

Constant Summary

FUN_DECLARATION = /^\s*fun\s+([A-Za-z_][A-Za-z0-9_]*)/
PRIMITIVE_TYPES = Set {"Void", "Nil", "Bool", "Int8", "Int16", "Int32", "Int64", "Int128", "UInt8", "UInt16", "UInt32", "UInt64", "UInt128", "Float32", "Float64", "Pointer", "StaticArray", "Proc", "Tuple"}

Crystal's own types, which a bare name inside a lib still refers to. Anything else written without a namespace (DWORD, Int, SizeT, ULONG_PTR) is a type the lib declares, possibly in another file or inside a {% if flag?(...) %} block the parser does not expand.

Class Method Summary

Class Method Detail

def self.default_target(crystal : String) : String #

Target triple reported by crystal --version, e.g. x86_64-pc-windows-msvc.


[View source]
def self.discover(crystal : String = WinMD.crystal_executable, target : String | Nil = nil) : Array(String) #

C symbol names of all funs declared for target by the stdlib of crystal.


[View source]
def self.discover_signatures(crystal : String = WinMD.crystal_executable, target : String | Nil = nil) : Hash(String, Signature) #

Signatures of all funs declared for target, keyed by C symbol name (the name a generated fun would collide with).


[View source]
def self.lib_c_directory(stdlib : Path, triple : String) : Path #

src/lib_c/<dir> for a target triple. Crystal names these directories without the vendor part: x86_64-pc-windows-msvc -> x86_64-windows-msvc, aarch64-apple-darwin -> aarch64-darwin, x86_64-unknown-linux-gnu -> x86_64-linux-gnu.


[View source]
def self.lib_c_directory_name(triple : String) : String #

[View source]
def self.load_list(path : Path) : Array(String) #

One name per line, # comments allowed; used instead of discovery when the generator must run without a Crystal compiler available.


[View source]
def self.parse_source(source : String, require_path : String) : Array(Signature) #

[View source]
def self.parse_sources(sources : Array(Tuple(String, String))) : Array(Signature) #

Parses Crystal sources ({source, require_path} pairs) and returns every fun declared inside a lib.

Declarations the parser does not see, because the file uses syntax newer than the parser built into winmd or because the fun sits inside a {% if flag?(...) %} block (whose body is only expanded by the compiler), are still found by a textual scan and reported with their name only, so they are not redeclared even though no wrapper can be generated.


[View source]
def self.stdlib_source(crystal : String) : Path #

src directory of the stdlib: the CRYSTAL_PATH entry that holds lib_c.


[View source]
def self.system_subdir(triple : String) : String #

Platform-specific stdlib sources that also declare LibC functions.


[View source]
def self.type_text(node : Crystal::ASTNode, lib_name : String) : String #

Spells a type restriction from a lib declaration so it resolves from generated code: names the lib declares get an absolute lib prefix, and Foo* / -> Bar become Pointer(Foo) / Proc(Bar), which are valid where a type is used as a value.


[View source]