-
Notifications
You must be signed in to change notification settings - Fork 15
/
Makefile
393 lines (348 loc) · 15.1 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# SPDX-License-Identifier: GPL-3.0-or-later
#
# Pickup our cross-toolchains automatically...
# c.f., http://trac.ak-team.com/trac/browser/niluje/Configs/trunk/Kindle/Misc/x-compile.sh
# https://github.com/NiLuJe/crosstool-ng
# https://github.com/koreader/koxtoolchain
# NOTE: We want the "bare" variant of the TC env, to make sure we vendor the right stuff...
# i.e., source ~SVN/Configs/trunk/Kindle/Misc/x-compile.sh kobo env bare
ifdef CROSS_TC
CC:=$(CROSS_TC)-gcc
STRIP:=$(CROSS_TC)-strip
else
CC?=gcc
STRIP?=strip
endif
DEBUG_CFLAGS:=-Og -fno-omit-frame-pointer -pipe -g
# Fallback CFLAGS, we honor the env first and foremost!
OPT_CFLAGS:=-O2 -fomit-frame-pointer -pipe
ifdef NILUJE
# Use the system's sqlite on my sandbox
# NOTE: We can't easily check the exit code of a command (outside of a recipe), so, instead,
# force verbosity to stdout to check the command's output, since it's blank on success.
ifeq "$(shell pkg-config --exists --print-errors --errors-to-stdout sqlite3)" ""
LIBS:=$(shell pkg-config --libs sqlite3)
else
# Couldn't find SQLite via pkg-config, shit may go horribly wrong...
LIBS:=-lsqlite3
endif
# And the sandbox's custom paths
EXTRA_CFLAGS+=-DNILUJE
else
# We want to link to sqlite3 explicitly statically
LIBS:=-l:libsqlite3.a
endif
# We need our bundled FBInk ;).
LIBS+=-l:libfbink.a
# NOTE: Remember to use gdb -ex 'set follow-fork-mode child' to debug, since we fork like wild bunnies...
ifdef DEBUG
OUT_DIR:=Debug
CFLAGS:=$(DEBUG_CFLAGS)
EXTRA_CFLAGS+=-DDEBUG
else
OUT_DIR:=Release
CFLAGS?=$(OPT_CFLAGS)
endif
# Detect whether our TC is cross (at least as far as the target arch is concerned)
HOST_ARCH:=$(shell uname -m)
TARGET_ARCH:=$(shell $(CC) $(CFLAGS) -dumpmachine 2>/dev/null)
CC_IS_CROSS:=0
# Host doesn't match target, assume it's a cross TC
ifeq (,$(findstring $(HOST_ARCH),$(TARGET_ARCH)))
CC_IS_CROSS:=1
endif
# Moar warnings!
EXTRA_CFLAGS+=-Wall
EXTRA_CFLAGS+=-Wextra -Wunused
EXTRA_CFLAGS+=-Wformat=2
EXTRA_CFLAGS+=-Wformat-signedness
# NOTE: -Wformat-truncation=2 is still a tad too aggressive w/ GCC 14, so, tone it down to avoid false-positives...
EXTRA_CFLAGS+=-Wformat-truncation=1
EXTRA_CFLAGS+=-Wnull-dereference
EXTRA_CFLAGS+=-Wuninitialized
EXTRA_CFLAGS+=-Wduplicated-branches -Wduplicated-cond
EXTRA_CFLAGS+=-Wundef
EXTRA_CFLAGS+=-Wbad-function-cast
EXTRA_CFLAGS+=-Wwrite-strings
EXTRA_CFLAGS+=-Wjump-misses-init
EXTRA_CFLAGS+=-Wlogical-op
EXTRA_CFLAGS+=-Wstrict-prototypes -Wold-style-definition
EXTRA_CFLAGS+=-Wshadow
EXTRA_CFLAGS+=-Wmissing-prototypes -Wmissing-declarations
EXTRA_CFLAGS+=-Wnested-externs
EXTRA_CFLAGS+=-Winline
EXTRA_CFLAGS+=-Wcast-qual
# NOTE: GCC 8 introduces -Wcast-align=strict to warn regardless of the target architecture (i.e., like clang)
EXTRA_CFLAGS+=-Wcast-align
EXTRA_CFLAGS+=-Wconversion
# Output padding info when debugging (NOTE: Clang is slightly more verbose)
# As well as function attribute hints
ifdef DEBUG
EXTRA_CFLAGS+=-Wpadded
EXTRA_CFLAGS+=-Wsuggest-attribute=pure -Wsuggest-attribute=const -Wsuggest-attribute=noreturn -Wsuggest-attribute=format -Wmissing-format-attribute
endif
# And disable this, because it obviously doesn't play well with using goto to handle cleanup on error codepaths...
EXTRA_CFLAGS+=-Wno-jump-misses-init
# Spammy when linking SQLite statically
ifndef NILUJE
EXTRA_CFLAGS+=-Wno-null-dereference
endif
# Match the harmless part of FBInk's enforced optimizations
ifndef DEBUG
SQLITE_CFLAGS+=-fno-semantic-interposition
EXTRA_CFLAGS+=-fno-semantic-interposition
endif
# A version tag...
KFMON_VERSION:=$(shell git describe)
EXTRA_CFLAGS+=-DKFMON_VERSION='"$(KFMON_VERSION)"'
# A timestamp, formatted according to ISO 8601 (latest commit)...
KFMON_TIMESTAMP:=$(shell git show -s --format=%ci)
# NOTE: We used to use __DATE__ @ __TIME__ (i.e., the build date), which we can format the same way like so:
# date +'%Y-%m-%d %H:%M:%S %z'
# If, instead, we'd want to emulate __TIMESTAMP__ (i.e., modification date of the file):
# date +'%Y-%m-%d %H:%M:%S %z' -r kfmon.c
# NOTE: If we ever need to tweak git's formatting:
# git show -s --format=%cd --date=format:'%Y-%m-%d @ %H:%M:%S %z' master
EXTRA_CFLAGS+=-DKFMON_TIMESTAMP='"$(KFMON_TIMESTAMP)"'
# NOTE: Always use as-needed to avoid unecessary DT_NEEDED entries :)
LDFLAGS?=-Wl,--as-needed
# Pick up our vendored build of SQLite when asked to
ifdef SQLITE
EXTRA_CPPFLAGS:=-ISQLiteBuild
EXTRA_LDFLAGS:=-LSQLiteBuild/.libs
endif
# And pick up FBInk, too.
ifdef DEBUG
EXTRA_LDFLAGS+=-LFBInk/Debug
else
EXTRA_LDFLAGS+=-LFBInk/Release
# We already enforce that in SQLite, so, follow suit everywhere
EXTRA_CPPFLAGS+=-DNDEBUG
endif
# We use pthreads, let GCC do its thing to do it right (c.f., gcc -dumpspecs | grep pthread).
# NOTE: It mostly consists of passing -D_REENTRANT to the preprocessor, -lpthread to the linker,
# and setting -fprofile-update to prefer-atomic instead of single.
# Since we're not doing pgo builds, that last one is irrelevant here, which explains why we can safely and simply
# emulate the autoconf SQLite builds by simply passing -D_REENTRANT to the preprocessor in the sqlite.built recipe.
EXTRA_CPPFLAGS+=-pthread
LIBS+=-lpthread
# And we need -lrt for clock_gettime, as our TC is targeting glibc 2.15, and it was in there before glibc 2.17...
# Yes, Kobo FW have since moved to glibc 2.19, but that's recent (-ish), and we want binaries that will work on earlier FW than that.
LIBS+=-lrt
# We already enforce that in FBInk & KFMon (& SQLite itself probably does, too), so, follow suit everywhere
EXTRA_CPPFLAGS+=-D_GNU_SOURCE
##
# Now that we're done fiddling with flags, let's build stuff!
SRCS:=kfmon.c
# Jump through a few hoops to be able to silence warnings on third-party code only
INIH_SRCS:=inih/ini.c
# We only need str5cpy
STR5_SRCS:=str5/str5cpy.c
# We always need OpenSSH's neat io wrappers
SSH_SRCS:=openssh/atomicio.c
# Keep our old helpers for socket handling around, even if we don't actually use them anymore
SOCK_SRCS:=utils/sock_utils.c
default: vendored
OBJS:=$(addprefix $(OUT_DIR)/, $(SRCS:.c=.o))
INIH_OBJS:=$(addprefix $(OUT_DIR)/, $(INIH_SRCS:.c=.o))
STR5_OBJS:=$(addprefix $(OUT_DIR)/, $(STR5_SRCS:.c=.o))
SSH_OBJS:=$(addprefix $(OUT_DIR)/, $(SSH_SRCS:.c=.o))
SOCK_OBJS:=$(addprefix $(OUT_DIR)/, $(SOCK_SRCS:.c=.o))
# And now we can silence a few inih-specific warnings
$(INIH_OBJS): QUIET_CFLAGS := -Wno-cast-qual
$(OUT_DIR)/%.o: %.c
$(CC) $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) $(QUIET_CFLAGS) -o $@ -c $<
outdir:
mkdir -p $(OUT_DIR)/inih $(OUT_DIR)/str5 $(OUT_DIR)/openssh $(OUT_DIR)/utils
# Make absolutely sure we create our output directories first, even with unfortunate // timings!
# c.f., https://www.gnu.org/software/make/manual/html_node/Prerequisite-Types.html#Prerequisite-Types
$(OBJS): | outdir
$(INIH_OBJS): | outdir
$(STR5_OBJS): | outdir
$(SSH_OBJS): | outdir
$(SOCK_OBJS): | outdir
all: kfmon
vendored: fbink.built | sqlite.built
$(MAKE) kfmon SQLITE=true
kfmon: $(OBJS) $(INIH_OBJS) $(STR5_OBJS) $(SSH_OBJS)
$(CC) $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -o$(OUT_DIR)/$@ $(OBJS) $(INIH_OBJS) $(STR5_OBJS) $(SSH_OBJS) $(LIBS)
shim: | outdir
$(CC) $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -o$(OUT_DIR)/shim utils/shim.c
$(STRIP) --strip-unneeded $(OUT_DIR)/shim
kfmon-ipc: | outdir $(STR5_OBJS) $(SSH_OBJS)
$(CC) $(CPPFLAGS) $(EXTRA_CPPFLAGS) $(CFLAGS) $(EXTRA_CFLAGS) $(LDFLAGS) $(EXTRA_LDFLAGS) -o$(OUT_DIR)/kfmon-ipc utils/kfmon-ipc.c $(STR5_OBJS) $(SSH_OBJS)
$(STRIP) --strip-unneeded $(OUT_DIR)/kfmon-ipc
strip: all
$(STRIP) --strip-unneeded $(OUT_DIR)/kfmon
armcheck:
ifeq (,$(findstring arm-,$(CC)))
$(error You forgot to setup a cross TC, you dummy!)
endif
kobo: armcheck release
mkdir -p Kobo/usr/local/kfmon/bin Kobo/usr/bin Kobo/etc/udev/rules.d Kobo/etc/init.d
ln -sf $(CURDIR)/scripts/99-kfmon.rules Kobo/etc/udev/rules.d/99-kfmon.rules
ln -sf $(CURDIR)/scripts/uninstall/kfmon-uninstall.sh Kobo/usr/local/kfmon/bin/kfmon-update.sh
ln -sf $(CURDIR)/scripts/uninstall/on-animator.sh Kobo/etc/init.d/on-animator.sh
tar --exclude="./mnt" --exclude="KFMon-*.zip" --owner=root --group=root -cvzhf Release/KoboRoot.tgz -C Kobo .
pushd Release && zip ../Kobo/KFMon-Uninstaller.zip KoboRoot.tgz && popd
rm -f Release/KoboRoot.tgz
rm -rf Kobo/usr/local/kfmon/bin Kobo/etc/udev/rules.d Kobo/etc/init.d
mkdir -p Kobo/usr/local/kfmon/bin Kobo/mnt/onboard/.kobo Kobo/etc/udev/rules.d Kobo/etc/init.d Kobo/mnt/onboard/.adds/kfmon/config Kobo/mnt/onboard/.adds/kfmon/bin Kobo/mnt/onboard/.adds/kfmon/log Kobo/mnt/onboard/icons
ln -f $(CURDIR)/resources/koreader.png Kobo/mnt/onboard/koreader.png
ln -f $(CURDIR)/resources/plato.png Kobo/mnt/onboard/icons/plato.png
ln -f $(CURDIR)/resources/kfmon.png Kobo/mnt/onboard/kfmon.png
ln -f $(CURDIR)/Release/kfmon Kobo/usr/local/kfmon/bin/kfmon
ln -f $(CURDIR)/Release/shim Kobo/usr/local/kfmon/bin/shim
ln -f $(CURDIR)/Release/kfmon-ipc Kobo/usr/local/kfmon/bin/kfmon-ipc
ln -sf /usr/local/kfmon/bin/kfmon-ipc Kobo/usr/bin/kfmon-ipc
ln -f $(CURDIR)/FBInk/Release/fbink Kobo/usr/local/kfmon/bin/fbink
ln -f $(CURDIR)/README.md Kobo/usr/local/kfmon/README.md
ln -f $(CURDIR)/LICENSE Kobo/usr/local/kfmon/LICENSE
ln -f $(CURDIR)/CREDITS Kobo/usr/local/kfmon/CREDITS
ln -f $(CURDIR)/scripts/99-kfmon.rules Kobo/etc/udev/rules.d/99-kfmon.rules
ln -f $(CURDIR)/scripts/kfmon-update.sh Kobo/usr/local/kfmon/bin/kfmon-update.sh
ln -f $(CURDIR)/scripts/on-animator.sh Kobo/etc/init.d/on-animator.sh
tar --exclude="./mnt" --exclude="KFMon-*.zip" --owner=root --group=root --hard-dereference -cvzf Release/KoboRoot.tgz -C Kobo .
ln -sf $(CURDIR)/Release/KoboRoot.tgz Kobo/mnt/onboard/.kobo/KoboRoot.tgz
ln -sf $(CURDIR)/config/kfmon.ini Kobo/mnt/onboard/.adds/kfmon/config/kfmon.ini
ln -sf $(CURDIR)/config/koreader.ini Kobo/mnt/onboard/.adds/kfmon/config/koreader.ini
ln -sf $(CURDIR)/config/plato.ini Kobo/mnt/onboard/.adds/kfmon/config/plato.ini
ln -sf $(CURDIR)/config/kfmon-log.ini Kobo/mnt/onboard/.adds/kfmon/config/kfmon-log.ini
ln -sf $(CURDIR)/scripts/kfmon-printlog.sh Kobo/mnt/onboard/.adds/kfmon/bin/kfmon-printlog.sh
pushd Kobo/mnt/onboard && zip -r ../../KFMon-$(KFMON_VERSION).zip . && popd
kobov5: armcheck release
mkdir -p KoboV5/.kobo
tar --owner=root --group=root -cvhf KoboV5/.kobo/update.tar -C scripts/uninstall driver.sh kfmon-uninstall.sh animator.sh
pushd KoboV5 && zip KFMon-Uninstaller.zip .kobo/update.tar && popd
rm -f Release/update.tar
mkdir -p KoboV5/usr/bin KoboV5/etc/init.d KoboV5/etc/rcS.d
mkdir -p KoboV5/usr/local/kfmon/bin KoboV5/mnt/onboard/.kobo KoboV5/mnt/onboard/.adds/kfmon/config KoboV5/mnt/onboard/.adds/kfmon/bin KoboV5/mnt/onboard/.adds/kfmon/log KoboV5/mnt/onboard/icons
ln -f $(CURDIR)/scripts/kfmon KoboV5/etc/init.d
ln -sf ../init.d/kfmon KoboV5/etc/rcS.d/S99kfmon
ln -f $(CURDIR)/resources/koreader.png KoboV5/mnt/onboard/koreader.png
ln -f $(CURDIR)/resources/plato.png KoboV5/mnt/onboard/icons/plato.png
ln -f $(CURDIR)/resources/kfmon.png KoboV5/mnt/onboard/kfmon.png
ln -f $(CURDIR)/Release/kfmon KoboV5/usr/local/kfmon/bin/kfmon
ln -f $(CURDIR)/Release/shim KoboV5/usr/local/kfmon/bin/shim
ln -f $(CURDIR)/Release/kfmon-ipc KoboV5/usr/local/kfmon/bin/kfmon-ipc
ln -sf /usr/local/kfmon/bin/kfmon-ipc KoboV5/usr/bin/kfmon-ipc
ln -f $(CURDIR)/FBInk/Release/fbink KoboV5/usr/local/kfmon/bin/fbink
ln -f $(CURDIR)/README.md KoboV5/usr/local/kfmon/README.md
ln -f $(CURDIR)/LICENSE KoboV5/usr/local/kfmon/LICENSE
ln -f $(CURDIR)/CREDITS KoboV5/usr/local/kfmon/CREDITS
ln -f $(CURDIR)/scripts/animator.sh KoboV5/usr/bin/animator.sh
tar --exclude="./mnt" --exclude="KFMon-*.zip" --exclude="./.kobo" --owner=root --group=root --hard-dereference -cvzf Release/kfmon.tgz -C KoboV5 .
tar --owner=root --group=root -cvhf Release/update.tar -C scripts driver.sh kfmon-install.sh
tar --owner=root --group=root -rvhf Release/update.tar -C Release kfmon.tgz
ln -sf $(CURDIR)/Release/update.tar KoboV5/mnt/onboard/.kobo/update.tar
ln -sf $(CURDIR)/config/kfmon.ini KoboV5/mnt/onboard/.adds/kfmon/config/kfmon.ini
ln -sf $(CURDIR)/config/koreader.ini KoboV5/mnt/onboard/.adds/kfmon/config/koreader.ini
ln -sf $(CURDIR)/config/plato.ini KoboV5/mnt/onboard/.adds/kfmon/config/plato.ini
ln -sf $(CURDIR)/config/kfmon-log.ini KoboV5/mnt/onboard/.adds/kfmon/config/kfmon-log.ini
ln -sf $(CURDIR)/scripts/kfmon-printlog.sh KoboV5/mnt/onboard/.adds/kfmon/bin/kfmon-printlog.sh
pushd KoboV5/mnt/onboard && zip -r ../../KFMon-$(KFMON_VERSION).zip . && popd
niluje:
$(MAKE) fbink.built NILUJE=true
$(MAKE) all NILUJE=true
nilujed:
$(MAKE) fbink.built NILUJE=true DEBUG=true
$(MAKE) all NILUJE=true DEBUG=true
clean:
rm -rf Release/inih/*.o
rm -rf Release/str5/*.o
rm -rf Release/openssh/*.o
rm -rf Release/utils/*.o
rm -rf Release/*.o
rm -rf Release/kfmon
rm -rf Release/shim
rm -rf Release/kfmon-ipc
rm -rf Release/KoboRoot.tgz
rm -rf Release/update.tar
rm -rf Release/kfmon.tgz
rm -rf Debug/inih/*.o
rm -rf Debug/str5/*.o
rm -rf Debug/openssh/*.o
rm -rf Debug/utils/*.o
rm -rf Debug/*.o
rm -rf Debug/kfmon
rm -rf Debug/shim
rm -rf Debug/kfmon-ipc
rm -rf Kobo
rm -rf KoboV5
sqlite.built:
mkdir -p SQLiteBuild
cd sqlite && \
patch -p1 < ../tools/sqlite-need_err_name.patch && \
autoreconf -fi && \
cd ../SQLiteBuild && \
env CPPFLAGS="$(CPPFLAGS) \
-DNDEBUG \
-D_GNU_SOURCE \
-DSQLITE_DQS=0 \
-DSQLITE_DEFAULT_MEMSTATUS=0 \
-DSQLITE_DEFAULT_WAL_SYNCHRONOUS=1 \
-DSQLITE_LIKE_DOESNT_MATCH_BLOBS \
-DSQLITE_MAX_EXPR_DEPTH=0 \
-DSQLITE_OMIT_DECLTYPE \
-DSQLITE_OMIT_DEPRECATED \
-DSQLITE_OMIT_PROGRESS_CALLBACK \
-DSQLITE_OMIT_SHARED_CACHE \
-DSQLITE_USE_ALLOCA \
-DSQLITE_OMIT_AUTOINIT \
-DSQLITE_OMIT_UTF16 \
-DSQLITE_NEED_ERR_NAME \
-DSQLITE_OMIT_DESERIALIZE \
-DSQLITE_OMIT_JSON" \
CFLAGS='$(CFLAGS) $(SQLITE_CFLAGS)' \
../sqlite/configure $(if $(CROSS_TC),--host=$(CROSS_TC),) \
--disable-amalgamation \
--enable-static \
--disable-static-shell \
--disable-shared \
--disable-threadsafe \
--disable-load-extension \
--disable-editline \
--disable-readline \
--disable-tcl \
--disable-math \
--enable-tempstore=yes \
--disable-releasemode && \
$(MAKE) sqlite3.h libsqlite3.la
touch sqlite.built
ifdef NILUJE
fbink.built:
cd FBInk && \
$(MAKE) strip LINUX=true MINIMAL=true BITMAP=true DEBUG=$(DEBUG)
touch fbink.built
else
fbink.built:
cd FBInk && \
$(MAKE) strip KOBO=true MINIMAL=true BITMAP=true
touch fbink.built
endif
release: fbink.built shim kfmon-ipc | sqlite.built
$(MAKE) strip SQLITE=true
debug: | sqlite.built
cd FBInk && \
$(MAKE) debug KOBO=true MINIMAL=true BITMAP=true
touch fbink.built
$(MAKE) all DEBUG=true SQLITE=true
fbinkclean:
cd FBInk && \
$(MAKE) distclean
sqliteclean:
cd sqlite && \
git reset --hard && \
git clean -fxdq
distclean: clean sqliteclean fbinkclean
rm -rf SQLiteBuild
rm -rf sqlite.built
rm -rf fbink.built
format:
clang-format -style=file -i *.c *.h openssh/*.c openssh/*.h str5/*.c str5/*.h utils/*.c utils/*.h
ocp:
./tools/one-click.py
./tools/upload.sh
cat /tmp/KFMon/KFMON_PUB_BB
rm -rf /tmp/KFMon
.PHONY: default outdir all vendored kfmon shim kfmon-ipc strip armcheck kobo kobov5 debug niluje nilujed clean release fbinkclean sqliteclean distclean format ocp