summaryrefslogtreecommitdiffhomepage
path: root/tools/unitctl/build/package.mk
blob: 7009e2b158ebfc12718f23a8ed7ffb0181318cc9 (plain) (blame)
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
130
131
132
133
134
135
136
137
138
139
.PHONY: install-packaging-deb
install-packaging-deb:
	$Q if ! command -v cargo-deb > /dev/null; then \
		$(CARGO) install --quiet cargo-deb; \
	fi

.PHONY: install-packaging-rpm
install-packaging-rpm:
	$Q if ! command -v cargo-generate-rpm > /dev/null; then \
		$(CARGO) install --quiet cargo-generate-rpm; \
	fi

## Installs tools needed for building distributable packages
.PHONY: install-packaging-tools
install-packaging-tools:
	$Q $(CARGO) install --quiet cargo-deb cargo-generate-rpm

target/dist:
	$Q mkdir -p $@

## Builds all packages for all targets
.PHONY: all-packages
all-packages: deb-packages rpm-packages gz-packages

target/dist/SHA256SUMS: target/dist
	$Q cd target/dist && $(CHECKSUM) * > SHA256SUMS

.PHONY: checksums
checksums: target/dist/SHA256SUMS ## Generates checksums for all packages

################################################################################
### Debian Packages
################################################################################

to_debian_arch = $(shell echo $(1) | \
	$(SED) -e 's/x86_64/amd64/' -e 's/aarch64/arm64/' -e 's/armv7/armhf/')
DEBIAN_PACKAGE_TARGETS := \
	$(foreach t, $(TARGETS), target/$(t)/debian/$(PACKAGE_NAME)_$(VERSION)_$(call to_debian_arch, $(firstword $(subst -,  , $(t)))).deb)

.ONESHELL: $(DEBIAN_PACKAGE_TARGETS)
.NOTPARALLEL: $(DEBIAN_PACKAGE_TARGETS)
$(DEBIAN_PACKAGE_TARGETS): $(TARGETS) target/man/$(OUTPUT_BINARY).1.gz target/dist
	$Q TARGET="$(word 2, $(subst /,  , $(dir $@)))"
	# Skip building debs for musl targets
	if echo "$(@)" | $(GREP) -q 'musl\|apple'; then \
		exit 0
	fi
	if [ ! -f "$(CURDIR)/$(@)" ]; then
		if [ -d "$(CURDIR)/target/release" ]; then \
			echo "$(M) removing existing release directory: $(CURDIR)/target/release"
			rm -rf "$(CURDIR)/target/release"
		fi
		echo "$(M) copying target architecture [$${TARGET}] build to target/release directory"
		cp -r "$(CURDIR)/target/$${TARGET}/release" "$(CURDIR)/target/release"
		echo "$(M) building debian package for target [$${TARGET}]: $(@)"
		$(CARGO) deb --package unitctl --no-build --target "$${TARGET}" --output "$(CURDIR)/$(@)"
		ln -f "$(CURDIR)/$(@)" "$(CURDIR)/target/dist/"
	fi

## Creates a debian package for the current platform
.PHONY: deb-packages
deb-packages: install-packaging-deb $(TARGETS) manpage $(DEBIAN_PACKAGE_TARGETS)

################################################################################
### RPM Packages
################################################################################

RPM_PACKAGE_TARGETS := $(foreach t, $(TARGETS), target/$(t)/generate-rpm/$(PACKAGE_NAME)_$(VERSION)_$(firstword $(subst -,  , $(t))).rpm)

.ONESHELL: $(RPM_PACKAGE_TARGETS)
.NOTPARALLEL: $(RPM_PACKAGE_TARGETS)
$(RPM_PACKAGE_TARGETS): $(TARGETS) target/man/$(OUTPUT_BINARY).1.gz target/dist
	$Q TARGET="$(word 2, $(subst /,  , $(dir $@)))"
	ARCH="$(firstword $(subst -,  , $(word 2, $(subst /,  , $(dir $@)))))"
	# Skip building rpms for musl targets
	if echo "$(@)" | $(GREP) -q 'musl\|apple'; then \
		exit 0
	fi
	if [ ! -f "$(CURDIR)/$(@)" ]; then
		if [ -d "$(CURDIR)/target/release" ]; then \
			echo "$(M) removing existing release directory: $(CURDIR)/target/release"
			rm -rf "$(CURDIR)/target/release"
		fi
		echo "$(M) copying target architecture [$${ARCH}] build to target/release directory"
		cp -r "$(CURDIR)/target/$${TARGET}/release" "$(CURDIR)/target/release"
		echo "$(M) building rpm package: $(@)"
		$(CARGO) generate-rpm --package unitctl --arch "$${ARCH}" --target "$${TARGET}" --output "$(CURDIR)/$(@)"
		rm -rf "$(CURDIR)/target/release"
		ln -f "$(CURDIR)/$(@)" "$(CURDIR)/target/dist/"
	fi

## Creates a rpm package for the current platform
.PHONY: rpm-packages
rpm-packages: install-packaging-rpm $(TARGETS) manpage $(RPM_PACKAGE_TARGETS)

################################################################################
### Homebrew Packages
################################################################################

## Modifies the homebrew formula to point to the latest release
.PHONY: homebrew-packages
.ONESHELL: homebrew-packages
homebrew-packages: target/dist/SHA256SUMS
ifdef NEW_VERSION
	VERSION=$(NEW_VERSION)
endif
	$Q \
	VERSION="$(VERSION)" \
    PACKAGE_NAME="$(PACKAGE_NAME)" \
    SRC_REPO="$(SRC_REPO)" \
	AARCH64_UNKNOWN_LINUX_GNU_SHA256="$$($(GREP) $(PACKAGE_NAME)_v$(VERSION)_aarch64-unknown-linux-gnu.tar.gz $(CURDIR)/target/dist/SHA256SUMS | cut -d ' ' -f 1)" \
	X86_64_UNKNOWN_LINUX_GNU_SHA256="$$($(GREP) $(PACKAGE_NAME)_v$(VERSION)_x86_64-unknown-linux-gnu.tar.gz $(CURDIR)/target/dist/SHA256SUMS | cut -d ' ' -f 1)" \
	X86_64_APPLE_DARWIN_SHA256="$$($(GREP) $(PACKAGE_NAME)_v$(VERSION)_x86_64-apple-darwin.tar.gz $(CURDIR)/target/dist/SHA256SUMS | cut -d ' ' -f 1)" \
    AARCH64_APPLE_DARWIN_SHA256="$$($(GREP) $(PACKAGE_NAME)_v$(VERSION)_aarch64-apple-darwin.tar.gz $(CURDIR)/target/dist/SHA256SUMS | cut -d ' ' -f 1)" \
	envsubst < $(CURDIR)/pkg/brew/$(PACKAGE_NAME).rb.template > $(CURDIR)/pkg/brew/$(PACKAGE_NAME).rb


################################################################################
### Tarball Packages
################################################################################

GZ_PACKAGE_TARGETS = $(foreach t, $(TARGETS), target/gz/$(t)/$(PACKAGE_NAME)_$(VERSION)_$(firstword $(subst -,  , $(t))).tar.gz)

.ONESHELL: $(GZ_PACKAGE_TARGETS)
$(GZ_PACKAGE_TARGETS): $(TARGETS) target/man/$(PACKAGE_NAME).1.gz target/dist
	$Q mkdir -p "$(CURDIR)/target/gz"
	TARGET="$(word 3, $(subst /,  , $(dir $@)))"
	PACKAGE="$(CURDIR)/target/gz/$(PACKAGE_NAME)_v$(VERSION)_$${TARGET}.tar.gz"
	if [ ! -f "$${PACKAGE}}" ]; then
		tar -cz -f $${PACKAGE} \
			-C $(CURDIR)/target/man $(PACKAGE_NAME).1.gz \
			-C $(CURDIR)/target/$${TARGET}/release $(PACKAGE_NAME) \
			-C $(CURDIR) LICENSE.txt
		ln -f "$${PACKAGE}" "$(CURDIR)/target/dist/"
	fi

## Creates a gzipped tarball all target platforms
.PHONE: gz-packages
gz-packages: $(GZ_PACKAGE_TARGETS)