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

crosscompile to windows with gfx library #486

Closed fboerman closed 3 years ago

fboerman commented 3 years ago

Hi I am trying to compile my program which uses gfx for windows. I am running archlinux. I cant get it to compile and keep getting this error:

./sdl_gfx_wrapper.h:2:11: fatal error: SDL2/SDL2_framerate.h: No such file or directory

this is probably due to the fact that gfx has no windows zip to put in mingw environment. How do I fix this?

veeableful commented 3 years ago

Hi @fboerman, sorry for the late response! We haven't fleshed out cross-compilation from Linux to Windows but perhaps you can try the steps below. It will require you to compile SDL2 and SDL2_gfx from source for MinGW as we don't have static library for SDL2_gfx yet.

  1. Create a working directory.

    mkdir sdl2-builds
    cd sdl2-builds
  2. Download SDL2 and SDL2_gfx and extract them to the working directory.

  3. Create a script with the following content in the working directory and execute it:

    
    #!/usr/bin/env bash

Download and extract SDL2 and SDL2_gfx and then run the following script:

TARGET="x86_64-w64-mingw32" SDL2_VERSION=2.0.14 GFX_VERSION=1.0.4

LIBDIR=build/.libs

cd SDL2-${SDL2_VERSION} mkdir -p .go-sdl2-libs/include/SDL2

rm -r build-windows-amd64 2> /dev/null export CC="${TARGET}-gcc" export CXX="${TARGET}-g++" export RC="${TARGET}-windres" mkdir -p build-windows-amd64 && cd build-windows-amd64 ../configure --host=${TARGET} --prefix="$HOME/.local/${TARGET}" make -j$(nproc) make install cp ${LIBDIR}/libSDL2.a ${LIBDIR}/libSDL2.a.debug ${TARGET}-strip ${LIBDIR}/libSDL2.a ${TARGET}-ranlib ${LIBDIR}/libSDL2.a cp ${LIBDIR}/libSDL2.a ../.go-sdl2-libs/libSDL2_windows_amd64.a cp include/SDL_config.h ../.go-sdl2-libs/include/SDL2/SDL_config_windows_amd64.h

cd ..

cd SDL2_gfx-${VERSION}

rm -r build-windows-amd64 2> /dev/null export CC="${TARGET}-gcc" export CXX="${TARGET}-g++" export RC="${TARGET}-windres" mkdir -p build-windows-amd64 && cd build-windows-amd64 ../configure --host=${TARGET} --prefix="$HOME/.local/${TARGET}" make -j$(nproc) make install

cd ..

4. Run the following build command in your project:

CGO_CFLAGS="-I $HOME/.local/x86_64-w64-mingw32/include" CGO_LDFLAGS="-L $HOME/.local/x86_64-w64-mingw32/lib" CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc GOOS=windows GOARCH=amd64 go build -tags static

fboerman commented 3 years ago

hi @veeableful thankyou for the very thorough reply. coincidentally I solved my issue already and was just gonna respond to the issue when I saw your response.

anway my fix is two options. Fort both statical compilation works the first proper crosscompiling option is that I found the package mingw-w64-sdl_gfx on the AUR which actually installs what I need. I have also enabled the OwnStuff unofficial user repo for the tools you need to build that package. You can then crosscompile with the command in the readme, without the extra include flags mentioned by you, since the packages takes care of it.

the second option is using github actions. I have the following basic yaml configuration which sucessfully builds a windows binary, I have stripped out the non necessary, for example uploading the artifacts.

  release-windows:
    name: release windows
    runs-on: windows-latest
    defaults:
      run:
        shell: msys2 {0}
    steps:
      - name: Checkout repo
        uses: actions/checkout@master
      - name: Setup msys
        uses: msys2/setup-msys2@v2
        with:
          msystem: MINGW64
          update: true
          install: mingw-w64-x86_64-go mingw-w64-x86_64-gcc mingw-w64-x86_64-SDL2 mingw-w64-x86_64-SDL2_image mingw-w64-x86_64-SDL2_mixer mingw-w64-x86_64-SDL2_ttf mingw-w64-x86_64-SDL2_gfx
      - name: Build release
        run: go build -tags static -ldflags "-s -w" -o <NAME>.exe
        env:
          GOARCH: amd64
          GOOS: windows
          CGO_ENABLED: 1
          CC: gcc

again thanks for your investigation! that will help other people on non arch based systems.

I now have a problem of the windows binary being significantly more slower, but I will open a seperate issue about this after collecting some more info on that.