Windows Executable Cross-Compile with MinGW

* Added Windows build instructions to Building.md and added a
  "make mingw" command to cross-compile the Windows binary into
  the bin/ folder.
* Fix a bug in the Wallpaper texture loader where it would error out
  when caching textures to disk the first time.
This commit is contained in:
Noah 2019-04-06 19:30:25 -07:00
parent 569e67bf9e
commit d0ff137b04
3 changed files with 64 additions and 5 deletions

View File

@ -29,6 +29,49 @@ sudo dnf -y install golang SDL2-devel SDL2_ttf-devel
sudo apt -y install golang libsdl2-dev libsdl2-ttf-dev
```
## Windows
## Windows Cross-Compile from Linux
TBD.
Install the Mingw C compiler:
```bash
# Fedora
sudo dnf -y install mingw64-gcc # for 64-bit
sudo dnf -y install mingw32-gcc # for 32-bit
# Arch Linux
pacman -S mingw-w64
```
Download the SDL2 Mingw development libraries [here](https://libsdl.org/download-2.0.php)
and SDL2_TTF from [here](https://www.libsdl.org/projects/).
Extract each and copy their library folder into the mingw path.
```bash
# e.g. /usr/x86_64-w64-mingw32 is usually the correct path, verify on e.g.
# Fedora with `rpm -ql mingw64-filesystem`
tar -xzvf SDL2_ttf-devel-2.0.15-mingw.tar.gz
cd SDL_ttf-2.0.15
sudo cp -r x86_64-w64-mingw32 /usr
```
Make and set permissions for Go to download the standard library for Windows:
```bash
mkdir /usr/lib/golang/pkg/windows_amd64
chown your_username /usr/lib/golang/pkg/windows_amd64
```
And run `make mingw` to build the Windows binary.
### Windows DLLs
For the .exe to run it will need SDL2.dll and such.
```
# SDL2.dll and SDL2_ttf.dll
cp /usr/x86_64-w64-mingw32/bin/SDL*.dll bin/
```
SDL2_ttf requires libfreetype, you can get its DLL here:
https://github.com/ubawurinna/freetype-windows-binaries

View File

@ -19,6 +19,18 @@ build:
go build $(LDFLAGS) -i -o bin/doodle cmd/doodle/main.go
go build $(LDFLAGS) -i -o bin/doodad cmd/doodad/main.go
# `make mingw` to cross-compile a Windows binary with mingw.
.PHONY: mingw
mingw:
env CGO_ENABLED="1" CC="/usr/bin/x86_64-w64-mingw32-gcc" \
GOOS="windows" CGO_LDFLAGS="-lmingw32 -lSDL2" CGO_CFLAGS="-D_REENTRANT" \
go build $(LDFLAGS) -i -o bin/doodle.exe cmd/doodle/main.go
env CGO_ENABLED="1" CC="/usr/bin/x86_64-w64-mingw32-gcc" \
GOOS="windows" CGO_LDFLAGS="-lmingw32 -lSDL2" CGO_CFLAGS="-D_REENTRANT" \
go build $(LDFLAGS) -i -o bin/doodad.exe cmd/doodad/main.go
# `make run` to run it in debug mode.
.PHONY: run
run:

View File

@ -58,13 +58,17 @@ func texture(e render.Engine, img *image.RGBA, name string) (render.Texturer, er
if _, err := os.Stat(filename); os.IsNotExist(err) {
fh, err := os.Create(filename)
if err != nil {
return nil, fmt.Errorf("CornerTexture: %s", err.Error())
return nil, fmt.Errorf("texture(%s): %s", name, err.Error())
}
defer fh.Close()
err = bmp.Encode(fh, img)
if err != nil {
return nil, fmt.Errorf("CornerTexture: bmp.Encode: %s", err.Error())
return nil, fmt.Errorf("texture(%s): bmp.Encode: %s", name, err.Error())
}
err = fh.Close()
if err != nil {
return nil, fmt.Errorf("texture(%s): fh.Close: %s", name, err.Error())
}
}