{"id":20345872,"url":"https://github.com/klitsche/ffigen","last_synced_at":"2025-04-12T00:41:12.164Z","repository":{"id":57007456,"uuid":"262419546","full_name":"klitsche/ffigen","owner":"klitsche","description":"ffigen is a simple cli helper to quickly generate and update PHP FFI bindings for C libraries.","archived":false,"fork":false,"pushed_at":"2023-01-21T22:36:06.000Z","size":133,"stargazers_count":13,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-25T20:22:06.232Z","etag":null,"topics":["ffi","generator","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/klitsche.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-05-08T20:14:26.000Z","updated_at":"2024-08-27T14:53:09.000Z","dependencies_parsed_at":"2023-02-12T13:01:27.902Z","dependency_job_id":null,"html_url":"https://github.com/klitsche/ffigen","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klitsche%2Fffigen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klitsche%2Fffigen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klitsche%2Fffigen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/klitsche%2Fffigen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/klitsche","download_url":"https://codeload.github.com/klitsche/ffigen/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248411271,"owners_count":21098875,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ffi","generator","php"],"created_at":"2024-11-14T22:10:08.410Z","updated_at":"2025-04-12T00:41:12.141Z","avatar_url":"https://github.com/klitsche.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ffigen - a FFI bindings generator for PHP\n\n[![Build Status](https://github.com/klitsche/ffigen/actions/workflows/test.yml/badge.svg)](https://github.com/klitsche/ffigen/actions/workflows/test.yml)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/74ba131ab73c58dc2864/test_coverage)](https://codeclimate.com/github/klitsche/ffigen/test_coverage)\n[![Maintainability](https://api.codeclimate.com/v1/badges/74ba131ab73c58dc2864/maintainability)](https://codeclimate.com/github/klitsche/ffigen/maintainability)\n[![Packagist Version](https://img.shields.io/packagist/v/klitsche/ffigen)](https://packagist.org/packages/klitsche/ffigen)\n\n`ffigen` is a simple cli helper to quickly generate and update low level PHP FFI bindings for C libraries. \n\nIt generates two PHP files out of provided C header file(s):\n\n* `constants.php` - holding constant values\n* `Methods.php` - holding function bindings as static methods plus phpdoc in a trait\n\nIt is heavily inspired by [FFIMe](https://github.com/ircmaxell/FFIMe) and depends on [PHPCParser](https://github.com/ircmaxell/php-c-parser) by ircmaxell. \n\n__WIP__: Expect breaking changes along all 0.* pre-releases.\n\n## Requirements\n\n* PHP ^7.4 || ^8.0\n* For examples: FFI extension must be available and enabled\n\n## Quick Start\n\nInstall in your project:\n\n    composer require --dev klitsche/ffigen\n    \nInstall a c library (eg. uuid).\n\nAdd a config file to your project root:\n\n    .ffigen.yml\n    \nTweak this config file (example):\n\n```yaml\nheaderFiles:\n  - uuid/uuid.h\nlibraryFile: libuuid.so.1\nparserClass: Klitsche\\FFIGen\\Examples\\UUID\\FFIGen\\Parser\noutputPath: ./\nexcludeConstants:\n  - /^(?!(FFI|UUID)_).*/\nexcludeMethods:\nnamespace: Klitsche\\FFIGen\\Examples\\UUID\n```\n\nOptional: add your own Parser class to customize pre oder post processing logic (example):\n\n```php\n\u003c?php\n\ndeclare(strict_types=1);\n\nnamespace Klitsche\\FFIGen\\Examples\\UUID\\FFIGen;\n\nuse Klitsche\\FFIGen\\Config;\n\nclass Parser extends \\Klitsche\\FFIGen\\Adapter\\PHPCParser\\Parser\n{\n    public function __construct(Config $config)\n    {\n        parent::__construct($config);\n\n        $this-\u003econtext-\u003edefineInt('_SYS_TYPES_H', 1);\n        $this-\u003econtext-\u003edefineInt('_SYS_TIME_H', 1);\n        $this-\u003econtext-\u003edefineInt('_TIME_H', 1);\n    }\n\n    protected function parseHeaderFile(string $file): array\n    {\n        $file = $this-\u003esearchHeaderFilePath($file);\n\n        $prependHeaderFile = '\n            typedef long time_t;\n        ';\n        $tmpfile = tempnam(sys_get_temp_dir(), 'ffigen');\n        file_put_contents($tmpfile, $prependHeaderFile . file_get_contents($file));\n\n        $declarations = parent::parseHeaderFile($tmpfile);\n\n        unlink($tmpfile);\n\n        return $declarations;\n    }\n\n    private function searchHeaderFilePath(string $file): string\n    {\n        if (file_exists($file)) {\n            return $file;\n        }\n        foreach ($this-\u003econtext-\u003eheaderSearchPaths as $headerSearchPath) {\n            if (file_exists($headerSearchPath . '/' . $file)) {\n                return $headerSearchPath . '/' . $file;\n            }\n        }\n\n        throw new \\RuntimeException(sprintf('File not found: %s', $file));\n    }\n}\n```\n\nDo not forget to register the Parser namespace in your composer.json for autoloading (dev is okay):\n\n```json\n    \"autoload-dev\": {\n        \"psr-4\": {\n            \"Klitsche\\\\FFIGen\\\\Examples\\\\\": \"examples\"\n        }\n    },\n```\n\nDump autoloading with \n\n    composer dump-autoload\n\nRun ffigen to generate binding files\n\n    vendor/bin/ffigen\n    \nThis generates the two files in the output path:\n\n* `constants.php` - add this to your autoloading\n* `Methods.php` - add this to your own class context and use it within your own high level php library\n\nDo not forget to add `constants.php` to your compose.json for autoloading:\n\n```json\n    \"autoload\": {\n        \"files\": [\n          \"tweak-path-to/constants.php\",\n        ]\n    },\n```\n\n## Play with examples\n\nBuild docker image with preinstalled c libraries (uuid, snappy \u0026 librdkafka):\n\n     docker-compose build php74\n        \nRun uuid example\n\n    docker-compose run --rm php74 php bin/ffigen generate -c examples/UUID/.ffigen.yml\n    docker-compose run --rm php74 php examples/UUID/test.php\n    \nRun snappy example (see Snappy class for a simple high level example)\n\n    docker-compose run --rm php74 bin/ffigen generate -c examples/Snappy/.ffigen.yml\n    docker-compose run --rm php74 php examples/Snappy/test.php\n        \nRun rdkafka example (librdkafka 1.5.2 \u0026 mock cluster)\n\n    docker-compose run --rm php74 bin/ffigen generate -c examples/RdKafka/.ffigen.yml\n    docker-compose run --rm php74 php examples/RdKafka/test.php\n        \n## Todos\n\n* [x] Add travis \n* [x] Add more tests \n* [ ] Add documentation\n* [ ] Add support for Windows, macOS\n* [ ] Add more examples (and learn from them)\n* [ ] Think about multi version support\n* [ ] Think about custom interface / class generation for types\n* [ ] Think about clang / cpp / readelf adapter (cpp defines only \u0026 clean file, clang -c11 ast-dump=json, readelf --dyn-syms)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklitsche%2Fffigen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fklitsche%2Fffigen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fklitsche%2Fffigen/lists"}