-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
183 lines (136 loc) · 7.21 KB
/
Makefile
File metadata and controls
183 lines (136 loc) · 7.21 KB
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
174
175
176
177
178
179
180
181
182
183
# Compiler
CC = gcc
QUIET = @ # remove this @ for verbose output
# Source files
SRCS = graph.c reduction.c pqueue.c greedy.c dynamic_array.c heuristic_solver.c weighted_sampling_tree.c
# Compiler flags
CFLAGS = -std=c17 -D_XOPEN_SOURCE=700 -W -Wall -Wextra -MMD -MP
PEDANTIC_FLAGS = -Werror -Wpedantic -Wshadow -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wswitch-default -Wcast-align=strict -Wbad-function-cast -Wstrict-overflow=4 -Winline -Wundef -Wnested-externs -Wunreachable-code -Wlogical-op -Wfloat-equal -Wredundant-decls -Wold-style-definition -Wwrite-strings -Wformat=2 -Wconversion -Wno-error=unused-parameter -Wno-error=inline -Wno-error=unreachable-code -Wno-error=unused-function -Wno-error=unused-variable -Wno-error=missing-prototypes
SANITIZE_FLAGS = -fanalyzer -fsanitize=address -fsanitize=undefined -fsanitize=leak -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds-strict -fsanitize=alignment -fsanitize=object-size -g
OPTIMIZATION_FLAGS = -Ofast -fno-signed-zeros -fipa-pta -fipa-reorder-for-locality
CFLAGS_RELEASE = $(CFLAGS) $(OPTIMIZATION_FLAGS) -DNDEBUG
CFLAGS_STRICT = $(CFLAGS) $(OPTIMIZATION_FLAGS) -DNDEBUG $(PEDANTIC_FLAGS)
CFLAGS_LOG = $(CFLAGS) $(OPTIMIZATION_FLAGS) -DNDEBUG $(PEDANTIC_FLAGS) -DDEBUG_LOG
CFLAGS_DEBUG = $(CFLAGS) $(OPTIMIZATION_FLAGS) $(PEDANTIC_FLAGS) $(SANITIZE_FLAGS) -DDEBUG_LOG
BUILD_DIR = build
DIR_RELEASE = $(BUILD_DIR)/release
DIR_STRICT = $(BUILD_DIR)/strict
DIR_LOG = $(BUILD_DIR)/log
DIR_DEBUG = $(BUILD_DIR)/debug
TARGET_RELEASE = $(DIR_RELEASE)/heuristic_solver
TARGET_STRICT = $(DIR_STRICT)/heuristic_solver
TARGET_LOG = $(DIR_LOG)/heuristic_solver
TARGET_DEBUG = $(DIR_DEBUG)/heuristic_solver
# Object files
OBJS_RELEASE = $(SRCS:%.c=$(DIR_RELEASE)/obj/%.o)
OBJS_STRICT = $(SRCS:%.c=$(DIR_STRICT)/obj/%.o)
OBJS_LOG = $(SRCS:%.c=$(DIR_LOG)/obj/%.o)
OBJS_DEBUG = $(SRCS:%.c=$(DIR_DEBUG)/obj/%.o)
# Dependency files
DEPS_RELEASE = $(OBJS_RELEASE:.o=.d)
DEPS_STRICT = $(OBJS_STRICT:.o=.d)
DEPS_LOG = $(OBJS_LOG:.o=.d)
DEPS_DEBUG = $(OBJS_DEBUG:.o=.d)
# For the preliminary test: choose which version you want
GREEDY_OPTIONS := STANDARD VOTE RANDOM
DECONSTRUCTION_OPTIONS := RANDOM LOCAL MIXED
GREEDY_DEFAULT := VOTE
DECONSTRUCTION_DEFAULT := MIXED
GREEDY ?= $(GREEDY_DEFAULT)
DECONSTRUCTION ?= $(DECONSTRUCTION_DEFAULT)
.DEFAULT_GOAL := release
ifeq ($(MAKECMDGOALS),)
$(info $(shell printf "No target specified. The default target is \033[1m%s\033[0m" "$(.DEFAULT_GOAL)"))
endif
TARGETS := $(if $(MAKECMDGOALS),$(MAKECMDGOALS),$(.DEFAULT_GOAL))
ifneq ($(filter-out help clean,$(TARGETS)),) # only show if there are targets besides help and clean
# Print default if user didn't set it explicitly
ifeq ($(origin GREEDY),file)
$(info $(shell printf "Defaulting to \033[1m%s\033[0m" "GREEDY=$(GREEDY_DEFAULT)"))
endif
ifeq ($(origin DECONSTRUCTION),file)
$(info $(shell printf "Defaulting to \033[1m%s\033[0m" "DECONSTRUCTION=$(DECONSTRUCTION_DEFAULT)"))
endif
ifeq ($(filter $(GREEDY),$(GREEDY_OPTIONS)),)
$(error $(shell printf "\033[1;31mInvalid GREEDY=%s. Must be one of: %s\033[0m" "$(GREEDY)" "$(GREEDY_OPTIONS)"))
endif
ifeq ($(filter $(DECONSTRUCTION),$(DECONSTRUCTION_OPTIONS)),)
$(error $(shell printf "\033[1;31mInvalid DECONSTRUCTION=%s. Must be one of: %s\033[0m" "$(DECONSTRUCTION)" "$(DECONSTRUCTION_OPTIONS)"))
endif
# always run this to update src/build_options if necessary
_ := $(info $(shell \
tmp_file=$$(mktemp); \
printf "// This file is generated by the Makefile and should not be edited manually.\n" > $$tmp_file; \
printf "// For details on how to compile with different options, run 'make help'.\n\n" >> $$tmp_file; \
echo "#define GREEDY_$(GREEDY)" >> $$tmp_file; \
echo "#define DECONSTRUCTION_$(DECONSTRUCTION)" >> $$tmp_file; \
if [ ! -f src/build_options.h ] || ! cmp -s src/build_options.h $$tmp_file; then \
mv $$tmp_file src/build_options.h; \
echo "Updated src/build_options.h"; \
else \
rm $$tmp_file; \
echo "src/build_options.h is up to date"; \
fi \
))
endif
release: $(TARGET_RELEASE)
strict: $(TARGET_STRICT)
log: $(TARGET_LOG)
debug: $(TARGET_DEBUG)
all: help
# avoid duplication for targets that only differ in the C flags used
define COMPILE_RULE
$(TARGET_$(1)): $(OBJS_$(1))
@echo Linking $$@
$$(QUIET)$$(CC) $$(CFLAGS_$(1)) -o $$@ $$(OBJS_$(1))
@printf '\033[1;32mFinished successfully. The compiled executable can be found at %s\033[0m\n' '$$(TARGET_$(1))'
$(DIR_$(1))/obj/%.o: src/%.c | $(DIR_$(1))/obj
@echo Compiling $$<
$$(QUIET)$$(CC) $$(CFLAGS_$(1)) -c $$< -o $$@
endef
$(eval $(call COMPILE_RULE,RELEASE))
$(eval $(call COMPILE_RULE,STRICT))
$(eval $(call COMPILE_RULE,LOG))
$(eval $(call COMPILE_RULE,DEBUG))
$(DIR_RELEASE)/obj $(DIR_STRICT)/obj $(DIR_LOG)/obj $(DIR_DEBUG)/obj:
$(QUIET)mkdir -p $@
# Clean up the build files
clean:
$(QUIET)rm -f $(OBJS_RELEASE) $(DEPS_RELEASE) $(TARGET_RELEASE)
$(QUIET)rm -f $(OBJS_STRICT) $(DEPS_STRICT) $(TARGET_STRICT)
$(QUIET)rm -f $(OBJS_LOG) $(DEPS_LOG) $(TARGET_LOG)
$(QUIET)rm -f $(OBJS_DEBUG) $(DEPS_DEBUG) $(TARGET_DEBUG)
$(QUIET)rmdir --ignore-fail-on-non-empty $(DIR_RELEASE)/obj $(DIR_RELEASE) 2>/dev/null || true
$(QUIET)rmdir --ignore-fail-on-non-empty $(DIR_STRICT)/obj $(DIR_STRICT) 2>/dev/null || true
$(QUIET)rmdir --ignore-fail-on-non-empty $(DIR_LOG)/obj $(DIR_LOG) 2>/dev/null || true
$(QUIET)rmdir --ignore-fail-on-non-empty $(DIR_DEBUG)/obj $(DIR_DEBUG) 2>/dev/null || true
$(QUIET)rmdir --ignore-fail-on-non-empty $(BUILD_DIR) 2>/dev/null || true
SPACE := $() $()
COMMASPACE := $(), $()
GREEDY_OPTIONS_COMMASEPARATED := $(subst $(SPACE),$(COMMASPACE),$(GREEDY_OPTIONS))
DECONSTRUCTION_OPTIONS_COMMASEPARATED := $(subst $(SPACE),$(COMMASPACE),$(DECONSTRUCTION_OPTIONS))
help:
@printf "\033[1mAvailable targets:\033[0m\n"
@printf " \033[1;32mrelease\033[0m - Build release (default)\n"
@printf " \033[1;32mstrict\033[0m - Build with pedantic compiler warnings\n"
@printf " \033[1;32mlog\033[0m - Same as strict but enable logging\n"
@printf " \033[1;32mdebug\033[0m - Build debug with pedantic compiler warnings, assertions, sanitizers, and logging\n"
@printf " \033[1;32mclean\033[0m - Remove compiled objects, binaries, and temporary build files\n"
@printf " \033[1;32mhelp\033[0m - Print this help text\n"
@printf "\n"
@printf "\n"
@printf "\033[1mPreliminary test versions:\033[0m\n"
@printf " You can optionally specify which greedy algorithm and deconstruction approach to use\n"
@printf " by adding the following arguments:\n"
@printf "\n"
@printf " \033[1;32mGREEDY=\033[3m<your choice>\033[23;0m\n"
@printf " Options: $(GREEDY_OPTIONS_COMMASEPARATED) (default: $(GREEDY_DEFAULT))\n"
@printf "\n"
@printf " \033[1;32mDECONSTRUCTION=\033[3m<your choice>\033[23;0m\n"
@printf " Options: $(DECONSTRUCTION_OPTIONS_COMMASEPARATED) (default: $(DECONSTRUCTION_DEFAULT))\n"
@printf "\n"
@printf "\033[1mExample:\033[0m\n"
@printf " \033[1;32mmake debug GREEDY=STANDARD DECONSTRUCTION=LOCAL\033[0m\n"
# Include auto-generated dependency files
-include $(DEPS_RELEASE) $(DEPS_STRICT) $(DEPS_LOG) $(DEPS_DEBUG)
.PHONY: release strict log debug clean help all