veandco / go-sdl2

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

About Static compilation gcc: error: unrecognized argument in option '-mabi=32' #484

Open mumbaicat opened 3 years ago

mumbaicat commented 3 years ago

main.go

package main

import "github.com/veandco/go-sdl2/sdl"

func main() {
    if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
        panic(err)
    }
    defer sdl.Quit()

    window, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
        200, 100, sdl.WINDOW_SHOWN)
    if err != nil {
        panic(err)
    }
    defer window.Destroy()

    surface, err := window.GetSurface()
    if err != nil {
        panic(err)
    }
    surface.FillRect(nil, 0)

    rect := sdl.Rect{0, 0, 50, 50}
    surface.FillRect(&rect, 0xffff0000)
    window.UpdateSurface()

    running := true
    for running {
        for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
            switch event.(type) {
            case *sdl.QuitEvent:
                println("Quit")
                running = false
                break
            }
        }
    }
}

build.bat

set GOOS=linux
set GOARCH=mipsle
set GOMIPS=softfloat
set CGO_ENABLED=1
set CC=gcc
go build  -tags static -ldflags "-s -w"

run it

# runtime/cgo
gcc: error: unrecognized argument in option '-mabi=32'
gcc: note: valid arguments to '-mabi=' are: ms sysv

gcc version 8.1.0 (x86_64-posix-sjlj-rev0, Built by MinGW-W64 project) go version go1.16.4 windows/amd64

What should I do to complete the compilation from windows to mips, thank you.

tower111 commented 2 years ago

I have the same problem, how did you solve it?

veeableful commented 2 years ago

Hi @tower111, do you have the same environment as the original poster? We don't currently support cross-compilation on Windows (only Linux is supported and GOARCH=mipsle will be supported on future version).

geniot commented 1 year ago

Hi @veeableful , I'm trying to build statically for mipsle:

mips-static:
    CGO_ENABLED=1 \
    GOMIPS=softfloat \
    GOOS=linux \
    GOARCH=mipsle \
    go build -tags static -ldflags "-s -w" -o bin/${PROGRAM_NAME}.gcw github.com/geniot/${PROGRAM_NAME}/cmd/${PROGRAM_NAME}

I use the latest go-sdl2: github.com/veandco/go-sdl2 v0.5.0-alpha.4.0.20221211132336-408ac46089e4 My uname is: Linux vitaly-laptop 4.15.0-137-generic #141-Ubuntu SMP Fri Feb 19 13:46:27 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

The error that I'm getting:

# runtime/cgo
gcc: error: unrecognized argument in option '-mabi=32'
gcc: note: valid arguments to '-mabi=' are: ms sysv
Makefile:12: recipe for target 'mips-static' failed
make: *** [mips-static] Error 2

Can you suggest how to fix this? Thanks.

veeableful commented 1 year ago

Hi @geniot, we currently don't support mipsle but I'll see if I can add support for it this week!

geniot commented 1 year ago

It will be particularly useful because SDL2 is not everywhere the same. My PocketGo2v2 eg. which has SDL2 doesn't have GetTicks64 and what I recently found after an hour of logging it doesn't have DrawRect! So I started to use DrawLines instead. But still static cross-compilation would fix these issues once and for all.

veeableful commented 1 year ago

Hi @geniot, could you try the master branch and use the following command to build and run the executable? It does require the use of mipsel-linux-gnu-gcc which should be available on Ubuntu if installed using apt install gcc-mipsel-linux-gnu.

CGO_ENABLED=1 \
CC=mipsel-linux-gnu-gcc \
GOOS=linux \
GOARCH=mipsle \
go build -tags static -ldflags "-s -w"
geniot commented 1 year ago

Thanks @veeableful . After refactoring some sdl calls in my program I saw a big list of undefineds. So I added CGO_LDFLAGS and PKG_CONFIG:

mips-static:
    CGO_ENABLED=1 \
    CC=mipsel-linux-gnu-gcc \
    CGO_LDFLAGS='-L/opt/gcw0-toolchain/usr/mipsel-gcw0-linux-uclibc/sysroot/usr/lib -lSDL2 -lpng16' \
    GOOS=linux \
    GOARCH=mipsle \
    PKG_CONFIG='/opt/gcw0-toolchain/usr/bin/pkg-config' \
    go build -tags static -ldflags "-s -w" -o bin/${PROGRAM_NAME}.gcw github.com/geniot/${PROGRAM_NAME}/cmd/${PROGRAM_NAME}

Now it takes some time to compile! But the error output is:

/home/vitaly/sdk/go1.19.3/pkg/tool/linux_amd64/link: running mipsel-linux-gnu-gcc failed: exit status 1
/usr/lib/gcc-cross/mipsel-linux-gnu/7/../../../../mipsel-linux-gnu/bin/ld: cannot find /lib/libpthread.so.0
/usr/lib/gcc-cross/mipsel-linux-gnu/7/../../../../mipsel-linux-gnu/bin/ld: cannot find /usr/lib/libpthread_nonshared.a
collect2: error: ld returned 1 exit status

Makefile:12: recipe for target 'mips-static' failed
make: *** [mips-static] Error 2

Update: So I tried different CGO_LDFLAGS paths and libs. I'm still getting different errors. My understanding is that I have a toolchain with a gcc compiler and precompiled so/a libs. You suggest to use a different compiler - mipsel-linux-gnu-gcc. Fine. But now do I need to compile SDL2 "a" libs with it and tell the linker where to find them? And how are the bindings compiled or are mipsel static libs part of the bindings? And should I tell the linker where to find them? I'm confused. C/C++ CGo cross-compilation is not my area of expertise.

veeableful commented 1 year ago

Hi @geniot, sorry I didn't update for additional libraries like ttf, img, and mix. I have pushed another update to the master branch. Could you try again with the same command I wrote above? You shouldn't need to give additional flags for it to work. Thanks!

geniot commented 1 year ago

Thanks @veeableful . I see some progress (but maybe it's not related to your changes). I can now successfully build with:

mips-static:
    CGO_ENABLED=1 \
    CC=mipsel-linux-gnu-gcc \
    CGO_LDFLAGS='-L/usr/mipsel-linux-gnu/lib -L/opt/gcw0-toolchain/usr/mipsel-gcw0-linux-uclibc/sysroot/usr/lib' \
    GOOS=linux \
    GOARCH=mipsle \
    go build -tags static -ldflags "-s -w" -o bin/${PROGRAM_NAME}.gcw github.com/geniot/${PROGRAM_NAME}/cmd/${PROGRAM_NAME}

But the resulting binary fails to start on PocketGo:

POCKETGO2V2:/media/data/local/home # ./digger.gcw 
-sh: ./digger.gcw: not found
POCKETGO2V2:/media/data/local/home # ldd digger.gcw 
checking sub-depends for 'not found'
checking sub-depends for 'not found'
checking sub-depends for '/lib/libpthread.so.0'
    ld-uClibc.so.0 => /lib/ld-uClibc.so.0 (0x7727e000)
    libdl.so.0 => /lib/libdl.so.0 (0x7726a000)
    libc.so.0 => /lib/libc.so.0 (0x77204000)
checking sub-depends for 'not found'
    libm.so.6 => not found (0x00000000)
    libdl.so.2 => not found (0x00000000)
    libpthread.so.0 => /lib/libpthread.so.0 (0x00000000)
    libc.so.6 => not found (0x00000000)
    /lib/ld-uClibc.so.0 => /lib/ld-uClibc.so.0 (0x00000000)

And if I just use:

CC=mipsel-linux-gnu-gcc \
CGO_LDFLAGS='-L/usr/mipsel-linux-gnu/lib' \

I'm getting:

/home/vitaly/sdk/go1.19.3/pkg/tool/linux_amd64/link: running mipsel-linux-gnu-gcc failed: exit status 1
/usr/lib/gcc-cross/mipsel-linux-gnu/7/../../../../mipsel-linux-gnu/bin/ld: cannot find -lstdc++
collect2: error: ld returned 1 exit status

which makes sense. /usr/mipsel-linux-gnu/lib doesn't have it.

If I use my toolchain's compiler I get a lot of errors like this:

undefined reference to `__stack_chk_fail'
/home/vitaly/GolandProjects/pkg/mod/github.com/veandco/go-sdl2@v0.5.0-alpha.4.0.20221221030605-d093ebe4e7cd/ttf/../.go-sdl2-libs/libSDL2_mixer_linux_mipsle.a(readmidi.o): In function `_timi_read_midi_file':
(.text+0x1330): undefined reference to `__stack_chk_fail'
veeableful commented 1 year ago

Hi @geniot, thanks for testing it again. I think I will need to look into integrating uClibc in the cross-compilation as we currently assume glibc as the C standard library. I won't be around until next month so I think I'll order the device for testing and continue implementing the support once it arrives!

geniot commented 1 year ago

Hi @veeableful, I don't think you need the device (PocketGo2v2? - it's rather old and a bit noisy). Instead you can probably download the toolchain for it: https://github.com/Ninoh-FOX/toolchain (it's related to this firmware: https://github.com/Ninoh-FOX/POCKETGO2_ROGUE_CFW) and see what's inside. But on another note: this firmware and toolchain are not perfect! And static compilation is a "nice to have" for me, not a blocker. So I was playing around with this option and I'm looking at the buildroot. Whether I can build my own Linux image with libs and versions that I can choose myself. Thanks for your help, please don't spend your time on this issue. It's not that important and maybe I can find a better and more solid solution.

veeableful commented 1 year ago

Thanks for the information @geniot! I was actually having trouble finding a place to order the device :sweat_smile: I will try the toolchain and see if I can make use of it.