|
| 1 | +# We cannot use $(shell pwd), which will return unix path format on Windows, |
| 2 | +# making it hard to use. |
| 3 | +cur_dir = $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) |
| 4 | + |
| 5 | +TOP := $(cur_dir) |
| 6 | +# RUSTFLAGS that are likely to be tweaked by developers. For example, |
| 7 | +# while we enable debug logs by default here, some might want to strip them |
| 8 | +# for minimal code size / consumed cycles. |
| 9 | +CUSTOM_RUSTFLAGS := --cfg debug_assertions |
| 10 | +# RUSTFLAGS that are less likely to be tweaked by developers. Most likely |
| 11 | +# one would want to keep the default values here. |
| 12 | +FULL_RUSTFLAGS := -C target-feature=+zba,+zbb,+zbc,+zbs,-a $(CUSTOM_RUSTFLAGS) |
| 13 | +# Additional cargo args to append here. For example, one can use |
| 14 | +# make test CARGO_ARGS="-- --nocapture" so as to inspect data emitted to |
| 15 | +# stdout in unit tests |
| 16 | +CARGO_ARGS := |
| 17 | +MODE := release |
| 18 | +# Tweak this to change the clang version to use for building C code. By default |
| 19 | +# we use a bash script with somes heuristics to find clang in current system. |
| 20 | +CLANG := $(shell $(TOP)/scripts/find_clang) |
| 21 | +# When this is set to some value, the generated binaries will be copied over |
| 22 | +BUILD_DIR := |
| 23 | +# Generated binaries to copy. By convention, a Rust crate's directory name will |
| 24 | +# likely match the crate name, which is also the name of the final binary. |
| 25 | +# However if this is not the case, you can tweak this variable. As the name hints, |
| 26 | +# more than one binary is supported here. |
| 27 | +BINARIES := $(notdir $(shell pwd)) |
| 28 | + |
| 29 | +ifeq (release,$(MODE)) |
| 30 | + MODE_ARGS := --release |
| 31 | +endif |
| 32 | + |
| 33 | +default: build test |
| 34 | + |
| 35 | +build: |
| 36 | + RUSTFLAGS="$(FULL_RUSTFLAGS)" TARGET_CC="$(CLANG)" \ |
| 37 | + cargo build --target=riscv64imac-unknown-none-elf $(MODE_ARGS) $(CARGO_ARGS) |
| 38 | + @set -eu; \ |
| 39 | + if [ "x$(BUILD_DIR)" != "x" ]; then \ |
| 40 | + for binary in $(BINARIES); do \ |
| 41 | + echo "Copying binary $$binary to build directory"; \ |
| 42 | + cp $(TOP)/target/riscv64imac-unknown-none-elf/$(MODE)/$$binary $(TOP)/$(BUILD_DIR); \ |
| 43 | + done \ |
| 44 | + fi |
| 45 | + |
| 46 | +# test, check, clippy and fmt here are provided for completeness, |
| 47 | +# there is nothing wrong invoking cargo directly instead of make. |
| 48 | +test: |
| 49 | + cargo test $(CARGO_ARGS) |
| 50 | + |
| 51 | +check: |
| 52 | + cargo check $(CARGO_ARGS) |
| 53 | + |
| 54 | +clippy: |
| 55 | + cargo clippy $(CARGO_ARGS) |
| 56 | + |
| 57 | +fmt: |
| 58 | + cargo fmt $(CARGO_ARGS) |
| 59 | + |
| 60 | +# Arbitrary cargo command is supported here. For example: |
| 61 | +# |
| 62 | +# make cargo CARGO_CMD=expand CARGO_ARGS="--ugly" |
| 63 | +# |
| 64 | +# Invokes: |
| 65 | +# cargo expand --ugly |
| 66 | +CARGO_CMD := |
| 67 | +cargo: |
| 68 | + cargo $(CARGO_CMD) $(CARGO_ARGS) |
| 69 | + |
| 70 | +clean: |
| 71 | + cargo clean |
| 72 | + |
| 73 | +prepare: |
| 74 | + rustup target add riscv64imac-unknown-none-elf |
| 75 | + |
| 76 | +.PHONY: build test check clippy fmt cargo clean prepare |
0 commit comments