mirror of
https://github.com/thepigeongenerator/tetris_clone
synced 2026-02-08 07:33:34 +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:
|
SHELL = bash
|
||||||
# - make
|
.SHELLFLAGS = -O globstar -c
|
||||||
# - clang
|
NAME = tetris
|
||||||
# - bear (debug)
|
VERSION = 0.0.3
|
||||||
# - sdl2
|
DEBUG ?= 0
|
||||||
# - git bash (windows)
|
CC ?= cc
|
||||||
#
|
LD ?= ld
|
||||||
# project name = the workspace directory name
|
MARCH ?= $(shell uname -m)
|
||||||
NAME := $(shell basename $(PWD))
|
KERNEL ?= $(shell uname -s)
|
||||||
DEBUG ?= 0
|
|
||||||
ARCH ?= 0
|
|
||||||
|
|
||||||
# compiler settings
|
CFLAGS += -c -std=gnu99 -Wall -Wextra -Wpedantic -MMD -MP
|
||||||
CC := clang
|
LDFLAGS += -flto
|
||||||
STD := c17
|
|
||||||
CFLAGS := -m32 -Wall -Wextra -Wpedantic -Wno-pointer-arith
|
|
||||||
LDFLAGS := -m32 -lm
|
|
||||||
|
|
||||||
ifneq ($(DEBUG),0)
|
ifeq ($(KERNEL),)
|
||||||
CFLAGS += -g -Og -fsanitize=address,undefined
|
ISWIN := $(if $(filter $(OS),Windows_NT),1,0)
|
||||||
LDFLAGS += -fsanitize=address,undefined
|
ifeq ($(ISWIN),1)
|
||||||
PROF := dbg
|
KERNEL = mingw
|
||||||
|
MARCH = x86_64
|
||||||
else
|
else
|
||||||
CFLAGS += -DNDEBUG -O2 -Werror
|
MARCH := $(shell uname -m)
|
||||||
PROF := rel
|
KERNEL := $(shell uname -s | tr '[:upper:]' '[:lower:]')
|
||||||
endif
|
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
|
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
|
endif
|
||||||
|
ifeq ($(MARCH),)
|
||||||
|
$(error must also set MARCH when manually setting KERNEL)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifneq ($(ARCH),0)
|
ifeq ($(DEBUG),1)
|
||||||
# dirs
|
PROF = dbg
|
||||||
DIR_BIN := bin/$(ARCH)/$(PROF)
|
CFLAGS += -UNDEBUG -Og -g -Wextra -Wpedantic
|
||||||
DIR_OBJ := obj/$(ARCH)/$(PROF)
|
CFLAGS += $(if $(filter 1,$(ISWIN)),,-fsanitize=address,undefined) -ftrapv
|
||||||
TARGET := $(DIR_BIN)/$(NAME)$(EXT)
|
LDFLAGS += $(if $(filter 1,$(ISWIN)),,-fsanitize=address,undefined) -ftrapv
|
||||||
|
else
|
||||||
# source files
|
PROF = rel
|
||||||
SRC := $(wildcard src/*.c) $(wildcard src/**/*.c) $(wildcard src/**/**/*.c) $(wildcard src/**/**/**/*.c) $(wildcard src/**/**/**/**/*.c)
|
CFLAGS += -DNDEBUG -O3
|
||||||
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
|
|
||||||
endif
|
endif
|
||||||
|
|
||||||
define wr_colour
|
CFLAGS += $(shell pkg-config --cflags glfw3) -Ilib/glad/include
|
||||||
@printf '\033[%sm%s\033[0m\n' $(2) $(1)
|
LDFLAGS += $(shell pkg-config --libs glfw3) -lm
|
||||||
endef
|
|
||||||
|
|
||||||
# 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
|
run: compile
|
||||||
cd $(dir $(TARGET)) && ./$(notdir $(TARGET))
|
$(BIN)
|
||||||
compile: compile_commands $(TARGET) $(ASSETS)
|
|
||||||
|
|
||||||
.NOTPARALLEL:
|
.PHONY:
|
||||||
|
compile: $(BIN)
|
||||||
|
|
||||||
|
.PHONY .NOTPARALLEL:
|
||||||
clean:
|
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}
|
$(BIN): $(OBJ)
|
||||||
@$(CC) -o $(TARGET) $^ $(LDFLAGS)
|
$(info [CC/LD] $@)
|
||||||
@$(call wr_colour,"current profile: '$(PROF)'",93)
|
@mkdir -p $(@D)
|
||||||
|
@$(CC) -o $@ $^ $(LDFLAGS)
|
||||||
|
|
||||||
# create .o and .d files
|
$(DIR_OBJ)/%.o: %.c
|
||||||
$(DIR_OBJ)/%.o: src/%.c
|
$(info [CC] $@)
|
||||||
@$(call wr_colour,"compiling $(notdir $@) from $(notdir $<)",92)
|
@mkdir -p $(@D)
|
||||||
@mkdir -p ${@D}
|
@$(CC) $(CFLAGS) -o $@ $<
|
||||||
@$(CC) $(CFLAGS) -c -MD -MP -std=$(STD) -x c -o $@ $<
|
|
||||||
|
|
||||||
# copy assets
|
$(DIR_OBJ)/res/%.o: res/%
|
||||||
$(DIR_BIN)/%: assets/%
|
$(info [LD] $@)
|
||||||
@mkdir -p ${@D}
|
@mkdir -p $(@D)
|
||||||
@cp -v $< $@
|
@$(LD) -r -b binary -o $@ $<
|
||||||
|
|
||||||
# update compile commands if the makefile has been updated (for linting)
|
.PHONY: x86_64-linux-gnu-gcc x86_64-w64-mingw32-gcc
|
||||||
compile_commands: # default, empty rule
|
x86_64-linux-gnu-gcc:; $(MAKE) $(CALL) $(MAKEFLAGS) CC=$@ MARCH=x86_64 KERNEL=linux
|
||||||
ifneq ($(shell which bear),)
|
x86_64-w64-mingw32-gcc:; $(MAKE) $(CALL) $(MAKEFLAGS) CC=$@ MARCH=x86_64 KERNEL=mingw
|
||||||
ifneq ($(COMPILE_COMMANDS),)
|
|
||||||
ifeq ($(NOCMDS),)
|
|
||||||
.NOTPARALLEL .PHONY:
|
|
||||||
compile_commands: $(COMPILE_COMMANDS)
|
|
||||||
@[ "$(readlink compile_commands.json)" != "$<" ] && ln -sf $< compile_commands.json
|
|
||||||
|
|
||||||
.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)
|
-include $(DEP)
|
||||||
|
|||||||
Reference in New Issue
Block a user