forked from static-web-server/static-web-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
307 lines (256 loc) · 8.31 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
PKG_TARGET=x86_64-unknown-linux-musl
PKG_TARGET_DARWIN=x86_64-apple-darwin
RUST_VERSION ?= $(shell rustc --version | cut -d ' ' -f2)
PKG_BIN_PATH=./bin
PKG_TMP_PATH=/tmp
PKG_NAME=$(shell cat Cargo.toml | sed -n 's/name = "\([^}]*\)"/\1/p' | head -n1)
PKG_TAG=$(shell cat Cargo.toml | sed -n 's/version = "\([^}]*\)"/\1/p' | head -n1)
PKG_RELEASE_NAME=$(PKG_NAME)-v$(PKG_TAG)-$(PKG_TARGET)
PKG_RELEASE_NAME_DARWIN=$(PKG_NAME)-v$(PKG_TAG)-$(PKG_TARGET_DARWIN)
PKG_TMP_BIN_PATH=$(PKG_TMP_PATH)/$(PKG_RELEASE_NAME)
PKG_TMP_BIN_PATH_DARWIN=$(PKG_TMP_PATH)/$(PKG_RELEASE_NAME_DARWIN)
#######################################
############# Development #############
#######################################
install:
@rustup target add $(PKG_TARGET)
@cargo install --force cargo-make
@cargo install cargo-audit
.PHONY: install
lint:
@rustc -vV
@cargo clippy --all-features -- -D warnings
.PHONY: lint
build:
@rustc -vV
@cargo build --release --target $(PKG_TARGET)
.PHONY: build
# Release test tasks
test.release:
@docker run --rm -it \
-v $(PWD):/root/src/static-web-server \
-v cargo-git:/root/.cargo/git \
-v cargo-registry:/root/.cargo/registry \
-v cargo-target:/root/src/static-web-server/target \
\
--workdir /root/src/static-web-server \
joseluisq/rust-linux-darwin-builder:$(RUST_VERSION) \
\
bash -c "make prod.release"
.PHONY: test.release
linux:
@docker run --rm -it \
-v $(PWD):/root/src/static-web-server \
-v cargo-git:/root/.cargo/git \
-v cargo-registry:/root/.cargo/registry \
-v cargo-target:/root/src/static-web-server/target \
\
--workdir /root/src/static-web-server \
joseluisq/rust-linux-darwin-builder:$(RUST_VERSION) \
\
bash -c "\
echo Building Linux release binary... && \
rustc -vV && \
cargo build --release --target $(PKG_TARGET) && \
du -sh ./target/$(PKG_TARGET)/release/$(PKG_NAME) && \
mkdir -p release && \
cp -rf ./target/$(PKG_TARGET)/release/$(PKG_NAME) release/$(PKG_NAME)-linux && \
echo \"Shrinking Linux binary file...\" && \
strip release/$(PKG_NAME)-linux && \
du -sh ./release/$(PKG_NAME)-linux"
.PHONY: linux
darwin:
@docker run --rm -it \
-v $(PWD):/root/src/static-web-server \
-v cargo-git:/root/.cargo/git \
-v cargo-registry:/root/.cargo/registry \
-v cargo-target:/root/src/static-web-server/target \
\
--workdir /root/src/static-web-server \
joseluisq/rust-linux-darwin-builder:$(RUST_VERSION) \
\
bash -c "\
echo Building Darwin release binary... && \
rustc -vV && \
env CC=o64-clang CXX=o64-clang++ cargo build --release --target $(PKG_TARGET_DARWIN) && \
du -sh ./target/$(PKG_TARGET_DARWIN)/release/$(PKG_NAME) && \
mkdir -p release && \
cp -rf ./target/$(PKG_TARGET_DARWIN)/release/$(PKG_NAME) release/$(PKG_NAME)-darwin && \
echo \"Shrinking Darwin binary file...\" && \
x86_64-apple-darwin20.2-strip release/$(PKG_NAME)-darwin && \
du -sh ./release/$(PKG_NAME)-darwin"
.PHONY: darwin
#######################################
########### Utility tasks #############
#######################################
test:
@echo "Testing application..."
@rustc -vV
@cargo test
.PHONY: test
fmt:
@cargo fix
@cargo fmt --all
.PHONY: fmt
## Development Docker images
docker.image:
@echo "Creating Docker Scratch image..."
@cp -frp ./target/x86_64-unknown-linux-musl/release/static-web-server ./docker/devel/
@docker build \
--rm=true -f ./docker/devel/Dockerfile.scratch \
-t joseluisq/${PKG_NAME}:devel . --pull=true
.PHONY: docker.image
docker.image.alpine:
@echo "Creating Docker Alpine image..."
@cp -frp ./target/x86_64-unknown-linux-musl/release/static-web-server ./docker/devel/
@docker build \
--rm=true -f ./docker/devel/Dockerfile.alpine \
-t joseluisq/${PKG_NAME}:devel-alpine . --pull=true
.PHONY: docker.image.alpine
docker.image.debian:
@echo "Creating Docker Alpine image..."
@cp -frp ./target/x86_64-unknown-linux-musl/release/static-web-server ./docker/devel/
@docker build \
--platform="linux/x86_64" \
--rm=true -f ./docker/devel/Dockerfile.debian \
-t joseluisq/${PKG_NAME}:devel-debian . --pull=true
.PHONY: docker.image.debian
#######################################
########## Production tasks ###########
#######################################
# Compile release binary
define build_release =
set -e
set -u
echo
echo "Compiling application..."
rustc -vV
echo
echo "Compiling release binary for $(PKG_TARGET)..."
cargo build --release --target $(PKG_TARGET)
echo
echo
echo "Compiling release binary for $(PKG_TARGET_DARWIN)..."
env CC=o64-clang CXX=o64-clang++ cargo build --release --target $(PKG_TARGET_DARWIN)
echo
echo "Release builds were compiled!"
endef
# Shrink a release binary size
define build_release_shrink =
set -e
set -u
echo "Copying release binaries..."
mkdir -p $(PKG_BIN_PATH)
# Linux
mkdir -p $(PKG_TMP_BIN_PATH)
cp -rf ./target/$(PKG_TARGET)/release/$(PKG_NAME) $(PKG_TMP_BIN_PATH)/
# Darwin
mkdir -p $(PKG_TMP_BIN_PATH_DARWIN)
cp -rf ./target/$(PKG_TARGET_DARWIN)/release/$(PKG_NAME) $(PKG_TMP_BIN_PATH_DARWIN)/
# Linux
echo "Performing Linux/Darwin binaries shrinking..."
echo "Binary sizes before:"
du -sh $(PKG_TMP_BIN_PATH)/$(PKG_NAME)
du -sh $(PKG_TMP_BIN_PATH_DARWIN)/$(PKG_NAME)
# Shrink binaries in place (tmp dir)
strip $(PKG_TMP_BIN_PATH)/$(PKG_NAME)
x86_64-apple-darwin20.2-strip $(PKG_TMP_BIN_PATH_DARWIN)/$(PKG_NAME)
echo "Binary sizes after (shrinking):"
du -sh $(PKG_TMP_BIN_PATH)/$(PKG_NAME)
du -sh $(PKG_TMP_BIN_PATH_DARWIN)/$(PKG_NAME)
# Copy only Linux binary for the Docker image build process
echo "Copying Linux binary from $(PKG_TMP_BIN_PATH) to $(PKG_BIN_PATH) directory..."
cp -rf $(PKG_TMP_BIN_PATH)/$(PKG_NAME) $(PKG_BIN_PATH)/
echo "Releases size shrinking completed!"
endef
# Creates release files (tarballs, zipballs)
define build_release_files =
set -e
set -u
echo "Creating tarballs with their checksums..."
# Enter to bin/ directory
mkdir -p $(PKG_BIN_PATH)
cd $(PKG_BIN_PATH)
pwd
# Tar/Gzip/sha256sum file for Linux
tar czvf $(PKG_RELEASE_NAME).tar.gz -C $(PKG_TMP_BIN_PATH) $(PKG_NAME)
sha256sum $(PKG_RELEASE_NAME).tar.gz > $(PKG_NAME)-v$(PKG_TAG)-SHA256SUM
echo "Linux tarball with sha256sum created."
# Tar/Gzip/sha256sum file for Darwin
tar czvf $(PKG_RELEASE_NAME_DARWIN).tar.gz -C $(PKG_TMP_BIN_PATH_DARWIN) $(PKG_NAME)
sha256sum $(PKG_RELEASE_NAME_DARWIN).tar.gz >> $(PKG_NAME)-v$(PKG_TAG)-SHA256SUM
echo "Darwin tarball with sha256sum created."
du -sh ./*
echo "Release tarballs and sha256sum files created!"
endef
prod.release:
set -e
set -u
echo "Building a release..."
$(build_release)
$(build_release_shrink)
$(build_release_files)
.ONESHELL: prod.release
prod.release.build:
@$(build_release)
.ONESHELL: prod.release.build
prod.release.shrink:
@$(build_release_shrink)
.ONESHELL: prod.release.shrink
prod.release.files:
@$(build_release_files)
.ONESHELL: prod.release.files
prod.release.version:
git add .
git commit . -m "v$(PKG_TAG)"
git tag v$(PKG_TAG)
git push
.ONESHELL: prod.release.version
docs-dev:
@docker-compose -f docs/docker-compose.yml up --build
.PHONY: docs-dev
crate-docs:
@cargo doc --no-deps
.PHONY: crate-docs
crate-docs-dev:
@env \
RUSTDOCFLAGS="--cfg docsrs" \
cargo doc --lib --no-deps --all-features --document-private-items
@echo "Crate documentation: http://localhost:8787/static_web_server"
@static-web-server -p 8787 -d target/doc/ \
& watchman-make -p 'src/**/*.rs' --run '\
env \
RUSTDOCFLAGS="--cfg docsrs" \
cargo doc --lib --no-deps --all-features --document-private-items'
.PHONY: crate-docs-dev
docs-deploy:
@git stash
@rm -rf /tmp/docs
@mkdir -p /tmp/docs
@docker-compose -f docs/docker-compose.yml build
@docker run -it --rm \
-v $(PWD)/.git:/docs/.git \
-v $(PWD)/docs/content:/docs/docs/content \
-v $(PWD)/docs/mkdocs.yml:/docs/mkdocs.yml \
-v /tmp/docs:/tmp/docs \
static-web-server-docs mkdocs build
@git checkout gh-pages
@git clean -fdx
@cp -rf docs/CNAME /tmp/docs/
@rm -rf docs/
@mkdir -p docs/
@cp -rf /tmp/docs/. docs/
@git add docs/
@git commit docs/ -m "docs: automatic documentation updates [skip ci]"
@git push origin gh-pages
@git push github gh-pages
@echo
@echo "Documentation built and published"
@git checkout master
.PHONY: docs-deploy
typos:
@typos . --config ./.github/workflows/config/typos.toml
.PHONY: typos
man:
@asciidoctor --doctype=manpage --backend=manpage docs/man/static-web-server.1.rst
.PHONY: man