-
Notifications
You must be signed in to change notification settings - Fork 29
/
zpp.mk
279 lines (239 loc) · 9.88 KB
/
zpp.mk
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/make -f
.SUFFIXES:
.SECONDARY:
.PHONY: \
all \
build \
build_single \
build_init \
rebuild \
rebuild_single \
clean_mode \
clean \
clean_single
mode ?= debug
assembly ?= false
target_type ?=
projects ?=
ifeq ($(filter $(mode), debug release), )
$(error Mode must either be debug or release)
endif
ZPP_CONFIGURATION := $(mode)
ZPP_GENERATE_ASSEMBLY := $(assembly)
ZPP_TARGET_TYPE := $(target_type)
all: build
ZPP_THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
ZPP_OUTPUT_DIRECTORY_ROOT := out
ZPP_INTERMEDIATE_DIRECTORY_ROOT := obj
ifeq ($(projects), )
ZPP_PROJECT_SETTINGS := true
include zpp_project.mk
ZPP_PROJECT_SETTINGS := false
endif
ifeq ($(ZPP_INCLUDE_PROJECTS), )
ZPP_INCLUDE_PROJECTS := $(projects)
endif
ifneq ($(ZPP_INCLUDE_PROJECTS), )
ZPP_PROJECTS_DIRECTORIES := $(ZPP_INCLUDE_PROJECTS)
ZPP_INCLUDE_PROJECTS :=
build:
@for project in $(ZPP_PROJECTS_DIRECTORIES); do \
$(MAKE) projects= -s -f `realpath $(ZPP_THIS_MAKEFILE) --relative-to $$project` -C $$project; \
done
clean:
@for project in $(ZPP_PROJECTS_DIRECTORIES); do \
$(MAKE) projects= -s -f `realpath $(ZPP_THIS_MAKEFILE) --relative-to $$project` -C $$project clean; \
done
rebuild:
@for project in $(ZPP_PROJECTS_DIRECTORIES); do \
$(MAKE) projects= -s -f `realpath $(ZPP_THIS_MAKEFILE) --relative-to $$project` -C $$project rebuild; \
done
else # ifneq ($(ZPP_INCLUDE_PROJECTS), )
ifeq ($(ZPP_TARGET_TYPE), )
build:
@for target_type in $(ZPP_TARGET_TYPES); do \
$(MAKE) -s -f $(ZPP_THIS_MAKEFILE) ZPP_TARGET_TYPE=$$target_type; \
done
clean:
@for target_type in $(ZPP_TARGET_TYPES); do \
$(MAKE) -s -f $(ZPP_THIS_MAKEFILE) clean ZPP_TARGET_TYPE=$$target_type; \
done
rebuild:
@for target_type in $(ZPP_TARGET_TYPES); do \
$(MAKE) -s -f $(ZPP_THIS_MAKEFILE) rebuild ZPP_TARGET_TYPE=$$target_type; \
done
else
build: build_single
clean: clean_single
rebuild: rebuild_single
ifeq ($(filter $(ZPP_TARGET_TYPE), $(ZPP_TARGET_TYPES)), )
$(error Invalid target type)
endif
ifeq ($(ZPP_SOURCE_DIRECTORIES), )
ZPP_SOURCE_FILES := $(ZPP_SOURCE_FILES)
else
ZPP_SOURCE_FILES := $(ZPP_SOURCE_FILES) \
$(shell find $(ZPP_SOURCE_DIRECTORIES) -type f -name "*.S") \
$(shell find $(ZPP_SOURCE_DIRECTORIES) -type f -name "*.c") \
$(shell find $(ZPP_SOURCE_DIRECTORIES) -type f -name "*.cc") \
$(shell find $(ZPP_SOURCE_DIRECTORIES) -type f -name "*.cpp")
endif
ifeq ($(strip $(ZPP_SOURCE_FILES)), )
$(error No source files)
endif
ZPP_INTERMEDIATE_DIRECTORY := $(ZPP_INTERMEDIATE_DIRECTORY_ROOT)/$(ZPP_CONFIGURATION)/$(ZPP_TARGET_TYPE)
ZPP_OUTPUT_DIRECTORY := $(ZPP_OUTPUT_DIRECTORY_ROOT)/$(ZPP_CONFIGURATION)/$(ZPP_TARGET_TYPE)
ZPP_COMPILE_COMMANDS_PATHS := $(ZPP_INTERMEDIATE_DIRECTORY)
ZPP_PATH_FROM_ROOT := $(shell echo $(ZPP_SOURCE_FILES) | grep -o "\(\.\./\)*" | sort --unique | tail -n 1)
ifneq ($(ZPP_PATH_FROM_ROOT), )
ZPP_INTERMEDIATE_SUBDIRECTORY := $(shell realpath . --relative-to $(ZPP_PATH_FROM_ROOT))
ZPP_INTERMEDIATE_DIRECTORY := $(ZPP_INTERMEDIATE_DIRECTORY)/$(ZPP_INTERMEDIATE_SUBDIRECTORY)
endif
ifeq ($(ZPP_COMPILE_COMMANDS_JSON), intermediate)
ZPP_COMPILE_COMMANDS_JSON := $(ZPP_INTERMEDIATE_DIRECTORY)/compile_commands.json
endif
ZPP_TOOLCHAIN_SETTINGS := true
include zpp_project.mk
ZPP_TOOLCHAIN_SETTINGS := false
ZPP_PROJECT_FLAGS := true
include zpp_project.mk
ZPP_PROJECT_FLAGS := false
ZPP_COMMA := ,
ZPP_EMPTY :=
ZPP_SPACE := $(ZPP_EMPTY) $(ZPP_EMPTY)
ifeq ($(ZPP_CONFIGURATION), debug)
ZPP_FLAGS := $(ZPP_FLAGS) $(ZPP_FLAGS_DEBUG)
ZPP_CFLAGS := $(ZPP_CFLAGS) $(ZPP_CFLAGS_DEBUG)
ZPP_CXXFLAGS := $(ZPP_CXXFLAGS) $(ZPP_CXXFLAGS_DEBUG)
ZPP_ASFLAGS := $(ZPP_ASFLAGS) $(ZPP_ASFLAGS_DEBUG)
ZPP_LFLAGS := $(ZPP_LFLAGS) $(ZPP_LFLAGS_DEBUG)
else ifeq ($(ZPP_CONFIGURATION), release)
ZPP_FLAGS := $(ZPP_FLAGS) $(ZPP_FLAGS_RELEASE)
ZPP_CFLAGS := $(ZPP_CFLAGS) $(ZPP_CFLAGS_RELEASE)
ZPP_CXXFLAGS := $(ZPP_CXXFLAGS) $(ZPP_CXXFLAGS_RELEASE)
ZPP_ASFLAGS := $(ZPP_ASFLAGS) $(ZPP_ASFLAGS_RELEASE)
ZPP_LFLAGS := $(ZPP_LFLAGS) $(ZPP_LFLAGS_RELEASE)
endif
ZPP_OBJECT_FILES := $(patsubst %.c, %.o, $(ZPP_SOURCE_FILES))
ZPP_OBJECT_FILES := $(ZPP_OBJECT_FILES) $(patsubst %.cpp, %.o, $(ZPP_SOURCE_FILES))
ZPP_OBJECT_FILES := $(ZPP_OBJECT_FILES) $(patsubst %.cc, %.o, $(ZPP_SOURCE_FILES))
ZPP_OBJECT_FILES := $(ZPP_OBJECT_FILES) $(patsubst %.S, %.o, $(ZPP_SOURCE_FILES))
ZPP_OBJECT_FILES := $(patsubst %, $(ZPP_INTERMEDIATE_DIRECTORY)/%, $(ZPP_OBJECT_FILES))
ZPP_OBJECT_FILES := $(filter %.o, $(ZPP_OBJECT_FILES))
ZPP_OBJECT_FILES_DIRECTORIES := $(dir $(ZPP_OBJECT_FILES))
ZPP_DEPENDENCY_FILES := $(patsubst %.o, %.d, $(ZPP_OBJECT_FILES))
ifeq ($(ZPP_LINK_TYPE), default)
ZPP_LINK_COMMAND := $(ZPP_LINK) -o $(ZPP_OUTPUT_DIRECTORY)/$(ZPP_TARGET_NAME) $(ZPP_OBJECT_FILES) $(ZPP_LFLAGS)
else ifeq ($(ZPP_LINK_TYPE), ld)
ZPP_LINK_COMMAND := $(ZPP_LINK) -o $(ZPP_OUTPUT_DIRECTORY)/$(ZPP_TARGET_NAME) $(ZPP_OBJECT_FILES) $(ZPP_LFLAGS)
else ifeq ($(ZPP_LINK_TYPE), link)
ZPP_LINK_COMMAND := $(ZPP_LINK) $(ZPP_LFLAGS) /out:$(ZPP_OUTPUT_DIRECTORY)/$(ZPP_TARGET_NAME) $(ZPP_OBJECT_FILES)
else ifeq ($(ZPP_LINK_TYPE), ar)
ZPP_LINK_COMMAND := $(ZPP_AR) rcs $(ZPP_OUTPUT_DIRECTORY)/$(ZPP_TARGET_NAME) $(ZPP_OBJECT_FILES)
else
$(error ZPP_LINK_TYPE must either be default, ld, link, or ar)
endif
define GENERATE_COMPILE_COMMANDS_SCRIPT
import os
import json
compile_commands = []
for root, _, files in os.walk('$(ZPP_COMPILE_COMMANDS_PATHS)'):
for file in files:
if not file.endswith('.zppcmd'):
continue
full_file = os.path.join(root, file)
with open(full_file, 'r') as f:
command, source_file = f.read().split('\n')[:2]
compile_commands.append(
{
'directory': os.path.abspath('.'),
'file': os.path.abspath(source_file),
'command': command,
}
)
with open('$(ZPP_COMPILE_COMMANDS_JSON)', 'w') as f:
json.dump(compile_commands, f, indent=2, sort_keys=True)
endef
export GENERATE_COMPILE_COMMANDS_SCRIPT
CALL_GENERATE_COMPILE_COMMANDS_SCRIPT := $(ZPP_PYTHON) -c "$$GENERATE_COMPILE_COMMANDS_SCRIPT"
ifeq ($(ZPP_COMPILE_COMMANDS_JSON), )
build_single: $(ZPP_OUTPUT_DIRECTORY)/$(ZPP_TARGET_NAME)
@echo "Built '$(ZPP_TARGET_TYPE)/$(ZPP_TARGET_NAME)'."
else
build_single: $(ZPP_COMPILE_COMMANDS_JSON) $(ZPP_OUTPUT_DIRECTORY)/$(ZPP_TARGET_NAME)
@echo "Built '$(ZPP_TARGET_TYPE)/$(ZPP_TARGET_NAME)'."
$(ZPP_COMPILE_COMMANDS_JSON): $(ZPP_OBJECT_FILES)
@echo "Building '$@'..."; \
$(CALL_GENERATE_COMPILE_COMMANDS_SCRIPT)
endif
build_init:
@echo "Building '$(ZPP_TARGET_TYPE)/$(ZPP_TARGET_NAME)' in '$(ZPP_CONFIGURATION)' mode..."; \
mkdir -p $(ZPP_INTERMEDIATE_DIRECTORY); \
mkdir -p $(ZPP_OUTPUT_DIRECTORY); \
mkdir -p $(ZPP_OBJECT_FILES_DIRECTORIES)
$(ZPP_OUTPUT_DIRECTORY)/$(ZPP_TARGET_NAME): $(ZPP_OBJECT_FILES)
@echo "Linking '$(ZPP_OUTPUT_DIRECTORY)/$(ZPP_TARGET_NAME)'..."; \
$(ZPP_LINK_COMMAND)
ifeq ($(ZPP_GENERATE_ASSEMBLY), true)
$(ZPP_INTERMEDIATE_DIRECTORY)/%.S: %.c | build_init
@echo "Compiling '$<'..."; \
$(ZPP_CC) -S $(ZPP_CFLAGS) -o $@ $< -MD -Wp,-MD,`dirname $@`/`basename $@ .S`.d; \
echo '$(ZPP_CC) -c $(ZPP_CFLAGS) -o $@ $< -MD -Wp,-MD,'`dirname $@`/`basename $@ .o`.d > [email protected]; \
echo $< >> [email protected]
$(ZPP_INTERMEDIATE_DIRECTORY)/%.S: %.cpp | build_init
@echo "Compiling '$<'..."; \
$(ZPP_CXX) -S $(ZPP_CXXFLAGS) -o $@ $< -MD -Wp,-MD,`dirname $@`/`basename $@ .S`.d; \
echo '$(ZPP_CXX) -c $(ZPP_CXXFLAGS) -o $@ $< -MD -Wp,-MD,'`dirname $@`/`basename $@ .o`.d > [email protected]; \
echo $< >> [email protected]
$(ZPP_INTERMEDIATE_DIRECTORY)/%.S: %.cc | build_init
@echo "Compiling '$<'..."; \
$(ZPP_CXX) -S $(ZPP_CXXFLAGS) -o $@ $< -MD -Wp,-MD,`dirname $@`/`basename $@ .S`.d; \
echo '$(ZPP_CXX) -c $(ZPP_CXXFLAGS) -o $@ $< -MD -Wp,-MD,'dirname $@`/`basename $@ .o`.d > [email protected]; \
echo $< >> [email protected]
$(ZPP_INTERMEDIATE_DIRECTORY)/%.o: $(ZPP_INTERMEDIATE_DIRECTORY)/%.S
@$(ZPP_CC) -Wno-unicode -c -o $@ $<
else ifeq ($(ZPP_GENERATE_ASSEMBLY), false)
$(ZPP_INTERMEDIATE_DIRECTORY)/%.o: %.c | build_init
@echo "Compiling '$<'..."; \
$(ZPP_CC) -c $(ZPP_CFLAGS) -o $@ $< -MD -Wp,-MD,`dirname $@`/`basename $@ .o`.d; \
echo '$(ZPP_CC) -c $(ZPP_CFLAGS) -o $@ $< -MD -Wp,-MD,'dirname $@`/`basename $@ .o`.d > [email protected]; \
echo $< >> [email protected]
$(ZPP_INTERMEDIATE_DIRECTORY)/%.o: %.cpp | build_init
@echo "Compiling '$<'..."; \
$(ZPP_CXX) -c $(ZPP_CXXFLAGS) -o $@ $< -MD -Wp,-MD,`dirname $@`/`basename $@ .o`.d; \
echo '$(ZPP_CXX) -c $(ZPP_CXXFLAGS) -o $@ $< -MD -Wp,-MD,'`dirname $@`/`basename $@ .o`.d > [email protected]; \
echo $< >> [email protected]
$(ZPP_INTERMEDIATE_DIRECTORY)/%.o: %.cc | build_init
@echo "Compiling '$<'..."; \
$(ZPP_CXX) -c $(ZPP_CXXFLAGS) -o $@ $< -MD -Wp,-MD,`dirname $@`/`basename $@ .o`.d; \
echo '$(ZPP_CXX) -c $(ZPP_CXXFLAGS) -o $@ $< -MD -Wp,-MD,'`dirname $@`/`basename $@ .o`.d > [email protected]; \
echo $< >> [email protected]
else
$(error ZPP_GENERATE_ASSEMBLY must either be true or false)
endif
$(ZPP_INTERMEDIATE_DIRECTORY)/%.o: %.S | build_init
@echo "Assemblying '$<'..."; \
$(ZPP_AS) -c $(ZPP_ASFLAGS) -o $@ $< -MD -Wp,-MD,`dirname $@`/`basename $@ .o`.d; \
echo '$(ZPP_AS) -c $(ZPP_ASFLAGS) -o $@ $< -MD -Wp,-MD,'`dirname $@`/`basename $@ .o`.d > [email protected]; \
echo $< >> [email protected]
-include $(ZPP_DEPENDENCY_FILES)
clean_single:
@echo "Cleaning '$(ZPP_TARGET_TYPE)/$(ZPP_TARGET_NAME)'..."; \
rm -rf $(ZPP_INTERMEDIATE_DIRECTORY_ROOT)/debug/$(ZPP_TARGET_TYPE); \
rm -rf $(ZPP_INTERMEDIATE_DIRECTORY_ROOT)/release/$(ZPP_TARGET_TYPE); \
rm -f $(ZPP_OUTPUT_DIRECTORY_ROOT)/debug/$(ZPP_TARGET_TYPE)/$(ZPP_TARGET_NAME); \
rm -f $(ZPP_OUTPUT_DIRECTORY_ROOT)/release/$(ZPP_TARGET_TYPE)/$(ZPP_TARGET_NAME); \
rm -f $(ZPP_COMPILE_COMMANDS_JSON); \
find $(ZPP_INTERMEDIATE_DIRECTORY_ROOT) -type d -empty -delete 2> /dev/null; \
find $(ZPP_OUTPUT_DIRECTORY_ROOT) -type d -empty -delete 2> /dev/null; \
echo "Cleaned '$(ZPP_TARGET_TYPE)/$(ZPP_TARGET_NAME)'."
clean_mode:
@rm -rf $(ZPP_INTERMEDIATE_DIRECTORY); \
rm -rf $(ZPP_OUTPUT_DIRECTORY)/$(ZPP_TARGET_NAME)
rebuild_single: clean_mode
@$(MAKE) -s -f $(ZPP_THIS_MAKEFILE) build
ZPP_PROJECT_RULES := true
include zpp_project.mk
ZPP_PROJECT_RULES := false
endif # ifeq ($(ZPP_TARGET_TYPE), )
endif # ifneq ($(ZPP_INCLUDE_PROJECTS), )