add makefile for static compilation

This commit is contained in:
Sebastian Hugentobler 2022-03-26 11:00:13 +01:00
parent 368e3b2b30
commit 602ff3595d
12 changed files with 100 additions and 9 deletions

2
.cargo/config Normal file
View File

@ -0,0 +1,2 @@
[target.aarch64-unknown-linux-musl]
linker = "aarch64-linux-musl-gcc"

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/target
dist
release

View File

@ -2,6 +2,7 @@ image: rust:1.59-alpine3.15
stages:
- test
- build
test:
stage: test
@ -16,3 +17,19 @@ audit:
- apk --no-cache add musl-dev cargo-audit
script:
- cargo audit
build-statically:
stage: build
before_script:
- apk --no-cache add musl-dev make
- wget https://musl.cc/aarch64-linux-musl-cross.tgz
- echo "c909817856d6ceda86aa510894fa3527eac7989f0ef6e87b5721c58737a06c38 aarch64-linux-musl-cross.tgz" | sha256sum -c - || exit 1
- tar -zxvf aarch64-linux-musl-cross.tgz -C / --exclude='aarch64-linux-musl-cross/usr' --strip 1
- wget https://musl.cc/x86_64-w64-mingw32-cross.tgz
- echo "3a5c90309209a8b2e7ea1715a34b1029692e34189c5e7ecd77e1f102f82f6a02 x86_64-w64-mingw32-cross.tgz" | sha256sum -c - || exit 1
- tar -zxvf x86_64-w64-mingw32-cross.tgz -C / --exclude='./x86_64-w64-mingw32-cross/usr' --strip 2
- wget https://musl.cc/x86_64-linux-musl-cross.tgz
- echo "c5d410d9f82a4f24c549fe5d24f988f85b2679b452413a9f7e5f7b956f2fe7ea x86_64-linux-musl-cross.tgz" | sha256sum -c - || exit 1
- tar -zxvf x86_64-linux-musl-cross.tgz -C / --exclude='x86_64-linux-musl-cross/usr' --strip 1
script:
- make all

1
Cargo.lock generated
View File

@ -710,7 +710,6 @@ name = "http-client"
version = "0.1.0"
dependencies = [
"bank",
"gloo-console",
"http-lib",
"js-sys",
"serde",

55
Makefile Normal file
View File

@ -0,0 +1,55 @@
LINUX_X86_64_MUSL_TARGET = x86_64-unknown-linux-musl
LINUX_AARCH64_MUSL_TARGET = aarch64-unknown-linux-musl
WINDOWS_GNU_TARGET = x86_64-pc-windows-gnu
x86_64-unknown-linux-musl_strip = strip
aarch64-unknown-linux-musl_strip = aarch64-linux-musl-strip
x86_64-pc-windows-gnu_strip = strip
BINARIES = socket-server http-server
RELEASE_DIR = release
.PHONY: audit clean test linux-x86_64-musl linux-aarch64-musl windows-gnu http-client $(RELEASE_DIR) all default
TARGETS = $(LINUX_X86_64_MUSL_TARGET) $(LINUX_AARCH64_MUSL_TARGET) $(WINDOWS_GNU_TARGET)
default: all
all: linux-x86_64-musl linux-aarch64-musl windows-gnu http-client
define BUILDER
target/$(TARGET)/release/%:
RUSTFLAGS='-C target-feature=+crt-static' cargo build --release --target $(TARGET) --package $$(basename $$(@F))
$($(TARGET)_strip) $$@
$(RELEASE_DIR)/$(TARGET)/%: target/$(TARGET)/release/% $(RELEASE_DIR)
mkdir -p $$(dir $$@)
cp $$< $$@
endef
$(foreach TARGET,$(TARGETS),$(eval $(BUILDER)))
define BINARY_TARGETS
linux-x86_64-musl: $(RELEASE_DIR)/$(LINUX_X86_64_MUSL_TARGET)/$(BINARY)
linux-aarch64-musl: $(RELEASE_DIR)/$(LINUX_AARCH64_MUSL_TARGET)/$(BINARY)
windows-gnu: $(RELEASE_DIR)/$(WINDOWS_GNU_TARGET)/$(BINARY).exe
endef
$(foreach BINARY,$(BINARIES),$(eval $(BINARY_TARGETS)))
http-client:
cd http-client && trunk build --release
rm -rf $(RELEASE_DIR)/$@
mkdir -p $(RELEASE_DIR)/$@
cp -r http-client/dist/* $(RELEASE_DIR)/$@
$(RELEASE_DIR):
mkdir -p $(RELEASE_DIR)
test:
cargo test
audit:
cargo audit
clean:
-rm -rf target $(RELEASE_DIR)

View File

@ -37,3 +37,22 @@ Build for release (the built artifacts can be found in `dist`):
```
trunk build --release
```
# Releases
Release binaries are built for multiple architectures (always statically compiled):
- x86_64-unknown-linux-musl (needs ``)
- aarch64-unknown-linux-musl (needs `aarch64-linux-musl-gcc` and `aarch64-linux-musl-gcc`)
- x86_64-pc-windows-gnu (needs `x86_64-w64-mingw32-gcc`)
These are the target triplet names as they are used by rustup (add them with `rustup target add $TARGET`).
To make it more convenient to build, a makefile is provided. Use the following targets for it:
- linux-x86_64-musl
- linux-aarch64-musl
- windows-gnu
- http-client
The `default` and `all` targets build all of them. After a successful build, everything is copied into
the `release` folder.

View File

@ -6,7 +6,6 @@ edition = "2021"
[dependencies]
bank = { path = "../bank" }
http-lib = { path = "../http-lib" }
gloo-console = "0.2.1"
js-sys = "0.3.56"
serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"

View File

@ -85,7 +85,7 @@ impl Component for Main {
});
false
}
Event::Transfer(amount, from, to) => false,
Event::Transfer(_amount, _from, _to) => false,
}
}

View File

@ -68,7 +68,6 @@ async fn main() -> std::io::Result<()> {
#[cfg(test)]
mod tests {
use actix_web::body::to_bytes;
use actix_web::dev::Service;
use actix_web::http::{Method as HttpMethod, StatusCode};
use actix_web::{test, App};
use serde_json::Value;