Make top-level Makefile pretty and DRY

This commit is contained in:
Andrew Dunham 2015-06-24 16:00:46 -07:00
parent 4539387380
commit 94f37ab5f3
2 changed files with 94 additions and 31 deletions

View File

@ -1,4 +1,5 @@
Q ?= @
SHELL := /bin/bash
ROOT := $(abspath .)
OUT_DIR := $(ROOT)/binaries2
@ -8,36 +9,17 @@ all:
$(OUT_DIR):
$(Q)mkdir -p $@
# TODO:
# 1. Use $(eval) to reduce duplication
# Helper rule to run a Docker image to perform a given build.
define DOCKER_RUN
.PHONY: $1-$2
$1-$2: | $$(OUT_DIR)
$$(Q)./run-docker.sh $1 $2 $3 $$(OUT_DIR) @
endef
.PHONY: linux-amd64
linux-amd64: | $(OUT_DIR)
$(Q)docker run \
--rm \
-v $(ROOT)/make:/make \
-v $(OUT_DIR):/output \
andrewd/musl-cross \
/bin/bash \
-c "cd /make && make PLATFORM=linux ARCH=amd64 Q=$(Q) install"
$(eval $(call DOCKER_RUN,linux,amd64,andrewd/musl-cross))
$(eval $(call DOCKER_RUN,android,arm,andrewd/musl-cross-arm))
$(eval $(call DOCKER_RUN,darwin,amd64,andrewd/osxcross))
.PHONY: android
android: | $(OUT_DIR)
$(Q)docker run \
--rm \
-v $(ROOT)/make:/make \
-v $(OUT_DIR):/output \
andrewd/musl-cross-arm \
/bin/bash \
-c "cd /make && make PLATFORM=android Q=$(Q) install"
.PHONY: darwin-amd64
darwin-amd64: | $(OUT_DIR)
$(Q)docker run \
--rm \
-v $(ROOT)/make:/make \
-v $(OUT_DIR):/output \
andrewd/osxcross \
/bin/bash \
-c "cd /make && make PLATFORM=darwin ARCH=amd64 Q=$(Q) install"
.PHONY: clean
clean:
$(Q)$(RM) -r $(OUT_DIR)

81
run-docker.sh Executable file
View File

@ -0,0 +1,81 @@
#!/bin/bash
set -e
set -o pipefail
#set -x
PLATFORM=$1
ARCH=$2
IMAGE=$3
OUT_DIR=$4
Q=$5
# Colors
RED="\x1b[31m"
GREEN="\x1b[32m"
YELLOW="\x1b[33m"
BLUE="\x1b[34m"
RESET="\x1b[0m"
ERASE_LINE="\x1b[2K"
# Find our real path of this directory
REALPATH=
for bin in realpath grealpath; do
##echo "Testing for ${bin}..."
if [ -x "`which $bin`" ]; then
REALPATH=`which $bin`
fi
done
if [ -z "$REALPATH" ]; then
printf "${RED}ERROR:${RESET} Could not find 'realpath' binary\n"
exit 1
fi
ROOT=`$REALPATH .`
if [ -z "$PLATFORM" ]; then
printf "${RED}ERROR:${RESET} No platform specified\n"
exit 1
fi
if [ -z "$ARCH" ]; then
printf "${RED}ERROR:${RESET} No architecture specified\n"
exit 1
fi
if [ -z "$IMAGE" ]; then
printf "${RED}ERROR:${RESET} No container specified\n"
exit 1
fi
if [ ! -d "$OUT_DIR" ]; then
printf "${RED}ERROR:${RESET} Output directory not given or not a directory\n"
printf " $$OUT_DIR = $OUT_DIR\n"
exit 1
fi
CID=$(docker run \
-d \
--name=static-build-${PLATFORM}-${ARCH} \
-v ${ROOT}/make:/make \
-v ${OUT_DIR}:/output \
$IMAGE \
/bin/bash \
-c "cd /make && make PLATFORM=${PLATFORM} ARCH=${ARCH} Q=$Q install"
)
DESC=$(printf "%s/%s" "$PLATFORM" "$ARCH")
HEADER=$(printf " ${GREEN}%-10s${RESET} | ${BLUE}%-15s${RESET} | %s | " "DOCKER" "$DESC" "${CID:0:12}")
printf "${HEADER}${YELLOW}WAIT${RESET}"
EXIT_CODE=$(docker wait $CID)
if [ "$EXIT_CODE" == "0" ]; then
docker rm $CID >/dev/null
printf "\r${ERASE_LINE}${HEADER}${GREEN}SUCCESS${RESET}\n"
else
printf "\r${ERASE_LINE}${HEADER}${RED}ERROR (code %d)${RESET}\n" "$EXIT_CODE"
fi