Skip to content

Commit a5b7276

Browse files
authored
Makefile for cross compilation (iter8-tools#1110)
* fixing makefile Signed-off-by: Srinivasan Parthasarathy <spartha@us.ibm.com> * fixing makefile further Signed-off-by: Srinivasan Parthasarathy <spartha@us.ibm.com> * staticcheck separate Signed-off-by: Srinivasan Parthasarathy <spartha@us.ibm.com>
1 parent b4c207e commit a5b7276

File tree

3 files changed

+115
-18
lines changed

3 files changed

+115
-18
lines changed

.github/workflows/tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ jobs:
5454
- name: Checkout repository
5555
uses: actions/checkout@v2
5656

57-
- run: make build
57+
- run: make install
5858

5959
- name: run Iter8 experiment and assert
6060
run: |

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ site
128128
Chart.lock
129129

130130
# Iter8 binary and yamls and reports
131+
bin/
132+
_dist/
131133
iter8
132134
clibase/clibase
133135
result.yaml

Makefile

Lines changed: 112 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,128 @@
1+
BINDIR := $(CURDIR)/bin
2+
INSTALL_PATH ?= /usr/local/bin
3+
DIST_DIRS := find * -type d -exec
4+
TARGETS := darwin/amd64 darwin/arm64 linux/amd64 linux/386 linux/arm linux/arm64 linux/ppc64le linux/s390x windows/amd64
5+
6+
BINNAME ?= iter8
7+
ITER8_IMG ?= iter8/iter8:latest
8+
9+
GOBIN = $(shell go env GOBIN)
10+
ifeq ($(GOBIN),)
11+
GOBIN = $(shell go env GOPATH)/bin
12+
endif
13+
GOX = $(GOBIN)/gox
14+
15+
# go options
16+
TAGS :=
17+
LDFLAGS := -w -s
18+
GOFLAGS :=
19+
20+
# Rebuild the binary if any of these files change
21+
SRC := $(shell find . -type f -name '*.go' -print) go.mod go.sum
22+
23+
# Required for globs to work correctly
24+
SHELL = /usr/bin/env bash
25+
26+
GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)
27+
28+
ifdef VERSION
29+
BINARY_VERSION = $(VERSION)
30+
endif
31+
BINARY_VERSION ?= ${GIT_TAG}
32+
33+
# Only set Version if building a tag or VERSION is set
34+
ifneq ($(BINARY_VERSION),)
35+
LDFLAGS += -X github.com/iter8-tools/iter8/basecli/RootCmd.Version=${BINARY_VERSION}
36+
endif
37+
38+
LDFLAGS += $(EXT_LDFLAGS)
39+
40+
.PHONY: all
41+
all: build
42+
43+
# ------------------------------------------------------------------------------
44+
# build
45+
46+
.PHONY: build
47+
build: $(BINDIR)/$(BINNAME)
48+
49+
$(BINDIR)/$(BINNAME): $(SRC)
50+
GO111MODULE=on go build $(GOFLAGS) -trimpath -tags '$(TAGS)' -ldflags '$(LDFLAGS)' -o '$(BINDIR)'/$(BINNAME) ./
51+
52+
.PHONY: cmddocs
53+
cmddocs:
54+
go run cmd/docs/main.go
55+
56+
# ------------------------------------------------------------------------------
57+
# install
58+
59+
.PHONY: install
60+
install: build
61+
@install "$(BINDIR)/$(BINNAME)" "$(INSTALL_PATH)/$(BINNAME)"
62+
63+
# ------------------------------------------------------------------------------
64+
# dependencies
65+
66+
# If go get is run from inside the project directory it will add the dependencies
67+
# to the go.mod file. To avoid that we change to a directory without a go.mod file
68+
# when downloading the following dependencies
69+
70+
$(GOX):
71+
(cd /; GO111MODULE=on go get -u github.com/mitchellh/gox)
72+
73+
# ------------------------------------------------------------------------------
74+
# release
75+
76+
.PHONY: build-cross
77+
build-cross: LDFLAGS += -extldflags "-static"
78+
build-cross: $(GOX)
79+
GOFLAGS="-trimpath" GO111MODULE=on CGO_ENABLED=0 $(GOX) -parallel=3 -output="_dist/{{.OS}}-{{.Arch}}/$(BINNAME)" -osarch='$(TARGETS)' $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' ./
80+
81+
.PHONY: dist
82+
dist: build-cross
83+
( \
84+
cd _dist && \
85+
$(DIST_DIRS) cp ../LICENSE {} \; && \
86+
$(DIST_DIRS) cp ../README.md {} \; && \
87+
$(DIST_DIRS) tar -zcf iter8-${VERSION}-{}.tar.gz {} \; && \
88+
$(DIST_DIRS) zip -r iter8-${VERSION}-{}.zip {} \; \
89+
)
90+
91+
.PHONY: clean
92+
clean:
93+
@rm -rf '$(BINDIR)' ./_dist
94+
95+
.PHONY: docker-build
96+
docker-build:
97+
docker build -f Dockerfile -t $(ITER8_IMG) .
98+
99+
.PHONY: docker-push
100+
docker-push:
101+
docker push $(ITER8_IMG)
102+
103+
# ------------------------------------------------------------------------------
104+
# test
105+
106+
.PHONY: fmt
1107
fmt: ## Run go fmt against code.
2108
go fmt ./...
3109

110+
.PHONY: vet
4111
vet: ## Run go vet against code
5112
go vet ./...
6113

114+
.PHONY: staticcheck
7115
staticcheck:
8116
staticcheck ./...
9117

118+
.PHONY: test
10119
test: fmt vet ## Run tests.
11120
go test ./... -race -coverprofile=coverage.out -covermode=atomic
12121

122+
.PHONY: coverage
13123
coverage: test
14124
@echo "test coverage: $(shell go tool cover -func coverage.out | grep total | awk '{print substr($$3, 1, length($$3)-1)}')"
15125

16-
htmlcov:
126+
.PHONY: htmlcov
127+
htmlcov: coverage
17128
go tool cover -html=coverage.out
18-
19-
cmddocs:
20-
go run cmd/docs/main.go
21-
22-
# complete path to iter8 binary
23-
ITER8_BIN ?= /usr/local/bin/iter8
24-
build:
25-
go build -o $(ITER8_BIN) main.go
26-
27-
28-
ITER8_IMG ?= iter8/iter8:latest
29-
docker-build:
30-
docker build -f Dockerfile -t $(ITER8_IMG) .
31-
32-
docker-push:
33-
docker push $(ITER8_IMG)

0 commit comments

Comments
 (0)