From 602ff3595d328847ef87802425c726dab63d8b38 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Sat, 26 Mar 2022 11:00:13 +0100 Subject: [PATCH] add makefile for static compilation --- .cargo/config | 2 ++ .gitignore | 1 + .gitlab-ci.yml | 19 ++++++++++- Cargo.lock | 1 - Makefile | 55 ++++++++++++++++++++++++++++++ README.md | 21 +++++++++++- bank/Cargo.toml | 2 +- http-client/Cargo.toml | 1 - http-client/src/components/main.rs | 2 +- http-lib/Cargo.toml | 2 +- http-server/Cargo.toml | 2 +- http-server/src/main.rs | 1 - 12 files changed, 100 insertions(+), 9 deletions(-) create mode 100644 .cargo/config create mode 100644 Makefile diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..a076ee4 --- /dev/null +++ b/.cargo/config @@ -0,0 +1,2 @@ +[target.aarch64-unknown-linux-musl] +linker = "aarch64-linux-musl-gcc" diff --git a/.gitignore b/.gitignore index 77fdd3e..5e3d905 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target dist +release diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a9b3c97..65b5f7d 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -2,6 +2,7 @@ image: rust:1.59-alpine3.15 stages: - test + - build test: stage: test @@ -15,4 +16,20 @@ audit: before_script: - apk --no-cache add musl-dev cargo-audit script: - - cargo audit \ No newline at end of file + - 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 \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index cb0d8a0..421e281 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -710,7 +710,6 @@ name = "http-client" version = "0.1.0" dependencies = [ "bank", - "gloo-console", "http-lib", "js-sys", "serde", diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3b56dbe --- /dev/null +++ b/Makefile @@ -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) + diff --git a/README.md b/README.md index defc782..15ff391 100644 --- a/README.md +++ b/README.md @@ -36,4 +36,23 @@ trunk serve Build for release (the built artifacts can be found in `dist`): ``` trunk build --release -``` \ No newline at end of file +``` + +# 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. diff --git a/bank/Cargo.toml b/bank/Cargo.toml index e3c07fa..e4d893b 100644 --- a/bank/Cargo.toml +++ b/bank/Cargo.toml @@ -5,4 +5,4 @@ edition = "2021" [dependencies] thiserror = "1.0.30" -uuid = { version = "0.8.2", features = ["v4", "wasm-bindgen"] } \ No newline at end of file +uuid = { version = "0.8.2", features = ["v4", "wasm-bindgen"] } diff --git a/http-client/Cargo.toml b/http-client/Cargo.toml index 6f93c15..2812118 100644 --- a/http-client/Cargo.toml +++ b/http-client/Cargo.toml @@ -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" diff --git a/http-client/src/components/main.rs b/http-client/src/components/main.rs index 9cb643f..078fd03 100644 --- a/http-client/src/components/main.rs +++ b/http-client/src/components/main.rs @@ -85,7 +85,7 @@ impl Component for Main { }); false } - Event::Transfer(amount, from, to) => false, + Event::Transfer(_amount, _from, _to) => false, } } diff --git a/http-lib/Cargo.toml b/http-lib/Cargo.toml index e78a0c4..ba33178 100644 --- a/http-lib/Cargo.toml +++ b/http-lib/Cargo.toml @@ -5,4 +5,4 @@ edition = "2021" [dependencies] bank = { path = "../bank" } -serde = { version = "1.0.136", features = ["derive"] } \ No newline at end of file +serde = { version = "1.0.136", features = ["derive"] } diff --git a/http-server/Cargo.toml b/http-server/Cargo.toml index 4e2d72c..c513eba 100644 --- a/http-server/Cargo.toml +++ b/http-server/Cargo.toml @@ -14,4 +14,4 @@ pretty_env_logger = "0.4.0" serde = { version = "1.0.136", features = ["derive"] } [dev-dependencies] -serde_json = "1.0.79" \ No newline at end of file +serde_json = "1.0.79" diff --git a/http-server/src/main.rs b/http-server/src/main.rs index e8063b9..0e41684 100644 --- a/http-server/src/main.rs +++ b/http-server/src/main.rs @@ -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;