webui-dev / go-webui

Use any web browser as GUI, with Go in the backend and modern web technologies in the frontend.
https://webui.me/
MIT License
161 stars 13 forks source link

Cannot cross-compile a project using go-webui #42

Open Mihara opened 1 month ago

Mihara commented 1 month ago

Description

Let me start by saying that I'm not sure this is actually a bug, but if anything, it's something insufficiently documented...

Assume a naive example project I plan to build for Windows while running under Linux. Simply running the setup.sh script as described in README produces no useful results:

$ sh -c "$(curl -fsSL https://raw.githubusercontent.com/webui-dev/go-webui/main/setup.sh)"
go: finding module for package github.com/webui-dev/go-webui/v2
go: downloading github.com/webui-dev/go-webui v0.1.0
go: found github.com/webui-dev/go-webui/v2 in github.com/webui-dev/go-webui/v2 v2.4.2
go: upgraded github.com/webui-dev/go-webui/v2 v2.4.2 => v2.4.3-0.20240711195955-8001a5e84f7d

$ go build
# github.com/webui-dev/go-webui/v2
../../../.local/golang/pkg/mod/github.com/webui-dev/go-webui/v2@v2.4.3-0.20240711195955-8001a5e84f7d/cgo.go:14:10: fatal error: webui/src/civetweb/civetweb.c: No such file or directory
   14 | #include "webui/src/civetweb/civetweb.c"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.

I'm trying to patch the library in by making a local copy as described here:

require (
...
        github.com/webui-dev/go-webui/v2 v2.4.3
)
replace github.com/webui-dev/go-webui/v2 v2.4.3 => ../vendor/go-webui/v2

At this point, go build produces a native binary that runs. Which is nice, but cross-compilation is mission critical (and the reason I picked Go for this project in the first place), so...

$ GOOS=windows GOARCH=amd64 go build
....
    imports github.com/webui-dev/go-webui/v2: build constraints exclude all Go files in ...

Not big surprise here, because that's only supposed to work on projects not making use of cgo. Ok, let's try it with explicitly invoking cgo...

$ GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CXX=x86_64-w32-mingw32-g++ CC=x86_64-w64-mingw32-gcc go build -v
...
/usr/lib/go-1.22/pkg/tool/linux_amd64/link: running x86_64-w64-mingw32-gcc failed: exit status 1
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lWs2_32: No such file or directory
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lOle32: No such file or directory
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lAdvapi32: No such file or directory
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lShell32: No such file or directory
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lUser32: No such file or directory

Now this is quite cryptic, and googling does not appear to produce any clues on what could possibly be wrong. Initially I assumed I just had a misconfigured mingw32 or something, but then I try xgo (https://github.com/techknowlogick/xgo) which is supposed, in theory, to take care of everything for me, and carries everything inside a docker image:

$ $ xgo --targets=windows/amd64 .
Checking docker installation...
Client: Docker Engine - Community
 Version:           27.0.3
 API version:       1.46
 Go version:        go1.21.11
 Git commit:        7d4bcd8
 Built:             Sat Jun 29 00:02:33 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.0.3
  API version:      1.46 (minimum version 1.24)
  Go version:       go1.21.11
  Git commit:       662f78c
  Built:            Sat Jun 29 00:02:33 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.18
  GitCommit:        ae71819c4f5e67bb4d5ae76a6b735f29cc25774e
 runc:
  Version:          1.7.18
  GitCommit:        v1.1.13-0-g58aa920
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Checking for required docker image techknowlogick/xgo:latest... found.
Cross compiling local repository: . : ...
Enabled Go module support
Building /source/go.mod...
Compiling for windows-4.0/amd64...
......
/usr/local/go/pkg/tool/linux_amd64/link: running x86_64-w64-mingw32-gcc-posix failed: exit status 1
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lWs2_32
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lOle32
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lAdvapi32
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lShell32
/usr/bin/x86_64-w64-mingw32-ld: cannot find -lUser32
collect2: error: ld returned 1 exit status

Notice the identical error message. Which suggests the problem is somewhere in go-webui...

Expected Behavior

I do expect cross-compilation to work or a statement that it isn't supported to be present in the documentation.

Reproduction Steps

No response

Error Logs

No response

Possible Solution

No response

Version

2.4.3

Environment Details (OS name, version, etc.)

Linux 6.9.9-1-liquorix-amd64 x86_64 GNU/Linux
Ubuntu 22.04
Mihara commented 1 month ago

After much digging, there was some progress.

Turns out, Linux cross-compilation does not tolerate using upper case in LDFLAGS, since while Windows filesystems might not be case-sensitive, Linux filesystems which the MinGW libraries are installed to are. Editing cgo.go to replace -lWs2_32 with -lws2_32 (and all the other cases) fixes this particular problem. It still doesn't compile, however, complaining about undefined reference to `clock_gettime'...

Mihara commented 1 month ago

...which is solved by adding -lpthread -static to LDFLAGS, although I'm not certain this is the correct way out of this particular difficulty. (where would a native Windows compile get clock_gettime anyway?)