From 050df1a7e6391db584d5bb88da6a845c26f19375 Mon Sep 17 00:00:00 2001 From: Daniel Nagy Date: Wed, 29 May 2024 00:45:00 +0200 Subject: [PATCH] go: Support WASI platform With this change it is now possible to build simple Go packages into WebAssembly and WASI. For example, it is now possible to build `wazero` to wasm: ```sh $ file $(nix-build -A pkgsCross.wasi32.wazero --no-out-link)/bin/wazero /nix/store/pzzzsnmyjjrxi8gmadksn99rgfbxqrp2-wazero-static-wasm32-unknown-wasi-1.7.2/bin/wazero: WebAssembly (wasm) binary module version 0x1 (MVP) ``` https://go.dev/blog/wasi --- pkgs/development/compilers/go/1.22.nix | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkgs/development/compilers/go/1.22.nix b/pkgs/development/compilers/go/1.22.nix index c0103090b331..aad242c88606 100644 --- a/pkgs/development/compilers/go/1.22.nix +++ b/pkgs/development/compilers/go/1.22.nix @@ -37,6 +37,7 @@ let "riscv64" = "riscv64"; "s390x" = "s390x"; "x86_64" = "amd64"; + "wasm32" = "wasm"; }.${platform.parsed.cpu.name} or (throw "Unsupported system: ${platform.parsed.cpu.name}"); # We need a target compiler which is still runnable at build time, @@ -90,7 +91,7 @@ stdenv.mkDerivation (finalAttrs: { ./go_no_vendor_checks-1.22.patch ]; - GOOS = stdenv.targetPlatform.parsed.kernel.name; + GOOS = if stdenv.targetPlatform.isWasi then "wasip1" else stdenv.targetPlatform.parsed.kernel.name; GOARCH = goarch stdenv.targetPlatform; # GOHOSTOS/GOHOSTARCH must match the building system, not the host system. # Go will nevertheless build a for host system that we will copy over in @@ -113,7 +114,8 @@ stdenv.mkDerivation (finalAttrs: { GOARM = toString (lib.intersectLists [ (stdenv.hostPlatform.parsed.cpu.version or "") ] [ "5" "6" "7" ]); GO386 = "softfloat"; # from Arch: don't assume sse2 on i686 - CGO_ENABLED = 1; + # Wasi does not support CGO + CGO_ENABLED = if stdenv.targetPlatform.isWasi then 0 else 1; GOROOT_BOOTSTRAP = if useGccGoBootstrap then goBootstrap else "${goBootstrap}/share/go"; @@ -184,7 +186,7 @@ stdenv.mkDerivation (finalAttrs: { homepage = "https://go.dev/"; license = licenses.bsd3; maintainers = teams.golang.members; - platforms = platforms.darwin ++ platforms.linux; + platforms = platforms.darwin ++ platforms.linux ++ platforms.wasi; mainProgram = "go"; }; })