moonbit-native-sdk

    Generate and verify C and .NET SDKs for MoonBit native libraries.

    Download zip
    Version
    0.2.0
    License
    Apache-2.0
    Last updated
    8 hours ago
    Downloads
    12

    #MoonBit Native SDK

    Build a Linux x64 or Windows x64 SDK from a MoonBit native library and a JSON interface. The SDK contains the shared library, a C header, C# P/Invoke declarations, and an ABI baseline for checking the next release. Consumers can build and run their programs with a C or .NET toolchain.

    #Build and verify

    Use MoonBit and Moon from toolchain build 20261126 or a compatible revision, Python 3.10 or later, and a .NET SDK that can build for .NET 8. On Linux x64 and WSL, install a C compiler and GNU binutils. On Windows x64, use an x64 Native Tools Command Prompt with MSVC and the Windows SDK. Run from the native filesystem on each platform.

    python3 scripts/sdk.py verify

    py -3 scripts/sdk.py verify

    The command builds bytes, image, and parsec. It builds one standalone C program and one standalone .NET program per example and runs them against the new shared libraries. For bytes, it reuses both compiled hosts with a compatible SDK that adds an export. The ABI checker also checks an interface that removes an export and changes a parameter's type.

    The published directories are under _build/sdk/linux-x64 and _build/sdk/win-x64. C applications include sdk.h and link the .so on Linux or the import .lib on Windows. Place the shared library on the runtime search path or use the colocated layout from the example hosts. .NET applications include Native.cs in an unsafe project, import the SDK's namespace, and place the native library alongside the managed assembly.

    #Publish an SDK

    Declare your native package with pkgtype(kind: "foreign_library"). Export its implementation functions with #export_name and list the public names and private implementation symbols in the interface.

    python3 scripts/sdk.py build \ --module examples/bytes \ --interface examples/bytes/interface.json \ --output _build/bytes-sdk

    Use --package lib if the module builds multiple artifacts. On Windows, run the same command with py -3 and PowerShell line continuations or put it on one line. The publisher builds the foreign archive, links the wrapper, and checks the actual shared library export table against the ABI manifest.

    Give --output a directory dedicated to one SDK. Reusing it for the same library replaces the artifacts after validation. Existing unrelated files or a different SDK in that directory cause publication to stop.

    Pass --baseline path/to/previous/abi.json when publishing a compatible release. The following command checks an interface before building.

    moon run --target native cmd/main -- check path/to/previous/abi.json path/to/interface.json

    The standalone generator is also available via moon run --target native cmd/main -- generate INTERFACE EXISTING_OUTPUT_DIR. It emits the C wrapper and the linker export files along with the header, P/Invoke declarations, and ABI baseline.

    #Verify against a released SDK

    Use the SDK archive for your platform and the matching Git tag. This runs the original C and .NET host sources against the released libraries, builds the current libraries against the released baselines, then runs the same host binaries with the current libraries.

    python3 scripts/verify_release.py --source-ref v0.1.0 --sdk-zip moonbit-native-sdk-0.1.0-linux-x64.zip

    On Windows, use py -3 and the win-x64 archive. Pass --example to check a single SDK.

    #Interface format

    Schema 1 requires a library name, "threading": "serialized", named status codes, and a list of functions. Each function declares its public name, its implementation symbol, its return type, and its ordered params. Both public and implementation names use the library name followed by an underscore as a prefix. Parameters declare name, type, and direction.

    TypeC boundaryC# boundary
    i32, u32, i64, u64int32_t, uint32_t, int64_t, uint64_tint, uint, long, ulong
    f32, f64float, doublefloat, double
    bytesuint8_t * and a uint64_t byte lengthbyte* and a ulong byte length
    utf8char * and a uint64_t byte lengthbyte* and a ulong byte length
    handleOpaque SDK pointervoid*
    statusint32_t return valueint return value

    An out parameter adds one level of indirection to each value, including a span's length. An in borrowed C span uses a const pointer. Status functions can use inputs and outputs. Primitive-only functions can return a numeric type or void. Reserve status ok for 0 and invalid_argument for 1. Additional status codes are distinct positive integers.

    Pointer inputs declare "ownership": "borrowed" and stay valid for the full call. The wrapper accepts a null span pointer with a zero length and checks all output pointers. Provider adapters validate data such as UTF-8 and limits specific to their implementation. Out pointers must refer to distinct writable storage. Adapters document which statuses produce useful outputs. The parsec example returns an owned error handle with its parse-failed status.

    Owned pointer outputs declare "ownership": "owned" and a release export of the same type. A release function returns void and takes exactly one in parameter marked "ownership": "consumed". Pass an owned span's returned pointer and length to its release function. A null release is a no-op. Release live values once, in the same loaded SDK that produced them.

    The C wrapper calls moonbit_init() once and serializes native calls from host threads within each loaded library. Provider code must avoid re-entering the SDK during a call. The generated wrapper passes pointer-shaped arguments to MoonBit using #external pointer types and passes scalars by value. A MoonBit export returning Unit uses the compiler's internal int32_t result, which the wrapper discards to expose a C void function.

    The baseline records the public interface without private implementation symbols. Changes to existing exports, parameter types and direction, ownership and release functions, or status values are reported as incompatible. Additions and private implementation changes are compatible. Behavioral changes still depend on tests from real hosts.