Skip to content

Commit

Permalink
Merge pull request #99 from ocean/reorganise-makefile
Browse files Browse the repository at this point in the history
chore: Realign binary naming & ldflags
  • Loading branch information
ocean authored Aug 1, 2022
2 parents 8fc2fd0 + 60c87a0 commit 8ae52e3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
7 changes: 7 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ jobs:
- run:
name: Install BATS.
command: git clone https://github.com/bats-core/bats-core.git && cd bats-core && sudo ./install.sh /usr/local && bats --version && cd -
- run:
name: Install BATS extras.
command: |
cd tests
git clone https://github.com/bats-core/bats-support.git ./test_helpers/bats-support
git clone https://github.com/bats-core/bats-assert.git ./test_helpers/bats-assert
cd -
- run:
name: Install Go linter.
command: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.46.2
Expand Down
41 changes: 15 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ VERSION := $(shell git describe --tag $(GITCOMMIT) 2>/dev/null)

GITBRANCH := $(shell git rev-parse --abbrev-ref HEAD 2>/dev/null)
BUILDTIME := $(shell TZ=GMT date "+%Y-%m-%d_%H:%M_GMT")
LDFLAGS := "-X main.version=$(VERSION) -X main.GitCommit=$(GITCOMMIT) -X main.GitBranch=$(GITBRANCH) -X main.BuildTime=$(BUILDTIME)"
LDFLAGS := "-s -w -X main.version=$(VERSION) -X main.GitCommit=$(GITCOMMIT) -X main.GitBranch=$(GITBRANCH) -X main.BuildTime=$(BUILDTIME)"

SRCS = $(shell find . -name '*.go' | grep -v '^./vendor/')
PKGS := $(foreach pkg, $(sort $(dir $(SRCS))), $(pkg))

OS := linux darwin windows
ARCH := amd64 arm64

TESTARGS ?=

default:
Expand All @@ -17,34 +20,20 @@ install:
cp ahoy /usr/local/bin/ahoy
chmod +x /usr/local/bin/ahoy

cross: build_dir
GOOS=linux GOARCH=amd64 \
go build -ldflags $(LDFLAGS) -v -o ./builds/linux_amd64/ahoy

GOOS=linux GOARCH=arm64 \
go build -ldflags $(LDFLAGS) -v -o ./builds/linux_arm64/ahoy

GOOS=darwin GOARCH=amd64 \
go build -ldflags $(LDFLAGS) -v -o ./builds/darwin_amd64/ahoy

GOOS=darwin GOARCH=arm64 \
go build -ldflags $(LDFLAGS) -v -o ./builds/darwin_arm64/ahoy
build_dir:
mkdir -p ./builds

cross_tars: cross
COPYFILE_DISABLE=1 tar -zcvf ./builds/ahoy_linux_amd64.tar.gz -C builds/linux_amd64 ahoy
COPYFILE_DISABLE=1 tar -zcvf ./builds/ahoy_linux_arm64.tar.gz -C builds/linux_arm64 ahoy
COPYFILE_DISABLE=1 tar -zcvf ./builds/ahoy_darwin_amd64.tar.gz -C builds/darwin_amd64 ahoy
COPYFILE_DISABLE=1 tar -zcvf ./builds/ahoy_darwin_arm64.tar.gz -C builds/darwin_arm64 ahoy
cross: build_dir
$(foreach os,$(OS), \
$(foreach arch,$(ARCH), \
GOOS=$(os) GOARCH=$(arch) go build -trimpath -ldflags $(LDFLAGS) -v -o ./builds/ahoy-bin-$(os)-$(arch); \
) \
)

build_dir:
mkdir -p ./builds/linux_amd64
mkdir -p ./builds/linux_arm64
mkdir -p ./builds/darwin_amd64
mkdir -p ./builds/darwin_arm64
$(foreach arch,$(ARCH),mv ./builds/ahoy-bin-windows-$(arch) ./builds/ahoy-bin-windows-$(arch).exe;)

clean:
cd builds
rm -Rf linux* darwin* *.tar.gz
rm -vRf ./builds/ahoy-bin-*

fmtcheck:
$(foreach file,$(SRCS),gofmt $(file) | diff -u $(file) - || exit;)
Expand All @@ -66,4 +55,4 @@ test: fmtcheck staticcheck vet
version:
@echo $(VERSION)

.PHONY: clean test fmtcheck staticcheck vet gocyclo version testdeps cross cross_tars build_dir default install
.PHONY: clean test fmtcheck staticcheck vet gocyclo version testdeps cross build_dir default install
31 changes: 31 additions & 0 deletions tests/cross-compile.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bats

load 'test_helpers/bats-support/load'
load 'test_helpers/bats-assert/load'

@test "test cross compilation command lifecycle" {
run make clean
assert_success
assert_output --partial 'rm -vRf ./builds/ahoy-bin-*'

run ls ./builds/ahoy-*
assert_failure
assert_output --partial 'No such file'

run make cross
assert_success
assert_output --partial 'mv ./builds/ahoy-bin-windows-amd64 ./builds/ahoy-bin-windows-amd64.exe; mv ./builds/ahoy-bin-windows-arm64 ./builds/ahoy-bin-windows-arm64.exe;'

run ls ./builds/ahoy-*
assert_output --partial 'ahoy-bin-darwin-amd64'
assert_output --partial 'ahoy-bin-linux-arm64'
assert_output --partial 'ahoy-bin-windows-amd64.exe'

run make clean
assert_success
assert_output --partial 'rm -vRf ./builds/ahoy-bin-*'

run ls ./builds/ahoy-*
assert_failure
assert_output --partial 'No such file'
}

0 comments on commit 8ae52e3

Please sign in to comment.