Skip to content

Commit 532283d

Browse files
committed
Switch to using xgo to build cross-platform binaries using CGO
1 parent 54307da commit 532283d

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

.circleci/config.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
version: 2
22
jobs:
33
build:
4-
docker:
5-
- image: cimg/go:1.20
4+
machine:
5+
image: ubuntu-2204:2023.07.2
66
steps:
77
- checkout
88
- run: go install github.com/mitchellh/[email protected]
@@ -11,7 +11,10 @@ jobs:
1111
- restore_cache: # restores saved cache if no changes are detected since last run
1212
keys:
1313
- go-mod-v4-{{ checksum "go.sum" }}
14-
- run: make dist
14+
- run:
15+
command: |
16+
export GOPATH=/home/circleci/.go_workspace
17+
make dist
1518
- save_cache:
1619
key: go-mod-v4-{{ checksum "go.sum" }}
1720
paths:

Makefile

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
VERSION=$(shell git describe --abbrev=0 --always)
2-
LDFLAGS = -ldflags "-X github.com/ForceCLI/force/lib.Version=${VERSION}"
2+
LDFLAGS = -ldflags "-s -w -X github.com/ForceCLI/force/lib.Version=${VERSION}"
3+
LINUX_LDFLAGS = -ldflags "-s -w -extldflags '-static' -X github.com/ForceCLI/force/lib.Version=${VERSION}"
34
GCFLAGS = -gcflags="all=-N -l"
45
EXECUTABLE=force
5-
WINDOWS=$(EXECUTABLE)_windows_amd64.exe
6-
LINUX=$(EXECUTABLE)_linux_amd64
7-
OSX_AMD64=$(EXECUTABLE)_osx_amd64
8-
OSX_ARM64=$(EXECUTABLE)_osx_arm64
6+
PACKAGE=.
7+
WINDOWS=$(EXECUTABLE)-windows-amd64.exe
8+
LINUX=$(EXECUTABLE)-linux-amd64
9+
OSX_AMD64=$(EXECUTABLE)-darwin-amd64
10+
OSX_ARM64=$(EXECUTABLE)-darwin-arm64
911
ALL=$(WINDOWS) $(LINUX) $(OSX_AMD64) $(OSX_ARM64)
1012

1113
default:
@@ -17,17 +19,27 @@ install:
1719
install-debug:
1820
go install ${LDFLAGS} ${GCFLAGS}
1921

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

23-
$(LINUX):
24-
env CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -v -o $(LINUX) ${LDFLAGS}
25+
# Build static binaries on linux
26+
$(LINUX): checkcmd-x86_64-linux-gnu-gcc checkcmd-x86_64-linux-gnu-g++
27+
env \
28+
GOOS=linux \
29+
GOARCH=amd64 \
30+
CC=x86_64-linux-gnu-gcc \
31+
CXX=x86_64-linux-gnu-g++ \
32+
CGO_ENABLED=1 \
33+
CGO_FLAGS="-static"
34+
go build -v -tags 'netgo osusergo' -o $(LINUX) ${LINUX_LDFLAGS} ${PACKAGE}
2535

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

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

3244
$(basename $(WINDOWS)).zip: $(WINDOWS)
3345
zip $@ $<
@@ -54,4 +66,8 @@ test:
5466
clean:
5567
-rm -f $(EXECUTABLE) $(EXECUTABLE)_*
5668

69+
checkcmd-%:
70+
@hash $(*) > /dev/null 2>&1 || \
71+
(echo "ERROR: '$(*)' must be installed and available on your PATH."; exit 1)
72+
5773
.PHONY: default dist clean docs

0 commit comments

Comments
 (0)