From 4faed269f255f226b820e17c243452eafd877284 Mon Sep 17 00:00:00 2001 From: guoxudong Date: Mon, 20 Jan 2020 14:37:16 +0800 Subject: [PATCH] add Makefile --- .gitignore | 1 + .goreleaser.yml | 3 +-- Makefile | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index ad1d74b7..cd06b62d 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ dist/ kubecm +target \ No newline at end of file diff --git a/.goreleaser.yml b/.goreleaser.yml index 67227f70..d4f1001c 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -2,7 +2,7 @@ # Make sure to check the documentation at http://goreleaser.com builds: - env: - - CGO_ENABLED=0 + - CGO_ENABLED=1 ldflags: -s -X github.com/sunny0826/kubecm/cmd.kubecmVersion={{.Version}} -X github.com/sunny0826/kubecm/cmd.gitCommit={{.Commit}} -X github.com/sunny0826/kubecm/cmd.buildDate={{.Date}} goos: - linux @@ -25,6 +25,5 @@ release: github: owner: sunny0826 name: kubecm - name_template: '{{.ProjectName}}-v{{.Version}}' diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..7e676bfb --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ +.PHONY: build clean + +# get tag of kubecm +KUBECM_VERSION=$(shell git describe --tags `git rev-list --tags --max-count=1`) + +GITVERSION:=$(shell git --version | grep ^git | sed 's/^.* //g') +GITCOMMIT:=$(shell git rev-parse HEAD) + +UNAME := $(shell uname) +BUILD_TARGET=target +BUILD_TARGET_DIR_NAME=kubecm-$(KUBECM_VERSION) +BUILD_TARGET_PKG_DIR=$(BUILD_TARGET)/kubecm-$(KUBECM_VERSION) +BUILD_TARGET_PKG_NAME=$(BUILD_TARGET)/kubecm-$(KUBECM_VERSION).tar.gz +BUILD_TARGET_PKG_FILE_PATH=$(BUILD_TARGET)/$(BUILD_TARGET_DIR_NAME) + +GO_ENV=CGO_ENABLED=1 +GO_MODULE=GO111MODULE=on +VERSION_PKG=github.com/sunny0826/kubecm/cmd +GO_FLAGS=-ldflags="-X ${VERSION_PKG}.kubecmVersion=$(KUBECM_VERSION) -X ${VERSION_PKG}.gitCommit=$(GITCOMMIT) -X '${VERSION_PKG}.buildDate=`date`'" +GO=env $(GO_ENV) $(GO_MODULE) go + +ifeq ($(GOOS), linux) + GO_FLAGS=-ldflags="-linkmode external -extldflags -static -X ${VERSION_PKG}.kubecmVersion=$(KUBECM_VERSION) -X ${VERSION_PKG}.gitCommit=$(GITCOMMIT) -X '${VERSION_PKG}.buildDate=`date`'" +endif + +build: pre_build + # build kubecm + $(GO) build $(GO_FLAGS) -o $(BUILD_TARGET_PKG_DIR)/kubecm . + # PTAH:$(BUILD_TARGET_PKG_DIR)/kubecm + +pre_build:mkdir_build_target + # clean target + rm -rf $(BUILD_TARGET_PKG_DIR) $(BUILD_TARGET_PKG_FILE_PATH) + +# create cache dir +mkdir_build_target: +ifneq ($(BUILD_TARGET_CACHE), $(wildcard $(BUILD_TARGET_CACHE))) + mkdir -p $(BUILD_TARGET_CACHE) +endif + +clean: + $(GO) clean ./... + rm -rf $(BUILD_TARGET) \ No newline at end of file