forked from wmbusmeters/wmbusmeters
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
389 lines (338 loc) · 12.9 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
# Copyright (C) 2017-2020 Fredrik Öhrström
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# To compile for Raspberry PI ARM:
# make HOST=arm
#
# To build with debug information:
# make DEBUG=true
# make DEBUG=true HOST=arm
DESTDIR?=/
ifeq "$(HOST)" "arm"
CXX?=arm-linux-gnueabihf-g++
STRIP?=arm-linux-gnueabihf-strip
BUILD=build_arm
DEBARCH=armhf
else
CXX?=g++
STRIP?=strip
#--strip-unneeded --remove-section=.comment --remove-section=.note
BUILD=build
DEBARCH=amd64
endif
ifeq "$(DEBUG)" "true"
DEBUG_FLAGS=-O0 -ggdb -fsanitize=address -fno-omit-frame-pointer -fprofile-arcs -ftest-coverage
STRIP_BINARY=
BUILD:=$(BUILD)_debug
ifneq '' '$(findstring clang++,$(CXX))'
DEBUG_LDFLAGS=-fsanitize=address --coverage
GCOV?=llvm-cov gcov
else
DEBUG_LDFLAGS=-lasan -lgcov --coverage
GCOV?=gcov
endif
else
DEBUG_FLAGS=-Os -g
STRIP_BINARY=cp $(BUILD)/wmbusmeters $(BUILD)/wmbusmeters.g; $(STRIP) $(BUILD)/wmbusmeters
GCOV=To_run_gcov_add_DEBUG=true
endif
$(shell mkdir -p $(BUILD))
COMMIT_HASH?=$(shell git log --pretty=format:'%H' -n 1)
TAG?=$(shell git describe --tags)
BRANCH?=$(shell git rev-parse --abbrev-ref HEAD)
CHANGES?=$(shell git status -s | grep -v '?? ')
ifeq ($(BRANCH),master)
BRANCH:=
else
BRANCH:=$(BRANCH)_
endif
VERSION:=$(BRANCH)$(TAG)
DEBVERSION:=$(BRANCH)$(TAG)
ifneq ($(strip $(CHANGES)),)
# There are local un-committed changes.
VERSION:=$(VERSION) with local changes
COMMIT_HASH:=$(COMMIT_HASH) with local changes
DEBVERSION:=$(DEBVERSION)l
endif
$(shell echo "#define VERSION \"$(VERSION)\"" > $(BUILD)/version.h.tmp)
$(shell echo "#define COMMIT \"$(COMMIT_HASH)\"" >> $(BUILD)/version.h.tmp)
PREV_VERSION=$(shell cat -n $(BUILD)/version.h 2> /dev/null)
CURR_VERSION=$(shell cat -n $(BUILD)/version.h.tmp 2>/dev/null)
ifneq ($(PREV_VERSION),$(CURR_VERSION))
$(shell mv $(BUILD)/version.h.tmp $(BUILD)/version.h)
else
$(shell rm $(BUILD)/version.h.tmp)
endif
$(info Building $(VERSION))
FUZZFLAGS ?= -DFUZZING=false
CXXFLAGS ?= $(DEBUG_FLAGS) $(FUZZFLAGS) -fPIC -std=c++11 -Wall -Werror=format-security
CXXFLAGS += -I$(BUILD)
LDFLAGS ?= $(DEBUG_LDFLAGS)
USBLIB = -lusb-1.0
ifeq ($(shell uname -s),FreeBSD)
CXXFLAGS += -I/usr/local/include
LDFLAGS += -L/usr/local/lib
USBLIB = -lusb
endif
$(BUILD)/%.o: src/%.cc $(wildcard src/%.h)
$(CXX) $(CXXFLAGS) $< -c -E > $@.src
$(CXX) $(CXXFLAGS) $< -MMD -c -o $@
METER_OBJS:=\
$(BUILD)/aes.o \
$(BUILD)/aescmac.o \
$(BUILD)/bus.o \
$(BUILD)/cmdline.o \
$(BUILD)/config.o \
$(BUILD)/dvparser.o \
$(BUILD)/mbus_rawtty.o \
$(BUILD)/meters.o \
$(BUILD)/manufacturer_specificities.o \
$(BUILD)/printer.o \
$(BUILD)/rtlsdr.o \
$(BUILD)/serial.o \
$(BUILD)/shell.o \
$(BUILD)/sha256.o \
$(BUILD)/threads.o \
$(BUILD)/util.o \
$(BUILD)/units.o \
$(BUILD)/wmbus.o \
$(BUILD)/meter_auto.o \
$(BUILD)/meter_unknown.o \
$(BUILD)/meter_amiplus.o \
$(BUILD)/wmbus_amb8465.o \
$(BUILD)/wmbus_im871a.o \
$(BUILD)/wmbus_cul.o \
$(BUILD)/wmbus_rtlwmbus.o \
$(BUILD)/wmbus_rtl433.o \
$(BUILD)/wmbus_simulator.o \
$(BUILD)/wmbus_rawtty.o \
$(BUILD)/wmbus_rc1180.o \
$(BUILD)/wmbus_utils.o \
$(BUILD)/meter_apator08.o \
$(BUILD)/meter_apator162.o \
$(BUILD)/meter_aventieswm.o \
$(BUILD)/meter_aventieshca.o \
$(BUILD)/meter_bfw240radio.o \
$(BUILD)/meter_cma12w.o \
$(BUILD)/meter_compact5.o \
$(BUILD)/meter_dme_07.o \
$(BUILD)/meter_ebzwmbe.o \
$(BUILD)/meter_ehzp.o \
$(BUILD)/meter_esyswm.o \
$(BUILD)/meter_elf.o \
$(BUILD)/meter_em24.o \
$(BUILD)/meter_emerlin868.o \
$(BUILD)/meter_ev200.o \
$(BUILD)/meter_evo868.o \
$(BUILD)/meter_eurisii.o \
$(BUILD)/meter_fhkvdataiii.o \
$(BUILD)/meter_fhkvdataiv.o \
$(BUILD)/meter_flowiq2200.o \
$(BUILD)/meter_hydrus.o \
$(BUILD)/meter_hydrocalm3.o \
$(BUILD)/meter_hydrodigit.o \
$(BUILD)/meter_ei6500.o \
$(BUILD)/meter_iperl.o \
$(BUILD)/meter_izar.o \
$(BUILD)/meter_izar3.o \
$(BUILD)/meter_lansendw.o \
$(BUILD)/meter_lansensm.o \
$(BUILD)/meter_lansenth.o \
$(BUILD)/meter_lansenpu.o \
$(BUILD)/meter_lse_07_17.o \
$(BUILD)/meter_mkradio3.o \
$(BUILD)/meter_mkradio4.o \
$(BUILD)/meter_multical21.o \
$(BUILD)/meter_multical302.o \
$(BUILD)/meter_multical403.o \
$(BUILD)/meter_multical602.o \
$(BUILD)/meter_multical603.o \
$(BUILD)/meter_multical803.o \
$(BUILD)/meter_omnipower.o \
$(BUILD)/meter_piigth.o \
$(BUILD)/meter_q400.o \
$(BUILD)/meter_qcaloric.o \
$(BUILD)/meter_qheat.o \
$(BUILD)/meter_rfmamb.o \
$(BUILD)/meter_rfmtx1.o \
$(BUILD)/meter_sharky.o \
$(BUILD)/meter_supercom587.o \
$(BUILD)/meter_sontex868.o \
$(BUILD)/meter_topaseskr.o \
$(BUILD)/meter_tsd2.o \
$(BUILD)/meter_ultrimis.o \
$(BUILD)/meter_vario451.o \
$(BUILD)/meter_waterstarm.o \
$(BUILD)/meter_whe46x.o \
$(BUILD)/meter_whe5x.o \
$(BUILD)/meter_sensostar.o \
$(BUILD)/meter_gransystems_ccx01.o \
$(BUILD)/meter_lse_08.o \
$(BUILD)/meter_weh_07.o \
$(BUILD)/meter_unismart.o \
$(BUILD)/meter_munia.o \
all: $(BUILD)/wmbusmeters $(BUILD)/wmbusmetersd $(BUILD)/wmbusmeters.g $(BUILD)/wmbusmeters-admin $(BUILD)/testinternals
deb: wmbusmeters_$(DEBVERSION)_$(DEBARCH).deb
check_deb:
lintian --no-tag-display-limit wmbusmeters_$(DEBVERSION)_$(DEBARCH).deb
check_docs:
@cat src/cmdline.cc | grep -o -- '--[a-z][a-z]*' | sort | uniq | grep -v internaltesting > /tmp/options_in_code
@cat wmbusmeters.1 | grep -o -- '--[a-z][a-z]*' | sort | uniq | grep -v internaltesting > /tmp/options_in_man
@cat README.md | grep -o -- '--[a-z][a-z]*' | sort | uniq | grep -v internaltesting > /tmp/options_in_readme
@./build/wmbusmeters --help | grep -o -- '--[a-z][a-z]*' | sort | uniq | grep -v internaltesting > /tmp/options_in_binary
@diff /tmp/options_in_code /tmp/options_in_man || echo CODE_VS_MAN
@diff /tmp/options_in_code /tmp/options_in_readme || echo CODE_VS_README
@diff /tmp/options_in_code /tmp/options_in_binary || echo CODE_VS_BINARY
@echo "OK docs"
install: $(BUILD)/wmbusmeters check_docs
@./install.sh $(BUILD)/wmbusmeters $(DESTDIR) $(EXTRA_INSTALL_OPTIONS)
uninstall:
@./uninstall.sh /
wmbusmeters_$(DEBVERSION)_$(DEBARCH).deb:
@./deb/create_deb.sh $(BUILD) $@ $(DEBVERSION) $(DEBARCH)
snapcraft:
snapcraft
$(BUILD)/main.o: $(BUILD)/short_manual.h $(BUILD)/version.h
# Build binary with debug information. ~15M size binary.
$(BUILD)/wmbusmeters.g: $(METER_OBJS) $(BUILD)/main.o $(BUILD)/short_manual.h
$(CXX) -o $(BUILD)/wmbusmeters.g $(METER_OBJS) $(BUILD)/main.o $(LDFLAGS) -lrtlsdr $(USBLIB) -lpthread
# Production build will have debug information stripped. ~1.5M size binary.
# DEBUG=true builds, which has address sanitizer code, will always keep the debug information.
$(BUILD)/wmbusmeters: $(BUILD)/wmbusmeters.g
cp $(BUILD)/wmbusmeters.g $(BUILD)/wmbusmeters
$(STRIP_BINARY)
$(BUILD)/wmbusmetersd: $(BUILD)/wmbusmeters
cp $(BUILD)/wmbusmeters $(BUILD)/wmbusmetersd
ifeq ($(shell uname -s),Darwin)
$(BUILD)/wmbusmeters-admin:
touch $(BUILD)/wmbusmeters-admin
else
$(BUILD)/wmbusmeters-admin: $(METER_OBJS) $(BUILD)/admin.o $(BUILD)/ui.o $(BUILD)/short_manual.h
$(CXX) -o $(BUILD)/wmbusmeters-admin.g $(METER_OBJS) $(BUILD)/admin.o $(BUILD)/ui.o $(LDFLAGS) -lmenu -lform -lncurses -lrtlsdr $(USBLIB) -lpthread
endif
$(BUILD)/short_manual.h: README.md
echo 'R"MANUAL(' > $(BUILD)/short_manual.h
sed -n '/wmbusmeters version/,/```/p' README.md \
| grep -v 'wmbusmeters version' \
| grep -v '```' >> $(BUILD)/short_manual.h
echo ')MANUAL";' >> $(BUILD)/short_manual.h
$(BUILD)/testinternals: $(METER_OBJS) $(BUILD)/testinternals.o
$(CXX) -o $(BUILD)/testinternals $(METER_OBJS) $(BUILD)/testinternals.o $(LDFLAGS) -lrtlsdr $(USBLIB) -lpthread
$(BUILD)/fuzz: $(METER_OBJS) $(BUILD)/fuzz.o
$(CXX) -o $(BUILD)/fuzz $(METER_OBJS) $(BUILD)/fuzz.o $(LDFLAGS) -lrtlsdr -lpthread
clean:
rm -rf build/* build_arm/* build_debug/* build_arm_debug/* *~
clean_cc:
find . -name "*.gcov" -delete
find . -name "*.gcda" -delete
# This generates annotated source files ending in .gcov
# inside the build_debug where non-executed source lines are marked #####
gcov:
@if [ "$(DEBUG)" = "" ]; then echo "You have to run \"make gcov DEBUG=true\""; exit 1; fi
$(GCOV) -o build_debug $(METER_OBJS)
mv *.gcov build_debug
lcov:
@if [ "$(DEBUG)" = "" ]; then echo "You have to run \"make lcov DEBUG=true\""; exit 1; fi
lcov --directory . -c --no-external --output-file build_debug/lcov.info
(cd build_debug; genhtml lcov.info)
xdg-open build_debug/src/index.html
test:
@./test.sh build/wmbusmeters
testd:
@./test.sh build_debug/wmbusmeters
update_manufacturers:
iconv -f utf-8 -t ascii//TRANSLIT -c DLMS_Flagids.csv -o tmp.flags
cat tmp.flags | grep -v ^# | cut -f 1 > list.flags
cat tmp.flags | grep -v ^# | cut -f 2 > names.flags
cat tmp.flags | grep -v ^# | cut -f 3 > countries.flags
cat countries.flags | sort -u | grep -v '^$$' > uniquec.flags
cat names.flags | tr -d "'" | tr -c 'a-zA-Z0-9\n' ' ' | tr -s ' ' | sed 's/^ //g' | sed 's/ $$//g' > ansi.flags
cat ansi.flags | sed 's/\(^.......[^0123456789]*\)[0123456789]\+.*/\1/g' > cleaned.flags
cat cleaned.flags | sed -e "$$(sed 's:.*:s/&//Ig:' uniquec.flags)" > cleanedc.flags
cat cleanedc.flags | sed \
-e 's/ ab\( \|$$\)/ /Ig' \
-e 's/ ag\( \|$$\)/ /Ig' \
-e 's/ a \?s\( \|$$\)/ /Ig' \
-e 's/ co\( \|$$\)/ /Ig' \
-e 's/ b \?v\( \|$$\)/ /Ig' \
-e 's/ bvba\( \|$$\)/ /Ig' \
-e 's/ corp\( \|$$\)/ /Ig' \
-e 's/ d \?o \?o\( \|$$\)/ /g' \
-e 's/ d \?d\( \|$$\)/ /g' \
-e 's/ gmbh//Ig' \
-e 's/ gbr//Ig' \
-e 's/ inc\( \|$$\)/ /Ig' \
-e 's/ kg\( \|$$\)/ /Ig' \
-e 's/ llc/ /Ig' \
-e 's/ ltd//Ig' \
-e 's/ limited//Ig' \
-e 's/ nv\( \|$$\)/ /Ig' \
-e 's/ oy//Ig' \
-e 's/ ood\( \|$$\)/ /Ig' \
-e 's/ooo\( \|$$\)/ /Ig' \
-e 's/ pvt\( \|$$\)/ /Ig' \
-e 's/ pte\( \|$$\)/ /Ig' \
-e 's/ pty\( \|$$\)/ /Ig' \
-e 's/ plc\( \|$$\)/ /Ig' \
-e 's/ private\( \|$$\)/ /Ig' \
-e 's/ s \?a\( \|$$\)/ /Ig' \
-e 's/ sarl\( \|$$\)/ /Ig' \
-e 's/ sagl\( \|$$\)/ /Ig' \
-e 's/ s c ul//Ig' \
-e 's/ s \?l\( \|$$\)/ /Ig' \
-e 's/ s \?p \?a\( \|$$\)/ /Ig' \
-e 's/ sp j\( \|$$\)/ /Ig' \
-e 's/ sp z o o//Ig' \
-e 's/ s r o//Ig' \
-e 's/ s \?r \?l//Ig' \
-e 's/ ug\( \|$$\)/ /Ig' \
> trimmed.flags
cat trimmed.flags | tr -s ' ' | sed 's/^ //g' | sed 's/ $$//g' > done.flags
paste -d '|,' list.flags done.flags countries.flags | sed 's/,/, /g' | sed 's/ |/|/g' > manufacturers.txt
echo '#ifndef MANUFACTURERS_H' > m.h
echo '#define MANUFACTURERS_H' >> m.h
echo '#define MANFCODE(a,b,c) ((a-64)*1024+(b-64)*32+(c-64))' >> m.h
echo "#define LIST_OF_MANUFACTURERS \\" >> m.h
cat manufacturers.txt | sed -e "s/\(.\)\(.\)\(.\).\(.*\)/X(\1\2\3,MANFCODE('\1','\2','\3'),\"\4\")\\\\/g" | sed 's/, ")/")/' >> m.h
echo >> m.h
cat manufacturers.txt | sed -e "s/\(.\)\(.\)\(.\).*/#define MANUFACTURER_\1\2\3 MANFCODE('\1','\2','\3')/g" >> m.h
echo >> m.h
echo '#endif' >> m.h
mv m.h src/manufacturers.h
rm *.flags manufacturers.txt
GCC_MAJOR_VERSION:=$(shell gcc --version | head -n 1 | sed 's/.* \([0-9]\)\.[0-9]\.[0-9]$$/\1/')
AFL_HOME:=AFLplusplus
$(AFL_HOME)/src/afl-cc.c:
mkdir -p AFLplusplus
if ! dpkg -s gcc-$(GCC_MAJOR_VERSION)-plugin-dev 2>/dev/null >/dev/null ; then echo "Please run: sudo apt install gcc-$(GCC_MAJOR_VERSION)-plugin-dev"; exit 1; fi
git clone https://github.com/AFLplusplus/AFLplusplus.git
afl_prepared: AFLplusplus/src/afl-cc.c
(cd AFLplusplus; make)
touch afl_prepared
build_fuzz: afl_prepared
$(MAKE) AFL_HARDEN=1 CXX=$(AFL_HOME)/afl-g++-fast FUZZFLAGS=-DFUZZING=true $(BUILD)/fuzz
$(MAKE) AFL_HARDEN=1 CXX=$(AFL_HOME)/afl-g++-fast FUZZFLAGS=-DFUZZING=true $(BUILD)/wmbusmeters
run_fuzz_difvifparser:
${AFL_HOME}/afl-fuzz -i fuzz_testcases/difvifparser -o fuzz_findings_difvifparser/ build/fuzz
run_fuzz_telegrams: extract_fuzz_telegram_seeds
${AFL_HOME}/afl-fuzz -i fuzz_testcases/telegrams -o fuzz_findings_telegrams/ build/wmbusmeters --listento=any stdin
extract_fuzz_telegram_seeds:
@cat simulations/simulation_* | grep "^telegram=" | tr -d '|' | sed 's/^telegram=//' > $(BUILD)/seeds
@mkdir -p fuzz_testcases/telegrams
@rm -f fuzz_testcases/telegrams/seed_*
@SEED=1; while read -r line; do echo "$${line}" | xxd -r -p - > "fuzz_testcases/telegrams/seed_$${SEED}"; SEED=$$((SEED + 1)); done < $(BUILD)/seeds; echo "Extracted $${SEED} seeds from simulations."
relay: utils/relay.c
gcc -g utils/relay.c -o relay -O0 -ggdb -fsanitize=address -fno-omit-frame-pointer -fprofile-arcs -ftest-coverage
# Include dependency information generated by gcc in a previous compile.
include $(wildcard $(patsubst %.o,%.d,$(METER_OBJS)))