PKGBUILD/Makefile

290 lines
8.8 KiB
Makefile

# VERSION/RELEASE
# If no version is specified as a parameter of make, the last git hash
# value is taken.
VERSION?=$(shell git describe --abbrev=0)+$(shell date +'%Y%m%d%H%I%S')
RELEASE?=1
# GOPROXY settings
# If no GOPROXY environment variable available, the pre-defined GOPROXY from go
# env to download and validate go modules is used. Exclude downloading and
# validation of all private modules which are pre-defined in GOPRIVATE. If no
# GOPRIVATE variable defined, the variable of go env is used.
GOPROXY?=$(shell go env GOPROXY)
GOPRIVATE?=$(shell go env GOPRIVATE)
# EXECUTABLE
# Executable binary which should be compiled for different architecures
EXECUTABLE:=flucky
# DARWIN_EXECUTABLES AND TARGETS
DARWIN_EXECUTABLES:=\
darwin/386/${EXECUTABLE} \
darwin/amd64/${EXECUTABLE}
DARWIN_EXECUTABLE_TARGETS:=\
${DARWIN_EXECUTABLES:%=bin/%}
# FREEBSD_EXECUTABLES AND TARGETS
FREEBSD_EXECUTABLES:=\
freebsd/amd64/${EXECUTABLE}
FREEBSD_EXECUTABLE_TARGETS:=\
${FREEBSD_EXECUTABLES:%=bin/%}
# LINUX_EXECUTABLES AND TARGETS
LINUX_EXECUTABLES:=\
linux/amd64/${EXECUTABLE} \
linux/386/${EXECUTABLE} \
linux/arm/5/${EXECUTABLE} \
linux/arm/7/${EXECUTABLE}
LINUX_EXECUTABLE_TARGETS:=\
${LINUX_EXECUTABLES:%=bin/%}
# UNIX_EXECUTABLES AND TARGETS
# Define all executables for different architectures and operation systems
UNIX_EXECUTABLES:= \
${DARWIN_EXECUTABLES} \
${FREEBSD_EXECUTABLES} \
${LINUX_EXECUTABLES}
UNIX_EXECUTABLE_TARGETS:= \
${DARWIN_EXECUTABLE_TARGETS} \
${FREEBSD_EXECUTABLE_TARGETS} \
${LINUX_EXECUTABLE_TARGETS}
# EXECUTABLES AND TARGETS
# Include all UNIX and Windows targets.
EXECUTABLES:=\
${UNIX_EXECUTABLES}
EXECUTABLE_TARGETS:= \
${UNIX_EXECUTABLE_TARGETS}
# CONTAINER_RUNTIME / BUILD_IMAGE
# The CONTAINER_RUNTIME variable will be used to specified the path to a
# container runtime. This is needed to start and run a container image defined
# by the BUILD_IMAGE variable.
CONTAINER_RUNTIME?=$(shell which docker)
# BUILD_IMAGE
# The BUILD_IMAGE container serve as build environment to execute the different
# make steps inside. Therefore, the bulid environment requires all necessary
# dependancies to build this project.
BUILD_IMAGE:=docker.io/volkerraschek/build-image:latest
# BASE_IMAGE
# Defines the name of the container base image on which should be built the new
# CONTAINER_IMAGE.
BASE_IMAGE=docker.io/library/alpine:3.11.2
# REGISTRY / CONTAINER_IMAGE
# Specification of the container registry. This is needed to build and push the
# container image on a container registry. Additionally, the CONTAINER_IMAGE
# variable is composed based on the REGISTRY_NAMESPACE, the CONTAINER_IMAGE_NAME
# and the CONTAINER_IMAGE_VERSION
REGISTRY_MIRROR:=docker.io
REGISTRY_NAMESPACE:=volkerraschek
CONTAINER_IMAGE_NAME:=${EXECUTABLE}
CONTAINER_IMAGE_VERSION:=latest
CONTAINER_IMAGE=${REGISTRY_NAMESPACE}/${CONTAINER_IMAGE_NAME}/${CONTAINER_IMAGE_VERSION:v%=%}
# BINARIES
# ==============================================================================
# current os
${EXECUTABLE}: bin/tmp/${EXECUTABLE}
# build all binaries
PHONY:=all
all: ${EXECUTABLE_TARGETS}
# darwin os
bin/darwin/386/${EXECUTABLE}: bindata
CGO_ENABLED=0 \
GOOS=darwin \
GOARCH=386 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
bin/darwin/amd64/${EXECUTABLE}: bindata
CGO_ENABLED=0 \
GOOS=darwin \
GOARCH=amd64 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
# freebsd os
bin/freebsd/amd64/${EXECUTABLE}: bindata
CGO_ENABLED=0 \
GOOS=freebsd \
GOARCH=amd64 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
# linux os
bin/linux/386/${EXECUTABLE}: bindata
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=386 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
bin/linux/amd64/${EXECUTABLE}: bindata
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=amd64 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
bin/linux/arm/5/${EXECUTABLE}: bindata
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=arm \
GOARM=5 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
bin/linux/arm/7/${EXECUTABLE}: bindata
CGO_ENABLED=0 \
GOOS=linux \
GOARCH=arm \
GOARM=7 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
bin/tmp/${EXECUTABLE}: bindata
CGO_ENABLED=0 \
GOPROXY=${GOPROXY} \
GOPRIVATE=${GOPRIVATE} \
go build -ldflags "-X main.version=${VERSION:v%=%}" -o ${@}
# GO-BINDATA
# ==============================================================================
BINDATA_TARGETS := \
pkg/storage/db/bindataSQL.go \
test/goldenfiles/bindata.go
PHONY+=bindata
bindata: ${BINDATA_TARGETS}
pkg/storage/db/bindataSQL.go:
go-bindata -pkg db -o ./pkg/storage/db/bindataSQL.go ./pkg/storage/db/sql/*** ./pkg/storage/db/sql/psql/schema/***
test/goldenfiles/bindata.go:
go-bindata -pkg goldenfiles -ignore ".*\.go" -o ./test/goldenfiles/bindata.go ./test/goldenfiles/***
# TEST
# ==============================================================================
PHONY+=test/unit
test/unit: clean bindata
go test -v ./...
# PACKAGES
# ==============================================================================
%.rpm: %
rpm-builder \
--exec-file "$<:/usr/bin/${EXECUTABLE}" \
--dir "systemd:/usr/lib/systemd/system" \
--license "Apache 2.0" \
--version ${VERSION:v%=%} \
--release ${RELEASE} \
--out ${@} \
${EXECUTABLE}
# OTHER STUFF
# ==============================================================================
PHONY+=clean
clean:
rm --force ${BINDATA_TARGETS} || true
rm --force --recursive bin/ || true
# CONTAINER IMAGE STEPS
# ==============================================================================
PHONY+=container-image/build/amd64
container-image/build/amd64:
${CONTAINER_RUNTIME} build \
--build-arg BASE_IMAGE=${BASE_IMAGE} \
--build-arg BUILD_IMAGE=${BUILD_IMAGE} \
--build-arg EXECUTABLE=${EXECUTABLE} \
--build-arg EXECUTABLE_TARGET=bin/linux/amd64/${EXECUTABLE} \
--build-arg GOPROXY=${GOPROXY} \
--build-arg GOPRIVATE=${GOPRIVATE} \
--build-arg VERSION=${VERSION} \
--file Dockerfile \
--no-cache \
--tag ${REGISTRY_MIRROR}/${CONTAINER_IMAGE} \
--tag ${CONTAINER_IMAGE} \
.
PHONY+=container-image/push/amd64
container-image/push/amd64: container-image/build/amd64
${CONTAINER_RUNTIME} push ${REGISTRY_MIRROR}/${CONTAINER_IMAGE}
# CONTAINER STEPS - BINARY
# ==============================================================================
# current os
PHONY+=container-run/${EXECUTABLE}
container-run/${EXECUTABLE}:
$(MAKE) container-run COMMAND=${@:container-run/%=%}
# build all binaries for any operating system
PHONY+=container-run/all
container-run/all:
$(MAKE) container-run COMMAND=${@:container-run/%=%}
PHONY+=${UNIX_EXECUTABLE_TARGETS:%=container-run/%}
${UNIX_EXECUTABLE_TARGETS:%=container-run/%}:
$(MAKE) container-run COMMAND=${@:container-run/%=%}
# CONTAINER STEPS - GO-BINDATA
# ==============================================================================
PHONY+=container-run/bindata
container-run/bindata:
$(MAKE) container-run COMMAND=${@:container-run/%=%}
# CONTAINER STEPS - TEST
# ==============================================================================
PHONY+=container-run/test
container-run/test:
$(MAKE) container-run COMMAND=${@:container-run/%=%}
# CONTAINER STEPS - OTHER STUF
# ==============================================================================
PHONY+=container-run/clean
container-run/clean:
$(MAKE) container-run COMMAND=${@:container-run/%=%}
# GENERAL CONTAINER COMMAND
# ==============================================================================
PHONY+=container-run
container-run:
${CONTAINER_RUNTIME} run \
--rm \
--volume ${PWD}:/workspace \
${BUILD_IMAGE} \
make ${COMMAND} \
VERSION=${VERSION:v%=%} \
RELEASE=${RELEASE}
# REMOTE
# ==============================================================================
PHONY+=${FLUCKY_REMOTE:%=remote/%}
remote/${FLUCKY_REMOTE}: bin/linux/arm/7/${EXECUTABLE}
scp bin/linux/arm/7/${EXECUTABLE} root@${FLUCKY_REMOTE}:/usr/local/bin/${EXECUTABLE}
ssh root@${FLUCKY_REMOTE} 'mkdir --parent /etc/bash_completion.d || true'
ssh root@${FLUCKY_REMOTE} 'flucky completion bash > /etc/bash_completion.d/flucky.sh && chmod +x /etc/bash_completion.d/flucky.sh'
ssh root@${FLUCKY_REMOTE} 'flucky completion zsh > /etc/bash_completion.d/flucky.zsh && chmod +x /etc/bash_completion.d/flucky.zsh'
ssh root@${FLUCKY_REMOTE} 'chmod +x /usr/local/bin/${EXECUTABLE}'
# PHONY
# ==============================================================================
# Declare the contents of the PHONY variable as phony. We keep that information
# in a variable so we can use it in if_changed.
.PHONY: ${PHONY}