aboutsummaryrefslogtreecommitdiff
path: root/Makefile
blob: 3a785b21c7e1aece530adf9900b5e754c3eb8faf (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# GENERAL VARIABLES
NAME            ?= polecat
VERSION		?= 0.1.9
TARGET          ?= release
DEBUG           := 0
ifeq ($(TARGET),debug)
	DEBUG       := 0
endif

PRETTY_OUTPUT   := 1
STATIC          := 0

# COMMAND VARIABLES
DOXYGEN		:= doxygen
RM              := rm -rf
MKDIR           := mkdir -p
PKGCONFIG       := pkg-config

ifndef HOSTPLATFORM
	HOSTPLATFORM := UNKNOWN
	ifeq ($(findstring Windows,$(OS)),Windows)
		HOSTPLATFORM := WINDOWS
	else
		uname := $(strip $(shell uname -s))
		ifeq ($(findstring Linux,$(uname)),Linux)
			HOSTPLATFORM := LINUX
		else ifeq ($(findstring MINGW,$(uname)),MINGW)
			HOSTPLATFORM := WINDOWS
		else ifeq ($(findstring MSYS,$(uname)),MSYS)
			HOSTPLATFORM := WINDOWS
		endif
	endif
endif

ifndef PLATFORM
	PLATFORM := $(HOSTPLATFORM)
endif

# CROSS COMPILATION
CROSS		:=
OBJ_EXT         := o

ifeq ($(PLATFORM),WINDOWS)
	OUT_EXT     := .exe
	STATIC      := 1
endif

# DIRECTORIES
BUILD_DIR	:= build
BIN_DIR         := $(BUILD_DIR)
OBJ_DIR         := $(BUILD_DIR)/$(TARGET)

SRC_DIR         := src

FILES           := cmake CMakeLists.txt LICENSE Makefile polecat.spec README.md ${SRC_DIR}

# FLAGS
COMMONFLAGS     :=
ifneq ($(STATIC), 0)
	COMMONFLAGS += -static
endif
ifneq ($(DEBUG), 1)
	COMMONFLAGS += -O2
else
	COMMONFLAGS += -ggdb3 -Og
endif
CFLAGS          := $(COMMONFLAGS) $(shell $(PKGCONFIG) --cflags json-c) $(shell $(PKGCONFIG) --cflags libarchive) $(shell $(PKGCONFIG) --cflags libcurl)  
WARNFLAGS       := -Wall -Wextra -pedantic -Wconversion -Wshadow -Wstrict-aliasing  -Winit-self -Wcast-align -Wpointer-arith -Wmissing-declarations -Wmissing-include-dirs -Wno-unused-parameter -Wuninitialized
LIBS            := $(shell $(PKGCONFIG) --libs json-c) $(shell $(PKGCONFIG) --libs libarchive) $(shell $(PKGCONFIG) --libs libcurl) 
BINFLAGS        := $(LIBS)
INCLUDEFLAGS    := 
DEFINES         := -DNAME=\"${NAME}\" -DVERSION=\"${VERSION}\"
ifneq ($(DEBUG), 1)
	DEFINES     += -DNDEBUG
endif

# OBJECT FILES
CC_SRC_FILES    := $(wildcard $(SRC_DIR)/*.c)

OBJ_FILES       := $(patsubst $(SRC_DIR)/%.c, $(OBJ_DIR)/%.$(OBJ_EXT), $(CC_SRC_FILES))


.PHONY: default
default: $(BIN_DIR)/$(NAME)$(OUT_EXT)

.PHONY: all
all: $(BIN_DIR)/$(NAME)$(OUT_EXT) docs

$(BIN_DIR):
	${MKDIR} $@

$(OBJ_DIR):
	${MKDIR} $@

$(BIN_DIR)/$(NAME)$(OUT_EXT): $(OBJ_FILES) | $(BIN_DIR)
	${LINK_STATUS}
	${RECIPE_IF} ${CROSS}${CC}    -o$@ $^ ${CFLAGS} ${WARNFLAGS} ${STATICFLAGS} ${INCLUDEFLAGS} ${DEFINES} ${BINFLAGS} ${RECIPE_RESULT_LINK}


$(OBJ_DIR)/%.$(OBJ_EXT): $(SRC_DIR)/%.c | $(OBJ_DIR)
	${COMPILE_STATUS}
	${RECIPE_IF} ${CROSS}${CC} -c -o$@ $< ${CFLAGS} ${WARNFLAGS} ${STATICFLAGS} ${INCLUDEFLAGS} ${DEFINES} ${RECIPE_RESULT_COMPILE}

.PHONY: cmake
cmake: $(BIN_DIR)
	cmake -S "." -B "${BIN_DIR}"
	cmake --build "${BIN_DIR}"

.PHONY: clean clean-bin-dir clean-bin clean-obj clean-tar
clean: clean-bin clean-obj clean-tar

clean-bin-dir:
	-${RM} ${BIN_DIR}

clean-bin:
	-${RM} ${BIN_DIR}/${NAME}${OUT_EXT}

clean-obj:
	-${RM} ${OBJ_DIR}

clean-tar:
	-${RM} ${NAME}.tar

.PHONY: docs
docs: Doxyfile
	-${DOXYGEN}

.PHONY: loc
loc:
	-find ${SRC_DIR} -type f | xargs wc -l

.PHONY: loc
tar: ${NAME}.tar

.PHONY: dist
dist: tar

${NAME}.tar:
	${ARCHIVE_STATUS}
	$(RECIPE_IF) tar -cf $@ ${FILES} $(RECIPE_RESULT_ARCHIVE)


ifeq ($(PRETTY_OUTPUT),1)
.SILENT:
RECIPE_IF = if
COMPILE_STATUS = printf "\033[K\033[0mBuilding object \033[1m$@\033[0m...\033[0m\r"
COMPILE_OK = printf "\033[K\033[0;32mBuilt object \033[1;32m$@\033[0;32m \033[0m\n"
COMPILE_FAILED = printf "\033[K\033[0;31mFailed building \033[1;31m$@\033[0;31m from\033[0m \033[1;31m$<\033[0;31m!\033[0m\n"; exit 1
RECIPE_RESULT_COMPILE = ; then $(COMPILE_OK); else $(COMPILE_FAILED); fi
ARCHIVE_STATUS = printf "\033[K\033[0mCreating library archive \033[1m$@\033[0m...\033[0m\r"
ARCHIVE_OK = printf "\033[K\033[0;32mCreated library archive \033[1;32m$@\033[0;32m \033[0m\n"
ARCHIVE_FAILED = printf "\033[K\033[0;31mFailed creating library archive \033[1;31m$@\033[0;31m from\033[0m \033[1;31m$<\033[0;31m!\033[0m\n"; exit 1
RECIPE_RESULT_ARCHIVE = ; then $(ARCHIVE_OK); else $(ARCHIVE_FAILED); fi
LINK_STATUS = printf "\033[K\033[0;0mLinking \033[1m$@\033[0;0m...\033[0m\r"
LINK_OK = printf "\033[K\033[0;32mLinked \033[1;32m$@\033[0;32m \033[0m\n"
LINK_FAILED = printf "\033[K\033[0;31mFailed linking \033[1;31m$@\033[0;31m!\nIf the build options, environment, or system packages have changed, run \'\033[1;31mmake clean\033[0;31m\' and try again.\033[0m\n"; exit 1
RECIPE_RESULT_LINK = ; then $(LINK_OK); else $(LINK_FAILED); fi
else
RECIPE_IF =
BUILD_STARTED =
COMPILE_STATUS =
COMPILE_OK = true
COMPILE_FAILED = false; exit 1
RECIPE_RESULT_COMPILE =
ARCHIVE_STATUS =
ARCHIVE_OK = true
ARCHIVE_FAILED = false; exit 1
RECIPE_RESULT_ARCHIVE =
LINK_STATUS =
LINK_OK = true
LINK_FAILED = false; exit 1
RECIPE_RESULT_LINK =
endif