mirror of
https://github.com/thepigeongenerator/tetris_clone.git
synced 2025-12-17 05:55:46 +01:00
rewrite makefile
dropping `SDL2` requirement, opting for `glfw3` instead.
This commit is contained in:
159
makefile
159
makefile
@@ -1,115 +1,86 @@
|
||||
# dependencies:
|
||||
# - make
|
||||
# - clang
|
||||
# - bear (debug)
|
||||
# - sdl2
|
||||
# - git bash (windows)
|
||||
#
|
||||
# project name = the workspace directory name
|
||||
NAME := $(shell basename $(PWD))
|
||||
DEBUG ?= 0
|
||||
ARCH ?= 0
|
||||
SHELL = bash
|
||||
.SHELLFLAGS = -O globstar -c
|
||||
NAME = tetris
|
||||
VERSION = 0.0.3
|
||||
DEBUG ?= 0
|
||||
CC ?= cc
|
||||
LD ?= ld
|
||||
MARCH ?= $(shell uname -m)
|
||||
KERNEL ?= $(shell uname -s)
|
||||
|
||||
# compiler settings
|
||||
CC := clang
|
||||
STD := c17
|
||||
CFLAGS := -m32 -Wall -Wextra -Wpedantic -Wno-pointer-arith
|
||||
LDFLAGS := -m32 -lm
|
||||
CFLAGS += -c -std=gnu99 -Wall -Wextra -Wpedantic -MMD -MP
|
||||
LDFLAGS += -flto
|
||||
|
||||
ifneq ($(DEBUG),0)
|
||||
CFLAGS += -g -Og -fsanitize=address,undefined
|
||||
LDFLAGS += -fsanitize=address,undefined
|
||||
PROF := dbg
|
||||
ifeq ($(KERNEL),)
|
||||
ISWIN := $(if $(filter $(OS),Windows_NT),1,0)
|
||||
ifeq ($(ISWIN),1)
|
||||
KERNEL = mingw
|
||||
MARCH = x86_64
|
||||
else
|
||||
CFLAGS += -DNDEBUG -O2 -Werror
|
||||
PROF := rel
|
||||
MARCH := $(shell uname -m)
|
||||
KERNEL := $(shell uname -s | tr '[:upper:]' '[:lower:]')
|
||||
endif
|
||||
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
ifeq ($(ARCH),linux-x86)
|
||||
CFLAGS += -target x86_64-pc-linux-gnu $(shell pkg-config --cflags sdl2 SDL2_ttf)
|
||||
LDFLAGS += -target x86_64-pc-linux-gnu $(shell pkg-config --libs sdl2 SDL2_ttf)
|
||||
else ifeq ($(ARCH),win-x86)
|
||||
CFLAGS += -target x86_64-pc-windows-gnu
|
||||
LDFLAGS += -target x86_64-pc-windows-gnu -fuse-ld=lld
|
||||
EXT := .exe
|
||||
else
|
||||
$(error you must set the ARCH environment variable to one of these: 'linux-x86' 'win-x86')
|
||||
ISWIN := $(if $(filter $(KERNEL),mingw),1,0)
|
||||
endif
|
||||
ifeq ($(MARCH),)
|
||||
$(error must also set MARCH when manually setting KERNEL)
|
||||
endif
|
||||
|
||||
ifneq ($(ARCH),0)
|
||||
# dirs
|
||||
DIR_BIN := bin/$(ARCH)/$(PROF)
|
||||
DIR_OBJ := obj/$(ARCH)/$(PROF)
|
||||
TARGET := $(DIR_BIN)/$(NAME)$(EXT)
|
||||
|
||||
# source files
|
||||
SRC := $(wildcard src/*.c) $(wildcard src/**/*.c) $(wildcard src/**/**/*.c) $(wildcard src/**/**/**/*.c) $(wildcard src/**/**/**/**/*.c)
|
||||
OBJ := $(patsubst src/%,$(DIR_OBJ)/%,$(SRC:.c=.o))
|
||||
DEP := $(OBJ:.o=.d)
|
||||
SRC_ASSETS := $(wildcard assets/*)
|
||||
ASSETS := $(patsubst assets/%,$(DIR_BIN)/%,$(SRC_ASSETS))
|
||||
|
||||
COMPILE_COMMANDS := $(DIR_OBJ)/compile_commands.json
|
||||
ifeq ($(DEBUG),1)
|
||||
PROF = dbg
|
||||
CFLAGS += -UNDEBUG -Og -g -Wextra -Wpedantic
|
||||
CFLAGS += $(if $(filter 1,$(ISWIN)),,-fsanitize=address,undefined) -ftrapv
|
||||
LDFLAGS += $(if $(filter 1,$(ISWIN)),,-fsanitize=address,undefined) -ftrapv
|
||||
else
|
||||
PROF = rel
|
||||
CFLAGS += -DNDEBUG -O3
|
||||
endif
|
||||
|
||||
define wr_colour
|
||||
@printf '\033[%sm%s\033[0m\n' $(2) $(1)
|
||||
endef
|
||||
CFLAGS += $(shell pkg-config --cflags glfw3) -Ilib/glad/include
|
||||
LDFLAGS += $(shell pkg-config --libs glfw3) -lm
|
||||
|
||||
# compiles and execute the binary
|
||||
SRC := $(shell echo src/**/*.c) lib/glad/src/gl.c
|
||||
RES := $(shell echo res/**.glsl)
|
||||
|
||||
NAME += $(if $(filter 1,$(ISWIN)),.exe,)
|
||||
DIR_BIN := bin/$(MARCH)-$(KERNEL)/$(VERSION)/$(PROF)
|
||||
DIR_OBJ := obj/$(MARCH)-$(KERNEL)/$(VERSION)/$(PROF)
|
||||
|
||||
BIN := $(DIR_BIN)/$(NAME)
|
||||
OBJ := $(SRC:%.c=$(DIR_OBJ)/%.o) $(RES:%=$(DIR_OBJ)/%.o)
|
||||
DEP := $(OBJ:%.o=%.d)
|
||||
|
||||
.PHONY:
|
||||
run: compile
|
||||
cd $(dir $(TARGET)) && ./$(notdir $(TARGET))
|
||||
compile: compile_commands $(TARGET) $(ASSETS)
|
||||
$(BIN)
|
||||
|
||||
.NOTPARALLEL:
|
||||
.PHONY:
|
||||
compile: $(BIN)
|
||||
|
||||
.PHONY .NOTPARALLEL:
|
||||
clean:
|
||||
rm -rf obj/ bin/ compile_commands.json
|
||||
@[ -d obj/ ] && rm -rv obj/ || true
|
||||
@[ -d bin/ ] && rm -rv bin/ || true
|
||||
|
||||
# create the binary (linking step)
|
||||
$(TARGET): $(OBJ)
|
||||
@$(call wr_colour,"CC: '$(CC)'",94)
|
||||
@$(call wr_colour,"CFLAGS: '$(CFLAGS)'",94)
|
||||
@$(call wr_colour,"LDFLAGS: '$(LDFLAGS)'",94)
|
||||
@$(call wr_colour,"linking to: '$@'",92)
|
||||
|
||||
@mkdir -p ${@D}
|
||||
@$(CC) -o $(TARGET) $^ $(LDFLAGS)
|
||||
@$(call wr_colour,"current profile: '$(PROF)'",93)
|
||||
$(BIN): $(OBJ)
|
||||
$(info [CC/LD] $@)
|
||||
@mkdir -p $(@D)
|
||||
@$(CC) -o $@ $^ $(LDFLAGS)
|
||||
|
||||
# create .o and .d files
|
||||
$(DIR_OBJ)/%.o: src/%.c
|
||||
@$(call wr_colour,"compiling $(notdir $@) from $(notdir $<)",92)
|
||||
@mkdir -p ${@D}
|
||||
@$(CC) $(CFLAGS) -c -MD -MP -std=$(STD) -x c -o $@ $<
|
||||
$(DIR_OBJ)/%.o: %.c
|
||||
$(info [CC] $@)
|
||||
@mkdir -p $(@D)
|
||||
@$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
# copy assets
|
||||
$(DIR_BIN)/%: assets/%
|
||||
@mkdir -p ${@D}
|
||||
@cp -v $< $@
|
||||
$(DIR_OBJ)/res/%.o: res/%
|
||||
$(info [LD] $@)
|
||||
@mkdir -p $(@D)
|
||||
@$(LD) -r -b binary -o $@ $<
|
||||
|
||||
# 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),)
|
||||
.NOTPARALLEL .PHONY:
|
||||
compile_commands: $(COMPILE_COMMANDS)
|
||||
@[ "$(readlink compile_commands.json)" != "$<" ] && ln -sf $< compile_commands.json
|
||||
.PHONY: x86_64-linux-gnu-gcc x86_64-w64-mingw32-gcc
|
||||
x86_64-linux-gnu-gcc:; $(MAKE) $(CALL) $(MAKEFLAGS) CC=$@ MARCH=x86_64 KERNEL=linux
|
||||
x86_64-w64-mingw32-gcc:; $(MAKE) $(CALL) $(MAKEFLAGS) CC=$@ MARCH=x86_64 KERNEL=mingw
|
||||
|
||||
.NOTPARALLEL:
|
||||
$(COMPILE_COMMANDS): makefile
|
||||
@$(call wr_colour,"regenerating compile_commands.json thus recompiling.",93)
|
||||
@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
|
||||
|
||||
# disable implicit rules
|
||||
.SUFFIXES:
|
||||
|
||||
# include the dependencies
|
||||
-include $(DEP)
|
||||
|
||||
Reference in New Issue
Block a user