Unverified Commit 050df1a7 authored by Daniel Nagy's avatar Daniel Nagy
Browse files

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
parent bd1033c3
Loading
Loading
Loading
Loading
+5 −3
Original line number Diff line number Diff line
@@ -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";
  };
})