Files
mcaselector-lite/makefile
Quinn cbefafacf8 rework makefile to be more os- and compiler-agnostic.
alongside removing non-crucial complexity and attempting to simplify
things as much as I can.
2025-06-02 12:41:01 +02:00

118 lines
3.4 KiB
Makefile

# dependencies:
# - make
# - C compiler
# - glfw3 (install glfw3:x64-mingw-dynamic via vcpkg for cross compilation)
# - (windows) git bash (recommended)
# build configuration, information about the current build process
NAME := mcaselector-lite
VERSION := 0.0.0
DEBUG ?= 0
CC ?= cc
CFLAGS += -c -std=c17 -Wall -Wextra -Wpedantic -Ilib -MMD -MP
LDFLAGS +=
MARCH ?= $(shell uname -m)
KERNEL ?= $(shell uname -s | tr '[:upper:]' '[:lower:]')
# check whether KERNEL is something nonsensical
ISWIN := $(if $(filter linux darwin freebsd netbsd openbsd,$(KERNEL)),0,1)
# profiles
ifeq ($(DEBUG),1) # check whether we're debugging
CFLAGS += -Og -g -fsanitize=address,undefined
LDFLAGS += -fsanitize=address,undefined
PROF := dbg
else ifeq ($(DEBUG),test) # check whether we're perhaps testing
CFLAGS += -O2
PROF := test
else # otherwise, assume release
CFLAGS += -O2 -DNDEBUG
PROF := rel
endif
# setup the VCPKG_TRIPLET
# because microsoft thinks they should use a different method of classifying stuff than the standard
ifneq ($(VCPKG_ROOT),)
VCPKG_TRIPLET ?= $(strip \
$(if $(filter x86_64,$(MARCH)),x64, \
))-$(strip \
$(if $(filter linux,$(KERNEL)),linux-dynamic, \
$(if $(filter darwin,$(KERNEL)),osx-dynamic, \
mingw-static)) \
)
# override the pkg config path, so it is used instead of system packages
export PKG_CONFIG_PATH := $(VCPKG_ROOT)/installed/$(VCPKG_TRIPLET)/lib/pkgconfig
else ifneq ($(shell which pkg_config),)
$(warning couldn't find VCPKG_ROOT, attempting to use system packages using pkg-config!)
else
$(error neither VCPKG_ROOT nor pkg_config were available!)
endif
# use pkg-config to set the include and linker information
CFLAGS += $(shell pkg-config --cflags glfw3)
LDFLAGS += $(shell pkg-config --libs glfw3)
# windows specific handling
ifeq ($(ISWIN),1)
NAME := $(NAME).exe
endif
# build directory structure
DIR_BIN := bin/$(MARCH)-$(KERNEL)/$(VERSION)/$(PROF)
DIR_OBJ := obj/$(MARCH)-$(KERNEL)/$(VERSION)/$(PROF)
# get source files
ifneq ($(DEBUG),test)
SRC := $(shell find src/ -name '*.c')
else
SRC := $(filter-out src/main.c, $(shell find test/ src/ -name '*.c'))
CFLAGS += -DGLFW_DLL
endif
# output files
BIN := $(DIR_BIN)/$(NAME)
OBJ := $(SRC:%.c=$(DIR_OBJ)/%.o)
DEP := $(OBJ:.o=.d)
COMPILE_COMMANDS := $(DIR_OBJ)/compile_commands.json
.PHONY: run compile
run: compile_commands $(BIN); $(BIN)
compile: compile_commands $(BIN)
# creates the binary (linking step)
$(BIN): $(OBJ)
@mkdir -p $(@D)
$(CC) -o $@ $^ $(LDFLAGS)
# compilation rule
$(DIR_OBJ)/%.o: %.c
@mkdir -p $(@D)
$(CC) $(CFLAGS) -o $@ $<
.PHONY .NOTPARALLEL:
clean:
rm -rf obj bin compile_commands.json
# update compile commands if the makefile has been updated (for linting)
compile_commands: # default, empty rule
ifneq ($(shell which bear),)
ifneq ($(COMPILE_COMMANDS),)
ifeq ($(NOCMDS),)
.PHONY .NOTPARALLEL:
compile_commands: $(COMPILE_COMMANDS)
ln -sf $< compile_commands.json
.NOTPARALLEL:
$(COMPILE_COMMANDS): makefile
@$(warning regenerating compile_commands.json thus recompiling.)
@mkdir -p ${@D} # ensure the target directory exists
@touch $@ # create the file so it isn't retriggered (will just change modification time if already exists)
@bear --output $@ -- make -B compile NOCMDS=1 # rebuild the current target using bear, to create the compile commands
endif
endif
endif
-include $(DEP)