veandco / go-sdl2

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

CGO enabled Cross build from BSD AMD64 to FreeBSD armv7 fails #573

Open MikHulk opened 11 months ago

MikHulk commented 11 months ago

Go version: go1.20.3 freebsd/amd64 Go-SDL2 version: github.com/veandco/go-sdl2 v0.4.35 SDL2 version: on host:

$ pkg info sdl2   
sdl2-2.26.5
Name           : sdl2
Version        : 2.26.5
Installed on   : Tue Apr 18 22:48:33 2023 CEST
Origin         : devel/sdl20
Architecture   : FreeBSD:14:amd64
Prefix         : /usr/local
Categories     : devel
Licenses       : ZLIB
Maintainer     : amdmi3@FreeBSD.org
WWW            : https://www.libsdl.org/
Comment        : Cross-platform multimedia development API
Options        :
    ALSA           : off
    ASM            : on
    CSD            : off
    DBUS           : off
    FCITX          : off
    HIDAPI         : off
    IBUS           : off
    JACK           : off
    JOYSTICK       : on
    KMSDRM         : on
    NAS            : off
    OPENGL         : on
    OPENGLES1      : on
    OPENGLES2      : on
    OSS            : on
    PIPEWIRE       : off
    PTHREADS       : on
    PULSEAUDIO     : off
    SAMPLERATE     : off
    SNDIO          : off
    UDEV           : off
    VULKAN         : on
    WAYLAND        : on
    X11            : on
Shared Libs required:
    libinotify.so.0
Shared Libs provided:
    libSDL2-2.0.so.0
Annotations    :
    FreeBSD_version: 1400085
Flat size      : 7.36MiB
Description    :
This library is designed to make it easy to write games that run on UNIX,
Win32, MacOS X and other platforms using the various native high-performance
media interfaces (for video, audio, etc) and presenting a single source-code
level API to your application. This is a fairly low level API, but using this,
completely portable applications can be written with a great deal of
flexibility.

WWW: https://www.libsdl.org/

on target

mik@beaglebone:~ % pkg info sdl2
sdl2-2.0.12_2
Name           : sdl2
Version        : 2.0.12_2
Installed on   : Thu Jun  8 23:19:15 2023 CEST
Origin         : devel/sdl20
Architecture   : FreeBSD:12:armv7
Prefix         : /usr/local
Categories     : devel
Licenses       : ZLIB
Maintainer     : amdmi3@FreeBSD.org
WWW            : https://www.libsdl.org/
Comment        : Cross-platform multimedia development API
Options        :
    ALSA           : off
    ASM            : on
    DLOPEN         : on
    HIDAPI         : off
    JACK           : off
    NAS            : off
    OSS            : on
    PTHREADS       : on
    PULSEAUDIO     : off
    SAMPLERATE     : off
    SDL_ATOMIC     : on
    SDL_AUDIO      : on
    SDL_CPUINFO    : on
    SDL_EVENTS     : on
    SDL_FILE       : on
    SDL_HAPTIC     : on
    SDL_JOYSTICK   : on
    SDL_LOADSO     : on
    SDL_POWER      : on
    SDL_RENDER     : on
    SDL_THREADS    : on
    SDL_TIMERS     : on
    SDL_VIDEO      : on
    SNDIO          : off
    UDEV           : off
    VIDEO_KMSDRM   : on
    VIDEO_OPENGL   : on
    VIDEO_OPENGLES2: on
    VIDEO_WAYLAND  : on
    VIDEO_X11      : on
Shared Libs provided:
    libSDL2-2.0.so.0
Annotations    :
    FreeBSD_version: 1201000
    repo_type      : binary
    repository     : FreeBSD
Flat size      : 4.73MiB
Description    :
This library is designed to make it easy to write games that run on UNIX,
Win32, MacOS X and other platforms using the various native high-performance
media interfaces (for video, audio, etc) and presenting a single source-code
level API to your application. This is a fairly low level API, but using this,
completely portable applications can be written with a great deal of
flexibility.

WWW: https://www.libsdl.org/

OS: FreeBSD Architecture: armv7

When I try to build some program from Freebsd amd64 for FreeBSD armv7 I get errors.

With gcc as compiler:

mik@labo $ CGO_ENABLED=1 CC=armv7-gnueabi-freebsd14.0-gcc12  GOARCH=arm GOARM=7 go build -v -tags static -ldflags "-s -w" xcb.go                                                                                    runtime/cgo                
# runtime/cgo
In file included from /usr/include/signal.h:43,
                 from gcc_freebsd_arm.c:9:
/usr/include/sys/_ucontext.h:46:9: error: unknown type name 'mcontext_t'
   46 |         mcontext_t      uc_mcontext;
      |         ^~~~~~~~~~

As this error is related to some system headers I thought I had to give location of this header. So I have made an extract of the target system file:

mik@labo $ CGO_ENABLED=1 CC=armv7-gnueabi-freebsd14.0-gcc12 CGO_CFLAGS=' -I ./freebsd-tree/usr/include -I
./freebsd-tree/usr/local/include -lSDL2 --sysroot=./freebsd-tree' GOARCH=arm GOARM=7 go build -v -tags static -ldflags "-s -w" xcb.go
runtime/cgo
# runtime/cgo
In file included from cgo-builtin-prolog:1:
/usr/local/lib/gcc/armv7-gnueabi-freebsd14.0/12.2.0/include/stddef.h:54:10: fatal error: sys/_types.h: No such file or directory
   54 | #include <sys/_types.h>
      |          ^~~~~~~~~~~~~~
compilation terminated.

I tried with clang as cgo compiler too:

mik@labo $ CGO_ENABLED=1 CC=clang CGO_CFLAGS='--target=armv7-none-freebsd -I ./freebsd-tree/usr/include -I ./freebsd-tree/usr/local/include --sysroot=./freebsd-tree' GOARCH=arm GOARM=7 go build -v -tags static -ldflags "-s -w" xcb.go                          
runtime/cgo
# runtime/cgo
cgo-builtin-prolog:1:10: fatal error: 'stddef.h' file not found
#include <stddef.h>
         ^~~~~~~~~~
1 error generated.
mik@labo $ CGO_ENABLED=1 CC=clang CGO_CFLAGS='--target=armv7-none-freebsd --sysroot=./freebsd-tree' GOARCH=arm GOARM=7 go build -v -tags static -ldflags "-s -w" xcb.go                                             runtime/cgo          
# runtime/cgo
cgo-builtin-prolog:1:10: fatal error: 'stddef.h' file not found
#include <stddef.h>
         ^~~~~~~~~~
1 error generated.

without sysroot I get the same error above with gcc but given by clang:

mik@labo $ CGO_ENABLED=1 CC=clang CGO_CFLAGS='--target=armv7-none-freebsd' GOARCH=arm GOARM=7 go build -v -tags static -ldflags "-s -w" xcb.go                                                                      runtime/cgo          
# runtime/cgo
In file included from gcc_freebsd_arm.c:9:
In file included from /usr/include/signal.h:43:
/usr/include/sys/_ucontext.h:46:2: error: unknown type name 'mcontext_t'

I don't know if the problem comes from BSD or GO or else.

I think I going to try with a linux target, see if it works. But Help is very welcome.

MikHulk commented 11 months ago

I forgot to say local run and compilation on FreeBSD AMD/64 work as expected. I add the go source file in the thread.

I would try to compile this file directly on target but go doesn't seems available on FreeBSD12. (And Xorg or wayland are not working on FreeBSD13 )

https://www.freshports.org/lang/go

// 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()

    println("create window")
    window, err := sdl.CreateWindow(
        "test",
        sdl.WINDOWPOS_UNDEFINED,
        sdl.WINDOWPOS_UNDEFINED,
        800, 600, 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{50, 50, 200, 200}
    colour := sdl.Color{R: 255, G: 80, B: 150, A: 255}
    pixel := sdl.MapRGBA(surface.Format, colour.R, colour.G, colour.B, colour.A)
    surface.FillRect(&rect, pixel)
    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
            }
        }
    }
}
MikHulk commented 11 months ago

I succeed to x-compile with clang using my sysroot so I don't think it comes from that:

#include <SDL2/SDL.h>
#include <stdio.h>

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

int main(int argc, char* args[]) {
  SDL_Window* window = NULL;
  SDL_Surface* screenSurface = NULL;
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    fprintf(stderr, "could not initialize sdl2: %s\n", SDL_GetError());
    return 1;
  }
  window = SDL_CreateWindow(
                "hello_sdl2",
                SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                SCREEN_WIDTH, SCREEN_HEIGHT,
                SDL_WINDOW_SHOWN
                );
  if (window == NULL) {
    fprintf(stderr, "could not create window: %s\n", SDL_GetError());
    return 1;
  }
  screenSurface = SDL_GetWindowSurface(window);
  SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
  SDL_UpdateWindowSurface(window);
  SDL_Delay(2000);
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}
mik@labo $ clang test_sdl.c -I ./freebsd-tree/usr/local/include -L ./freebsd-tree/usr/local/lib -l SDL2 --sysroot=./freebsd-tree --target=armv7-none-freebsd -o test_sdl                              
mik@labo $ file test_sdl
test_sdl: ELF 32-bit LSB executable, ARM, EABI5 version 1 (FreeBSD), dynamically linked, interpreter /libexec/ld-elf.so.1, for FreeBSD 12.4 (1204500), FreeBSD-style, with debug_info, not stripped

If I try to x-compile the go program with the same options I get an error about sdl go module:

mik@labo $ CC=clang CGO_CFLAGS='--target=armv7-none-freebsd -l SDL2 -L ./freebsd-tree/usr/local/lib -I ./freebsd-tree/usr/local/include  --sysroot=./freebsd-tree' GOARCH=arm GOARM=7 go build -v xcb.go
command-line-arguments
# command-line-arguments
./xcb.go:9:16: undefined: sdl.Init
./xcb.go:9:25: undefined: sdl.INIT_EVERYTHING
./xcb.go:12:12: undefined: sdl.Quit
./xcb.go:15:21: undefined: sdl.CreateWindow
./xcb.go:17:7: undefined: sdl.WINDOWPOS_UNDEFINED
./xcb.go:18:7: undefined: sdl.WINDOWPOS_UNDEFINED
./xcb.go:19:17: undefined: sdl.WINDOW_SHOWN
./xcb.go:32:14: undefined: sdl.Rect
./xcb.go:33:16: undefined: sdl.Color
./xcb.go:34:15: undefined: sdl.MapRGBA
./xcb.go:34:15: too many errors
veeableful commented 11 months ago

Hi @MikHulk, unfortunately we don't have static libraries built for FreeBSD so the static compilation would fail but please let me know if the Linux one works for you.

MikHulk commented 11 months ago

Hi @veeableful, thank you ! I will try and keep you update.

MikHulk commented 11 months ago

I thought maybe I could build this missing lib.