Skip to content

Commit

Permalink
Faster make v2 (#3724)
Browse files Browse the repository at this point in the history
* Don’t use simply expanded variables for constants

Signed-off-by: David Gageot <[email protected]>

* Eval only when needed

This makes the Makefile faster for other targets.

Signed-off-by: David Gageot <[email protected]>

* Use built-in variable for current path

Signed-off-by: David Gageot <[email protected]>

* Simplify `make` and `make install`

We don’t need a target to go build for every GOOS
since this done by `make cross`.

Signed-off-by: David Gageot <[email protected]>

* Not useful for local dev and not used at release time.

At release time, we build in a Docker container
so the path is predictable.

Signed-off-by: David Gageot <[email protected]>

* Generate statik files only when needed

Signed-off-by: David Gageot <[email protected]>

* These variables can be deferred

Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot authored Feb 28, 2020
1 parent 7ee0c0b commit 64d8dcb
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 30 deletions.
56 changes: 27 additions & 29 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
GOOS ?= $(shell go env GOOS)
GOARCH = amd64
BUILD_DIR ?= ./out
ORG := github.com/GoogleContainerTools
PROJECT := skaffold
ORG = github.com/GoogleContainerTools
PROJECT = skaffold
REPOPATH ?= $(ORG)/$(PROJECT)
RELEASE_BUCKET ?= $(PROJECT)
GSC_BUILD_PATH ?= gs://$(RELEASE_BUCKET)/builds/$(COMMIT)
Expand All @@ -28,10 +28,11 @@ GCP_PROJECT ?= k8s-skaffold
GKE_CLUSTER_NAME ?= integration-tests
GKE_ZONE ?= us-central1-a

SUPPORTED_PLATFORMS := linux-$(GOARCH) darwin-$(GOARCH) windows-$(GOARCH).exe
SUPPORTED_PLATFORMS = linux-$(GOARCH) darwin-$(GOARCH) windows-$(GOARCH).exe
BUILD_PACKAGE = $(REPOPATH)/cmd/skaffold

SKAFFOLD_TEST_PACKAGES := $(shell go list ./... | grep -v diag)
SKAFFOLD_TEST_PACKAGES = $(shell go list ./... | grep -v diag)
GO_FILES = $(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./pkg/diag/*")

VERSION_PACKAGE = $(REPOPATH)/pkg/skaffold/version
COMMIT = $(shell git rev-parse HEAD)
Expand All @@ -45,16 +46,13 @@ endif
export GO111MODULE = on
export GOFLAGS = -mod=vendor

GO_GCFLAGS := "all=-trimpath=${PWD}"
GO_ASMFLAGS := "all=-trimpath=${PWD}"

LDFLAGS_linux = -static
LDFLAGS_darwin =
LDFLAGS_windows =

GO_BUILD_TAGS_linux := "osusergo netgo static_build release"
GO_BUILD_TAGS_darwin := "release"
GO_BUILD_TAGS_windows := "release"
GO_BUILD_TAGS_linux = "osusergo netgo static_build release"
GO_BUILD_TAGS_darwin = "release"
GO_BUILD_TAGS_windows = "release"

GO_LDFLAGS = -X $(VERSION_PACKAGE).version=$(VERSION)
GO_LDFLAGS += -X $(VERSION_PACKAGE).buildDate=$(shell date +'%Y-%m-%dT%H:%M:%SZ')
Expand All @@ -66,16 +64,23 @@ GO_LDFLAGS_windows =" $(GO_LDFLAGS) -extldflags \"$(LDFLAGS_windows)\""
GO_LDFLAGS_darwin =" $(GO_LDFLAGS) -extldflags \"$(LDFLAGS_darwin)\""
GO_LDFLAGS_linux =" $(GO_LDFLAGS) -extldflags \"$(LDFLAGS_linux)\""

GO_FILES := $(shell find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./pkg/diag/*")
DEPS_DIGEST := $(shell ./hack/skaffold-deps-sha1.sh)
STATIK_FILES = cmd/skaffold/app/cmd/statik/statik.go

# Build for local development.
$(BUILD_DIR)/$(PROJECT): $(STATIK_FILES) $(GO_FILES) $(BUILD_DIR)
GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=1 go build -tags $(GO_BUILD_TAGS_$(GOOS)) -ldflags $(GO_LDFLAGS_$(GOOS)) -o $@ $(BUILD_PACKAGE)

.PHONY: install
install: $(BUILD_DIR)/$(PROJECT)
cp $(BUILD_DIR)/$(PROJECT) $(GOPATH)/bin/$(PROJECT)

$(BUILD_DIR)/$(PROJECT): $(BUILD_DIR)/$(PROJECT)-$(GOOS)-$(GOARCH)
cp $(BUILD_DIR)/$(PROJECT)-$(GOOS)-$(GOARCH) $@
# Build for a release.
.PRECIOUS: $(foreach platform, $(SUPPORTED_PLATFORMS), $(BUILD_DIR)/$(PROJECT)-$(platform))

$(BUILD_DIR)/$(PROJECT)-$(GOOS)-$(GOARCH): generate-statik $(GO_FILES) $(BUILD_DIR)
GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=1 go build -tags $(GO_BUILD_TAGS_$(GOOS)) -ldflags $(GO_LDFLAGS_$(GOOS)) -gcflags $(GO_GCFLAGS) -asmflags $(GO_ASMFLAGS) -o $@ $(BUILD_PACKAGE)
.PHONY: cross
cross: $(foreach platform, $(SUPPORTED_PLATFORMS), $(BUILD_DIR)/$(PROJECT)-$(platform).sha256)

$(BUILD_DIR)/$(PROJECT)-%-$(GOARCH): generate-statik $(GO_FILES) $(BUILD_DIR)
$(BUILD_DIR)/$(PROJECT)-%-$(GOARCH): $(STATIK_FILES) $(GO_FILES) $(BUILD_DIR)
docker build \
--build-arg PROJECT=$(REPOPATH) \
--build-arg TARGETS=$*/$(GOARCH) \
Expand All @@ -99,11 +104,6 @@ $(BUILD_DIR)/VERSION: $(BUILD_DIR)
$(BUILD_DIR):
mkdir -p $(BUILD_DIR)

.PRECIOUS: $(foreach platform, $(SUPPORTED_PLATFORMS), $(BUILD_DIR)/$(PROJECT)-$(platform))

.PHONY: cross
cross: $(foreach platform, $(SUPPORTED_PLATFORMS), $(BUILD_DIR)/$(PROJECT)-$(platform).sha256)

.PHONY: test
test: $(BUILD_DIR)
@ ./hack/gotest.sh -count=1 -race -short -timeout=90s $(SKAFFOLD_TEST_PACKAGES)
Expand All @@ -122,12 +122,8 @@ checks: $(BUILD_DIR)
quicktest:
@ ./hack/gotest.sh -short -timeout=60s $(SKAFFOLD_TEST_PACKAGES)

.PHONY: install
install: $(GO_FILES) $(BUILD_DIR)
GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=1 go install -tags $(GO_BUILD_TAGS_$(GOOS)) -ldflags $(GO_LDFLAGS_$(GOOS)) -gcflags $(GO_GCFLAGS) -asmflags $(GO_ASMFLAGS) $(BUILD_PACKAGE)

.PHONY: integration
integration: generate-statik install
integration: install
ifeq ($(GCP_ONLY),true)
gcloud container clusters get-credentials \
$(GKE_CLUSTER_NAME) \
Expand Down Expand Up @@ -166,6 +162,7 @@ clean:

.PHONY: build_deps
build_deps:
$(eval DEPS_DIGEST := $(shell ./hack/skaffold-deps-sha1.sh))
docker build \
-f deploy/skaffold/Dockerfile.deps \
-t gcr.io/$(GCP_PROJECT)/build_deps:$(DEPS_DIGEST) \
Expand Down Expand Up @@ -243,6 +240,7 @@ build-docs-preview:
generate-schemas:
go run hack/schemas/main.go

.PHONY: generate-statik
generate-statik:
# static files

$(STATIK_FILES): go.mod docs/content/en/schemas/*
hack/generate-statik.sh
2 changes: 1 addition & 1 deletion deploy/skaffold/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ COPY . .

FROM builder as release
ARG VERSION
RUN make clean && make out/skaffold-linux-amd64 VERSION=$VERSION && mv out/skaffold-linux-amd64 /usr/bin/skaffold
RUN make clean out/skaffold VERSION=$VERSION && mv out/skaffold /usr/bin/skaffold
RUN skaffold credits -d /THIRD_PARTY_NOTICES

0 comments on commit 64d8dcb

Please sign in to comment.