Skip to content

Commit

Permalink
Build linux with musl-cross (#1960)
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna authored Apr 10, 2024
1 parent bac3931 commit 1089944
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 38 deletions.
17 changes: 6 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,19 @@ $(BUILDDIR)/apple/Tun2socks.xcframework: $(BUILDDIR)/ios/Tun2socks.xcframework $
find $^ -name "Tun2socks.framework" -type d | xargs -I {} echo " -framework {} " | \
xargs xcrun xcodebuild -create-xcframework -output "$@"

XGO=$(GOBIN)/xgo
TUN2SOCKS_VERSION=v1.16.11
XGO_LDFLAGS='-w -X main.version=$(TUN2SOCKS_VERSION)'
# -w disable DWARF generation
LDFLAGS='-static -w -X main.version=$(TUN2SOCKS_VERSION)'
ELECTRON_PKG="./client/src/tun2socks/outline/electron"

# TODO: build directly when on linux
LINUX_BUILDDIR=$(BUILDDIR)/linux

linux: $(LINUX_BUILDDIR)/tun2socks

$(LINUX_BUILDDIR)/tun2socks: $(XGO)
mkdir -p "$(LINUX_BUILDDIR)/$(IMPORT_PATH)"
$(XGO) -ldflags $(XGO_LDFLAGS) --targets=linux/amd64 -dest "$(LINUX_BUILDDIR)" -pkg $(ELECTRON_PKG) .
mv "$(LINUX_BUILDDIR)/$(IMPORT_PATH)-linux-amd64" "$@"
rm -r "$(LINUX_BUILDDIR)/$(IMPORT_HOST)"
$(LINUX_BUILDDIR)/tun2socks:
mkdir -p "$(@D)"
GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC='zig cc -target x86_64-linux' go build -trimpath -ldflags=--extldflags=$(LDFLAGS) -o "$@" $(ELECTRON_PKG)

# TODO: build directly when on windows
WINDOWS_BUILDDIR=$(BUILDDIR)/windows
Expand All @@ -57,15 +55,12 @@ windows: $(WINDOWS_BUILDDIR)/tun2socks.exe

$(WINDOWS_BUILDDIR)/tun2socks.exe:
mkdir -p "$(@D)"
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC="x86_64-w64-mingw32-gcc" go build --ldflags=--extldflags=$(XGO_LDFLAGS) -o "$@" $(ELECTRON_PKG)
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC='zig cc -target x86_64-windows' go build -trimpath -ldflags=--extldflags=$(LDFLAGS) -o "$@" $(ELECTRON_PKG)

$(GOMOBILE): go.mod
mkdir -p "$(@D)"
go build -o "$(@D)" golang.org/x/mobile/cmd/gomobile golang.org/x/mobile/cmd/gobind

$(XGO): go.mod
env GOBIN="$(GOBIN)" go install github.com/crazy-max/xgo

go.mod: tools.go
go mod tidy
touch go.mod
Expand Down
34 changes: 7 additions & 27 deletions src/electron/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,6 @@

Unlike the Android and Apple clients, the Windows and Linux clients use the Electron framework, rather than Cordova.

You will need [Docker](https://www.docker.com/) installed to build the Electron clients.

> If you can't use Docker, you can use [podman](https://podman.io) as substitute by running the following (for macOS):
```sh
podman machine init
sudo ln -s $(which podman) /usr/local/bin/docker
sudo /opt/homebrew/Cellar/podman/<podman version>/bin/podman-mac-helper install
podman machine start
```

> You may run into the error: `/var/folders/<path>/xgo-cache: no such file or directory`. If so, simply create that directory with `mkdir -p /var/folders/<path>/xgo-cache` and try again.
To build the Electron clients, run (it will also package an installer executable into `build/dist`):

```sh
Expand All @@ -27,28 +14,21 @@ To run the Electron clients, run:
npm run action electron/start [windows|linux]
```

## Windows
## Cross-Compiling

To build for Windows on a macOS, you need to first install [MinGW-w64](https://www.mingw-w64.org/) v11.0.1+.
To build the app for a platform target on a different host target, you will need a cross-compiler. We use [zig to cross-compile with cgo](https://dev.to/kristoff/zig-makes-go-cross-compilation-just-work-29ho).

With [MacPorts](https://www.mingw-w64.org/downloads/#macports) (official channel):
[Install zig](https://ziglang.org/learn/getting-started/#installing-zig) and make sure it's in the PATH.

```sh
sudo port install x86_64-w64-mingw32-gcc @11.0.1
```

With Homebrew (unofficial, how to ensure consistent version?):
You can download the binary tarball, or [use a package manager](https://github.com/ziglang/zig/wiki/Install-Zig-from-a-Package-Manager), like Homebrew:

```sh
brew install mingw-w64
brew install zig
```

You can also build it on Ubuntu. To install MinGW-w64:

```sh
apt update && apt install -y gcc-mingw-w64-x86-64
```
## Release

To build the _release_ version of Windows installer, you'll also need:

- [Java 8+ Runtime](https://www.java.com/en/download/). This is required for the cross-platform Windows executable signing tool [Jsign](https://ebourg.github.io/jsign/). If you don't need to sign the executables, feel free to skip this.

0 comments on commit 1089944

Please sign in to comment.