veandco / go-sdl2

SDL2 binding for Go
https://godoc.org/github.com/veandco/go-sdl2
BSD 3-Clause "New" or "Revised" License
2.18k stars 219 forks source link

Need Help: Cross-Compile to MIPS Architecture #447

Open Zaya6 opened 4 years ago

Zaya6 commented 4 years ago

For a hobby project I've been trying to create a program to run on the rg350 handheld, with a jz4770 processor. When I try to build I get these errors:

./hey.go:11:12: undefined: sdl.Init ./hey.go:11:21: undefined: sdl.INIT_EVERYTHING ./hey.go:14:8: undefined: sdl.Quit ./hey.go:16:17: undefined: sdl.CreateWindow ./hey.go:18:3: undefined: sdl.WINDOWPOS_UNDEFINED ./hey.go:19:3: undefined: sdl.WINDOWPOS_UNDEFINED ./hey.go:21:3: undefined: sdl.WINDOW_FULLSCREEN ./hey.go:36:19: undefined: sdl.CreateRenderer ./hey.go:36:50: undefined: sdl.RENDERER_ACCELERATED ./hey.go:47:22: undefined: sdl.Rect ./hey.go:47:22: too many errors

Building regularlly works just fine. Btw I'm pretty new to the go language itself and any help would be greatly appreciated.

veeableful commented 4 years ago

Hi @Zaya6, do you already have SDL2 compiled for MIPS architecture? If so, what happens if you run go get -v github.com/veandco/go-sdl2/sdl?

geniot commented 1 year ago

So I went a bit further than this :) Obviously you need a toolchain to cross-compile. The error that I'm getting: mipsel-gcw0-linux-uclibc-gcc: error: unrecognized command line option '-mno-odd-spreg'

Do you know where no-odd-spreg option is set? Why is it set and why my toolchain's mipsel gcc doesn't recognize it?

gen2brain commented 1 year ago

@geniot no-odd-spreg is set in Go, here https://github.com/golang/go/blob/master/src/cmd/cgo/gcc.go#L1639 depends if GOMIPS is hardfloat or softfloat, that also defaults in GCC for 32bit arch (hard-float), see here https://gcc.gnu.org/onlinedocs/gcc/MIPS-Options.html.

How did you try to build, did you manage to build and install SDL2 first?

geniot commented 1 year ago

Thanks @gen2brain for replying. What I'm trying to achieve: using Go create a simple SDL2 demo for my PocketGo2v2 which has MIPS architecture. What I did:

  1. simple "helloworld.go" on my Linux host - OK
  2. same with GOARCH=mipsle, upload, run on PocketGo - OK
  3. simple SDL2 red rectangle program taken from your examples (on my host) - OK
  4. same with cross compilation - FAIL

My env variables: CC=/opt/gcw0-toolchain/usr/bin/mipsel-gcw0-linux-uclibc-gcc CGO_CFLAGS=-I/opt/gcw0-toolchain/usr/mipsel-gcw0-linux-uclibc/sysroot/usr/include -D_REENTRANT CGO_ENABLED=1 CGO_LDFLAGS=-L/opt/gcw0-toolchain/usr/mipsel-gcw0-linux-uclibc/sysroot/usr/lib -lSDL2 GOARCH=mipsle GOOS=linux

I get an error: mipsel-gcw0-linux-uclibc-gcc: error: unrecognized command line option '-mno-odd-spreg'

Firmware that I have: https://github.com/Ninoh-FOX/POCKETGO2_ROGUE_CFW Toolchain that I'm using: https://github.com/Ninoh-FOX/toolchain

I looked at the gcc.go code. Do you know how to force gomips=softfloat? Update: I added GOMIPS=softfloat to my env variables. Now the compilation is really running! But I'm getting this:

# github.com/veandco/go-sdl2/sdl
In file included from /usr/include/SDL2/SDL.h:38:0,
                 from ./sdl_wrapper.h:5,
                 from ../pkg/mod/github.com/veandco/go-sdl2@v0.4.27/sdl/audio.go:4:
/usr/include/SDL2/SDL_cpuinfo.h:64:23: fatal error: immintrin.h: No such file or directory
 #include <immintrin.h>
                       ^
compilation terminated.
gen2brain commented 1 year ago

@geniot Now you should compile SDL2 with that same toolchain. pkg-config is now trying to use your system SDL2, which is not good. Point PKG_CONFIG_PATH and PKG_CONFIG_LIBDIR to i.e. /opt/gcw0-toolchain/usr/mipsel-gcw0-linux-uclibc/sysroot/usr/lib/pkgconfig. If you have mipsel-gcw0-linux-uclibc-pkg-config, set that as PKG_CONFIG though I don't think it is necessary to have.

The goal is to build SDL2 for mipsel, and to tell pkg-config where is that build installed, go-sdl2 will just use pkg-config: sdl2. You can manually check the output with pkg-config --cflags --libs sdl2, you are also free to change .pc files in lib/pkgconfig.

gen2brain commented 1 year ago

Btw. if that toolchain is using GCC-4.3 that is the reason why you don't have -mno-odd-spreg, the GCC is too old. You can either build/install some newer toolchain, or you can hack the Go source and use that build instead. That is if the device actually has support for hard float.

geniot commented 1 year ago

Hurray! It's working! Thanks a lot! Now I can write a retro emulator in Go, test it on my laptop and then compile it for PocketGo.

red

To get it working I only had to add PKG_CONFIG: env_vars

The build log has a lot of warnings like this: /opt/gcw0-toolchain/usr/lib/gcc/mipsel-gcw0-linux-uclibc/4.9.1/../../../../mipsel-gcw0-linux-uclibc/bin/ld: Warning: $WORK/b001/exe/a.out uses hard float, /tmp/go-link-2142246393/go.o uses soft float

But I guess I'm ok with this.

The execution on PocketGo also has some dev/dri warnings:

POCKETGO2V2:/media/data/local/home # ./go_build_pgtest_cross 
The path /dev/dri/ cannot be opened or is not available
The path /dev/dri/ cannot be opened or is not available
fbdev_display succesful
Kernel: Vivante GPL kernel driver 4.6.6.1381
Physical address of internal memory: 00000000
* Video memory:
  Internal physical: 0x00000000
  Internal size: 0x00000000
  External physical: 00000000
  External size: 0x00000000
  Contiguous physical: 0x8c7efb00
  Contiguous size: 0x00400000
Succesfully opened device
native_fbdev: 2 buffers of 320x240
Framebuffer format: 6, flip_rb=0
Framebuffer format: 6, flip_rb=0

So, to sum up: I didn't have to compile SDL2 for MIPS. I guess because it's already part of the toolchain or operating system (on PocketGo) and pkg-config knows about it.