129 lines
4.1 KiB
Makefile
129 lines
4.1 KiB
Makefile
CARGO ?= cargo
|
|
STRIP ?= strip
|
|
CC ?= gcc
|
|
|
|
RUST_ARCH ?= x86_64
|
|
|
|
FEATURES := ""#"env_logger"
|
|
|
|
THIS_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))
|
|
REPO_ROOT := $(realpath $(THIS_DIR)/..)
|
|
export SERVICEPOINT_HEADER_OUT := $(REPO_ROOT)/include
|
|
|
|
# TODO: check if override is needed
|
|
override _libc := $(if $(MUSL),musl,gnu)
|
|
override _profile := $(if $(PROFILE),$(PROFILE),release)
|
|
override _rust_cli_profile := $(if $(filter $(_profile),debug),dev,$(_profile))
|
|
override _link_type := $(if $(LINK),$(LINK),dynamic)
|
|
|
|
# TODO: make LINK=static fails with linker error "undefined reference to `_dl_find_object'" in libgcc_eh.a
|
|
|
|
_all_profiles := release debug size-optimized
|
|
ifeq (,$(filter $(_all_profiles),$(_profile)))
|
|
_profile := $(error "PROFILE has to be set to one of: debug, release, size-optimized")
|
|
endif
|
|
|
|
STRIPFLAGS += -s --strip-unneeded -R .comment -R .gnu.version -R .note -R .note.gnu.build-id -R .note.ABI-tag
|
|
STATIC_LINK_LIBS += -lservicepoint_binding_c
|
|
|
|
RUST_TARGET := $(RUST_ARCH)-unknown-linux-$(_libc)
|
|
|
|
size-optimized_CARGOFLAGS += -Zbuild-std="core,std,alloc,proc_macro,panic_abort" \
|
|
-Zbuild-std-features="panic_immediate_abort"
|
|
CARGOFLAGS += --manifest-path=$(REPO_ROOT)/Cargo.toml \
|
|
--profile=$(_rust_cli_profile) \
|
|
--no-default-features \
|
|
--features=$(FEATURES) \
|
|
--target=$(RUST_TARGET) \
|
|
$($(_profile)_CARGOFLAGS)
|
|
|
|
CFLAGS += -Wall -Wextra -pedantic -fwhole-program -fPIE -pie
|
|
_no_debug_cflags := -ffunction-sections -fdata-sections -Wl,--gc-sections
|
|
size-optimized_CFLAGS += -Oz \
|
|
-fwrapv -fomit-frame-pointer -fno-stack-protector\
|
|
-fno-unroll-loops \
|
|
-fno-unwind-tables -fno-asynchronous-unwind-tables \
|
|
-fmerge-all-constants \
|
|
-Wl,-z,norelro \
|
|
-Wl,--hash-style=gnu \
|
|
-fvisibility=hidden \
|
|
-Bsymbolic \
|
|
-Wl,--exclude-libs,ALL \
|
|
-fno-ident \
|
|
-fno-exceptions \
|
|
$(_no_debug_cflags)
|
|
release_CFLAGS += -O2 \
|
|
$(_no_debug_cflags)
|
|
debug_CFLAGS += -Og
|
|
static_CFLAGS += -static $(STATIC_LINK_LIBS)
|
|
dynamic_CFLAGS += -Wl,-Bstatic $(STATIC_LINK_LIBS) -Wl,-Bdynamic
|
|
|
|
size-optimized_RUSTFLAGS += -Zlocation-detail=none \
|
|
-Zfmt-debug=none \
|
|
-C link-arg=-z,norelro \
|
|
-C panic=abort
|
|
#-C link-arg=--hash-style=gnu
|
|
musl_RUSTFLAGS += --crate-type=staticlib -Ctarget-feature=-crt-static
|
|
|
|
RUSTFLAGS += $($(_libc)_RUSTFLAGS) $($(_profile)_RUSTFLAGS) $($(_link_type)_RUSTFLAGS)
|
|
ifneq ($(_profile), debug)
|
|
RUSTFLAGS += -C link-arg=-s -C link-arg=-Wl,--gc-sections
|
|
endif
|
|
|
|
CFLAGS += $($(_libc)_CFLAGS) $($(_profile)_CFLAGS) $($(_link_type)_CFLAGS)
|
|
ifeq ($(LTO), 1)
|
|
CFLAGS += -flto
|
|
endif
|
|
|
|
RUST_TARGET_DIR := $(REPO_ROOT)/target/$(RUST_TARGET)/$(_profile)
|
|
|
|
_c_src := $(wildcard ./src/*.c)
|
|
_programs := $(basename $(notdir $(_c_src)))
|
|
_bins := $(addprefix out/, $(_programs))
|
|
_unstripped_bins := $(addsuffix _unstripped, $(_bins))
|
|
_run_programs := $(addprefix run_, $(_programs))
|
|
|
|
# TODO: use cargo --target-dir to put rust binaries into same dir
|
|
|
|
all: $(_bins)
|
|
|
|
clean: clean-c clean-rust
|
|
|
|
clean-c:
|
|
rm -r out || true
|
|
|
|
clean-rust:
|
|
rm $(SERVICEPOINT_HEADER_OUT)/servicepoint.h || true
|
|
cargo clean
|
|
|
|
.PHONY: all clean sizes $(_run_programs) clean-c clean-rust build-rust help FORCE
|
|
|
|
$(_unstripped_bins): out/%_unstripped: src/%.c src/helpers.h build-rust
|
|
mkdir -p out || true
|
|
$(CC) $< \
|
|
-I $(SERVICEPOINT_HEADER_OUT) \
|
|
-L $(RUST_TARGET_DIR) \
|
|
$(CFLAGS) \
|
|
-o $@
|
|
|
|
$(_bins): out/%: out/%_unstripped
|
|
$(STRIP) $(STRIPFLAGS) $^ -o $@
|
|
|
|
build-rust: FORCE
|
|
mkdir -p $(SERVICEPOINT_HEADER_OUT) || true
|
|
# generate servicepoint header and library to link against
|
|
$(CARGO) rustc $(CARGOFLAGS) -- $(RUSTFLAGS)
|
|
|
|
FORCE: ;
|
|
|
|
help:
|
|
@echo "You have the choice of the following parameters:"
|
|
@echo ""
|
|
@echo "Variable | Description | Default | Values"
|
|
@echo "---------+-----------------------------------+-----------+---------------------------"
|
|
@echo "CARGO | which cargo binary to use | 'cargo' | 'rustup run nightly cargo'"
|
|
@echo "CC | which C compiler to use | 'gcc' | 'musl-gcc'"
|
|
@echo "STRIP | which strip command to use | 'strip' | -"
|
|
@echo "MUSL | if set, use musl instead of glibc | unset | '1'"
|
|
@echo "PROFILE | Compilation profile | 'release' | 'debug' or 'size-optimized'"
|