diff --git a/.gitignore b/.gitignore index f0aa2b6c35..4b09b6a732 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -dep \ No newline at end of file +dep +build diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..613bbaac2a --- /dev/null +++ b/Makefile @@ -0,0 +1,64 @@ +# Set an output prefix, which is the local directory if not specified +PREFIX?=$(shell pwd) + +# Setup name variables for the package/tool +NAME := dep +PKG := github.com/golang/$(NAME) + +# Set any default go build tags +BUILDTAGS := + +# Set the build dir, where built cross-compiled binaries will be output +BUILDDIR := ${PREFIX}/build + +# Populate version variables +# Add to compile time flags +VERSION := $(shell cat VERSION) +GITCOMMIT := $(shell git rev-parse --short HEAD) +GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no) +ifneq ($(GITUNTRACKEDCHANGES),) + GITCOMMIT := $(GITCOMMIT)-dirty +endif +CTIMEVAR=-X main.GITCOMMIT=$(GITCOMMIT) -X main.VERSION=$(VERSION) +GO_LDFLAGS=-ldflags "-w $(CTIMEVAR)" +GO_LDFLAGS_STATIC=-ldflags "-w $(CTIMEVAR) -extldflags -static" + +# List the GOOS and GOARCH to build +GOOSARCHES = darwin/amd64 darwin/386 freebsd/amd64 freebsd/386 linux/arm linux/arm64 linux/amd64 linux/386 solaris/amd64 windows/amd64 windows/386 + +.PHONY: build +build: $(NAME) ## Builds a dynamic executable + +$(NAME): *.go VERSION + @echo "+ $@" + go build -tags "$(BUILDTAGS)" ${GO_LDFLAGS} -o $(NAME) . + +.PHONY: static +static: ## Builds a static executable + @echo "+ $@" + CGO_ENABLED=0 go build \ + -tags "$(BUILDTAGS) static_build" \ + ${GO_LDFLAGS_STATIC} -o $(NAME) . + +define buildrelease +mkdir -p $(BUILDDIR)/$(1)/$(2); +GOOS=$(1) GOARCH=$(2) CGO_ENABLED=0 go build \ + -o $(BUILDDIR)/$(1)/$(2)/$(NAME) \ + -a -tags "$(BUILDTAGS) static_build netgo" \ + -installsuffix netgo ${GO_LDFLAGS_STATIC} .; +endef + +.PHONY: cross +cross: *.go VERSION ## Builds the cross-compiled binaries, creating a clean directory structure (eg. GOOS/GOARCH/binary) + @echo "+ $@" + $(foreach GOOSARCH,$(GOOSARCHES), $(call buildrelease,$(subst /,,$(dir $(GOOSARCH))),$(notdir $(GOOSARCH)))) + +.PHONY: clean +clean: ## Cleanup any build binaries + @echo "+ $@" + $(RM) $(NAME) + $(RM) -r $(BUILDDIR) + +.PHONY: help +help: + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' diff --git a/VERSION b/VERSION new file mode 100644 index 0000000000..77d6f4ca23 --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +0.0.0 diff --git a/main.go b/main.go index a3f007e7a2..5da1df2da8 100644 --- a/main.go +++ b/main.go @@ -95,6 +95,7 @@ var commands = []*command{ initCmd, statusCmd, ensureCmd, + versionCmd, // help added here at init time. } diff --git a/version.go b/version.go new file mode 100644 index 0000000000..c99a7d47c4 --- /dev/null +++ b/version.go @@ -0,0 +1,28 @@ +package main + +import ( + "fmt" + "runtime" +) + +var ( + // VERSION indicates which version of the binary is running. + VERSION string + + // GITCOMMIT indicates which git hash the binary was built off of + GITCOMMIT string +) + +var versionCmd = &command{ + fn: runVersion, + name: "version", + short: ` + Version prints the version, git commit, runtime OS and ARCH. + `, + long: `Version prints the version, git commit, runtime OS and ARCH.`, +} + +func runVersion(args []string) error { + fmt.Printf("dep version %s %s %s/%s\n", VERSION, GITCOMMIT, runtime.GOOS, runtime.GOARCH) + return nil +}