Skip to content

Commit 45f48d9

Browse files
committed
bring over makefile
1 parent 76fc5e6 commit 45f48d9

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

Makefile

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Set the shell
2+
SHELL := /bin/bash
3+
4+
# Set an output prefix, which is the local directory if not specified
5+
PREFIX?=$(shell pwd)
6+
7+
NAME := zoo
8+
9+
# Set the build dir, where built cross-compiled binaries will be output
10+
BUILDDIR := ${PREFIX}/cross
11+
12+
GENERATED_DOCS_DIR := ${PREFIX}/generated_docs
13+
14+
UNAME := $(shell uname)
15+
16+
# These are chosen from: https://doc.rust-lang.org/nightly/rustc/platform-support.html
17+
ifeq ($(UNAME), Darwin)
18+
CROSS_TARGETS := x86_64-apple-darwin \
19+
aarch64-apple-darwin
20+
else
21+
CROSS_TARGETS := x86_64-pc-windows-gnu \
22+
x86_64-unknown-linux-musl \
23+
aarch64-unknown-linux-musl
24+
# Turn this back on when it works.
25+
# x86_64-unknown-illumos
26+
# i686-pc-windows-gnu
27+
# x86_64-unknown-freebsd
28+
endif
29+
30+
# For this to work, you need to install toml-cli: https://github.com/gnprice/toml-cli
31+
# `cargo install toml-cli`
32+
VERSION := $(shell toml get $(CURDIR)/Cargo.toml package.version | jq -r .)
33+
34+
GITCOMMIT := $(shell git rev-parse --short HEAD)
35+
GITUNTRACKEDCHANGES := $(shell git status --porcelain --untracked-files=no)
36+
ifneq ($(GITUNTRACKEDCHANGES),)
37+
GITCOMMIT := $(GITCOMMIT)-dirty
38+
endif
39+
ifeq ($(GITCOMMIT),)
40+
GITCOMMIT := ${GITHUB_SHA}
41+
endif
42+
43+
define buildrelease
44+
rustup target add $(1)
45+
cargo build --release --target $(1) || cross build --release --target $(1)
46+
mv $(CURDIR)/target/$(1)/release/$(NAME) $(BUILDDIR)/$(NAME)-$(1) || mv $(CURDIR)/target/$(1)/release/$(NAME).exe $(BUILDDIR)/$(NAME)-$(1)
47+
md5sum $(BUILDDIR)/$(NAME)-$(1) > $(BUILDDIR)/$(NAME)-$(1).md5;
48+
sha256sum $(BUILDDIR)/$(NAME)-$(1) > $(BUILDDIR)/$(NAME)-$(1).sha256;
49+
echo -e "### $(1)\n\n" >> $(BUILDDIR)/README.md;
50+
echo -e "\`\`\`console" >> $(BUILDDIR)/README.md;
51+
echo -e "# Export the sha256sum for verification." >> $(BUILDDIR)/README.md;
52+
echo -e "$$ export ZOO_CLI_SHA256=\"`cat $(BUILDDIR)/$(NAME)-$(1).sha256 | awk '{print $$1}'`\"\n\n" >> $(BUILDDIR)/README.md;
53+
echo -e "# Download and check the sha256sum." >> $(BUILDDIR)/README.md;
54+
echo -e "$$ curl -fSL \"https://dl.zoo.dev/releases/machine-api/v$(VERSION)/$(NAME)-$(1)\" -o \"/usr/local/bin/$(NAME)\" \\" >> $(BUILDDIR)/README.md;
55+
echo -e "\t&& echo \"\$${ZOO_CLI_SHA256} /usr/local/bin/$(NAME)\" | sha256sum -c - \\" >> $(BUILDDIR)/README.md;
56+
echo -e "\t&& chmod a+x \"/usr/local/bin/$(NAME)\"\n\n" >> $(BUILDDIR)/README.md;
57+
echo -e "$$ echo \"$(NAME) machine-api installed!\"\n" >> $(BUILDDIR)/README.md;
58+
echo -e "# Run it!" >> $(BUILDDIR)/README.md;
59+
echo -e "$$ $(NAME) -h" >> $(BUILDDIR)/README.md;
60+
echo -e "\`\`\`\n\n" >> $(BUILDDIR)/README.md;
61+
endef
62+
63+
# If running on a Mac you will need:
64+
# brew install filosottile/musl-cross/musl-cross
65+
.PHONY: release
66+
release: src/*.rs Cargo.toml ## Builds the cross-compiled binaries, naming them in such a way for release (eg. binary-OS-ARCH).
67+
@echo "+ $@"
68+
mkdir -p $(BUILDDIR)
69+
$(foreach TARGET,$(CROSS_TARGETS), $(call buildrelease,$(TARGET)))
70+
71+
.PHONY: tag
72+
tag: ## Create a new git tag to prepare to build a release.
73+
git tag -sa v$(VERSION) -m "v$(VERSION)"
74+
@echo "Run git push origin v$(VERSION) to push your new tag to GitHub and trigger a release."
75+
76+
.PHONY: AUTHORS
77+
AUTHORS:
78+
@$(file >$@,# This file lists all individuals having contributed content to the repository.)
79+
@$(file >>$@,# For how it is generated, see `make AUTHORS`.)
80+
@echo "$(shell git log --format='\n%aN <%aE>' | LC_ALL=C.UTF-8 sort -uf)" >> $@
81+
82+
.PHONY: clean
83+
clean: ## Cleanup any build binaries or packages.
84+
@echo "+ $@"
85+
$(RM) -r $(BUILDDIR)
86+
$(RM) -r $(GENERATED_DOCS_DIR)
87+
88+
build: Cargo.toml $(wildcard src/*.rs) ## Build the Rust crate.
89+
cargo build
90+
91+
.PHONY: gen-docs
92+
gen-docs: gen-md gen-man ## Generate all the docs.
93+
94+
.PHONY: gen-md
95+
gen-md: build ## Generate the markdown documentation.
96+
$(CURDIR)/target/debug/$(NAME) generate markdown --dir $(GENERATED_DOCS_DIR)/md
97+
98+
.PHONY: gen-man
99+
gen-man: build ## Generate the man pages.
100+
$(CURDIR)/target/debug/$(NAME) generate man-pages --dir $(GENERATED_DOCS_DIR)/man
101+
102+
.PHONY: help
103+
help:
104+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | sed 's/^[^:]*://g' | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
105+
106+
check_defined = \
107+
$(strip $(foreach 1,$1, \
108+
$(call __check_defined,$1,$(strip $(value 2)))))
109+
110+
__check_defined = \
111+
$(if $(value $1),, \
112+
$(error Undefined $1$(if $2, ($2))$(if $(value @), \
113+
required by target `$@')))
114+

0 commit comments

Comments
 (0)