-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMakefile
129 lines (110 loc) · 4.8 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# Which operating system to target for a Go build?
# Defaults to linux because the extension runs in a linux compute environment.
# Override in development if you wish to build and run on your dev host.
# Example: GOOS=darwin make build
GOOS ?= linux
# CIRCLE_TAG is generally not set unless CircleCI is running a workflow
# triggered by a git tag creation.
# If set, the value will be used for the version of the build.
# If unset, determine a reasonable version identifier for current current commit
# based on the closest vX.Y.Z tag in the branch's history. For example: v10.4.0-6-ged57c1e
# --tags :: consider all tags, not only annotated tags
# --match :: a regex to select a tag that matches our version number tag scheme
CIRCLE_TAG ?= $(shell git describe --always --tags --match "v[0-9]*" HEAD)
.PHONY: test
#: run the tests!
test:
ifeq (, $(shell which gotestsum))
@echo " ***"
@echo "Running with standard go test because gotestsum was not found on PATH. Consider installing gotestsum for friendlier test output!"
@echo " ***"
go test -race ./...
else
gotestsum --junitfile unit-tests.xml --format testname -- -race ./...
endif
.PHONY: version
version:
@echo $(CIRCLE_TAG)
# target directory for artifact builds
ARTIFACT_DIR := artifacts
$(ARTIFACT_DIR):
mkdir -p $@
BUILD_DIR := $(ARTIFACT_DIR)/$(GOOS)
$(BUILD_DIR):
mkdir -p $@
# List of the Go source files; the build target will then know if these are newer than an executable present.
GO_SOURCES := go.mod go.sum $(wildcard *.go) $(wildcard */*.go)
ldflags := "-X main.version=$(CIRCLE_TAG)"
$(BUILD_DIR)/honeycomb-lambda-extension-arm64: $(GO_SOURCES) | $(BUILD_DIR)
@echo "\n*** Building honeycomb-lambda-extension for ${GOOS}/arm64"
GOOS=${GOOS} GOARCH=arm64 go build -ldflags ${ldflags} -o $@ .
$(BUILD_DIR)/honeycomb-lambda-extension-x86_64: $(GO_SOURCES) | $(BUILD_DIR)
@echo "\n*** Building honeycomb-lambda-extension for ${GOOS}/x86_64"
GOOS=${GOOS} GOARCH=amd64 go build -ldflags ${ldflags} -o $@ .
.PHONY: build
#: build the executables
build: $(BUILD_DIR)/honeycomb-lambda-extension-arm64 $(BUILD_DIR)/honeycomb-lambda-extension-x86_64
### ZIPs for layer publishing
#
# Linux is the only supported OS.
#
# The ZIP file for the content of a lambda layers a.k.a. extention MUST have:
# * an extensions/ directory
# * the executable that is the extension located within the extensions/ directory
#
# some of the Make automatic variables in use in these recipes:
# $(@D) - the directory portion of the target, e.g. artifacts/linux/thingie.zip $(@D) == artifacts/linux
# $(@F) - the file portion of the target, e.g. artifacts/linux/thingie.zip, $(@F) == thingie.zip
# $< - the first prerequisite, in this case the executable being put into the zip file
$(ARTIFACT_DIR)/linux/extension-arm64.zip: $(ARTIFACT_DIR)/linux/honeycomb-lambda-extension-arm64
@echo "\n*** Packaging honeycomb-lambda-extension for linux into layer contents zipfile"
rm -rf $(@D)/extensions
mkdir -p $(@D)/extensions
cp $< $(@D)/extensions
cd $(@D) && zip --move --recurse-paths $(@F) extensions
$(ARTIFACT_DIR)/linux/extension-x86_64.zip: $(ARTIFACT_DIR)/linux/honeycomb-lambda-extension-x86_64
@echo "\n*** Packaging honeycomb-lambda-extension for linux into layer contents zipfile"
rm -rf $(@D)/extensions
mkdir -p $(@D)/extensions
cp $< $(@D)/extensions
cd $(@D) && zip --move --recurse-paths $(@F) extensions
#: build the zipfiles destined to be published as layer contents (GOOS=linux only)
ifeq ($(GOOS),linux)
zips: $(ARTIFACT_DIR)/linux/extension-arm64.zip $(ARTIFACT_DIR)/linux/extension-x86_64.zip
else
zips:
@echo "\n*** GOOS is set to ${GOOS}. Zips destined for publishing as a layer can only be for linux."
exit 1
endif
.PHONY: publish-sandbox
# Ex: AWS_ARCH=arm64 AWS_REGION=us-east-1 make publish_sandbox
#
#: for serious, don't use this as-is for real publishing
publish_sandbox: are_you_sure arch_required region_required zips
@echo "\n*** Publishing honeycomb-lambda-extension-${AWS_ARCH} to ${AWS_REGION}"
aws lambda publish-layer-version \
--layer-name honeycomb-lambda-extension-${AWS_ARCH} \
--region ${AWS_REGION} \
--compatible-architectures ${AWS_ARCH} \
--zip-file "fileb://${ARTIFACT_DIR}/linux/extension-${AWS_ARCH}.zip" \
--no-cli-pager
#: clean up the workspace
clean:
rm -rf artifacts
### Utilities
.PHONY: region_required
region_required:
@:$(call check_defined, AWS_REGION, the region to publish to)
.PHONY: arch_required
arch_required:
@:$(call check_defined, AWS_ARCH, the architecture to publish)
.PHONY: are_you_sure
are_you_sure:
@( read -p "Are you sure?!? [y/N]: " sure && case "$$sure" in [yY]) true;; *) false;; esac )
check_defined = \
$(strip $(foreach 1,$1, \
$(call __check_defined,$1,$(strip $(value 2)))))
__check_defined = \
$(if $(value $1),, \
$(error Unset environment variable $1$(if $2, ($2))$(if $(value @), \
required by target `$@')))