From 2433e31f3d5be1ade97895d20d7ae398061a5816 Mon Sep 17 00:00:00 2001 From: Vinicius Fortuna Date: Fri, 5 Apr 2024 20:11:35 -0400 Subject: [PATCH 1/5] Build linux with musl-cross --- Makefile | 10 ++++------ src/electron/README.md | 13 +++++++++++-- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index d19085aaf1..a179ef2ba1 100644 --- a/Makefile +++ b/Makefile @@ -44,11 +44,9 @@ 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=x86_64-linux-musl-gcc go build --ldflags=--extldflags=$(XGO_LDFLAGS) -o "$@" $(ELECTRON_PKG) # TODO: build directly when on windows WINDOWS_BUILDDIR=$(BUILDDIR)/windows @@ -57,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=$(XGO_LDFLAGS) -o "$@" $(ELECTRON_PKG) + GOOS=windows GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-w64-mingw32-gcc go build --ldflags=--extldflags=$(XGO_LDFLAGS) -o "$@" $(ELECTRON_PKG) $(GOMOBILE): go.mod mkdir -p "$(@D)" diff --git a/src/electron/README.md b/src/electron/README.md index 5db4ed4306..3f0f317513 100644 --- a/src/electron/README.md +++ b/src/electron/README.md @@ -29,7 +29,7 @@ npm run action electron/start [windows|linux] ## Windows -To build for Windows on a macOS, you need to first install [MinGW-w64](https://www.mingw-w64.org/) v11.0.1+. +To build for Windows on a macOS or Linux, you need to first install [MinGW-w64](https://www.mingw-w64.org/) v11.0.1+. With [MacPorts](https://www.mingw-w64.org/downloads/#macports) (official channel): @@ -43,7 +43,7 @@ With Homebrew (unofficial, how to ensure consistent version?): brew install mingw-w64 ``` -You can also build it on Ubuntu. To install MinGW-w64: +On Ubuntu: ```sh apt update && apt install -y gcc-mingw-w64-x86-64 @@ -52,3 +52,12 @@ apt update && apt install -y gcc-mingw-w64-x86-64 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 +``` From 10dfd0b64aadb9ce19779ef668b99117d032f78f Mon Sep 17 00:00:00 2001 From: Vinicius Fortuna Date: Wed, 10 Apr 2024 10:23:33 -0400 Subject: [PATCH 2/5] Use -static --- Makefile | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index a179ef2ba1..f1c5e47b41 100644 --- a/Makefile +++ b/Makefile @@ -34,9 +34,8 @@ $(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)' +LDFLAGS='-static -w -X main.version=$(TUN2SOCKS_VERSION)' ELECTRON_PKG="./client/src/tun2socks/outline/electron" # TODO: build directly when on linux @@ -46,7 +45,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=$(XGO_LDFLAGS) -o "$@" $(ELECTRON_PKG) + GOOS=linux GOARCH=amd64 CGO_ENABLED=1 CC=x86_64-linux-musl-gcc go build --ldflags=--extldflags=$(LDFLAGS) -o "$@" $(ELECTRON_PKG) # TODO: build directly when on windows WINDOWS_BUILDDIR=$(BUILDDIR)/windows @@ -55,15 +54,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=x86_64-w64-mingw32-gcc go build --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 From afbaa317ed2f6edf66a0498597940738e059159c Mon Sep 17 00:00:00 2001 From: Vinicius Fortuna Date: Wed, 10 Apr 2024 12:14:19 -0400 Subject: [PATCH 3/5] Use zig --- Makefile | 5 +++-- src/electron/README.md | 41 ++++++----------------------------------- 2 files changed, 9 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index f1c5e47b41..c541f87240 100644 --- a/Makefile +++ b/Makefile @@ -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" @@ -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 @@ -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)" diff --git a/src/electron/README.md b/src/electron/README.md index 3f0f317513..6aec628d26 100644 --- a/src/electron/README.md +++ b/src/electron/README.md @@ -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//bin/podman-mac-helper install -podman machine start -``` - -> You may run into the error: `/var/folders//xgo-cache: no such file or directory`. If so, simply create that directory with `mkdir -p /var/folders//xgo-cache` and try again. - To build the Electron clients, run (it will also package an installer executable into `build/dist`): ```sh @@ -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 -``` From ab04bf49285697f2eee836ed2be01804d95f4d1e Mon Sep 17 00:00:00 2001 From: Vinicius Fortuna Date: Wed, 10 Apr 2024 12:22:06 -0400 Subject: [PATCH 4/5] Flags --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index c541f87240..d8d55f83f6 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ linux: $(LINUX_BUILDDIR)/tun2socks $(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) + 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 @@ -55,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 --trimpath --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)" From 7767855a696034722ec0f86c3612ee1a734c2213 Mon Sep 17 00:00:00 2001 From: Vinicius Fortuna Date: Wed, 10 Apr 2024 14:07:35 -0400 Subject: [PATCH 5/5] Address comments --- Makefile | 2 +- src/electron/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index d8d55f83f6..07509e76fa 100644 --- a/Makefile +++ b/Makefile @@ -55,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 -trimpath -ldflags=--extldflags=$(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)" diff --git a/src/electron/README.md b/src/electron/README.md index 6aec628d26..d737bdfb95 100644 --- a/src/electron/README.md +++ b/src/electron/README.md @@ -20,7 +20,7 @@ To build the app for a platform target on a different host target, you will need [Install zig](https://ziglang.org/learn/getting-started/#installing-zig) and make sure it's in the PATH. -You can download the binary tarball, or use a package manager, like Homebrew: +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 zig