Framework
gir2swift-plugin
A SwiftPM build tool plugin that runs gir2swift automatically during swift build.
Overview
gir2swift-plugin integrates gir2swift into the Swift Package Manager build system as a build tool plugin. When a target declares the plugin, SwiftPM calls the plugin’s createBuildCommands method before compiling Swift sources. The plugin:
-
Locates the
gir2swift-manifest.yamlfor the target (target-local file takes precedence over the package-level file). -
Reads the primary GIR name from the manifest.
-
Scans the standard GIR installation paths to find a directory that contains the primary GIR file and all prerequisite GIR files discovered from the target’s dependency graph.
-
Declares all relevant input files (GIR files, manifest, and target-local configuration files) so SwiftPM can track changes and re-run the command when any input changes.
-
Predicts the set of output files (one Swift file per alphabetical bucket plus the fixed-suffix files).
-
Returns a single build command that invokes
gir2swift.
Adding the plugin to your package
// swift-tools-version: 5.10
import PackageDescription
let package = Package(
name: "MyPackage",
dependencies: [
.package(url: "https://github.com/rhx/gir2swift.git", branch: "main"),
],
targets: [
.target(
name: "MyLibraryTarget",
plugins: [
.plugin(name: "gir2swift-plugin", package: "gir2swift"),
]
),
]
)
Your target directory (or the package root) must contain a gir2swift-manifest.yaml, for example:
version: 1
gir-name: GLib-2.0
pkg-config: glib-2.0
output-directory: Sources/GLib
alpha-names: true
GIR search paths
The plugin checks the following directories in order and uses the first one that contains every required GIR file:
-
/opt/homebrew/share/gir-1.0(Homebrew on Apple Silicon) -
/usr/local/share/gir-1.0(Homebrew on Intel or manual installs) -
/usr/share/gir-1.0(Linux system packages)
Error handling
Discovery failures throw descriptive errors:
-
failedToGetGirNameFromManifest— the manifest does not declare a GIR name. -
failedToGetGirDirectory(containing:)— no single directory contains all required GIR files.