-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMakefile
115 lines (92 loc) · 3.82 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
SHELL := /bin/bash
# TODO
# - more fpm arguments
# - package-static with libname.a
OSNAME ?= xenial
ifndef PROJECT_DIR
PROJECT_DIR := $(shell git rev-parse --show-toplevel)
endif
# TODO make it as Simply expanded variables (":=")
PROJECT_NAME ?= $(notdir $(PROJECT_DIR))
PRE_PACKAGE_GOALS ?=
# based on https://stackoverflow.com/questions/10858261/abort-makefile-if-variable-not-set
check_non_empty = $(if $(1),$(1),$(error $(2)))
check_defined = $(if $(value $1),, $(error Undefined $1$(if $2, ($2))$(if $(value @), \
required by target '$@')))
docker_env_value = $(call check_non_empty,$(shell grep -oP '^ENV $(1)=\K.*' $(2)), \
"Value for ENV $(1) not found in dockerfile $(2)")
docker_from_tag = $(call check_non_empty,$(shell grep -oP '^FROM $(1):\K.*' $(2)), \
"Tag for FROM $(1) not found in dockerfile $(2)")
docker_from_version = $(firstword $(subst -, ,$(call docker_from_tag,$(1),$(2))))
BASE_DOCKERFILE_PATH = $(_THIS_MK_ABS_DIR)/docker/base/$(OSNAME)/Dockerfile.$(BASE_DOCKER_VERSION)
RUST_DOCKERFILE_PATH = $(_THIS_MK_ABS_DIR)/docker/rust/$(OSNAME)/Dockerfile.$(RUST_DOCKER_VERSION)
# required for in-docker targets
DOCKER_NAME ?=
DOCKER_TAG ?=
# TODO consider to use GID as well if necessary
ifndef DOCKER_UID
DOCKER_UID := $(shell id -u)
endif
# SELF POSITIONING
ROOT_MK_REL_DIR := $(subst $(PROJECT_DIR),.,$(CURDIR))
# is unlikely to be an absolute one
ROOT_MK_REL_PATH := $(word 1,$(MAKEFILE_LIST))
_THIS_MK_ABS_PATH := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
# absolute include/read
ifneq ($(wildcard $(abspath $(_THIS_MK_ABS_PATH))),)
_THIS_MK_ABS_PATH := $(abspath $(_THIS_MK_ABS_PATH))
# relative include/read
else ifneq ($(wildcard $(CURDIR)/$(_THIS_MK_ABS_PATH)),)
_THIS_MK_ABS_PATH := $(CURDIR)/$(_THIS_MK_ABS_PATH))
else
$(error "Failed to determine abspath to this makefile used as '$(_THIS_MK_ABS_PATH)'")
endif
_THIS_MK_ABS_DIR := $(dir $(_THIS_MK_ABS_PATH))
include $(_THIS_MK_ABS_DIR)/fpm.mk
CP = cp
DOCKER = docker
DOCKER_COMPOSE = docker-compose
FPM = fpm
.PHONY: _check_fpm_args package check_dockerfile clean_docker clean_build _default_docker_image_target
# list of conditionally (using '?=') defined variables that
# should be passed to in-docker re-run
EXPORT_ENV += OSNAME PROJECT_NAME
export $(EXPORT_ENV)
# PACKAGING ROUTINE
_check_fpm_args:
ifeq ($(FPM_P_NAME),)
$(error Package name is not specified)
else ifeq ($(FPM_P_VERSION),)
$(error Package version is not specified)
endif
# TODO deps with versions like "package (>= 1.0)"
package: _check_fpm_args $(PRE_PACKAGE_GOALS)
$(FPM) $(FPM_ARGS)
image_rust_base_version:
$(call check_defined,RUST_DOCKER_VERSION)
$(eval BASE_DOCKER_VERSION=$(call docker_from_version,'evernym/base',$(RUST_DOCKERFILE_PATH)))
echo $(BASE_DOCKER_VERSION)
# TODO refactor that once compose provides support for images
# build oreder: https://github.com/docker/compose/issues/5228
image_base:
$(call check_defined,BASE_DOCKER_VERSION)
BASE_DOCKER_VERSION=$(BASE_DOCKER_VERSION) \
docker-compose -f $(_THIS_MK_ABS_DIR)/docker/docker-compose.yml build base
image_rust: image_rust_base_version image_base
RUST_DOCKER_VERSION=$(RUST_DOCKER_VERSION) \
docker-compose -f $(_THIS_MK_ABS_DIR)/docker/docker-compose.yml build rust
%_in_docker: image_%
echo "Re-running goal $* in docker $(DOCKER_NAME):$(DOCKER_TAG)"
$(DOCKER) run --rm -t -u $(DOCKER_UID) \
-v $(PROJECT_DIR):/tmp/$(PROJECT_NAME) \
$(DOCKER_NAME):$(DOCKER_TAG) \
/bin/bash -cl \
'MAKEFLAGS="$(MAKEFLAGS)" $(foreach var,$(EXPORT_ENV),$(var)="$($(var))") \
$(MAKE) -C /tmp/$(PROJECT_NAME)/$(ROOT_MK_REL_DIR) -f $(ROOT_MK_REL_PATH) $*'
# CLEAN ROUTINE
# TODO update, consider to use docker-compose down --rmi
#clean_docker:
# $(DOCKER) images -q $(DOCKER_NS)/$(PROJECT_NAME)-* | xargs -r $(DOCKER) rmi -f
# -rm -f $(patsubst %,$(BUILD_DIR)/%, $(IMAGES))
#clean_build:
# rm -rf $(BUILD_DIR)