diff --git a/Makefile b/Makefile index 163e108e3..6c023ffa5 100644 --- a/Makefile +++ b/Makefile @@ -24,19 +24,26 @@ endif GO_DIRS ?= $(shell glide nv -x ) GO_PKGS ?= $(shell glide nv) +ROOTFS := rootfs + .PHONY: build build: gocheck @scripts/build-go.sh +.PHONY: build-static +build-static: gocheck + @BUILD_TYPE=STATIC scripts/build-go.sh + .PHONY: build-cross build-cross: gocheck - @BUILD_CROSS=1 scripts/build-go.sh + @BUILD_TYPE=CROSS scripts/build-go.sh .PHONY: all all: build .PHONY: clean clean: + $(MAKE) -C $(ROOTFS) $@ go clean -v $(GO_PKGS) rm -rf bin @@ -47,14 +54,12 @@ test: build test-style test-unit test-flake8 quicktest: test-style go test $(GO_PKGS) -ROOTFS := rootfs - .PHONY: push -push: all +push: build-static $(MAKE) -C $(ROOTFS) $@ .PHONY: container -container: all +container: build-static $(MAKE) -C $(ROOTFS) $@ .PHONY: test-unit diff --git a/rootfs/Makefile b/rootfs/Makefile index 150bac8dd..b584a53d9 100644 --- a/rootfs/Makefile +++ b/rootfs/Makefile @@ -13,7 +13,7 @@ # limitations under the License. SUBDIRS := expandybird/. resourcifier/. manager/. -TARGETS := info push container +TARGETS := info push container clean SUBDIRS_TARGETS := \ $(foreach t,$(TARGETS),$(addsuffix $t,$(SUBDIRS))) diff --git a/rootfs/expandybird/Makefile b/rootfs/expandybird/Makefile index c87a43ac0..80c03a30c 100644 --- a/rootfs/expandybird/Makefile +++ b/rootfs/expandybird/Makefile @@ -21,4 +21,4 @@ extras: expansion .PHONY: expansion expansion: - cp -R ../../expansion ./opt + mkdir -p ./opt && cp -R ../../expansion ./opt diff --git a/rootfs/include.mk b/rootfs/include.mk index 5a5a535d8..deea4fe50 100644 --- a/rootfs/include.mk +++ b/rootfs/include.mk @@ -30,13 +30,17 @@ FULL_IMAGE := $(PREFIX)/$(IMAGE) TAG ?= git-$(shell git rev-parse --short HEAD) -DEFAULT_PLATFORM := $(shell uname | tr '[:upper:]' '[:lower:]') +DEFAULT_PLATFORM := linux PLATFORM ?= $(DEFAULT_PLATFORM) -DEFAULT_ARCH := $(shell uname -m) +DEFAULT_ARCH := amd64 ARCH ?= $(DEFAULT_ARCH) +.PHONY: clean +clean: + rm -rf bin opt + .PHONY: info info: @echo "Build tag: ${TAG}" @@ -73,17 +77,9 @@ endif .docker: @if [[ -z `which docker` ]] || ! docker --version &> /dev/null; then echo "docker is not installed correctly"; exit 1; fi -CROSS_IMAGE := $(PLATFORM)-$(ARCH)/$(IMAGE)/$(IMAGE) - .PHONY: binary binary: - @if [[ -z $(CROSS_IMAGE) ]]; then \ - echo cp ../../bin/$(CROSS_IMAGE) ./bin ; \ - cp ../../bin/$(CROSS_IMAGE) ./bin ; \ - else \ - echo cp ../../bin/$(IMAGE) ./bin ; \ - cp ../../bin/$(IMAGE) ./bin ; \ - fi + @if [[ ! -x "bin/$(IMAGE)" ]] ; then echo "binary bin/$(IMAGE) not found" ; exit 1 ; fi .PHONY: kubectl kubectl: diff --git a/scripts/common.sh b/scripts/common.sh index 3ca5afaaa..e6f92c960 100644 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -16,7 +16,10 @@ set -o errexit set -o pipefail -readonly ALL_TARGETS=(cmd/expandybird cmd/helm cmd/manager cmd/resourcifier) +readonly ROOTFS="${DIR}/rootfs" + +readonly STATIC_TARGETS=(cmd/expandybird cmd/manager cmd/resourcifier) +readonly ALL_TARGETS=(${STATIC_TARGETS[@]} cmd/helm) error_exit() { # Display error message and exit @@ -42,6 +45,22 @@ version_from_git() { echo "${git_tag}+${git_commit}" } +build_binary_static() { + local target="$1" + local basename="${target##*/}" + local context="${ROOTFS}/${basename}" + + echo "Building ${target}" + CGO_ENABLED=0 \ + GOOS=linux \ + GOARCH=amd64 \ + go build \ + -ldflags="${LDFLAGS}" \ + -a -installsuffix cgo \ + -o "${context}/bin/${basename}" \ + "${REPO}/${target}" +} + build_binary_cross() { local target="$1" @@ -50,25 +69,32 @@ build_binary_cross() { -ldflags="${LDFLAGS}" \ -os="linux darwin" \ -arch="amd64 386" \ - -output="bin/{{.OS}}-{{.Arch}}/{{.Dir}}" "${REPO}/${target}" + -output="bin/{{.OS}}-{{.Arch}}/{{.Dir}}" \ + "${REPO}/${target}" } +#TODO: accept specific os/arch build_binaries() { local -a targets=($@) - #TODO: accept specific os/arch - local build_cross="${BUILD_CROSS:-}" + local build_type="${BUILD_TYPE:-}" if [[ ${#targets[@]} -eq 0 ]]; then - targets=("${ALL_TARGETS[@]}") + if [[ ${build_type} == STATIC ]]; then + targets=("${STATIC_TARGETS[@]}") + else + targets=("${ALL_TARGETS[@]}") + fi fi assign_version assign_ldflags for t in "${targets[@]}"; do - if [[ -n "$build_cross" ]]; then + if [[ ${build_type} == STATIC ]]; then + build_binary_static "$t" + elif [[ ${build_type} == CROSS ]]; then build_binary_cross "$t" - else + else build_binary "$t" fi done