Skip to content

Commit

Permalink
build: use caimake to build this project (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jun Zhang committed Apr 24, 2018
1 parent 0551326 commit 8e1c6b1
Show file tree
Hide file tree
Showing 13 changed files with 219 additions and 1,472 deletions.
107 changes: 107 additions & 0 deletions .caimake/update.sh
@@ -0,0 +1,107 @@
#!/bin/bash

# Exit on error. Append "|| true" if you expect an error.
set -o errexit
# Do not allow use of undefined vars. Use ${VAR:-} to use an undefined VAR
set -o nounset
# Catch the error in pipeline.
set -o pipefail

# Controls verbosity of the script output and logging.
VERBOSE="${VERBOSE:-5}"

# If set true, the log will be colorized
readonly COLOR_LOG=${COLOR_LOG:-true}

readonly CAIMAKE_HOME=${CAIMAKE_HOME:-${HOME}/.caimake}

readonly CAIMAKE_AUTO_UPDATE=${CAIMAKE_AUTO_UPDATE:-false}

readonly project="caicloud/build-infra"

if [[ ${COLOR_LOG} == "true" ]]; then
readonly blue="\033[34m"
readonly green="\033[32m"
readonly red="\033[31m"
readonly yellow="\033[36m"
readonly strong="\033[1m"
readonly reset="\033[0m"
else
readonly blue=""
readonly green=""
readonly red=""
readonly yellow=""
readonly strong=""
readonly reset=""
fi

# Print a status line. Formatted to show up in a stream of output.
log::status() {
local V="${V:-0}"
if [[ $VERBOSE < $V ]]; then
return
fi

timestamp=$(date +"[%m%d %H:%M:%S]")
echo -e "${blue}==> $timestamp${reset} ${strong}$1${reset}"
shift
for message; do
echo " $message"
done
}

# Log an error but keep going. Don't dump the stack or exit.
log::error() {
timestamp=$(date +"[%m%d %H:%M:%S]")
echo -e "${red}!!! $timestamp${reset} ${strong}${1-}${reset}"
shift
for message; do
echo " $message"
done
}

command_exists() {
command -v "$@" >/dev/null 2>&1
}

caimake::update() {
mkdir -p ${CAIMAKE_HOME}

if ! command_exists caimake; then
# add CAIMAKE_HOME to PATH
export PATH=${CAIMAKE_HOME}:${PATH}
if ! command_exists caimake; then
# check again
caimake::download_binary
fi
fi

if [[ ${CAIMAKE_AUTO_UPDATE-} == "true" ]]; then
caimake update
fi
}

caimake::get_version_from_url() {
local url=${1-}
local version=${url##*/releases/download/}
version=${version%/*}
echo ${version}
}

caimake::download_binary() {
local hostos="$(uname -s | tr '[A-Z]' '[a-z]')"
local latest="$(curl -s https://api.github.com/repos/${project}/releases/latest | grep browser_download_url | cut -d '"' -f 4 | grep -m 1 ${hostos})"
local version="$(caimake::get_version_from_url ${latest-})"

if [[ -z ${version} ]]; then
# no release found on github
log::error "No caimake release found on Github"
exit 1
fi

log::status "Latest caimake release on Github is ${version}"

curl -L ${latest} -o ${CAIMAKE_HOME}/caimake.temp
mv ${CAIMAKE_HOME}/caimake.temp ${CAIMAKE_HOME}/caimake
chmod +x ${CAIMAKE_HOME}/caimake
}
140 changes: 112 additions & 28 deletions Makefile
@@ -1,17 +1,26 @@
# Copyright 2017 The Caicloud Authors.
#
# =====================================================================
# This file was autogenerated. Do not edit it manually!
# save all your custom variables and targets in Makefile.expansion!
# =====================================================================
#
# Old-skool build tools.
#
# Commonly used targets (see each target for more information):
# all: Build code.
# test: Run tests.
# clean: Clean up.
#
# see docs/Makefile.md for more information.
# see https://github.com/caicloud/build-infra/blob/master/docs/Specification.md for more information.


# It's necessary to set this because some environments don't link sh -> bash.
SHELL := /bin/bash

# Default target
.DEFAULT_GOAL := all

# We don't need make's built-in rules.
MAKEFLAGS += --no-builtin-rules
.SUFFIXES:
Expand All @@ -36,20 +45,55 @@ GO_BUILD_TARGETS := cmd/controller cmd/nginx-proxy
# Targets using CGO_ENABLED=0. It is a single word without dir prefix.
GO_STATIC_LIBRARIES :=
# Skip go unittest under the following dir.
GO_TEST_EXCEPTIONS :=
GO_TEST_EXCEPTIONS :=

# Pre-defined all directories containing Dockerfiles for building containers.
DOCKER_BUILD_TARGETS := build/controller build/nginx-proxy
DOCKER_BUILD_TARGETS := build/controller build/nginx-base build/nginx-proxy
# Container registries.
DOCKER_REGISTRIES := cargo.caicloudprivatetest.com/caicloud
DOCKER_REGISTRIES := cargo.caicloudprivatetest.com/caicloud
# Force pushing to override images in remote registries
DOCKER_FORCE_PUSH ?= true
# Container image prefix and suffix added to targets.
# The final built images are:
# $[REGISTRY]/$[IMAGE_PREFIX]$[TARGET]$[IMAGE_SUFFIX]:$[VERSION]
# $[REGISTRY] is an item from $[DOCKER_REGISTRIES], $[TARGET] is the basename from $[DOCKER_BUILD_TARGETS[@]].
DOCKER_IMAGE_PREFIX := $(strip canary-)
DOCKER_IMAGE_SUFFIX := $(strip )
DOCKER_IMAGE_PREFIX := canary-
DOCKER_IMAGE_SUFFIX :=

# =========================================================
# variables for caimake.
# =========================================================

# add caimake home to path
CAIMAKE_HOME ?= $(HOME)/.caimake
PATH := $(CAIMAKE_HOME):$(PATH)
# auto update caimake binary
CAIMAKE_AUTO_UPDATE ?= true
# check if caimake.sh exists
CAIMAKE_OFFLINE := $(shell if [ -e ".caimake/caimake.sh" ]; then echo "true"; else echo "false"; fi;)
ifeq ($(CAIMAKE_OFFLINE),true)
# use local script
CAIMAKE := bash .caimake/caimake.sh
else
# use binary
CAIMAKE := caimake
endif

# user defined Makefile to expands targets
-include Makefile.expansion

# check if pre-build defined in Makefile.expansion
PRE_BUILD := $(shell $(MAKE) -n pre-build 2> /dev/null)
ifdef PRE_BUILD
PRE_BUILD := pre-build
endif

# check caimake binary
.PHONY: check-caimake
check-caimake:
ifeq ($(CAIMAKE_OFFLINE),false)
@source .caimake/update.sh && caimake::update
endif

define ALL_HELP_INFO
# Build code.
Expand All @@ -73,12 +117,37 @@ define ALL_HELP_INFO
# debugging tools like delve.
endef
.PHONY: all build
ifeq ($(PRINT_HELP),y)
ifeq ($(HELP),y)
all build:
@echo "$$ALL_HELP_INFO"
else
all build:
hack/make-rules/build.sh $(WHAT)
all build: check-caimake $(PRE_BUILD)
$(CAIMAKE) go build $(WHAT)
endif

define GO_BUILD_HELP_INFO
# Build code.
#
# Args:
# GOFLAGS: Extra flags to pass to 'go' when building.
# GOLDFLAGS: Extra linking flags passed to 'go' when building.
# GOGCFLAGS: Additional go compile flags passed to 'go' when building.
#
# Example:
# make $(1)
# make $(1) GOFLAGS=-v
# make $(1) GOGCFLAGS="-N -l"
# Note: Use the -N -l options to disable compiler optimizations an inlining.
# Using these build options allows you to subsequently use source
# debugging tools like delve.
endef
.PHONY: $(GO_BUILD_TARGETS)
ifeq ($(HELP),y)
$(GO_BUILD_TARGETS):
$(call GO_BUILD_HELP_INFO, $@)
else
$(GO_BUILD_TARGETS): check-caimake $(PRE_BUILD)
$(CAIMAKE) go build $@
endif

define UNITTEST_HELP_INFO
Expand All @@ -92,19 +161,19 @@ define UNITTEST_HELP_INFO
# Example:
# make
# make unittest
# make unittest WHAT=cmd/server GOFLAGS=-v
# make unittest GOFLAGS="-v -x"
# make unittest GOGCFLAGS="-N -l"
# Note: Use the -N -l options to disable compiler optimizations an inlining.
# Using these build options allows you to subsequently use source
# debugging tools like delve.
endef
.PHONY: unittest
ifeq ($(PRINT_HELP),y)
ifeq ($(HELP),y)
unittest:
@echo "$$UNITTEST_HELP_INFO"
else
unittest:
hack/make-rules/unittest.sh
unittest: check-caimake
$(CAIMAKE) go unittest
endif

define BUILD_LOCAL_HELP_INFO
Expand All @@ -128,12 +197,12 @@ define BUILD_LOCAL_HELP_INFO
# debugging tools like delve.
endef
.PHONY: build-local
ifeq ($(PRINT_HELP),y)
ifeq ($(HELP),y)
build-local:
@echo "$$BUILD_LOCAL_HELP_INFO"
else
build-local:
LOCAL_BUILD=true hack/make-rules/build.sh $(WHAT)
build-local: check-caimake $(PRE_BUILD)
LOCAL_BUILD=true $(CAIMAKE) go build $(WHAT)
endif

define BUILD_IN_CONTAINER_HELP_INFO
Expand All @@ -157,12 +226,12 @@ define BUILD_IN_CONTAINER_HELP_INFO
# debugging tools like delve.
endef
.PHONY: build-in-container
ifeq ($(PRINT_HELP),y)
ifeq ($(HELP),y)
build-in-container:
@echo "$$BUILD_LINUX_HELP_INFO"
else
build-in-container:
LOCAL_BUILD=false hack/make-rules/build.sh $(WHAT)
build-in-container: check-caimake $(PRE_BUILD)
LOCAL_BUILD=false $(CAIMAKE) go build $(WHAT)
endif

define CONTAINER_HELP_INFO
Expand All @@ -176,12 +245,27 @@ define CONTAINER_HELP_INFO
# make container WAHT=build/server
endef
.PHONY: container
ifeq ($(PRINT_HELP),y)
ifeq ($(HELP),y)
container:
@echo "$$CONTAINER_HELP_INFO"
else
container:
PRJ_DOCKER_BUILD=1 hack/make-rules/docker.sh $(WHAT)
container: check-caimake
$(CAIMAKE) docker build $(WHAT)
endif

define DOCKER_BUILD_HELP_INFO
# Build docker image.
#
# Example:
# make $(1)
endef
.PHONY: $(DOCKER_BUILD_TARGETS)
ifeq ($(HELP),y)
$(DOCKER_BUILD_TARGETS):
$(call DOCKER_BUILD_HELP_INFO, $@)
else
$(DOCKER_BUILD_TARGETS): check-caimake
$(CAIMAKE) docker build $@
endif

define PUSH_HELP_INFO
Expand All @@ -196,12 +280,12 @@ define PUSH_HELP_INFO
# make push WAHT=build/server
endef
.PHONY: push
ifeq ($(PRINT_HELP),y)
ifeq ($(HELP),y)
push:
@echo "$$PUSH_HELP_INFO"
else
push:
PRJ_DOCKER_PUSH=1 hack/make-rules/docker.sh $(WHAT)
push: check-caimake
$(CAIMAKE) docker push $(WHAT)
endif


Expand All @@ -213,10 +297,10 @@ define CLEAN_HELP_INFO
#
endef
.PHONY: clean
ifeq ($(PRINT_HELP),y)
ifeq ($(HELP),y)
clean:
@echo "$$CLEAN_HELP_INFO"
else
clean:
hack/make-rules/clean.sh
clean: check-caimake
$(CAIMAKE) clean
endif

0 comments on commit 8e1c6b1

Please sign in to comment.