PROJECT_NAME = $(shell cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].name')
VERSION = $(shell cargo metadata --no-deps --format-version 1 | jq -r '.packages[0].version')
DEB_ARCH_x86_64-unknown-linux-gnu := amd64
DEB_ARCH_aarch64-unknown-linux-gnu := arm64
LINUX_TARGETS = x86_64-unknown-linux-gnu \
aarch64-unknown-linux-gnu
MACOS_TARGETS = x86_64-apple-darwin \
aarch64-apple-darwin
WINDOWS_TARGETS = x86_64-pc-windows-gnu \
x86_64-pc-windows-msvc
release: linux macos
linux: check_cross check_linux_crosscompilation_on_macos $(LINUX_TARGETS) $(addprefix deb-, $(LINUX_TARGETS))
macos: check_cargo check_toolchain check_cross $(MACOS_TARGETS)
$(LINUX_TARGETS):
@echo "building for target $@"
ifeq ($@, x86_64-unknown-linux-gnu)
CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc cross build --target $@ --release
else
@cross build --release --target=$@
endif
mkdir -p release/$@
cp target/$@/release/my release/$@/
tar czf release/my-$@.tar.gz -C release/$@ my
x86_64-apple-darwin:
cross build --target $@ --release
mkdir -p release/$@
cp target/$@/release/my release/$@/
tar czf release/my-$@.tar.gz -C release/$@ my
aarch64-apple-darwin:
cargo build --target $@ --release
mkdir -p release/$@
cp target/$@/release/my release/$@/
tar czf release/my-$@.tar.gz -C release/$@ my
deb-%: check_deb
@echo "Building Debian package for $*"
cargo deb --no-build --target $* --output target/$*/$(PROJECT_NAME)-$(VERSION)-$*-$(DEB_ARCH_$*).deb
mv target/$*/$(PROJECT_NAME)-$(VERSION)-$*-$(DEB_ARCH_$*).deb release/
clean:
rm -rf target
rm -rf release
check_cross:
@command -v cross > /dev/null 2>&1 || { echo >&2 "Error: 'cross' command not found. Please install 'cross' by running 'cargo install cross'."; exit 1; }
check_cargo:
@command -v cargo > /dev/null 2>&1 || { echo >&2 "Error: 'cargo' command not found. Please install Rust using the instructions at https://www.rust-lang.org/tools/install."; exit 1; }
check_toolchain:
@rustup target list --toolchain stable | grep aarch64-apple-darwin > /dev/null 2>&1 || { echo >&2 "Error: The 'aarch64-apple-darwin' Rustup stable toolchain is not installed. Please install it by running 'rustup target add aarch64-apple-darwin --toolchain stable'."; exit 1; }
check_deb:
@command -v cargo-deb > /dev/null 2>&1 || { echo >&2 "Error: 'cargo-deb' command not found. Please install 'cargo-deb' by running 'cargo install cargo-deb'."; exit 1; }
check_mingw:
@command -v x86_64-w64-mingw32-gcc > /dev/null 2>&1 || { echo >&2 "Error: 'mingw-w64' is not installed. Please install it according to your platform (macOS: 'brew install mingw-w64', Debian/Ubuntu: 'sudo apt-get install mingw-w64', Fedora: 'sudo dnf install mingw64-gcc')."; exit 1; }
check_linux_crosscompilation_on_macos:
ifeq ($(shell uname),Darwin)
@if ! brew tap | grep -q 'sergiobenitez/osxct'; then \
echo "Homebrew tap 'sergiobenitez/osxct' is missing. Installing it now..."; \
brew tap sergiobenitez/osxct; \
fi
@if ! brew list --formula | grep -q 'x86_64-unknown-linux-gnu'; then \
echo "Homebrew formula 'x86_64-unknown-linux-gnu' is missing. Installing it now..."; \
brew install x86_64-unknown-linux-gnu; \
fi
@if ! command -v x86_64-unknown-linux-gnu-gcc >/dev/null; then \
echo "x86_64-unknown-linux-gnu-gcc not found in PATH. Please add it to your PATH."; \
exit 1; \
fi
endif
.PHONY: check_cross check_cargo check_toolchain check_deb check_rpm linux windows macos release $(LINUX_TARGETS) $(WINDOWS_TARGETS) $(MACOS_TARGETS) deb rpm