mirror of
https://github.com/thepigeongenerator/mcaselector-lite
synced 2026-02-07 23:28:09 +01:00
Rework Makefile, to be more robust, reliable and minimal.
- Improved logic for overriding compiler flags. - Made tooling and logging more robust. - Added functionality to disable "silent mode" by listening to `--no-silent` - Removed OSX/Darwin build support (for now) - No longer utilising /obj directory, but instead just building files right in the worktree. - Generating dependency files seperately from general code, for clairity.
This commit is contained in:
148
Makefile
148
Makefile
@@ -1,26 +1,41 @@
|
|||||||
# This file is part of MCA-Selector-lite,
|
# This file is part of MCA-Selector-lite,
|
||||||
# and is licensed under GPL-2.0-only.
|
# and is licensed under GPL-2.0-only.
|
||||||
# Copyright (C)2025 quinnthepigeon@proton.me Quinn
|
# Copyright (C)2026 quinnthepigeon@proton.me Quinn
|
||||||
# For further information, view COPYING and CONTRIBUTORS
|
# For further information, view COPYING and CONTRIBUTORS
|
||||||
# at: www.github.com/thepigeongenerator/mcaselector-lite
|
# at: www.github.com/thepigeongenerator/mcaselector-lite
|
||||||
|
|
||||||
SHELL = /bin/sh
|
SHELL = /bin/sh
|
||||||
.SUFFIXES:
|
.SUFFIXES:
|
||||||
|
|
||||||
NAME = mcaselector-lite
|
VERSION = 0.0
|
||||||
|
|
||||||
CMAKE ?= cmake -G 'Unix Makefiles'
|
CMAKE ?= cmake -G 'Unix Makefiles'
|
||||||
|
XXD ?= xxd
|
||||||
|
TAR ?= tar
|
||||||
|
|
||||||
CPPFLAGS = -DNDEBUG
|
RES := $(wildcard res/*.glsl)
|
||||||
CFLAGS = -O2
|
SRC := $(shell find src/ -name '*.c' -print) $(addsuffix .c,$(RES)) lib/glad/src/gl.c
|
||||||
LDFLAGS = -flto
|
OBJ := $(addsuffix .o,$(SRC))
|
||||||
|
DEP := $(addsuffix .d,$(SRC))
|
||||||
|
|
||||||
CPPFLAGS += -DGLFW_INCLUDE_NONE
|
CFLAGS := -O2 $(CFLAGS) -g -std=gnu99
|
||||||
CPPFLAGS += -Iinclude -Ilib/glad/include -Ilib/glfw/include -Ilib/libarchive/libarchive
|
|
||||||
LDFLAGS += -Llib/obj/glfw/src -Llib/obj/libarchive/libarchive
|
|
||||||
LDLIBS += -lglfw3 -larchive -lm
|
|
||||||
CFLAGS += -std=gnu99 -g -MMD -MP
|
|
||||||
CFLAGS += -Wall -Wextra -Wpedantic -Wno-pointer-arith
|
CFLAGS += -Wall -Wextra -Wpedantic -Wno-pointer-arith
|
||||||
|
CPPFLAGS := -DNDEBUG $(CPPFLAGS) -DGLFW_INCLUDE_NONE
|
||||||
|
CPPFLAGS += -Iinclude -Ilib/glad/include -Ilib/glfw/include -Ilib/libarchive/libarchive
|
||||||
|
LDFLAGS := -flto $(LDFLAGS)
|
||||||
|
LDFLAGS := -Llib/obj/glfw/src -Llib/obj/libarchive/libarchive
|
||||||
|
LDLIBS := $(LDLIBS) -lglfw3 -larchive -lm
|
||||||
|
|
||||||
|
msg-cc = $(info [CC] $(1))
|
||||||
|
msg-clean = $(info [CLEAN] $(1))
|
||||||
|
msg-ld = $(info [LD] $(1))
|
||||||
|
msg-mkdir = $(info [MKDIR] $(1))
|
||||||
|
msg-tar = $(info [TAR] $(1))
|
||||||
|
msg-xxd = $(info [XXD] $(1))
|
||||||
|
|
||||||
|
# Set Q to @ to silence commands being printed, unless --no-silent has been set
|
||||||
|
ifeq (0, $(words $(findstring --no-silent,$(MAKEFLAGS))))
|
||||||
|
Q=@
|
||||||
|
endif
|
||||||
|
|
||||||
# detect if we're compiling on Windows, meaning
|
# detect if we're compiling on Windows, meaning
|
||||||
# a lot of things considered "standard" are unavailable.
|
# a lot of things considered "standard" are unavailable.
|
||||||
@@ -28,78 +43,49 @@ ifeq ($(OS),Windows_NT)
|
|||||||
NAME := $(NAME).exe
|
NAME := $(NAME).exe
|
||||||
LDLIBS += -lopengl32 -lgdi32
|
LDLIBS += -lopengl32 -lgdi32
|
||||||
$(warning Detected Windows_NT, please refer to the documentation if you encounter issues.)
|
$(warning Detected Windows_NT, please refer to the documentation if you encounter issues.)
|
||||||
# in the case of Mac OS X
|
|
||||||
else ifeq ($(shell uname -s),Darwin)
|
|
||||||
LDLIBS += -framework Coca -framework OpenGL -framework IOKit
|
|
||||||
$(info Mac OS X detected.)
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# find all the source files using wildcards
|
# Default target; compiles everything.
|
||||||
# TODO: find a better method to find all source files
|
.PHONY:
|
||||||
# NOTE: MS-DOS and MS-Windows uses backslash `\`, this might break.
|
all: bin/mcaselector-lite bin/mcaselector-lite.stripped
|
||||||
RES := $(wildcard res/*)
|
|
||||||
SRC := $(wildcard src/*.c src/*/*.c src/*/*/*.c src/*/*/*/*.c src/*/*/*/*/*.c src/*/*/*/*/*/*.c src/*/*/*/*/*/*/*.c src/*/*/*/*/*/*/*/*.c)
|
|
||||||
SRC += lib/glad/src/gl.c
|
|
||||||
TSRC := $(wildcard test/*.c test/*/*.c test/*/*/*.c test/*/*/*/*.c test/*/*/*/*/*.c test/*/*/*/*/*/*.c test/*/*/*/*/*/*/*.c test/*/*/*/*/*/*/*/*.c)
|
|
||||||
|
|
||||||
OBJ := $(RES:%=obj/%.o) $(SRC:%.c=obj/%.o)
|
# TODO: Compile libraries with `all`
|
||||||
TOBJ := $(TSRC:%.c=obj/%.o)
|
# TODO: Write install recipe to install into DESTDIR, for UNIX® type systems, and Windows NT alongside uninstall recipes.
|
||||||
|
# TODO: Same goes for install-strip, which does the same, but with the stripped binary instead.
|
||||||
# TODO: potentially automatically detect whether we should compile libs, or if we can just go ahead.
|
# TODO: Include distclean which recompiles the libraries.
|
||||||
|
|
||||||
.PHONY: all libs check clean clean-libs
|
|
||||||
all: bin/$(NAME)
|
|
||||||
libs: lib/obj/glfw/ lib/obj/libarchive/
|
|
||||||
check: bin/TEST_$(NAME); ./$<
|
|
||||||
clean:; @-$(RM) -rv bin/ obj/
|
|
||||||
clean-libs:; @-$(RM) -rv lib/obj/
|
|
||||||
|
|
||||||
.PHONY:
|
.PHONY:
|
||||||
install: all
|
clean:
|
||||||
ifneq ($(OS),Windows_NT)
|
-$(Q)$(RM) $(OBJ) $(DEP)
|
||||||
# TODO: POSIX-compliant installation
|
-$(Q)$(RM) -r bin/
|
||||||
else
|
|
||||||
# TODO: WINDOWS_NT installation
|
# Links together the object files into the final binary.
|
||||||
|
bin/mcaselector-lite: $(OBJ) | bin/
|
||||||
|
$(Q)$(call msg-ld,$@)
|
||||||
|
$(Q)$(CC) $(LDFLAGS) $(LDLIBS) -o $@ $^
|
||||||
|
bin/mcaselector-lite.stripped: $(OBJ) | bin/
|
||||||
|
$(Q)$(call msg-ld,$@)
|
||||||
|
$(Q)$(CC) -s $(LDFLAGS) $(LDLIBS) -o $@ $^
|
||||||
|
|
||||||
|
# Compiles C sources into object files
|
||||||
|
%.c.o: %.c
|
||||||
|
$(Q)$(call msg-cc,$@)
|
||||||
|
$(Q)$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<
|
||||||
|
|
||||||
|
# Embed files in /res as plain binary into the final binary.
|
||||||
|
# TODO: Find some fix for this clogging up the workspace
|
||||||
|
.INTERMEDIATE:
|
||||||
|
res/%.c: res/%
|
||||||
|
$(Q)$(call msg-xxd,$@)
|
||||||
|
$(Q)cd $(<D) && $(XXD) -i $(patsubst res/%,%,$<) $(abspath $@)
|
||||||
|
|
||||||
|
%/:
|
||||||
|
$(Q)$(call msg-mkdir,$@)
|
||||||
|
$(Q)mkdir $@
|
||||||
|
|
||||||
|
# Generate and include dependencies,
|
||||||
|
# ignoring any errors that may occur when doing so.
|
||||||
|
%.c.d: %.c; $(Q)$(CC) -MM $(CPPFLAGS) -MF $@ $<
|
||||||
|
ifeq (0, $(words $(findstring $(MAKECMDGOALS), clean)))
|
||||||
|
-include $(DEP)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
.PHONY:
|
|
||||||
install-strip: install
|
|
||||||
# TODO: strip the produced installation
|
|
||||||
|
|
||||||
# compiles the libraries using cmake
|
|
||||||
lib/obj/%/: lib/%/
|
|
||||||
$(CMAKE) -S $< -B $@
|
|
||||||
$(MAKE) -C $@
|
|
||||||
|
|
||||||
# link together a runtime binary
|
|
||||||
bin/$(NAME): $(OBJ)
|
|
||||||
$(info [LD] $@)
|
|
||||||
@mkdir -p $(@D)
|
|
||||||
@$(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)
|
|
||||||
|
|
||||||
# link together a testing binary
|
|
||||||
bin/TEST_$(NAME): $(TOBJ) $(filter-out obj/src/main.o,$(OBJ))
|
|
||||||
$(info [LD] $@)
|
|
||||||
@mkdir -p $(@D)
|
|
||||||
@$(CC) -o $@ $^ $(LDFLAGS) $(LDLIBS)
|
|
||||||
|
|
||||||
obj/res/%.c: res/%
|
|
||||||
$(info [XXD] $@)
|
|
||||||
@mkdir -p $(@D)
|
|
||||||
@cd res/ && xxd -i $(patsubst res/%,%,$<) $(abspath $@)
|
|
||||||
|
|
||||||
obj/%.o: %.c
|
|
||||||
$(info [CC] $@)
|
|
||||||
@mkdir -p $(@D)
|
|
||||||
@$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<
|
|
||||||
|
|
||||||
obj/%.o: obj/%.c
|
|
||||||
$(info [CC] $@)
|
|
||||||
@mkdir -p $(@D)
|
|
||||||
@$(CC) -c $(CPPFLAGS) $(CFLAGS) -o $@ $<
|
|
||||||
|
|
||||||
# Include the generated dependency files.
|
|
||||||
# Which creates rules for all dependencies,
|
|
||||||
# as a result updating an .o file when a .h is updated.
|
|
||||||
-include $(OBJ:%.o=%.d)
|
|
||||||
-include $(TOBJ:%.o=%.d)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user