Help with porting golang

I had been about to start my own thread re: getting to a self hosted go compiler… but then it seemed maybe better to resurrect this old thread.

I’d love to have haiku as a daily driver and honestly Go is the big thing holding it back for me. Most of my work, both for pleasure and business, is in Go. There has also been talk of Go being needed for docker and podman. I’m sure there are lots of other great pieces of software that could be built for Haiku with golang available. It would also likely be possible to make a wrapper lib to support native haiku GUI development from Go… but first we need a decent working version (or at least something stable and newer than 1.4).

Is there a sense of how much dev time would be required to get us to the following:

  • A self-hosted go compiler (preferably with the rest of the toolchain)
  • Appropriate syscalls for haiku within the syscall section of the stdlib

My C abilities are nowhere near the level required to work on getting to self-hosting. :frowning: So, sadly I am of little direct help.

I imagine that if there was someone that could easily do this it would be done by now. However, sometimes money can be a motivating factor for choosing how to spend available time: would money put toward this be something that would make a difference? I would be happy to donate some funds (maybe a few hundred USD) to get this working and it is possible that others would also be interested in chipping in to get some focused dev time on this. That said, I am not sure that there is anyone that can do this that was holding back because they needed the funds… but I figured I’d ask.

3 Likes

I don’t think it’s possible to start from C anymore.

Basically you have two options:

  • Use the existing Go 1.4 to compile later versions, one by one, until you get an up to date one, or
  • Figure out how to compile Go on Linux so it emits Haiku executables, then use that to build Go for Haiku

In older versions of Go there used to be a way to build starting with a C compiler, but not anymore. The newer versions of Go are written in Go.

5 Likes

I started a cross compiling effort, but have not had much time yet. It seems to be off to a good start but there is a lot of work to be done. Wiki info here Building from Linux · tqh/go Wiki · GitHub, and the source tree contains my work.

(Note that when doing a port it is very important to document places that needs work, decisions made and other factors so they can be revisited as you progress further.)

5 Likes

This looks like a great start @tqh and I think @PulkoMandy is right and going this route (cross compiling) seems like it may be the better route at this point.

@tqh is this something you are wanting to do/spearhead (as time permits)? If so, is there any specific support or help the community here can provide? I’m not sure what I can offer (I do not know a lot about compiler internals or low level Haiku code/binary support), but I am open to researching things. It sounds like you do not have a lot of time on your hands to work on this, but if you manage to find time to open issues on that repo with what needs to be done (to your knowledge) maybe various members here could try to pick off some of it or help organize and tag it to enlist efforts from other developers.

3 Likes

@korli is probably further along, so pinging him here.
Some of the code in my tree might be useful (signals, TLS?), so can perhaps be good to look at my commit history if needed.

2 Likes

thanks @tqh

I’m posting what I have so far:

Build tested with Go 1.18.x on Linux amd64 from the src directory.

GOROOT_BOOTSTRAP=~/go-1.18-linux/ GOOS=haiku GOARCH=amd64 ./bootstrap.bash

The one problem is the use of fork/exec for go compilation which requires a good swap on Haiku. When it fails, one can launch again until it succeeds.
Runtime requires hrev56220 or newer.

10 Likes

Oooh, nice, can you give some info on status? Does the build then produce working go apps?

3 Likes

Using what you have posted @korli, I was also able to cross-compile from macOS to bootstrap a go 1.18.2 toolchain for Haiku and it does indeed cross-compile a working executable in Haiku which is more further than we have gotten with porting Go. I believe this is the latest version of the complier we have working right now. Nice work @korli!

I haven’t tried yet but I guess that attempting to compile some go code would result in errors due to the fork/exec issues @korli already mentioned.

11 Likes

This is great news! A running compiler built for the system is a big step :smiley:

Thanks @tqh @korli and @return_0e for all your work trying to get this to happen!

I managed to cross compile from MacOS Monterey on a MacBook Pro M1, too.
I did not succeed to build GitHub CLI from source, though.

go: downloading github.com/cli/safeexec v1.0.0
script/build.go:37:2: github.com/cli/safeexec@v1.0.0: Get "https://proxy.golang.org/github.com/cli/safeexec/@v/v1.0.0.zip": dial tcp: lookup proxy.golang.org on [::1]:53: read udp [::1]:63588->[::1]:53: i/o timeout

Could it be related to the fork/exec problem @korli mentioned before or something else?

4 Likes

I think the network doesn’t work yet.

2 Likes

Hello… I am a student interested in learning something about this project. This project seems fun but I cannot research much info for a beginner on “How to port a language to support an OS” or “How to port a program to support a new language”, I want to learn a bit of the above topic, could I ask some help to recommend some course/website/tutorial on above topic.

Here is a topic I opended Advice on porting language to support HaiKu

1 Like

Meanwhile Go 1.20 Released With Preview PGO Support, CPU Architecture Feature Build Flags - Phoronix

Golang 1.20.5 has been released 5 days ago: https://groups.google.com/g/golang-announce/c/q5135a9d924

2 Likes

Resurrecting this thread, because I think Korli’s work is the most recent work on porting Go?

I was able to use Korli’s code to cross-compile Go 1.18 for Haiku today (from macOS 10.13 High Sierra running Go 1.20). The resulting Go compiler really can compile working Haiku programs, but I have to recompile several times to get through the fork/exec bugs in the compiler. Also networking doesn’t work, but at least I can compile programs with networking code, which is more than I can with Go 1.4.

Some errors seems spurious - one compile error while compiling my own Go programs claimed this when it encountered a call to os.Hostname():

cannot find package internal/syscall/execenv (using -importcfg)

But compiling my program again, it was suddenly fine! Another error along the way was:

go build internal/reflectlite: mkdir: errno 2147483653

And most errors were of the form:

go build [packagename]: [path-to-compile-or-asm] fork/exec [path-to-compile-or-asm] errno 2147483653

Does anyone know what “errno 2147483653” is? My guess is it’s meant to be a negative error code and should actually be “errno -6”, but I still don’t know what that means.

I’d need networking to work in Go before I can actually get work done with this. My Go programs mostly call out to SFTP servers. But this seems like it’s actually really far along!

Also, pictures or it didn’t happen, so…

4 Likes

If it’s a Haiku error code, you’d have better luck converting it to hex.
In this case, the error translates to 0x80000005, which corresponds to B_BAD_VALUE.

iirc there is a commandlinetool called „error“ that should comvert this to a readable name

3 Likes

Awesome, thank you! The error command is giving values that make sense in this context. To save others a lookup:

2147483653 - 0x80000005: Invalid Argument
2147508227 - 0x80006003: No such file or directory

The “Invalid Argument” is the error happening on all the fork/exec calls to compile or asm.

1 Like

Nice!
I never had time to sync the work I did with korli’s. I was careful in trying to get every commit clean, so if you have time, you might want to just walk through the commits there and compare if I did something differently.

3 Likes

You might be able to use strace to find the failing fork/exec calls and see if any of the arguments are obviously bad. (Note you might need to specify some flags to strace if there’s multiple threads/processes in use.)