{"id":26519683,"url":"https://github.com/speakez-llc/farscape","last_synced_at":"2025-03-21T11:23:15.799Z","repository":{"id":283107672,"uuid":"950017672","full_name":"speakez-llc/Farscape","owner":"speakez-llc","description":"F# Native Library Binding Generator","archived":false,"fork":false,"pushed_at":"2025-03-18T15:23:32.000Z","size":79,"stargazers_count":2,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-18T16:30:04.348Z","etag":null,"topics":["clang","cpp","dotnet","fsharp"],"latest_commit_sha":null,"homepage":"","language":"F#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/speakez-llc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-17T13:58:05.000Z","updated_at":"2025-03-18T16:19:08.000Z","dependencies_parsed_at":"2025-03-18T16:30:10.468Z","dependency_job_id":"5d782042-f584-4b86-b935-f6f23e6b3793","html_url":"https://github.com/speakez-llc/Farscape","commit_stats":null,"previous_names":["speakez-llc/farscape"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/speakez-llc%2FFarscape","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/speakez-llc%2FFarscape/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/speakez-llc%2FFarscape/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/speakez-llc%2FFarscape/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/speakez-llc","download_url":"https://codeload.github.com/speakez-llc/Farscape/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244787662,"owners_count":20510203,"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":["clang","cpp","dotnet","fsharp"],"created_at":"2025-03-21T11:23:15.109Z","updated_at":"2025-03-21T11:23:15.788Z","avatar_url":"https://github.com/speakez-llc.png","language":"F#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Farscape: F# Native Library Binding Generator\n\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nFarscape is a command-line tool that aims to automatically generate idiomatic F# bindings for C/C++ libraries, preserving F# intrinsics. It leverages LibClang through CppSHarp to parse C++ headers and produces F# code that can be directly used in both native and .NET based F# applications (and we have plans to eventually support C#/.NET as well through fluent API bindings).\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\" width=\"100%\"\u003e\n      \u003cstrong\u003e⚠️ Caution: Experimental ⚠️\u003c/strong\u003e\u003cbr\u003e\n      This project is in early development and not ready for production use.\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n## Features\n\n- **C++ Header Parsing**: Uses CppSharp/LibClang to accurately parse C++ header files\n- **Idiomatic F# Code Generation**:\n  - C++ namespaces to F# modules for harmony between APIs\n  - C++ unions/variants to F# discriminated unions for type-safe pattern matching\n  - C++ const-correctness principles translated to idiomatic F# immutability\n  - C++ templates to F# generics and SRTPs for type-safe polymorphism\n  - More Demanding C++ templates could be supported by F# type providers\n- **P/Invoke Support**: Automatically creates proper P/Invoke declarations for native functions\n- **Type Mapping**: Precise numeric types __*with matching bit widths and signedness*__\n- **Cross-Platform Bindings**: Generated code will work seamlessly with dependencies built for Windows, Linux, macOS, as well as mobile and IoT platforms\n- **Project Generation**: 🚧 Targets the creation of complete F# projects ready for building 🚧\n- **Documentation**: C++ documentation transferred as F# XML docs for consistent developer experience in all IDEs and code editor environments.\n\n---\n![alt text](img/Farscape_social.png)\n---\n### Current Implementation\n\nWe've successfully created a working implementation for cJSON.h that:\n- Automatically extracts function declarations, structs, and type definitions\n- Generates appropriate P/Invoke declarations with correct calling conventions\n- Maps C++ types to their F# equivalents (though with some conversion issues like char* → byte)\n- Produces usable bindings that can be incorporated into F# projects\n\nThe current solution is intentionally focused on cJSON as a proof of concept. This simplified approach paves the way to a progressive implementation of more supported features. Eventually more complex C++ projects will be \"placed on the bench\" to determine how best to support more complicated APIs. The end result will eventually yield a mature tool that will cover a majority of support scenarios.\n\n### Generalization Requirements\n\nTo fulfill Farscape's vision of supporting any C++ library, the implementation needs to be generalized:\n\n1. **Develop a more complex header parsing system**:\n    - Enhance CppSharp integration for more detailed and varied header parsing\n    - Support standard C/C++ constructs across various library styles\n    - Handle platform-specific details and preprocessor directives\n\n2. **Improve type mapping**:\n    - Refine string handling (currently mapping to byte instead of proper string marshaling)\n    - Broader support for complex types, structs, and templates\n    - Transform C++ function pointers into F# functional delegate definitions including built-in lifecycle management\n\n3. **Support diverse library patterns**:\n    - Handle C-style libraries like cJSON\n    - Support C++ classes and object-oriented patterns\n    - Accommodate different calling conventions and export styles\n\nThe current hybrid approach for cJSON demonstrates the feasibility of this vision, and serves as a template for generalization to other libraries.\n\n___\n![alt text](\u003cimg/Screenshot 2025-03-18 113946.png\u003e)\n---\n\n## Prerequisites\n\n- [.NET 9.0 SDK](https://dotnet.microsoft.com/download/dotnet/9.0) or later\n- [LLVM/Clang](https://releases.llvm.org/download.html) with development components (for LibClang)\n\n## Installation\n\n### From Source\n\n```bash\n# Clone the repository\ngit clone https://github.com/speakez-llc/farscape.git\ncd farscape\n\n# Build the project\n./build.ps1\n\n# Install as a global tool\ndotnet tool install --global --add-source ./src/Farscape.Cli/nupkg farscape\n```\n\n## Usage\n\n```bash\n# Basic usage\nfarscape --header path/to/header.h --library libname\n\n# With additional options\nfarscape --header [path/to/header.h] \\\n         --library [libname] \\\n         --output ./output \\\n         --namespace [MyProject].Bindings \\\n         --include-paths /usr/include,/usr/local/include \\\n         --verbose\n```\n\n### Command Line Options\n\n| Option | Description |\n|--------|-------------|\n| `--header`, `-h` | Path to the C++ header file (required) |\n| `--library`, `-l` | Name of the native library to bind to (required) |\n| `--output`, `-o` | Output directory for the generated F# project (default: ./output) |\n| `--namespace`, `-n` | Namespace prefix for the generated F# code (default: NativeBindings) |\n| `--include-paths`, `-i` | Additional include paths for C++ header parsing (comma separated) |\n| `--verbose`, `-v` | Enable verbose output |\n\n## Examples\n\n### Basic Example\n\nAssume you have a simple C library with a header like this:\n\n```c\n// math_lib.h\n#pragma once\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n// Adds two integers\nint add(int a, int b);\n\n// Multiplies two doubles\ndouble multiply(double a, double b);\n\n#ifdef __cplusplus\n}\n#endif\n```\n\nGenerate F# bindings with:\n\n```bash\nfarscape --header math_lib.h --library mathlib\n```\n\nThe generated F# code will look like:\n\n```fsharp\nnamespace NativeBindings\n\nopen System\nopen System.Runtime.InteropServices\n\nmodule NativeBindings =\n    /// \u003csummary\u003e\n    /// Adds two integers\n    /// \u003c/summary\u003e\n    [\u003cDllImport(\"mathlib\", CallingConvention = CallingConvention.Cdecl)\u003e]\n    extern int add(int a, int b)\n    \n    /// \u003csummary\u003e\n    /// Multiplies two doubles\n    /// \u003c/summary\u003e\n    [\u003cDllImport(\"mathlib\", CallingConvention = CallingConvention.Cdecl)\u003e]\n    extern double multiply(double a, double b)\n```\n\n## Advanced Topics\n\n### Self-Hosting as Working Proof\n\nFarscape will eventually be used to generate F# bindings for LibClang itself, creating a self-hosting cycle.\n\n### MLIR/LLVM Integration\n\nThe architecture is designed with potential pairing with F#-built executables in mind - [built with MLIR/LLVM lowering](https://github.com/speakez-llc/fsharp-mlir-hello), enabling compilation to native code as well as support the current standard .NET runtime.\n\n### Delegate Pointer Handling\n\nSpecial handling for C++ function pointers and delegates will be included, with support for marshaling between F# functions and C++ callbacks.\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request.\n\n1. Fork the repository\n2. Create your feature branch (`git checkout -b feature/amazing-feature`)\n3. Commit your changes (`git commit -m 'Add some amazing feature'`)\n4. Push to the branch (`git push origin feature/amazing-feature`)\n5. Open a Pull Request\n\n## License\n\nThis project is licensed under the MIT License - see the LICENSE file for details.\n\n## Acknowledgments\n\n- [CppSharp](https://github.com/mono/CppSharp) for its deep coverage of LibClang\n- [LLVM/Clang](https://llvm.org/) project for LibClang\n- [SpectreCoff](https://github.com/EluciusFTW/SpectreCoff) and Spectre.Console that it wraps\n- F# community for inspiration and support\n- .NET runtime for P/Invoke support\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspeakez-llc%2Ffarscape","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspeakez-llc%2Ffarscape","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspeakez-llc%2Ffarscape/lists"}