Skip to content

Commit

Permalink
Use zig
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna committed Apr 10, 2024
1 parent 10dfd0b commit afbaa31
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 37 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ $(BUILDDIR)/apple/Tun2socks.xcframework: $(BUILDDIR)/ios/Tun2socks.xcframework $
xargs xcrun xcodebuild -create-xcframework -output "$@"

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

Expand All @@ -45,7 +46,7 @@ linux: $(LINUX_BUILDDIR)/tun2socks

$(LINUX_BUILDDIR)/tun2socks:
mkdir -p "$(@D)"
GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-linux-musl-gcc go build --ldflags=--extldflags=$(LDFLAGS) -o "$@" $(ELECTRON_PKG)
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 @@ -54,7 +55,7 @@ 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=$(LDFLAGS) -o "$@" $(ELECTRON_PKG)
GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build --trimpath --ldflags=--extldflags=$(LDFLAGS) -o "$@" $(ELECTRON_PKG)

$(GOMOBILE): go.mod
mkdir -p "$(@D)"
Expand Down
41 changes: 6 additions & 35 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,37 +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 or Linux, 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):

```sh
sudo port install x86_64-w64-mingw32-gcc @11.0.1
```
[Install zig](https://ziglang.org/learn/getting-started/#installing-zig) and make sure it's in the PATH.

With Homebrew (unofficial, how to ensure consistent version?):
You can download the binary tarball, or use a package manager, like Homebrew:

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

On Ubuntu:

```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.

## Linux

To build for Linux on a macOS, you need to first install the [musl-cross compiler](https://github.com/GregorR/musl-cross).
You can do that with a [Homebrew formula](https://github.com/FiloSottile/homebrew-musl-cross):

```sh
brew install filosottile/musl-cross/musl-cross
```

0 comments on commit afbaa31

Please sign in to comment.