-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMakefile
144 lines (118 loc) · 6.2 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# @author Qu1nel (Иван Ковач, "Ivan Kovach") 2023
########################################################## Static Library ##
##
## This Makefile template is used for static library projects .a for C,
## including the '.build' folder and 'include' folder for keep header files.
##
#################################################################### DESC ##
#----------------------------------------------------------------------------#
# VARIABLES #
#----------------------------------------------------------------------------#
# TARGET - Name of result static library #
# TARGET_NAME - Full-fledged name of static library #
# #
# SRC_DIR - Name for source directory #
# INCLD_DIR - Nmae for include directory #
# BUILD_DIR - Name for build directory #
# OBJ_DIR - Name for object files in build directory #
# #
# TARGET - Main target in EXEC_DIR #
# #
# OBJECTS - Object files #
# DEPS - Dependency files #
# #
# CC - Compiler #
# CFLAGS - Compiler flags #
# CPPFLAGS - Preprocessor flags #
# AR - Object file archiver #
# ARFLAGS - Archiver flags #
# #
# RM - Force remove #
# MAKEFLAGS - Make flags #
# DIR_DUP - Duplicate directory tree #
#----------------------------------------------------------------------------#
LIBNAME := Print
TARGET_NAME := $(LIBNAME).lib
SRC_DIR := src
INCLD_DIR := include
BUILD_DIR := build
OBJ_DIR := $(BUILD_DIR)/objects
TEST_DIR := test
TARGET := $(BUILD_DIR)/$(TARGET_NAME)
OBJECTS := $(patsubst %.c, $(OBJ_DIR)/%.o, $(patsubst $(SRC_DIR)/%, %, $(shell find $(SRC_DIR) -type f -name '*.c' 2>/dev/null)))
SOURCES := $(shell find $(SRC_DIR) -type f -name '*.c' 2>/dev/null)
DEPS := $(OBJECTS:.o=.d)
CC := clang
CFLAGS := -std=c17 -Wall -Werror -Wshadow -Wvla -Walloca -Wundef -Wfloat-equal \
-Wstrict-prototypes -Wconversion -Wswitch-enum -Wswitch-default -Wimplicit-fallthrough \
-Wunreachable-code -Wformat=2 -Wparentheses -Wmisleading-indentation -Wpedantic -pedantic
CPPFLAGS := -I$(INCLD_DIR) -I$(TEST_DIR) -MMD -MP
LDFLAGS :=
AR := ar
ARFLAGS := -r -c -s
RM := rm -f
MAKEFLAGS += --no-print-directory
DIR_DUP = mkdir -p $(@D)
#--------------------------------------------------------------------------------------------------#
# RECIPES #
#--------------------------------------------------------------------------------------------------#
# all - Alias for build (DEFAULT GOAL) #
# $(TARGET) - Linking *.o + *.a -> static library #
# %.o - Compilation source files *.c -> *.o #
# build - Build library #
# re - Rebuild library #
# example - Build excample file for testing
# clean - Remove all *.o and *.d #
# fcrean - Remove all *.o and *.d + library #
# #
# help - Show help about commands #
# info-% - Rule will execute a make % command with --dry-run #
# print-% - That works like print-<rule> will print the value of an arbitrary variable #
#--------------------------------------------------------------------------------------------------#
.PHONY: all
all: build
$(TARGET): $(OBJECTS)
$(AR) $(ARFLAGS) $(TARGET) $(OBJECTS)
$(info [BUILD] $@ : $(AR) $(ARFLAGS) $(TARGET) $(OBJECTS))
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
$(DIR_DUP)
$(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS)
$(info [COMPILED] $@ : $(CC) -c $< -o $@ $(CFLAGS) $(CPPFLAGS))
-include $(DEPS)
.PHONY: test
test: ## Testing framework
@mkdir -p $(TEST_DIR)
gcc $(TEST_DIR)/tests.c $(SOURCES) $(CPPFLAGS) -std=c17 -o testing
$(info [COMPILED] $@ : gcc $(TEST_DIR)/tests.c $(SOURCES) $(CPPFLAGS) -std=c17 -o test)
./testing
rm ./testing
.PHONY: build
build: $(TARGET) ## Build library
$(shell ranlib $(TARGET))
$(info [RABLIB] ralib $(TARGET))
.PHONY: re
re: ## Rebuild library
$(MAKE) fclean
$(MAKE) all
.PHONY: example
example: example.c ## Builds a file using the library from the readme
$(CC) $(CFLAGS) -L$(BUILD_DIR) $(CPPFLAGS) -o main $< -l$(LIBNAME)
$(info [COMPILED] main: $(CC) $(CFLAGS) -L$(BUILD_DIR) $(CPPFLAGS) -o main $< -l$(LIBNAME))
rm main.d
.PHONY: clean
clean: ## Remove all object and dep files
$(info $(RM) $(OBJECTS) $(DEPS))
$(RM) $(OBJECTS) $(DEPS)
.PHONY: fclean
fclean: clean ## Remove library + all object files + dep files
$(info $(RM) $(TARGET))
$(RM) $(TARGET)
.PHONY: help
help: ## Show this output, i.e. help to use the commands
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' Makefile | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
info-%:
@make --dry-run --always-make $* | grep -v "info"
print-%:
@$(info '$*'='$($*)')
.DEFAULT_GOAL := help
.SILENT: