libperl-macrogen
Rust library and CLI tool for generating Rust FFI bindings from C macro functions and inline functions in Perl header files.
Pre-alpha Release: This project is in early development. APIs may change without notice.
Overview
libperl-macrogen parses C header files (particularly Perl's internal headers) and generates Rust wrapper functions for:
- C macro functions - Converted to Rust
unsafe fnwith type inference - Inline functions - Extracted and converted to Rust equivalents
This tool is designed to complement rust-bindgen, which cannot handle C macros.
Installation
Or add to your Cargo.toml:
[]
= "0.1"
Apidoc data
The crate bundles a pre-extracted snapshot of perlapi documentation
(apidoc.tar.gz, ~1.9 MiB) that the type inferencer needs to
disambiguate macro return types. This means no network access is
required at build time — works under docs.rs's --network none
sandbox, in air-gapped CI, etc.
- When building from a checkout of this repository (with the
apidoc/directory present),build.rsre-tars locally each build so edits toapidoc/v5.X.jsonpropagate. - When building from crates.io, the bundled
apidoc.tar.gzis copied intoOUT_DIRdirectly. - Advanced override: set
LIBPERL_APIDOC_URLto download a different apidoc dataset (offline mirror, pre-release data, ...) instead.
Usage
CLI
# Generate Rust wrapper functions from Perl headers
# Output to stdout (for inspection)
# With rustfmt validation
Options
| Option | Description |
|---|---|
--auto |
Auto-detect Perl include paths and defines from Config.pm |
--gen-rust |
Generate Rust code for macros and inline functions |
--bindings <FILE> |
Path to bindgen-generated Rust bindings (for type inference) |
-o <FILE> |
Output file (stdout if omitted) |
--strict-rustfmt |
Fail if generated code doesn't pass rustfmt |
-I <DIR> |
Add include directory |
-D <MACRO> |
Define a macro |
Library API
The library provides a Pipeline API with three phases:
- Preprocess - Parse C header files with macro expansion
- Infer - Perform type inference on macros and inline functions
- Generate - Generate Rust code
Simple Usage (build.rs)
use File;
use Pipeline;
Step-by-Step Execution
For more control, you can execute each phase separately:
use File;
use Pipeline;
Pipeline Builder Options
builder
// Preprocessor options
.with_auto_perl_config? // Auto-detect from Perl's Config.pm
.add_include_path
.add_define
.with_target_dir
// Inference options
.with_bindings // bindgen output for type info
.with_apidoc_path // Perl API documentation
// Codegen options
.with_strict_rustfmt // Fail if rustfmt fails
.with_codegen_defaults // Apply default codegen settings
.build?
.generate?;
Features
- C preprocessor with full macro expansion
- C parser for declarations, expressions, and inline function bodies
- Type inference from function call context and Perl's apidoc (
embed.fnc) - GCC extensions (
__attribute__,__typeof__, statement expressions, etc.) - Automatic Perl configuration detection via
Config.pm
Acknowledgments
This project was inspired by and references the implementation of TinyCC, originally created by Fabrice Bellard. The preprocessor and parser design draws from TinyCC's elegant approach to C compilation.
Special thanks to the TinyCC community for continuing the development and maintenance of TinyCC. Their work served as a valuable reference throughout this project's development.
License
This project is licensed under the GNU Lesser General Public License v2.1 or later (LGPL-2.1-or-later), following TinyCC's licensing.
Author
hkoba (CPAN ID: HKOBA)
Related Projects
- libperl-rs - Rust bindings for Perl