summaryrefslogtreecommitdiff
blob: c6ab881bf96e0ee0e91d7dae5911c9e01a234fef (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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# Example commands:
#
#   make
#   make test
#       Runs all tests.
#
#   make test-exe
#       Runs exe regression tests. These use $(gs) and $(mutool) to generate
#       intermediate data about pdf content, then uses $(exe) to convert to
#       docx.
#
#   make test-mutool
#       Runs mutool regression tests. This uses $(mutool) to convert directly
#       from pdf to docx. We require that $(mutool) was built with extract=yes.
#
#   make test-gs
#       Runs gs regression tests. This uses $(gs) to convert directly from pdf
#       to docx. We require that $(gs) was built with --with-extract-dir=... We
#       also do a simple test of output-file-per-page.
#
#   make test-tables
#       Tests handling of tables, using mutool with docx device's html output.
#
#   make test-buffer test-misc test-src
#       Runs unit tests etc.
#
#   make build=debug-opt ...
#       Set build flags.
#
#   make build=memento msqueeze
#       Run memento squeeze test.
#
#   Assuming we are in mupdf/thirdparty/extract, and there is a ghostpdl at
#   the same level as mupdf, with a softlink from ghostpdl to extract:
#
#   make test-rebuild-dependent-binaries
#       Clean/Configure/Rebuild the required mupdf and gs binaries.
#
#   make test-build-dependent-binaries
#       Build the required mupdf and gs binaries.


# Build flags.
#
# Note that OpenBSD's clang-8 appears to ignore -Wdeclaration-after-statement.
#
build = debug

flags_link      = -W -Wall -lm
flags_compile   = -W -Wall -Wextra -Wpointer-sign -Wmissing-declarations -Wmissing-prototypes -Wdeclaration-after-statement -Wpointer-arith -Wconversion -Wno-sign-conversion -Werror -MMD -MP

uname = $(shell uname)

ifeq ($(build),)
    $(error Need to specify build=debug|opt|debug-opt|memento)
else ifeq ($(build),debug)
    flags_link      += -g
    flags_compile   += -g
else ifeq ($(build),opt)
    flags_link      += -O2
    flags_compile   += -O2
else ifeq ($(build),debug-opt)
    flags_link      += -g -O2
    flags_compile   += -g -O2
else ifeq ($(build),memento)
    flags_link      += -g -dl
    ifeq ($(uname),OpenBSD)
        flags_link += -l execinfo
    endif
    flags_compile   += -g -D MEMENTO
else
    $(error unrecognised $$(build)=$(build))
endif

gdb = gdb
ifeq ($(uname),OpenBSD)
    flags_link += -L /usr/local/lib -l execinfo
    $(warning have added -L /usr/local/lib)
    gdb = egdb
    # For some reason OpenBSD's gmake defaults CXX to g++, which is not helpful.
    CXX = c++
endif

# Locations of mutool and gs. By default we assume these are not available.
#
# If this extract checkout is within a mupdf tree (typically as a git
# submodule) we assume ghostpdl is checked out nearby and both mutool gs and gs
# binaries are available and built with extract enabled.
#
# Disable this by running: make we_are_mupdf_thirdparty= ...
#
we_are_mupdf_thirdparty = $(findstring /mupdf/thirdparty/extract, $(abspath .))
ifneq ($(we_are_mupdf_thirdparty),)
    $(warning we are mupdf thirdparty)
    ifeq ($(build),memento)
        mutool := ../../build/memento/mutool
    else
        mutool := ../../build/debug/mutool
    endif
    gs := ../../../ghostpdl/debug-extract-bin/gs
    libbacktrace = ../../../libbacktrace/.libs
endif

# If mutool/gs are specified, they must exist.
#
ifneq ($(mutool),)
ifeq ($(wildcard $(mutool)),)
    $(error mutool does not exist: $(mutool))
endif
$(warning mutool=$(mutool))
endif

ifeq ($(build),memento)
    mutool_run := MEMENTO_ABORT_ON_LEAK=1 $(mutool)
else
    mutool_run := $(mutool)
endif

ifneq ($(gs),)
ifeq ($(wildcard $(gs)),)
    $(error gs does not exist: $(gs))
endif
$(warning gs=$(gs))
endif


# Default target - run all tests.
#
test: test-buffer test-misc test-src test-exe test-mutool test-gs test-html test-tables
	@echo $@: passed

# Define the main test targets.
#
# test/Python2clipped.pdf is same as test/Python2.pdf except it as a modified
# MediaBox that excludes some glyphs.
#
pdfs = test/Python2.pdf test/Python2clipped.pdf test/zlib.3.pdf test/text_graphic_image.pdf
pdfs_generated = $(patsubst test/%, test/generated/%, $(pdfs))

# Generate targets that check all combinations of mu/gs and the various
# rotate/autosplit options of extract-exe.
#
tests_exe :=
ifneq ($(mutool),)
    tests_exe := $(tests_exe) $(patsubst %, %.intermediate-mu.xml, $(pdfs_generated))
endif
ifneq ($(gs),)
# 2022-02-23: don't check intermediate-gs, because gs's txtwrite device doesn't
# work easily with multi-page documents since the change to pdfi.
#    tests_exe := $(tests_exe) $(patsubst %, %.intermediate-gs.xml, $(pdfs_generated))
endif

tests_exe := \
        $(patsubst %, %.extract.docx,                   $(tests_exe)) \
        $(patsubst %, %.extract-rotate.docx,            $(tests_exe)) \
        $(patsubst %, %.extract-rotate-spacing.docx,    $(tests_exe)) \
        $(patsubst %, %.extract-autosplit.docx,         $(tests_exe)) \
        $(patsubst %, %.extract-template.docx,          $(tests_exe)) \

tests_exe := $(patsubst %, %.diff, $(tests_exe))

ifneq ($(mutool),)
# Targets that test direct conversion with mutool.
#
    tests_mutool := \
            $(patsubst %, %.mutool.docx.diff, $(pdfs_generated)) \
            $(patsubst %, %.mutool-norotate.docx.diff, $(pdfs_generated)) \
            $(patsubst %, %.mutool.odt.diff, $(pdfs_generated)) \
            $(patsubst %, %.mutool.text.diff, $(pdfs_generated)) \

    tests_mutool_odt := \
            $(patsubst %, %.mutool.odt.diff, $(pdfs_generated)) \

    tests_mutool_text := \
            $(patsubst %, %.mutool.text.diff, $(pdfs_generated)) \

    tests_html := test/generated/table.pdf.mutool.html.diff
endif
ifneq ($(gs),)
# Targets that test direct conversion with gs.
#
    tests_gs := \
            $(patsubst %, %.gs.docx.diff, $(pdfs_generated)) \
            test_gs_fpp

    # We don't yet do clipping with gs so exclude Python2clipped.pdf.*:
    tests_gs := $(filter-out test/generated/Python2clipped.pdf.%, $(tests_gs))

    #$(warning tests_gs: $(tests_gs))
endif
#$(warning $(pdfs_generated_intermediate_docx_diffs))
#$(warning $(tests))

test-exe: $(tests_exe)
	@echo $@: passed

# Checks output of mutool conversion from .pdf to .docx/.odt.
#
test-mutool: $(tests_mutool)
	@echo $@: passed

# Checks output of mutool conversion from .pdf to .odt.
#
test-mutool-odt: $(tests_mutool_odt)
	@echo $@: passed

# Checks output of mutool conversion from .pdf to .text.
#
test-mutool-text: $(tests_mutool_text)
	@echo $@: passed

# Checks output of gs conversion from .pdf to .docx. Requires that gs was built
# with extract as a third-party library. As of 2021-02-10 this requires, for
# example ghostpdl/extract being a link to an extract checkout and configuring
# with --with-extract-dir=extract.
#
test-gs: $(tests_gs)
	@echo $@: passed

# Check behaviour of gs when writing file-per-page.
#
test_gs_fpp: $(gs)
	@echo
	@echo == Testing gs file-page-page
	rm test/generated/text_graphic_image.pdf.gs.*.docx || true
	$(gs) -sDEVICE=docxwrite -o test/generated/Python2.pdf.gs.%i.docx test/Python2.pdf
	rm test/generated/text_graphic_image.pdf.gs.*.docx || true
	$(gs) -sDEVICE=docxwrite -o test/generated/zlib.3.pdf.gs.%i.docx test/zlib.3.pdf
	rm test/generated/text_graphic_image.pdf.gs.*.docx || true
	$(gs) -sDEVICE=docxwrite -o test/generated/text_graphic_image.pdf.gs.%i.docx test/text_graphic_image.pdf
	@echo Checking for correct number of generated files.
	ls -l test/generated/*.pdf.gs.*.docx
	ls test/generated/text_graphic_image.pdf.gs.*.docx | wc -l | grep '^ *1$$'
	ls test/generated/Python2.pdf.gs.*.docx | wc -l | grep '^ *1$$'
	ls test/generated/zlib.3.pdf.gs.*.docx | wc -l | grep '^ *2$$'


test-html: $(tests_html)

test-rebuild-dependent-binaries:
	@echo == Rebuilding gs and mupdf binaries
	cd ../../../ghostpdl && ./autogen.sh --with-extract-dir=extract && make -j 8 debugclean DEBUGDIRPREFIX=debug-extract- && make -j 8 debug DEBUGDIRPREFIX=debug-extract-
	cd ../.. && make -j 8 build=debug clean && make -j 8 build=debug

test-build-dependent-binaries:
	@echo == Building gs and mupdf binaries
	cd ../../../ghostpdl && make -j 8 debug DEBUGDIRPREFIX=debug-extract-
	cd ../.. && make -j 8 build=debug


ifneq ($(mutool),)
    test_tables_pdfs = \
            test/agstat.pdf \
            test/background_lines_1.pdf \
            test/background_lines_2.pdf \
            test/column_span_1.pdf \
            test/column_span_2.pdf \
            test/electoral_roll.pdf \
            test/rotated.pdf \
            test/row_span.pdf \
            test/table.pdf \
            test/twotables_1.pdf \
            test/twotables_2.pdf \

    test_tables_generated = $(patsubst test/%, test/generated/%, $(test_tables_pdfs))

    test_tables_html    = $(patsubst test/%.pdf, test/generated/%.pdf.mutool.html.diff, $(test_tables_pdfs))
    test_tables_docx    = $(patsubst test/%.pdf, test/generated/%.pdf.mutool.docx.diff, $(test_tables_pdfs))
    test_tables_odt     = $(patsubst test/%.pdf, test/generated/%.pdf.mutool.odt.diff,  $(test_tables_pdfs))

    test_tables = $(test_tables_html) $(test_tables_docx) $(test_tables_odt)
endif

test-tables-html:       $(test_tables_html)
test-tables-docx:       $(test_tables_docx)
test-tables-odt:        $(test_tables_odt)

test-tables:            $(test_tables)
	@echo $@: passed

test/generated/%.pdf.mutool.html.diff: test/generated/%.pdf.mutool.html test/%.pdf.mutool.html.ref
	@echo
	@echo == Checking $<
	diff -u $^

test/generated/%.pdf.mutool.cv.html.diff: test/generated/%.pdf.mutool.cv.html test/%.pdf.mutool.html.ref
	@echo
	@echo == Checking $<
	diff -u $^

test/generated/%.pdf.mutool.cv.html: test/%.pdf $(mutool)
	$(mutool) convert -O resolution=300 -o $<..png $<
	EXTRACT_OPENCV_IMAGE_BASE=$< $(mutool_run) convert -F docx -O html -o $@ $<

test/generated/%.pdf.mutool.text.diff: test/generated/%.pdf.mutool.text test/%.pdf.mutool.text.ref
	@echo
	@echo == Checking $<
	diff -u $^


# Main executable.
#
exe = src/build/extract-$(build).exe
exe_src = \
        src/alloc.c \
        src/astring.c \
        src/boxer.c \
        src/buffer.c \
        src/document.c \
        src/docx.c \
        src/docx_template.c \
        src/extract-exe.c \
        src/extract.c \
        src/html.c \
        src/join.c \
        src/mem.c \
        src/odt.c \
        src/odt_template.c \
        src/outf.c \
        src/rect.c \
        src/sys.c \
        src/text.c \
        src/xml.c \
        src/zip.c \


ifeq ($(build),memento)
    exe_src += src/memento.c
    ifeq ($(uname),Linux)
        flags_compile += -D HAVE_LIBDL
        flags_link += -L $(libbacktrace) -l backtrace -l dl
    endif
endif
exe_obj := $(exe_src)
exe_obj := $(patsubst src/%.c, src/build/%.c-$(build).o, $(exe_obj))
exe_obj := $(patsubst src/%.cpp, src/build/%.cpp-$(build).o, $(exe_obj))
exe_dep = $(exe_obj:.o=.d)
exe: $(exe)
$(exe): $(exe_obj)
	$(CXX) $(flags_link) -o $@ $^ -lz -lm

run_exe = $(exe)
ifeq ($(build),memento)
    ifeq ($(uname),Linux)
        run_exe = MEMENTO_ABORT_ON_LEAK=1 MEMENTO_HIDE_MULTIPLE_REALLOCS=1 LD_LIBRARY_PATH=$(libbacktrace) $(exe)
        #run_exe = LD_LIBRARY_PATH=../libbacktrace/.libs $(exe)
    endif
    ifeq ($(uname),OpenBSD)
        run_exe = MEMENTO_ABORT_ON_LEAK=1 MEMENTO_HIDE_MULTIPLE_REALLOCS=1 $(exe)
    endif
endif

exe_tables = src/build/extract-tables-$(build).exe
exe-tables: $(exe_tables)
exe-tables-test: $(exe_tables)
	$< test/agstat.pdf

ifeq (0,1)
# Do not commit changes to above line.
#
# Special rules for populating .ref directories with current output. Useful to
# initialise references outputs for new output type.
#
test/%.docx.dir.ref/: test/generated/%.docx.dir/
	rsync -ai $< $@
test/%.odt.dir.ref/: test/generated/%.odt.dir/
	rsync -ai $< $@
test/%.text.ref: test/generated/%.text
	rsync -ai $< $@

_update_tables_leafs = $(patsubst test/%, %, $(test_tables_pdfs))
# Update all table docx reference outputs.
#
_update-docx-tables:
	for i in $(_update_tables_leafs); do rsync -ai test/generated/$$i.mutool.docx.dir/ test/$$i.mutool.docx.dir.ref/; done
# Update all table odt reference outputs.
#
_update-odt-tables:
	for i in $(_update_tables_leafs); do rsync -ai test/generated/$$i.mutool.odt.dir/ test/$$i.mutool.odt.dir.ref/; done
endif

# Rules that make the various intermediate targets required by $(tests).
#

test/generated/%.pdf.intermediate-mu.xml: test/%.pdf $(mutool)
	@echo
	@echo == Generating intermediate file for $< with mutool.
	@mkdir -p test/generated
	$(mutool_run) draw -F xmltext -o $@ $<

test/generated/%.pdf.intermediate-gs.xml: test/%.pdf $(gs)
	@echo
	@echo == Generating intermediate file for $< with gs.
	@mkdir -p test/generated
	$(gs) -sDEVICE=txtwrite -dTextFormat=4 -o $@ $<

%.extract.docx: % $(exe)
	@echo
	@echo == Generating docx with extract.exe
	$(run_exe) -r 0 -i $< -f docx -o $@

%.extract.odt: % $(exe)
	@echo
	@echo == Generating odt with extract.exe
	$(run_exe) -r 0 -i $< -f odt -o $@

%.extract-rotate.docx: % $(exe) Makefile
	@echo
	@echo == Generating docx with rotation with extract.exe
	$(run_exe) -r 1 -s 0 -i $< -f docx -o $@

%.extract-rotate-spacing.docx: % $(exe) Makefile
	@echo
	@echo == Generating docx with rotation with extract.exe
	$(run_exe) -r 1 -s 1 -i $< -f docx -o $@

%.extract-autosplit.docx: % $(exe)
	@echo
	@echo == Generating docx with autosplit with extract.exe
	$(run_exe) -r 0 -i $< -f docx --autosplit 1 -o $@

%.extract-template.docx: % $(exe)
	@echo
	@echo == Generating docx using src/template.docx with extract.exe
	$(run_exe) -r 0 -i $< -f docx -t src/template.docx -o $@

test/generated/%.diff: test/generated/%.dir/ test/%.dir.ref/
	@echo
	@echo == Checking $<
	diff -ru $^
#if diff -ruq $^; then true; else echo "@@@ failure... fix with: rsync -ai" $^; false; fi

test/generated/%.html.diff: test/generated/%.html test/%.html.ref
	@echo
	@echo == Checking $<
	diff -u $^

# This checks that -t src/template.docx gives identical results.
#
test/generated/%.extract-template.docx.diff: test/generated/%.extract-template.docx.dir/ test/%.extract.docx.dir.ref/
	@echo
	@echo == Checking $<
	diff -ru $^

# Unzips .docx into .docx.dir/ directory, and prettyfies the .xml files.
#
# Note that we requires a trailing '/' in target.
#
%.docx.dir/: %.docx .ALWAYS
	@echo
	@echo == Extracting .docx into directory.
	@rm -r $@ 2>/dev/null || true
	unzip -q -d $@ $<

# Unzips .odt into .odt.dir/ directory, and prettyfies the .xml files.
#
# Note that we requires a trailing '/' in target.
#
%.odt.dir/: %.odt
	@echo
	@echo == Extracting .odt into directory.
	@rm -r $@ 2>/dev/null || true
	unzip -q -d $@ $<

%.xml.pretty.xml: %.xml
	xmllint --format $< > $@

# Uses zip to create .docx file by zipping up a directory. Useful to recreate
# .docx from reference directory test/*.docx.dir.ref.
%.docx: %
	@echo
	@echo == Zipping directory into .docx file.
	@rm -r $@ 2>/dev/null || true
	cd $< && zip -r ../$(notdir $@) .

# Uses zip to create .odt file by zipping up a directory. Useful to recreate
# .docx from reference directory test/*.odt.dir.ref.
%.odt: %
	@echo
	@echo == Zipping directory into .odt file.
	@rm -r $@ 2>/dev/null || true
	cd $< && zip -r ../$(notdir $@) .

# Prettifies each .xml file within .docx.dir/ directory.
%.docx.dir.pretty: %.docx.dir/
	@rm -r $@ $@- 2>/dev/null || true
	cp -pr $< $@-
	./src/docx_template_build.py --pretty $@-
	mv $@- $@

# Converts .pdf directly to .docx using mutool.
test/generated/%.pdf.mutool.docx: test/%.pdf $(mutool)
	@echo
	@echo == Converting .pdf directly to .docx using mutool.
	@mkdir -p test/generated
	$(mutool_run) convert -O mediabox-clip=yes -o $@ $<

test/generated/%.pdf.mutool-norotate.docx: test/%.pdf $(mutool)
	@echo
	@echo == Converting .pdf directly to .docx using mutool.
	@mkdir -p test/generated
	$(mutool_run) convert -O mediabox-clip=yes,rotation=no -o $@ $<

test/generated/%.pdf.mutool-spacing.docx: test/%.pdf $(mutool)
	@echo
	@echo == Converting .pdf directly to .docx using mutool.
	@mkdir -p test/generated
	$(mutool_run) convert -O mediabox-clip=yes,spacing=yes -o $@ $<

# Converts .pdf directly to .docx using gs.
test/generated/%.pdf.gs.docx: test/%.pdf $(gs)
	@echo
	@echo == Converting .pdf directly to .docx using gs.
	@mkdir -p test/generated
	$(gs) -sDEVICE=docxwrite -o $@ $<

# Converts .pdf directly to .odt using mutool.
test/generated/%.pdf.mutool.odt: test/%.pdf $(mutool)
	@echo
	@echo == Converting .pdf directly to .odt using mutool.
	@mkdir -p test/generated
	$(mutool_run) convert -O mediabox-clip=no -o $@ $<

# Converts .pdf directly to .html using mutool 
test/generated/%.pdf.mutool.html: test/%.pdf $(mutool)
	@echo
	@echo == Converting .pdf directly to .html using mutool.
	@mkdir -p test/generated
	$(mutool_run) convert -F docx -O html -o $@ $<

# Converts .pdf directly to .text using mutool 
test/generated/%.pdf.mutool.text: test/%.pdf $(mutool)
	@echo
	@echo == Converting .pdf directly to .text using mutool.
	@mkdir -p test/generated
	$(mutool_run) convert -F docx -O text -o $@ $<

# Valgrind test
#
valgrind: $(exe) test/generated/Python2.pdf.intermediate-mu.xml
	valgrind --leak-check=full $(exe) -h -r 1 -s 0 -i test/generated/Python2.pdf.intermediate-mu.xml -o test/generated/valgrind-out.docx
	@echo $@: passed

# Memento tests.
#
ifeq ($(build),memento)
mutool_memento_extract = ../../build/memento/mutool
memento_failat_gdb := $(gdb) -ex 'b Memento_breakpoint' -ex r -ex c -ex bt --args

# Memento squeeze with test/text_graphic_image.pdf runs quickly - just 2,100 events taking 20s.
#
# test/Python2.pdf is much slower - 301,900 events, taking around 8h.
#
msqueeze-mutool-docx:
	MEMENTO_SQUEEZEAT=1 ./src/memento.py -q 100 $(mutool_run) convert -o $@.docx test/text_graphic_image.pdf
msqueeze-mutool-docx-failat:
	MEMENTO_FAILAT=1960 $(memento_failat_gdb) $(mutool) convert -o $@.docx test/text_graphic_image.pdf
msqueeze-mutool-odt:
	MEMENTO_SQUEEZEAT=1 ./src/memento.py -q 100 $(mutool_run) convert -o $@.docx test/text_graphic_image.pdf
msqueeze-mutool-odt2:
	MEMENTO_SQUEEZEAT=4000 ./src/memento.py -q 100 $(mutool_run) convert -o $@.docx test/Python2.pdf
msqueeze-mutool-table:
	MEMENTO_SQUEEZEAT=1 ./src/memento.py -q 100 $(mutool_run) convert -F docx -O html -o $@.html test/agstat.pdf
msqueeze-mutool-table-docx:
	MEMENTO_SQUEEZEAT=1 ./src/memento.py -q 100 $(mutool_run) convert -o $@.docx test/agstat.pdf
msqueeze-mutool-table-odt:
	MEMENTO_SQUEEZEAT=1 ./src/memento.py -q 100 $(mutool_run) convert -o $@.odt test/agstat.pdf
msqueeze-mutool-table-failat:
	MEMENTO_FAILAT=296643 MEMENTO_HIDE_MULTIPLE_REALLOCS=1 $(gdb) -ex 'b Memento_breakpoint' -ex r -ex c -ex bt --args $(mutool_memento_extract) convert -F docx -O html -o $@.html test/agstat.pdf
endif


# Temporary rules for generating reference files.
#
#temp_ersdr = \
#        $(patsubst %, %.intermediate-mu.xml.extract-rotate-spacing.docx.dir.ref, $(pdfs)) \
#        $(patsubst %, %.intermediate-gs.xml.extract-rotate-spacing.docx.dir.ref, $(pdfs)) \
#
#temp: $(temp_ersdr)
#test/%.xml.extract-rotate-spacing.docx.dir.ref: test/generated/%.xml.extract-rotate-spacing.docx.dir
#	@echo
#	@echo copying $< to %@
#	rsync -ai $</ $@/


# Buffer unit test.
#
exe_buffer_test = src/build/buffer-test-$(build).exe
exe_buffer_test_src = src/buffer.c src/buffer-test.c src/outf.c src/alloc.c src/mem.c
ifeq ($(build),memento)
    exe_buffer_test_src += src/memento.c
endif
exe_buffer_test_obj = $(patsubst src/%.c, src/build/%.c-$(build).o, $(exe_buffer_test_src))
exe_buffer_test_dep = $(exe_buffer_test_obj:.o=.d)
$(exe_buffer_test): $(exe_buffer_test_obj)
	$(CC) $(flags_link) -o $@ $^
test-buffer: $(exe_buffer_test)
	@echo
	@echo == Running test-buffer
	mkdir -p test/generated
	./$<
	@echo $@: passed
test-buffer-valgrind: $(exe_buffer_test)
	@echo
	@echo == Running test-buffer with valgrind
	mkdir -p test/generated
	valgrind --leak-check=full ./$<
	@echo $@: passed

ifeq ($(build),memento)
test-buffer-msqueeze: $(exe_buffer_test)
	MEMENTO_SQUEEZEAT=1 ./src/memento.py -q 1 ./$<
endif

# Misc unit test.
#
exe_misc_test = src/build/misc-test-$(build).exe
exe_misc_test_src = \
        src/alloc.c \
        src/astring.c \
        src/buffer.c \
        src/mem.c \
        src/misc-test.c \
        src/outf.c \
        src/xml.c \

ifeq ($(build),memento)
    exe_misc_test_src += src/memento.c
endif
exe_misc_test_obj = $(patsubst src/%.c, src/build/%.c-$(build).o, $(exe_misc_test_src))
exe_misc_test_dep = $(exe_buffer_test_obj:.o=.d)
$(exe_misc_test): $(exe_misc_test_obj)
	$(CC) $(flags_link) -o $@ $^
test-misc: $(exe_misc_test)
	@echo
	@echo == Running test-misc
	./$<
	@echo $@: passed

# Source code check.
#
test-src:
	@echo
	@echo == Checking for use of ssize_t in source.
	if grep -wn ssize_t src/*.c src/*.h include/*.h; then false; else true; fi
	@echo == Checking for use of strdup in source.
	if grep -wn strdup `ls -d src/*.c src/*.h|grep -v src/memento.` include; then false; else true; fi
	@echo == Checking for use of bzero in source.
	if grep -wn bzero src/*.c src/*.h include/*.h; then false; else true; fi
	@echo Checking for variables defined inside for-loop '(...)'.
	if egrep -wn 'for *[(] *[a-zA-Z0-9]+ [a-zA-Z0-9]' src/*.c src/*.h; then false; else true; fi
	@echo $@: passed

# Check that all defined global symbols start with 'extract_'. This is not
# included in the overall 'test' target because the use of '!egrep ...' appears
# to break on some cluster machines.
#
test-obj:
	@echo
	nm -egPC $(exe_obj) | egrep '^[a-zA-Z0-9_]+ T' | grep -vw ^main | ! egrep -v ^extract_
	@echo $@: passed

# Compile rule. We always include src/docx_template.c as a prerequisite in case
# code #includes docx_template.h. We use -std=gnu90 to catch 'ISO C90 forbids
# mixing declarations and code' errors while still supporting 'inline'.
#
src/build/%.c-$(build).o: src/%.c src/docx_template.c src/odt_template.c 
	@mkdir -p src/build
	$(CC) -std=gnu90 -c $(flags_compile) -o $@ $<

src/build/%.cpp-$(build).o: src/%.cpp
	@mkdir -p src/build
	$(CXX) -c -Wall -W -I /usr/local/include/opencv4 -o $@ $<

# Rule for machine-generated source code, src/docx_template.c. Also generates
# src/docx_template.h.
#
# These files are also in git to allow builds if python is not available.
#
src/docx_template.c: src/docx_template_build.py .ALWAYS
	@echo
	@echo == Building $@
	./src/docx_template_build.py -i src/template.docx -n docx -o src/docx_template

src/odt_template.c: src/docx_template_build.py .ALWAYS
	@echo
	@echo == Building $@
	./src/docx_template_build.py -i src/template.odt -n odt -o src/odt_template

.ALWAYS:
.PHONY: .ALWAYS

# Tell make to preserve all intermediate files.
#
.SECONDARY:


# Rule for tags.
#
tags: .ALWAYS
	ectags -R --extra=+fq --c-kinds=+px .


# Clean rule.
#
clean:
	rm -r src/build test/generated src/template.docx.dir 2>/dev/null || true

# Cleans test/generated except for intermediate files, which are slow to create
# (when using gs).
clean2:
	rm -r test/generated/*.pdf.intermediate-*.xml.* 2>/dev/null || true
	rm -r test/generated/*.pdf.mutool*.docx* 2>/dev/null || true
	rm -r src/build 2>/dev/null || true
.PHONY: clean


# Include dynamic dependencies.
#
# We use $(sort ...) to remove duplicates
#
dep = $(sort $(exe_dep) $(exe_buffer_test_dep) $(exe_misc_test_dep) $(exe_ziptest_dep))

-include $(dep)