Skip to content

ptxmac/rules_tinygo

Repository files navigation

Tinygo rules for Bazel

Caution

EXPERIMENTAL

This package provides rules for building Go code with TinyGo.

Dependencies

Make sure rules_go is configured in your project.

Setup

In WORKSPACE:

Start by loading the rules:

git_repository(
    name = "rules_tinygo",
    commit = "0677286b5a5ceeb077044019152f5e72b23c37c9",
    remote = "https://github.com/ptxmac/rules_tinygo.git",
)

To use the TinyGo rules, both the tinygo and binaryen tools must be downloaded for the host architecture.

TINYGO_VERSION = "0.31.2"
BINARYEN_VERSION = "117"

tinygo_download(
    name = "tinygo_darwin_arm64",
    sha256 = "5b9ff15881bd23eb44ccd0e6c917db11e65c5532d654fc7198e6f6289aa0449d",
    urls = ["https://github.com/tinygo-org/tinygo/releases/download/v{0}/tinygo{0}.darwin-arm64.tar.gz".format(TINYGO_VERSION)],
)

binaryen_download(
    name = "binaryen_darwin_arm64",
    prefix = "binaryen-version_{0}".format(BINARYEN_VERSION),
    sha256 = "f2d962ff294b38ea3cfbbae8f6c728089d9375a57bac9a1880eb86779d6d3a84",
    urls = ["https://github.com/WebAssembly/binaryen/releases/download/version_{0}/binaryen-version_{0}-arm64-macos.tar.gz".format(BINARYEN_VERSION)],
)

Then all toolchains must be registered:

register_toolchains(
    "@tinygo_darwin_arm64//:toolchain_darwin_arm64",
    "@binaryen_darwin_arm64//:binaryen_darwin_arm64",
)

Usage

load("@rules_tinygo//:def.bzl", "tinygo_binary")

tinygo_binary( name = "hello_wasi", srcs = ["hello.go"], out = "hello.wasi", target = "wasi", )

Rules

tinygo_binary

tinygo_binary(name, srcs, out, go_sdk, target)

Compiles a Go binary using TinyGo.

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this target. Name required
srcs Source files to compile. List of labels optional []
out Output binary. Label optional None
go_sdk Go SDK to use. Label optional "@go_sdk"
target Target architecture. String optional ""

binaryen_download

binaryen_download(name, prefix, repo_mapping, sha256, urls)

Downloads and extracts the Binaryen tools.

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this repository. Name required
prefix - String optional ""
repo_mapping In WORKSPACE context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).

This attribute is not supported in MODULE.bazel context (when invoking a repository rule inside a module extension's implementation function).
Dictionary: String -> String optional
sha256 - String required
urls - List of strings required

tinygo_download

tinygo_download(name, repo_mapping, sha256, urls)

ATTRIBUTES

Name Description Type Mandatory Default
name A unique name for this repository. Name required
repo_mapping In WORKSPACE context only: a dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.

For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target).

This attribute is not supported in MODULE.bazel context (when invoking a repository rule inside a module extension's implementation function).
Dictionary: String -> String optional
sha256 - String required
urls - List of strings required

---- SNIP ----

Bazel rules for tinygo

Installation

From the release you wish to use: https://github.com/ptxmac/rules_tinygo/releases copy the WORKSPACE snippet into your WORKSPACE file.

To use a commit rather than a release, you can point at any SHA of the repo.

For example to use commit abc123:

  1. Replace url = "https://github.com/ptxmac/rules_tinygo/releases/download/v0.1.0/rules_tinygo-v0.1.0.tar.gz" with a GitHub-provided source archive like url = "https://github.com/ptxmac/rules_tinygo/archive/abc123.tar.gz"
  2. Replace strip_prefix = "rules_tinygo-0.1.0" with strip_prefix = "rules_tinygo-abc123"
  3. Update the sha256. The easiest way to do this is to comment out the line, then Bazel will print a message with the correct value. Note that GitHub source archives don't have a strong guarantee on the sha256 stability, see https://github.blog/2023-02-21-update-on-the-future-stability-of-source-code-archives-and-hashes/

About

A small set of bazel rules for TinyGo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published