wailsapp / wails

Create beautiful applications using Go
https://wails.io
MIT License
24.34k stars 1.17k forks source link

[v2 linux] Cross-compilation targetting windows doesn't work in v2 but works with manual build #1921

Open dergyitheron opened 1 year ago

dergyitheron commented 1 year ago

Description

I am doing cross compilation targeting windows amd64. My application uses confluent-kafka-go package which needs mingw for compilation for windows.

The compilation worked in beta versions and now doesn't work with wails build command. I also tried to use go build with same flags and it works without an issue and the app runs on windows as expected.

Building for linux works just fine.

To Reproduce

Having new wails 2.0.0

  1. Create project with wails init, add confluent-kafka-go package and add some code from the official examples
  2. Install gcc-mingw-w64 and g++-mingw-w64-x86-64 with for example sudo apt-get install gcc-mingw-w64 g++-mingw-w64-x86-64
  3. Run the app locally in development mode wails dev
  4. Build the app targetting windows with the following command: env GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ CGO_CXXFLAGS="-IC:\msys64\mingw64\include" wails build -ldflags '-extldflags "-static"' (fails)

Alternatively, compile the app with the following command env GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ CGO_CXXFLAGS="-IC:\msys64\mingw64\include" go build -tags desktop,production -ldflags '-extldflags "-static"' (ok)

Expected behaviour

wails build cross compilation should work same way manual build does

Screenshots

image

Building with -platform flag even yields different errors image

Here is the successful build with go build. I have a windows server where I tried the kafka-tester.exe executable and it works as expected. image

Building for linux works just fine image

Attempted Fixes

I tried the manual build by following these instructions: https://wails.io/docs/guides/manual-builds/#compile-application

That worked just fine.

System Details

Wails CLI v2.0.0

Scanning system - Please wait (this may take a long time)...Done.

System
------
OS:     Debian GNU/Linux
Version:    11
ID:     debian
Go Version: go1.19.1
Platform:   linux
Architecture:   amd64

Wails
------
Version:        v2.0.0
Package Manager:    apt

Dependency  Package Name        Status      Version
----------  ------------        ------      -------
*docker     docker.io       Available   20.10.5+dfsg1-1+deb11u2
gcc         build-essential     Installed   12.9
libgtk-3    libgtk-3-dev        Installed   3.24.24-4+deb11u2
libwebkit   libwebkit2gtk-4.0-dev   Installed   2.38.0-1~deb11u1
npm         npm             Installed   8.15.0
*nsis       nsis            Available   3.06.1-1
pkg-config  pkg-config      Installed   0.29.2

* - Optional Dependency

Diagnosis
---------
Your system is ready for Wails development!
Optional package(s) installation details: 
  - docker: sudo apt install docker.io
  - nsis: sudo apt install nsis

If Wails is useful to you or your company, please consider sponsoring the project:
https://github.com/sponsors/leaanthony

uname -a

Linux ACZ-60DM373 5.10.0-18-amd64 #1 SMP Debian 5.10.140-1 (2022-09-02) x86_64 GNU/Linux


### Additional context

Worked on v2.0.0-beta.44.2 . Doesn't work on v2.0.0 .
stefpap commented 1 year ago

I'm also cross compiling from ubuntu docker to windows. After updating wails cli to v2.0.0 I started facing the following error

» GOOS=windows GOARCH=386 wails build -platform windows -debug -ldflags "-w -s -H windowsgui -X main.version=dev -X main.env=business" -o launcher.exe

Wails CLI v2.0.0

App Type:               desktop
Platforms:              windows
Compiler:               /usr/local/go/bin/go
Skip Bindings:          false
Build Mode:             Debug
Obfuscated:             false
Skip Frontend:          false
Compress:               false
Package:                true
Clean Build Dir:        false
LDFlags:                "-w -s -H windowsgui -X main.version=dev -X main.env=business"
Tags:                   []
Race Detector:          false
Output File:            launcher.exe

Building target: windows/386
----------------------------
  - Generating bindings: Error: 

fork/exec /var/folders/92/821w0x2x71g33yrjsmhlq7q00000gp/T/wailsbindings: exec format error

Removing GOOS/GOARCH and adding -platform windows/386 as @stffabi suggested I get:

» wails build -platform windows/386 -debug -ldflags "-H windowsgui -X main.version=dev -X main.env=business" -o launcher.exe
Wails CLI v2.0.0

App Type:               desktop
Platforms:              windows/386
Compiler:               /usr/local/go/bin/go
Skip Bindings:          false
Build Mode:             Debug
Obfuscated:             false
Skip Frontend:          false
Compress:               false
Package:                true
Clean Build Dir:        false
LDFlags:                "-H windowsgui -X main.version=dev -X main.env=business"
Tags:                   []
Race Detector:          false
Output File:            launcher.exe

Building target: windows/386
----------------------------
  - Generating bindings: Error: 
package github.com/xxx/launcher
        imports golang.org/x/sys/windows: build constraints exclude all Go files in /Users/yyy/go/pkg/mod/golang.org/x/sys@v0.0.0-20220114195835-da31bd327af9/windows
package github.com/xxx/launcher
        imports golang.org/x/sys/windows/svc: build constraints exclude all Go files in /Users/yyy/go/pkg/mod/golang.org/x/sys@v0.0.0-20220114195835-da31bd327af9/windows/svc
package github.com/xxx/launcher
        imports golang.org/x/sys/windows/svc/mgr: build constraints exclude all Go files in /Users/yyy/go/pkg/mod/golang.org/x/sys@v0.0.0-20220114195835-da31bd327af9/windows/svc/mgr

exit status 1

I feel like I'm doing something wrong? Haven't tested manual builds yet. But I also manage to build on my windows pc

leaanthony commented 1 year ago

I'll investigate this tonight. It should be something trivial. Just FYI windows/386 is only supported on a best effort basis. Does the 64bit version compile ok?

stefpap commented 1 year ago

Yes, I'm aware of that, thanks! I get the same error with windows/amd64 as well

stffabi commented 1 year ago

When using GOOS=windows GOARCH=386 for starting wails, the default environment contains those env variables. When we are building the bindings, we initiate a go build with custom "binding" build tags. In this case GOOS/GOARCH will be implicitly passed over to the go build and as a result the bindings binary is targeted to that GOOS and GOARCH. As soon as we want to start the binary to build the bindings, this won't work since the host platform is Linux but the binary was built for Windows.

@stefpap in your case when leaving away the GOOS/GOARCH the bindings binary is now built for Linux so it could be started on host. The problem now seems like the bindings app can't be generated for Linux because the imports of imports golang.org/x/sys/windows are fully excluded due to the Linux target but the import is not constraint to a Windows only build. You should see the same problem when just using go build in the docker container in the folder where main.go is.

leaanthony commented 1 year ago

If you have the bindings already generated as part of your codebase, you could try skipping them using wails build -skipbindings

dergyitheron commented 1 year ago

Hi guys,

today I've tried latest version of wails 2.2.0 with go go version go1.19.3 linux/amd64. Still no luck on builds targeting windows/amd64.

The only difference is that I now get different error when building with same flags as in the original post, "To Reproduce" section, paragraph 4. env GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ CGO_CXXFLAGS="-IC:\msys64\mingw64\include" wails build -ldflags '-extldflags "-static"'

Screenshot: image

ERROR: Cannot open assembly '/tmp/wailsbindings': File does not contain a valid CIL image. exit status 2

Any idea why would wails be looking for /tmp/wailsbindings? I've monitored the /tmp directory during the unsuccessful build and the file is not present.

Other things stay the same - building with go build works with same command as mentioned, using wails build -platform still shows same errors. Build targeting linux/amd64 works fine.

stffabi commented 1 year ago

Have you tried to skip the building of your bindings with -skipbindings? For generating the bindings, a binary is build and executed. Since you are using GOOS and GOARCH the generated binary is a windows binary, which can't be executed on Linux.

dergyitheron commented 1 year ago

I have just tried env GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ CGO_CXXFLAGS="-IC:\msys64\mingw64\include" wails build -ldflags '-extldflags "-static"' -skipbindings and it's built successfully, runs on Windows but it doesn't function as it should, works on linux and the previous version built with beta v2 works too.

What does the -skipbindings do? I don't see it in the CLI Reference page. The CLI help command only says 'Skips generation of bindings', I don't know what that means to be honest. Could it be related to how the confluent-kafka-go library needs librdkafka C library in order to compile? That's the main reason I'm using mingw32.

dergyitheron commented 1 year ago

Update: It looks like the issue with windows build is now solved and the app not working on windows correctly is purely networking issue.

The final command that worked: env GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc CXX=x86_64-w64-mingw32-g++ CGO_CXXFLAGS="-IC:\msys64\mingw64\include" wails build -ldflags '-extldflags "-static"' -skipbindings with v2.2.0.

I still have couple of questions:

  1. What does the -skipbindings do? What bindings does it skip? Could it cause problems somewhere else?
  2. What about the -platform windows/amd64 not behaving same way as GOOS=windows GOARCH=amd64? I use the latter just because the former is not working, as described in my previous comments.