Go Platform
A minimal go platform.
Full Code
main.roc:
app [main] { pf: platform "platform/main.roc" } # Can segfault on some Ubuntu 20.04 CI machines, see #164. main = "Roc <3 Go!\n"
platform/main.roc:
platform "go-platform" requires {} { main : Str } exposes [] packages {} imports [] provides [mainForHost] mainForHost : Str mainForHost = main
platform/go.mod:
module roc-with-go go 1.21.5
platform/main.go:
package main
/*
#cgo LDFLAGS: -L. -lapp
#include "./host.h"
*/
const is64Bit = uint64 == ^uint64
//export roc_alloc
.Pointer
//export roc_realloc
.Pointer
//export roc_dealloc
//export roc_dbg
platform/host.h:
;
extern void ;
Build Instructions
The Roc compiler can't build a go platform by itself so we have to execute some commands manually. In the future there will be a standardized way for a platform to define the build process.
- Let's make sure we're in the right directory:
- We turn our Roc app into a library so go can use it:
- We build a go package using the platform directory.
pie
is used to create a Position Independent Executable. Roc expects a file called dynhost so that's what we'll provide.
- We use the subcommand
preprocess-host
to make the surgical linker preprocessor generate.rh
and.rm
files.
- With our platform built we can run our app:
Publish the platform
-
Make sure you've built the platform first.
-
We'll create a compressed archive of our platform so anyone can use it easily. You can use
tar.br
for maximal compression ortar.gz
if you're in a hurry:
-
Put the created
tar.br
on a server. You can use github releases like we do with basic-cli. -
Now you can use the platform from inside a Roc file with:
app [goUsingRocApp] { pf: platform "YOUR_URL" }
When running with a platform from a URL, the --prebuilt-platform
flag is not needed.
‼ This build procedure only builds the platform for your kind of operating system and architecture. If you want to support users on all Roc supported operating systems and architectures, you'll need this kind of setup.