Files
mcaselector-lite/makefile
Quinn 64691550bd use a more dynamic makefile
- remove CLANG env, as it might be confused for the compiler. And it
doesn't fit the build system that well anyway. (as we're compiling based
on file extension already)
- locate source files using find, rather than a bunch of wildcards.
- rename TARGET to BIN and store DIR_BIN and DIR_OBJ for the output
directories
- use pattern matchin for C object file creation, rather than specifying
all the sources manually
- remove directory target as directory creation is handled by the
recipe's themselves now.
2025-05-01 16:48:38 +02:00

110 lines
2.7 KiB
Makefile

# dependencies:
# - make
# - clang
# - rustc
# - git bash (windows)
NAME := mcaselector-lite
DEBUG ?= 0
ARCH ?= 0
# C compiler options
CC := clang
CSTD := c17
CFLAGS := -Wall -Wextra -Wpedantic -Wno-pointer-arith -static
LDFLAGS :=
# Rust compiler options
RUSTC := cargo rustc
RSFLAGS :=
ifeq ($(DEBUG),1)
CFLAGS += -DDEBUG -g -Og
RSOUT := debug
PROF := dbg
else
CFLAGS += -O2 -Werror
RSOUT := release
RSFLAGS := --release
PROF := rel
endif
ifneq ($(MAKECMDGOALS),clean)
ifeq ($(ARCH),linux-x86_64)
CFLAGS += -target x86_64-pc-linux-gnu
LDFLAGS += -target x86_64-pc-linux-gnu
RSFLAGS += --target=x86_64-unknown-linux-gnu
RSOUT := target/x86_64-unknown-linux-gnu/$(RSOUT)
else ifeq ($(ARCH),win-x86_64)
CFLAGS += -target x86_64-pc-windows-gnu
LDFLAGS += -target x86_64-pc-windows-gnu
RSFLAGS += --target=x86_64-pc-windows-gnu
RSOUT := target/x86_64-pc-windows-gnu/$(RSOUT)
EXT := .exe
else
$(error you must set the ARCH environment variable to one of these: 'linux-x86_64' 'win-x86_64')
endif
endif
ifneq ($(ARCH),0)
C_SRC := $(shell find -name '*.c')
C_OBJ := $(patsubst src/%,obj/$(ARCH)/$(PROF)/%,$(C_SRC:.c=.o))
C_DEP := $(C_OBJ:.o=.d)
RS_SRC := $(shell find -name '*.rs')
RS_LIB := $(RSOUT)/libmcaselector_lite.a
RS_DEP := $(RSOUT)/libmcaselector_lite.d
RSOUT :=
DIR_BIN := bin/$(ARCH)/$(PROF)
DIR_OBJ := obj/$(ARCH)/$(PROF)
BIN := $(DIR_BIN)/$(NAME)$(EXT)
endif
define wr_colour
@printf "\033[%sm%s\033[0m\n" $(2) $(1)
endef
# compiles and executes the produced binary
run: compile
./$(BIN)
compile: compile_commands.json $(BIN)
clean:
rm -rf bin/ obj/ target/ compile_commands.json
# TODO: write a structure for the unit tests in this
# create the binary (linking step)
$(BIN): $(C_OBJ) $(RS_LIB)
@$(call wr_colour,"RUSTC: '$(RUSTC)'",94)
@$(call wr_colour,"CC: '$(CC)'",94)
@$(call wr_colour,"CFLAGS: '$(CFLAGS)'",94)
@$(call wr_colour,"RSFLAGS: '$(RSFLAGS)'",94)
@$(call wr_colour,"LDFLAGS: '$(LDFLAGS)'",94)
@$(call wr_colour,"linking to: '$@'",92)
@mkdir -p $(@D)
@$(CC) $(LDFLAGS) -o $@ $(C_OBJ) $(RS_LIB)
@$(call wr_colour,"current profile: '$(PROF)'",93)
# create .o and .d files for C sources
$(DIR_OBJ)/%.o: src/%.c
@$(call wr_colour,"compiling $(notdir $@) from $(notdir $<)",92)
@mkdir -p $(@D)
@$(CC) $(CFLAGS) -c -MD -MP -std=$(CSTD) -x c -o $@ $<
# create .o and .d files for the entire rust codebase
$(RS_LIB): $(RS_SRC)
$(RUSTC) $(RSFLAGS)
# update compile commands if the makefile has been updated (for linting)
ifneq ($(shell which bear),)
compile_commands.json: makefile
$(MAKE) clean
@touch compile_commands.json
bear -- make compile
else
compile_commands.json:
endif
# include the dependencies
-include $(C_DEP)
-include $(RS_DEP)