feat: add TinyGo

This commit is contained in:
AJ ONeal
2023-11-05 07:54:16 +00:00
parent 4fe71af9d0
commit a0222527c0
4 changed files with 237 additions and 0 deletions

94
tinygo/README.md Normal file
View File

@@ -0,0 +1,94 @@
---
title: TinyGo
homepage: https://tinygo.org
tagline: |
TinyGo: The Power of Go, in Kilobytes.
---
To update or switch versions, run `webi tinygo@stable` (or `@v0.30`, `@beta`,
etc).
## Cheat Sheet
> TinyGo is an alternate, llvm-based compiler for Go with a much smaller and
> simpler runtime with a minimum size of _kilobytes_ - suitable for
> micro-controllers, embedded devices, and good old fashioned CLI utilities that
> don't need a high-performance garbage collector.
```sh
GOOS=linux GOARCH=arm64 \
tinygo build -short -no-debug \
-o hello-v1.0.0-linux-arm64
```
You may also want to install the Go IDE tooling:
[go-essentials](/go-essentials).
## Table of Contents
- Files
- Effective TinyGo
- Compatibility
- Gotchas
### Files
These are the files / directories that are created and/or modified with this
install:
```text
~/.config/envman/PATH.env
~/.local/opt/tinygo/
~/.local/opt/go/
~/go/
```
### Effective TinyGo
Core differences from Go's self-hosted compiler:
- optimizes for stack usage over heap usage
- avoids reflection
- simpler (but slower) garbage collection
- reimplements some of Go's standard library for performance
By following certain patterns, you can avoid forcing heap allocation and get
better memory usage and performance.
See:
- TinyGo: Heap Allocation:
<https://tinygo.org/docs/concepts/compiler-internals/heap-allocation/>.
- TinyGo: A New Compiler:
<https://tinygo.org/docs/concepts/faq/why-a-new-compiler/>
### Go Compatibility
Your _Go_, _TinyGo_, and dependency versions (particularly `golang.org/x`) will
need to be paired.
See the [Go Compatibility Matrix][go-compat]
[go-compat]: https://tinygo.org/docs/reference/go-compat-matrix/
### Gotchas
**Standard Library**
- avoid packages that rely heavily on reflection, like `encoding/json`
**Windows**:
- not all Windows syscalls are implemented
**macOS**:
- you may need to install a conflict-free version of `llvm` from
[conflict-free brew](../brew/) to get small sizes when cross-compiling
See also:
- TinyGo: Language Support: <https://tinygo.org/docs/reference/lang-support/>
- TinyGo: Gotchas: <https://tinygo.org/docs/guides/tips-n-tricks/>
- TinyGo: Differences from Go:
<https://tinygo.org/docs/concepts/compiler-internals/differences-from-go/>

67
tinygo/install.ps1 Normal file
View File

@@ -0,0 +1,67 @@
#!/usr/bin/env pwsh
#################
# Install tinygo #
#################
# Every package should define these variables
$pkg_cmd_name = "tinygo"
$pkg_dst_cmd = "$HOME\.local\opt\tinygo\bin\tinygo.exe"
$pkg_dst_dir = "$HOME\.local\opt\tinygo"
$pkg_dst = "$pkg_dst_dir"
$pkg_src_cmd = "$HOME\.local\opt\tinygo-v$Env:WEBI_VERSION\bin\tinygo.exe"
$pkg_src_bin = "$HOME\.local\opt\tinygo-v$Env:WEBI_VERSION\bin"
$pkg_src_dir = "$HOME\.local\opt\tinygo-v$Env:WEBI_VERSION"
$pkg_src = "$pkg_src_dir"
New-Item "$HOME\Downloads\webi" -ItemType Directory -Force | Out-Null
$pkg_download = "$HOME\Downloads\webi\$Env:WEBI_PKG_PATHNAME"
# Fetch archive
IF (!(Test-Path -Path "$pkg_download")) {
Write-Output "Downloading tinygo from $Env:WEBI_PKG_URL to $pkg_download"
& curl.exe -A "$Env:WEBI_UA" -fsSL "$Env:WEBI_PKG_URL" -o "$pkg_download.part"
& Move-Item "$pkg_download.part" "$pkg_download"
}
# Fetch Go compiler
Write-Output "Checking for Go compiler..."
IF (-Not (Get-Command -Name "go" -ErrorAction Silent)) {
& "$Env:USERPROFILE\.local\bin\webi-pwsh.ps1" go
$Env:Path = "$HOME\.local\opt\go\bin;$Env:Path"
$Env:Path = "$HOME\go\bin;$Env:Path"
[System.Environment]::SetEnvironmentVariable(
"Path",
$env:Path,
[System.EnvironmentVariableTarget]::User)
}
IF (!(Test-Path -Path "$pkg_src")) {
Write-Output "Installing tinygo"
# TODO: create package-specific temp directory
# Enter tmp
Push-Location .local\tmp
# Remove any leftover tmp cruft
Remove-Item -Path ".\tinygo*" -Recurse -ErrorAction Ignore
# Unpack archive file into this temporary directory
# Windows BSD-tar handles zip. Imagine that.
Write-Output "Unpacking $pkg_download"
& tar xf "$pkg_download"
# Settle unpacked archive into place
Write-Output "Install Location: $pkg_src"
Move-Item -Path ".\tinygo*" -Destination "$pkg_src_dir"
# Exit tmp
Pop-Location
}
Write-Output "Copying into '$pkg_dst' from '$pkg_src'"
Remove-Item -Path "$pkg_dst" -Recurse -ErrorAction Ignore | Out-Null
# no New-Item because $HOME\.local\opt" always exists
Copy-Item -Path "$pkg_src" -Destination "$pkg_dst" -Recurse

53
tinygo/install.sh Normal file
View File

@@ -0,0 +1,53 @@
#!/bin/sh
# shellcheck disable=SC2034
# "'pkg_cmd_name' appears unused. Verify it or export it."
__init_tinygo() {
set -e
set -u
##################
# Install tinygo #
##################
# Every package should define these 6 variables
pkg_cmd_name="tinygo"
pkg_dst_cmd="$HOME/.local/opt/tinygo/bin/tinygo"
pkg_dst_dir="$HOME/.local/opt/tinygo"
pkg_dst="$pkg_dst_dir"
pkg_src_cmd="$HOME/.local/opt/tinygo-v$WEBI_VERSION/bin/tinygo"
pkg_src_dir="$HOME/.local/opt/tinygo-v$WEBI_VERSION"
pkg_src="$pkg_src_dir"
# pkg_install must be defined by every package
pkg_install() {
echo "Checking for Go compiler..."
if ! command -v go 2> /dev/null; then
"$HOME/.local/bin/webi" go
export PATH="$HOME/.local/opt/go/bin:$PATH"
fi
# ~/.local/opt/tinygo-v0.30.0/
mkdir -p "$(dirname "${pkg_src_dir}")"
# mv ./tinygo*/ ~/.local/opt/tinygo-v0.30.0/
mv ./tinygo*/ "${pkg_src_dir}"
}
# pkg_get_current_version is recommended, but not required
pkg_get_current_version() {
# 'tinygo --version' has output in this format:
# tinygo version 0.30.0 darwin/amd64 (using go version go1.21.1 and LLVM version 16.0.1)
# This trims it down to just the version number:
# 0.30.0
tinygo version 2> /dev/null |
head -n 1 |
cut -d' ' -f3
}
}
__init_tinygo

23
tinygo/releases.js Normal file
View File

@@ -0,0 +1,23 @@
'use strict';
var github = require('../_common/github.js');
var owner = 'tinygo-org';
var repo = 'tinygo';
module.exports = function (request) {
return github(request, owner, repo).then(function (all) {
// all.releases = all.releases.filter(function (rel) {
// return !rel.name.endsWith('.deb');
// });
return all;
});
};
if (module === require.main) {
module.exports(require('@root/request')).then(function (all) {
all = require('../_webi/normalize.js')(all);
// just select the first 5 for demonstration
all.releases = all.releases.slice(0, 5);
console.info(JSON.stringify(all, null, 2));
});
}