From 3c607ed3275f216ccd034f51c8cbdb8f83d98733 Mon Sep 17 00:00:00 2001 From: Junyi Yi Date: Mon, 11 Nov 2024 20:41:47 -0500 Subject: [PATCH] add library build script to Go taskfile --- client/go/Taskfile.yml | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/client/go/Taskfile.yml b/client/go/Taskfile.yml index 5d17c13b0d..d100d225eb 100644 --- a/client/go/Taskfile.yml +++ b/client/go/Taskfile.yml @@ -25,32 +25,45 @@ vars: tasks: electron: - desc: "Build the tun2socks binary for Electron platforms" + desc: "Build the tun2socks binary and library for Electron platforms" internal: true - requires: {vars: [TARGET_OS]} + requires: {vars: [TARGET_OS, TARGET_ARCH]} vars: # TODO(fortuna): remove this, it's not really needed since we don't release it separately anymore. TUN2SOCKS_VERSION: "v1.16.11" OUTPUT: '{{.OUT_DIR}}/{{.TARGET_OS}}/tun2socks{{if eq .TARGET_OS "windows"}}.exe{{end}}' + # Linux=libbackend.so; Windows=backend.dll + OUTPUT_LIB: '{{.OUT_DIR}}/{{.TARGET_OS}}/{{if eq .TARGET_OS "linux"}}libbackend.so{{else}}backend.dll{{end}}' cmds: - rm -rf "{{dir .OUTPUT}}" && mkdir -p "{{dir .OUTPUT}}" + # C cross-compile (zig) targets: + # Linux (x64) = x86_64-linux-gnu (`x86_64-linux` defaults to `x86_64-linux-musl`, prefer `gnu` over `musl`) + # Windows (x86) = x86-windows # Linker flags: https://pkg.go.dev/cmd/link - # -s Omit the symbol table and debug information. - # -w Omit the DWARF symbol table. - # -X Set the value of the string variable. + # -s Omit the symbol table and debug information. + # -w Omit the DWARF symbol table. + # -X Set the value of the string variable. - | - {{if ne OS .TARGET_OS -}} - GOOS={{.TARGET_OS}} GOARCH=amd64 CGO_ENABLED=1 CC='zig cc -target x86_64-{{.TARGET_OS}}' + {{if or (ne OS .TARGET_OS) (ne ARCH .TARGET_ARCH) -}} + GOOS={{.TARGET_OS}} GOARCH={{.TARGET_ARCH}} CGO_ENABLED=1 \ + CC='zig cc -target {{if eq .TARGET_ARCH "386"}}x86{{else}}x86_64{{end}}-{{.TARGET_OS}}{{if eq .TARGET_OS "linux"}}-gnu{{end}}' {{- end}} \ go build -trimpath -ldflags="-s -w -X=main.version={{.TUN2SOCKS_VERSION}}" -o '{{.OUTPUT}}' '{{.TASKFILE_DIR}}/outline/electron' + - | + {{if or (ne OS .TARGET_OS) (ne ARCH .TARGET_ARCH) -}} + GOOS={{.TARGET_OS}} GOARCH={{.TARGET_ARCH}} CGO_ENABLED=1 \ + CC='zig cc -target {{if eq .TARGET_ARCH "386"}}x86{{else}}x86_64{{end}}-{{.TARGET_OS}}{{if eq .TARGET_OS "linux"}}-gnu{{end}}' + {{- end}} \ + go build -trimpath -buildmode=c-shared -ldflags="-s -w -X=main.version={{.TUN2SOCKS_VERSION}}" -o '{{.OUTPUT_LIB}}' '{{.TASKFILE_DIR}}/outline/electron' windows: - desc: "Build the tun2socks binary for Windows" - cmds: [{task: electron, vars: {TARGET_OS: "windows"}}] + desc: "Build the tun2socks binary and library for Windows" + # 32bit Windows 10 still exists until October 14, 2025 + cmds: [{task: electron, vars: {TARGET_OS: "windows", TARGET_ARCH: "386"}}] linux: - desc: "Build the tun2socks binary for Linux" - cmds: [{task: electron, vars: {TARGET_OS: "linux"}}] + desc: "Build the tun2socks binary and library for Linux" + cmds: [{task: electron, vars: {TARGET_OS: "linux", TARGET_ARCH: "amd64"}}] android: desc: "Build the tun2socks.aar library for Android"