mirror of
https://github.com/thepigeongenerator/cpusetcores.git
synced 2025-12-16 21:45:46 +01:00
update makefile
makefile is now more compiler-agnostic and consistent with how one expects a makefile to be built.
This commit is contained in:
80
makefile
80
makefile
@@ -1,72 +1,32 @@
|
|||||||
# project name = the workspace directory name
|
SHELL = bash -e -o pipefail -O globstar
|
||||||
NAME := $(shell basename $(PWD))
|
|
||||||
|
|
||||||
# compiler settings
|
NAME = cpusetcores
|
||||||
CC := clang
|
VERSION = 0.0.2
|
||||||
STD := c17
|
|
||||||
LANG = c
|
|
||||||
CFLAGS := -target x86_64-pc-linux-gnu -Wall -Wextra -Wpedantic -Wno-pointer-arith
|
|
||||||
LDFLAGS := -lc
|
|
||||||
DEBUG ?= 0
|
DEBUG ?= 0
|
||||||
|
CC ?= cc
|
||||||
|
|
||||||
ifeq ($(DEBUG),1)
|
CFLAGS = -c -std=gnu99 -Wall -Wextra -Wpedantic -MMD -MP
|
||||||
CFLAGS += -DDEBUG -fsanitize=address,undefined -g -Og
|
LFLAGS = -flto
|
||||||
PROF := dbg
|
|
||||||
else
|
|
||||||
REL_FLAGS += -O2
|
|
||||||
PROF := rel
|
|
||||||
endif
|
|
||||||
|
|
||||||
# dirs
|
SRC := $(wildcard src/*.c) $(wildcard src/**/*.c)
|
||||||
DIR_BIN := bin
|
OBJ := $(SRC:%.c=obj/%.o)
|
||||||
DIR_OBJ := obj
|
BIN := bin/$(NAME)
|
||||||
DIR_BUILD := $(DIR_BIN)/$(PROF)
|
|
||||||
DIR := $(DIR_BIN)/$(PROF) $(DIR_OBJ)/$(PROF)
|
|
||||||
|
|
||||||
# source files
|
.PHONY:
|
||||||
SRC := $(wildcard src/*.c) $(wildcard src/**/*.c) $(wildcard src/**/**/*.c) $(wildcard src/**/**/**/*.c) $(wildcard src/**/**/**/**/*.c)
|
compile: $(BIN)
|
||||||
|
|
||||||
# output locations
|
$(BIN): $(OBJ)
|
||||||
OBJ := $(patsubst src/%,$(DIR_OBJ)/$(PROF)/%,$(SRC:.c=.o))
|
$(info [CC/LD] $@)
|
||||||
DEP := $(OBJ:.o=.d)
|
@mkdir -p $(@D)
|
||||||
TARGET := $(DIR_BUILD)/$(NAME)
|
@$(CC) -o $@ $^ $(LDFLAGS)
|
||||||
|
|
||||||
define wr_colour
|
obj/%.o: %.c
|
||||||
@echo -e "\033[$(2)m$(1)\033[0m"
|
$(info [CC] $@)
|
||||||
endef
|
@mkdir -p $(@D)
|
||||||
|
@$(CC) $(CFLAGS) -o $@ $<
|
||||||
|
|
||||||
compile: compile_commands.json $(DIR) $(TARGET)
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(DIR_BIN) $(DIR_OBJ) compile_commands.json
|
rm -rv obj/ bin/
|
||||||
|
|
||||||
# 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)
|
|
||||||
@$(CC) -o $(TARGET) $^ $(CFLAGS) $(LDFLAGS)
|
|
||||||
@$(call wr_colour,"current profile: '$(PROF)'",93)
|
|
||||||
|
|
||||||
# create .o and .d files
|
|
||||||
$(DIR_OBJ)/$(PROF)/%.o: src/%.c
|
|
||||||
@$(call wr_colour,"compiling $(notdir $@) from $(notdir $<)",92)
|
|
||||||
@mkdir -p $(dir $@)
|
|
||||||
@$(CC) -o $@ -MD -MP -c $< $(CFLAGS) -std=$(STD) -x $(LANG) -Wno-unused-command-line-argument
|
|
||||||
|
|
||||||
# create directories
|
|
||||||
$(DIR):
|
|
||||||
mkdir -p $@
|
|
||||||
|
|
||||||
# update compile commands if the makefile has been updated (for linting)
|
|
||||||
ifeq ($(DEBUG),1)
|
|
||||||
compile_commands.json: makefile
|
|
||||||
$(MAKE) clean
|
|
||||||
@touch compile_commands.json
|
|
||||||
bear -- make compile
|
|
||||||
else
|
|
||||||
compile_commands.json:
|
|
||||||
endif
|
|
||||||
|
|
||||||
# include the dependencies
|
# include the dependencies
|
||||||
-include $(DEP)
|
-include $(DEP)
|
||||||
|
|||||||
Reference in New Issue
Block a user