Skip to content

Commit

Permalink
Switch to using xgo to build cross-platform binaries using CGO
Browse files Browse the repository at this point in the history
  • Loading branch information
cwarden committed Nov 16, 2023
1 parent 54307da commit 659e5a0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 16 deletions.
16 changes: 13 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
version: 2
jobs:
build:
docker:
- image: cimg/go:1.20
machine:
image: ubuntu-2204:2023.07.2
steps:
- checkout
- run: go install github.com/mitchellh/[email protected]
- run: go install github.com/tcnksm/[email protected]
- run: sudo apt-get update && sudo apt-get install p7zip-full
- run: sudo bash -c "curl -L --output - https://github.com/crazy-max/xgo/releases/download/v0.30.0/xgo_0.30.0_linux_amd64.tar.gz | tar xvz -C /usr/local/bin/ ./xgo"
- restore_cache: # restores saved cache if no changes are detected since last run
keys:
- go-mod-v4-{{ checksum "go.sum" }}
- run: make dist
- run:
command: |
sudo chmod 666 /var/run/docker.sock
if [ -f /var/run/docker-temp.sock ]; then
sudo chmod 666 /var/run/docker-temp.sock
fi
- run:
command: |
export GOPATH=/home/circleci/.go_workspace
make dist
- save_cache:
key: go-mod-v4-{{ checksum "go.sum" }}
paths:
Expand Down
42 changes: 29 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
VERSION=$(shell git describe --abbrev=0 --always)
LDFLAGS = -ldflags "-X github.com/ForceCLI/force/lib.Version=${VERSION}"
LDFLAGS = -ldflags "-s -w -X github.com/ForceCLI/force/lib.Version=${VERSION}"
LINUX_LDFLAGS = -ldflags "-s -w -extldflags '-static' -X github.com/ForceCLI/force/lib.Version=${VERSION}"
GCFLAGS = -gcflags="all=-N -l"
EXECUTABLE=force
WINDOWS=$(EXECUTABLE)_windows_amd64.exe
LINUX=$(EXECUTABLE)_linux_amd64
OSX_AMD64=$(EXECUTABLE)_osx_amd64
OSX_ARM64=$(EXECUTABLE)_osx_arm64
PACKAGE=.
WINDOWS=$(EXECUTABLE)-windows-amd64.exe
LINUX=$(EXECUTABLE)-linux-amd64
OSX_AMD64=$(EXECUTABLE)-darwin-amd64
OSX_ARM64=$(EXECUTABLE)-darwin-arm64
ALL=$(WINDOWS) $(LINUX) $(OSX_AMD64) $(OSX_ARM64)

default:
Expand All @@ -17,17 +19,27 @@ install:
install-debug:
go install ${LDFLAGS} ${GCFLAGS}

$(WINDOWS):
env CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -v -o $(WINDOWS) ${LDFLAGS}
$(WINDOWS): checkcmd-xgo
xgo -go 1.21 -out $(EXECUTABLE) -dest . ${LDFLAGS} -buildmode default -trimpath -targets windows/amd64 -pkg ${PACKAGE} -x .

$(LINUX):
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -o $(LINUX) ${LDFLAGS}
# Build static binaries on linux
$(LINUX): checkcmd-x86_64-linux-gnu-gcc checkcmd-x86_64-linux-gnu-g++
env \
GOOS=linux \
GOARCH=amd64 \
CC=x86_64-linux-gnu-gcc \
CXX=x86_64-linux-gnu-g++ \
CGO_ENABLED=1 \
CGO_FLAGS="-static"
go build -v -tags 'netgo osusergo' -o $(LINUX) ${LINUX_LDFLAGS} ${PACKAGE}

$(OSX_AMD64):
env CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -v -o $(OSX_AMD64) ${LDFLAGS}
# Build macOS binaries using docker images that contain SDK
# See https://github.com/crazy-max/xgo and https://github.com/tpoechtrager/osxcross
$(OSX_ARM64): checkcmd-xgo
xgo -go 1.21 -out $(EXECUTABLE) -dest . ${LDFLAGS} -buildmode default -trimpath -targets darwin/arm64 -pkg ${PACKAGE} -x .

$(OSX_ARM64):
env CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -v -o $(OSX_ARM64) ${LDFLAGS}
$(OSX_AMD64): checkcmd-xgo
xgo -go 1.21 -out $(EXECUTABLE) -dest . ${LDFLAGS} -buildmode default -trimpath -targets darwin/amd64 -pkg ${PACKAGE} -x .

$(basename $(WINDOWS)).zip: $(WINDOWS)
zip $@ $<
Expand All @@ -54,4 +66,8 @@ test:
clean:
-rm -f $(EXECUTABLE) $(EXECUTABLE)_*

checkcmd-%:
@hash $(*) > /dev/null 2>&1 || \
(echo "ERROR: '$(*)' must be installed and available on your PATH."; exit 1)

.PHONY: default dist clean docs

0 comments on commit 659e5a0

Please sign in to comment.