nativelib

package
v0.11.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package nativelib is what the CLI and packages ffi and native share about the native libraries that Go packages bind: mygo-plugin.json, the file names of libraries on each platform, and where libraries are built and cached.

A package names its libraries in mygo-plugin.json, next to its sources:

{"libraries": [{
	"name": "adder",
	"build": {"cargo": "./rust"},
	"release": "https://github.com/me/app/releases/download/adder-v1",
	"files": {"darwin-arm64": {"name": "libadder.dylib", "url": "…", "sha256": "…"}, …}
}]}

build is how the CLI builds the library from its sources (a Rust crate with cargo, a Zig package with zig build, a Swift package with swift build, on macOS); files are the published builds, one per platform (GOOS-GOARCH), which `mygo native build` writes and apps download when they do not build the library themselves.

Index

Constants

View Source
const ManifestFile = "mygo-plugin.json"

ManifestFile is the file that names a package's native libraries.

View Source
const Universal = "darwin-universal"

Universal is the key of Library.Files for a build of macOS's two architectures in one file, as Swift libraries are published.

Variables

View Source
var Targets = []string{"darwin-arm64", "darwin-amd64", "linux-amd64", "linux-arm64", "windows-amd64", "windows-arm64"}

Targets are the platforms MyGo builds libraries for, as GOOS-GOARCH.

Functions

func CachePath

func CachePath(f File) (string, error)

CachePath returns where a published file is kept in the user's cache: <cache>/mygo/natives/<sha256>/<name>.

func Ext

func Ext(goos string) string

Ext returns the extension of dynamic libraries on goos.

func FileName

func FileName(name, goos string) string

FileName returns the file of the library named name on goos when no published file names another: lib<name>.dylib, lib<name>.so and <name>.dll, as cargo and zig build them.

func IsArchive

func IsArchive(name string) bool

IsArchive reports whether a published file is a .tar.gz of the library and the files that go next to it, as the frameworks a Swift package brings, rather than the library itself.

func LocalDir

func LocalDir(root, goos, goarch string) string

LocalDir returns where the CLI puts the libraries it builds from sources in the module at root for goos/goarch, which programs that are not packaged apps look into too: <root>/.mygo/native/<goos>-<goarch>.

func ModuleRoot

func ModuleRoot(dir string) (string, error)

ModuleRoot returns the directory of the go.mod that dir is in.

func Skipped

func Skipped(name string) bool

Skipped reports whether a file or directory of a library's sources is left out of them: hidden ones, and those cargo, zig and npm write (target, zig-out, zig-cache, zig-pkg, node_modules).

func SourceHash

func SourceHash(dir string, b *Build) (string, error)

SourceHash returns the hash of the sources of a library built in dir (the build's directory), which its arguments and macOS version are part of: the SHA-256 of the paths and contents of its files, but Skipped ones, with line endings as LF, so that checkouts on every platform agree.

Types

type Build

type Build struct {
	// Cargo is the directory of the Rust crate (its Cargo.toml), whose
	// crate-type includes "cdylib".
	Cargo string `json:"cargo,omitempty"`
	// Zig is the directory of the Zig package (its build.zig), which
	// installs a dynamic library.
	Zig string `json:"zig,omitempty"`
	// Swift is the directory of the Swift package (its Package.swift),
	// whose product named as the library is a dynamic library. Swift
	// libraries build on macOS, universal, and go into the app's
	// Frameworks with the frameworks they bring.
	Swift string `json:"swift,omitempty"`
	// Args are more arguments of `cargo build`, `zig build` or `swift
	// build`.
	Args []string `json:"args,omitempty"`
	// MacOS is the oldest macOS the library runs on, when it needs a later
	// one than the app's minimum or 12.0, as "13.0".
	MacOS string `json:"macOS,omitempty"`
}

Build is how a library is built from its sources: exactly one of Cargo, Zig and Swift, a directory relative to the package's, written "./rust" to read as one.

func (*Build) IsSwift

func (b *Build) IsSwift() bool

IsSwift reports whether the library builds with Swift.

func (*Build) MinMacOS

func (b *Build) MinMacOS(app string) string

MinMacOS returns the macOS a build for an app whose minimum is app ("" is 12.0) targets: the later of app's and the library's.

func (*Build) Tool

func (b *Build) Tool() (tool, dir string)

Tool returns the build's tool, "cargo", "zig" or "swift", and its directory.

type File

type File struct {
	Name   string `json:"name"`
	URL    string `json:"url,omitempty"`
	SHA256 string `json:"sha256"`
	// SourceHash is the hash of the sources the file was built from (see
	// SourceHash): the CLI builds the library of a local package from its
	// sources only when they changed since, so that the package's own
	// published files serve it until then, without Rust or Zig.
	SourceHash string `json:"sourceHash,omitempty"`
}

File is a published build of a library.

func (File) Check

func (f File) Check() error

Check checks that the file's name and SHA-256 can be parts of a path: a package may not name another file than the library it downloads.

type Library

type Library struct {
	// Name names the library, as ffi.New does: its files are
	// lib<name>.dylib, lib<name>.so and <name>.dll by default (a leading
	// "lib" is not doubled, and dropped on Windows).
	Name string `json:"name"`
	// Build builds the library from sources next to the package.
	Build *Build `json:"build,omitempty"`
	// Release is the URL of the directory the files are published in,
	// as https://github.com/<owner>/<repo>/releases/download/<tag>.
	Release string `json:"release,omitempty"`
	// Files are the published builds, by GOOS-GOARCH, or Universal for
	// one that serves both architectures of macOS.
	Files map[string]File `json:"files,omitempty"`
}

Library is a native library a package binds.

func (Library) File

func (l Library) File(goos, goarch string) (File, bool)

File returns the published build of the library for goos/goarch: the platform's own, or on macOS the universal one.

func (Library) FileName

func (l Library) FileName(goos, goarch string) string

FileName returns the library's file on goos/goarch.

type Manifest

type Manifest struct {
	Libraries []Library `json:"libraries"`
}

Manifest is the content of mygo-plugin.json.

func Parse

func Parse(data []byte) (Manifest, error)

Parse reads and checks a manifest.

func (Manifest) Lookup

func (m Manifest) Lookup(name string) (Library, bool)

Lookup returns the library named name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL