]> git.draconx.ca Git - dxcommon.git/blob - tests/scripts.at
gen-options.awk: Fix test with older gettext.
[dxcommon.git] / tests / scripts.at
1 dnl Copyright © 2021-2023 Nick Bowler
2 dnl
3 dnl License WTFPL2: Do What The Fuck You Want To Public License, version 2.
4 dnl This is free software: you are free to do what the fuck you want to.
5 dnl There is NO WARRANTY, to the extent permitted by law.
6
7 AT_BANNER([Script tests])
8
9 m4_define([TEST_GEN_OPTIONS],
10 [AT_KEYWORDS([gen-options awk script scripts])dnl
11 AT_DATA([m4_default([$2], [options.def])], [$1])
12 AT_CHECK([$AWK -f "$srcdir/scripts/gen-options.awk" dnl
13 <m4_default([$2], [options.def]) >options.h])])
14
15 m4_define([TEST_GEN_OPTIONS_SAMPLE],
16 [[--option-only
17 --option-with-val (5)
18 --option-with-flagval (&x, 5)
19 --option-with-arg=ARG
20 some "text" goes here
21 --option-with-optional-arg[=OPTIONAL]
22
23 hello
24 -a, --option-with-sopt
25
26 -b, --option-with-sopt-and-arg=SOPTARG
27 -c, --option-with-sopt-and-optional-arg[=SOPTOPTIONAL]
28 --option-with-arg-and-val=ARGVAL (42)
29 --option-with-arg-and-flagval=ARGFLAGVAL (&a[1], 'x')
30 --option-with-optional-arg-and-val[=OPTIONALARGVAL] (54)
31 --option-with-optional-arg-and-flagval[=OPTIONALFLAGVAL] (0, 0)
32 --with-sopt
33 Here is a help string
34     that has a line randomly indented
35 # with a comment
36     @&t@
37 and a blank line
38 --with-arg=ARG
39 do stuff with ARG
40 --flagval
41 ]])
42
43 AT_SETUP([gen-options.awk])
44 TEST_GEN_OPTIONS([TEST_GEN_OPTIONS_SAMPLE])
45
46 AT_DATA([context.h],
47 [[struct option { const char *name; int has_arg; int *flag; int val; };
48 int x, a[5];
49 ]])
50
51 # test 0: sanity test
52 AT_DATA([test0.c],
53 [[#include "context.h"
54 #include "options.h"
55
56 static const char sopts[] = SOPT_STRING;
57 static const struct option opts[] = { LOPTS_INITIALIZER, {0} };
58
59 int main(void)
60 {
61   return 0;
62 }
63 ]])
64 AT_CHECK([$CC -o test0$EXEEXT test0.c && ./test0$EXEEXT], [0], [], [ignore])
65
66 # test 1: long option names and help text
67 AT_DATA([test1.c],
68 [[#include <stdio.h>
69 #include <stdlib.h>
70
71 #include "context.h"
72 #include "options.h"
73
74 static const struct option opts[] = { LOPTS_INITIALIZER };
75
76 int main(void)
77 {
78   unsigned i;
79
80   for (i = 0; i < sizeof opts / sizeof opts[0]; i++) {
81     struct lopt_help help = { "INVALID", "INVALID" };
82
83     if (!lopt_get_help(&opts[i], &help))
84       return EXIT_FAILURE;
85
86     printf("--%s", opts[i].name);
87     if (opts[i].has_arg)
88       printf("=%s", help.arg);
89     printf("\n%s", help.desc);
90     if (help.desc[0])
91       putchar('\n');
92   }
93
94   return 0;
95 }
96 ]])
97
98 AT_DATA([lopthelp.awk],
99 [[/^#/ { next }
100 /^-/ {
101   if ($1 !~ /^--/)
102     $1 = $2;
103   if (sub(/\@:>@$/, "", $1))
104     sub(/\@<:@/, "", $1);
105
106   print $1;
107   next;
108 }
109
110 { sub(/^[ \t]*/, ""); }
111 /./ { print; }
112 ]])
113
114 $AWK -f lopthelp.awk options.def >expout
115 AT_CHECK([$CC -o test1$EXEEXT test1.c && ./test1$EXEEXT],
116   [0], [expout], [ignore])
117
118 # test 2: short option string
119 AT_DATA([test2.c],
120 [[#include <stdio.h>
121 #include <stdlib.h>
122
123 #include "context.h"
124 #include "options.h"
125
126 int main(void)
127 {
128   struct option lopts[] = {LOPTS_INITIALIZER};
129   unsigned i, j;
130
131   for (i = 0; i < sizeof SOPT_STRING - 1; i++) {
132     if (SOPT_STRING[i] != ':') {
133       for (j = 0; j < sizeof lopts / sizeof lopts[0]; j++) {
134         if (lopts[j].val == SOPT_STRING[i]) {
135           printf("--%s ", lopts[j].name);
136           break;
137         }
138       }
139     }
140     putchar(SOPT_STRING[i]);
141     if (SOPT_STRING[i+1] != ':')
142       putchar('\n');
143   }
144   return 0;
145 }
146 ]])
147
148 AT_DATA([soptstr.awk],
149 [[/^-/ {
150   if ($1 ~ /^--/)
151     next;
152
153   sopt = substr($1, 2, 1);
154   arg = sub(/\@:>@$/, "", $2);
155   arg += sub(/\@<:@?=.*$/, "", $2);
156
157   print $2 " " sopt substr("::", 1, arg);
158 }
159 ]])
160
161 $AWK -f soptstr.awk options.def >expout
162 AT_CHECK([$CC -o test2$EXEEXT test2.c && ./test2$EXEEXT],
163   [0], [expout], [ignore])
164
165 AT_CLEANUP
166
167 AT_SETUP([gen-options.awk xgettext annotation])
168 TEST_GEN_OPTIONS([TEST_GEN_OPTIONS_SAMPLE])
169
170 # Check that all help strings are translatable
171 AT_DATA([messages.awk],
172 [[BEGIN { lines = -1; }
173 END { output(); }
174
175 /^#/ { next }
176 /^-/ {
177   output();
178   if ($1 !~ /^--/)
179     $1 = $2;
180
181   tmp=$1;
182   arg="";
183   if (sub(/\@<:@?=.*/, "", $1)) {
184     arg = substr(tmp, index(tmp, "=")+1);
185     sub(/\@:>@$/, "", arg);
186   }
187
188   sub(/^--/, "", $1);
189   ctxt=("msgctxt \"" $1 "\" msgid");
190
191   if (arg)
192     print ctxt, ("\"" arg "\"");
193   next;
194 }
195
196 { sub(/^[ \t]*/, ""); }
197 /./ {
198   gsub(/"/, "\\\"", $0);
199   help[lines++] = $0;
200 }
201
202 function output(i)
203 {
204   if (lines >= 0) {
205     printf "%s", ctxt;
206     for (i = 0; i < lines; i++) {
207       nl = (i+1 < lines ? "\\n" : "");
208       printf(" \"%s%s\"", help[i], nl);
209     }
210     print "";
211   }
212
213   lines = 0;
214 }
215 ]])
216
217 dnl Antique versions of xgettext which predate the pgettext/msgctx feature
218 dnl will produce an output po file with no msgctx lines.  So try to spot
219 dnl that and skip the test with such versions.
220 AT_CHECK([xgettext --keyword=PN_:1c,2 options.h
221   test -f messages.po || exit 77
222   grep msgid messages.po >/dev/null &&
223     { grep msgctx messages.po >/dev/null || exit 77; }])
224
225 $AWK -f messages.awk options.def | LC_ALL=C sort >expout
226 AT_CHECK([sed -n '/^msgctxt/{
227 t next
228 :next
229 N
230 s/\nmsgstr.*//
231 t done
232 s/\s*""//
233 s/\n/ /
234 t next
235 :done
236 p
237 }' messages.po | LC_ALL=C sort], [0], [expout])
238
239 AT_CLEANUP
240
241 AT_SETUP([gen-options.awk packed format])
242
243 AT_DATA([test.c], [[#include <stdio.h>
244 struct option { const char *name; int has_arg; int *flag; int val; };
245
246 #include "options.h"
247
248 static unsigned opts[] = { LOPTS_PACKED_INITIALIZER };
249
250 int main(void)
251 {
252   unsigned i;
253   int x =
254 #if !LOPT_PACK_BITS
255   0
256 #elif LOPT_PACK_BITS <= 8
257   1
258 #elif LOPT_PACK_BITS <= 16
259   2
260 #elif LOPT_PACK_BITS <= 32
261   3
262 #else
263 #  error too big
264 #endif
265   ;
266   printf("%d\n", x);
267   for (i = 0; i < sizeof opts / sizeof opts[0]; i++) {
268     struct option o;
269
270     LOPT_UNPACK(o, opts[i]);
271     printf("--%s, %d, ", o.name, o.has_arg);
272     if (o.val > UCHAR_MAX)
273       printf("%d\n", o.val - UCHAR_MAX - 1);
274     else
275       printf("'%c'\n", o.val);
276   }
277   return 0;
278 }
279 ]])
280
281 TEST_GEN_OPTIONS([[--single-option
282 ]], [single.dat])
283 AT_CHECK([$CC -o single$EXEEXT test.c && ./single$EXEEXT], [0],
284 [[0
285 --single-option, 0, 0
286 ]])
287
288 TEST_GEN_OPTIONS([[-a, --the-first-option
289 -b, --the-second-option=ARG
290 -c, --the-third-option[=ARG]
291 -d, --the-fourth-option
292 ]], [16bit.dat])
293 AT_CHECK([$CC -o 16bit$EXEEXT test.c && ./16bit$EXEEXT], [0],
294 [[2
295 --the-first-option, 0, 'a'
296 --the-second-option, 1, 'b'
297 --the-third-option, 2, 'c'
298 --the-fourth-option, 0, 'd'
299 ]])
300
301 AT_CLEANUP
302
303 AT_SETUP([gen-strtab.awk])
304 AT_KEYWORDS([gen-strtab awk script scripts])
305
306 AT_DATA([test.def],
307 [[
308 &a world
309 &b
310 hello world
311 &c
312 hello
313 world
314 &d world\n
315 &e
316 \\not a newline
317 &f
318 \not a newline
319 &g inline
320 continued
321 &h    no\
322 newline\
323 &i
324 \   leading whitespace
325 &j oneline
326 # with a comment
327 ]])
328
329 AT_CHECK([$AWK -f "$srcdir/scripts/gen-strtab.awk" <test.def >test.h])
330
331 sed -n 's/^[[&]]\([[^ ]]*\).*/\1/p' test.def >identifiers
332
333 # test 0: sanity test
334 AT_DATA([test0.c],
335 [[#include "test.h"
336 #include <stdio.h>
337
338 int main(void)
339 {
340   printf("---\n");
341 ]])
342 exec 3<identifiers 4>>test0.c
343 while read ident <&3; do
344   AS_ECHO(['  printf("%s\n---\n", '"strtab+$ident);"]) >&4
345 done
346 AS_ECHO(['  return 0;']) >&4
347 AS_ECHO(['}']) >&4
348 exec 3<&- 4>&-
349
350 AT_CHECK([$CC -o test0$EXEEXT test0.c && ./test0$EXEEXT], [0], [---
351 world
352 ---
353 hello world
354
355 ---
356 hello
357 world
358
359 ---
360 world
361
362 ---
363 \not a newline
364
365 ---
366
367 ot a newline
368
369 ---
370 inline
371 continued
372
373 ---
374 nonewline
375 ---
376    leading whitespace
377
378 ---
379 oneline
380 ---
381 ], [ignore])
382
383 AT_CLEANUP
384
385 AT_SETUP([gen-strtab.awk @nozero option])
386 AT_KEYWORDS([gen-strtab awk script scripts])
387
388 AT_DATA([test0.def],
389 [[&hello hello
390 ]])
391 AT_CHECK([$AWK -f "$srcdir/scripts/gen-strtab.awk" <test0.def >test0.h])
392
393 AT_DATA([test1.def],
394 [[@nozero
395 &hello hello
396 ]])
397 AT_CHECK([$AWK -f "$srcdir/scripts/gen-strtab.awk" <test1.def >test1.h])
398
399 AT_DATA([test.c],
400 [[#include <stdio.h>
401 #include HEADER
402 int main(void) { printf("%d %s\n", hello, strtab+hello); return 0; }
403 ]])
404 AT_CHECK([$CC -DHEADER='"test0.h"' -o test0$EXEEXT test.c && ./test0$EXEEXT],
405   [0], [[0 hello
406 ]])
407 AT_CHECK([$CC -DHEADER='"test1.h"' -o test1$EXEEXT test.c && ./test1$EXEEXT],
408   [0], [[1 hello
409 ]])
410
411 AT_CLEANUP
412
413 AT_SETUP([gen-strtab.awk @macro option])
414 AT_KEYWORDS([gen-strtab awk script scripts])
415
416 AT_DATA([test0.def],
417 [[@macro
418 &foo foobar
419 &bar bar
420 &baz baz
421 ]])
422 AT_CHECK([$AWK -f "$srcdir/scripts/gen-strtab.awk" <test0.def >test0.h])
423
424 AT_DATA([test0.c],
425 [[#include <stdio.h>
426 extern const char strtab[];
427 #include "test0.h"
428
429 int main(void)
430 {
431   static const char mystrtab[] = STRTAB_INITIALIZER;
432   printf("%s\n%s\n%s\n", mystrtab+foo, mystrtab+bar, mystrtab+baz);
433   return 0;
434 }
435 ]])
436 AT_CHECK([$CC -o test0$EXEEXT test0.c && ./test0$EXEEXT], [0],
437 [[foobar
438 bar
439 baz
440 ]])
441
442 AT_CLEANUP
443
444 AT_SETUP([gen-strtab.awk l10n options])
445 AT_KEYWORDS([gen-strtab awk script scripts])
446
447 AT_DATA([l10n.sed], dnl (
448 [[/^#/b
449 s/.*N_(\([^)]*\)).*/\1/p
450 ]])
451
452 AT_DATA([test0.def],
453 [[&a hello world
454 &b world
455 &c goodbye
456 ]])
457 AT_CHECK([$AWK -f "$srcdir/scripts/gen-strtab.awk" <test0.def >test0.h])
458 AT_CHECK([sed -n -f l10n.sed test0.h | LC_ALL=C sort], [0],
459 [["goodbye"
460 "hello world"
461 "world"
462 ]])
463
464 AT_DATA([test1.def],
465 [[&a hello world
466 &&b world
467 &&c goodbye
468 ]])
469 AT_CHECK([$AWK -f "$srcdir/scripts/gen-strtab.awk" <test1.def >test1.h])
470 AT_CHECK([sed -n -f l10n.sed test1.h], [0],
471 [["hello world"
472 ]])
473
474 AT_DATA([test.c],
475 [[#include <stdio.h>
476 #include HEADER
477
478 int main(void)
479 {
480   printf("%s %s %s\n", strtab+a, strtab+b, strtab+c);
481   return 0;
482 }
483 ]])
484
485 AT_CHECK([$CC -DHEADER='"test0.h"' -o test0$EXEEXT test.c && ./test0$EXEEXT],
486   [0], [[hello world world goodbye
487 ]])
488
489 AT_CHECK([$CC -DHEADER='"test1.h"' -o test1$EXEEXT test.c && ./test1$EXEEXT],
490   [0], [[hello world world goodbye
491 ]])
492
493
494 AT_CLEANUP
495
496 AT_SETUP([gen-tree.awk])
497 AT_KEYWORDS([gen-tree awk script scripts])
498
499 AT_DATA([tree.def],
500 [[# comment
501 ROOT0
502   r0a, r0a_OFFSET
503     r0b, r0b_OFFSET
504       r0c
505     r0d
506   r0e, r0e_OFFSET
507     r0f
508     r0g
509 # comment
510 ROOT1
511   r1a, r1a_OFFSET
512     r1b, r1b_OFFSET
513       r1b
514       r1e
515       r1b
516   r1c, r1c_OFFSET
517     r1d, r1d_OFFSET
518       r1e
519       r1b
520       r1e
521 # comment
522 ]])
523
524 AT_CHECK([$AWK -f "$srcdir/scripts/gen-tree.awk" <tree.def >tree.h])
525
526 AT_DATA([test0.c],
527 [[#include "tree.h"
528 #include <stdio.h>
529
530 struct tree { unsigned id, subtree; };
531
532 static const struct tree tree0[] = {
533   ROOT0_INITIALIZER
534 };
535 static const struct tree tree1[] = {
536   ROOT1_INITIALIZER
537 };
538
539 void print_subtree(const struct tree *root, unsigned offset, int depth)
540 {
541   const struct tree *node;
542
543   for (node = &root[offset]; node->id; node++) {
544     printf("%*s%s", 2*depth, "", &tree_strtab[node->id]);
545     if (node->subtree) {
546       printf(", %s_OFFSET\n", &tree_strtab[node->id]);
547       print_subtree(root, node->subtree, depth+1);
548     } else {
549       putchar('\n');
550     }
551   }
552 }
553
554 int main(void)
555 {
556   printf("ROOT0\n");
557   print_subtree(tree0, 0, 1);
558   printf("ROOT1\n");
559   print_subtree(tree1, 0, 1);
560   return 0;
561 }
562 ]])
563 sed '/^#/d' tree.def >expout
564 AT_CHECK([$CC -o test0$EXEEXT test0.c && ./test0$EXEEXT], [0], [expout])
565
566 AT_CLEANUP
567
568 # Test the gen-tree features that avoid creating string labels for nodes.
569 AT_SETUP([gen-tree.awk @nostrtab option])
570 AT_KEYWORDS([gen-tree awk script scripts])
571
572 AT_DATA([tree.def],
573 [[@nostrtab
574 ROOT
575  a 1, a_OFFSET
576   b 1
577   c 2
578  d 2, d_OFFSET
579   e 1
580   f 2
581 ]])
582 AT_CHECK([$AWK -f "$srcdir/scripts/gen-tree.awk" <tree.def >tree.h])
583
584 AT_DATA([test0.c],
585 [[float tree_strtab = 0;
586 #define a []
587 #define b []
588 #define c []
589 #define e []
590 #define f []
591 #include "tree.h"
592 #include <stdio.h>
593
594 static struct { int num, offset; } root[] = { ROOT_INITIALIZER };
595
596 int main(void)
597 {
598   unsigned i;
599   for (i = 0; i < sizeof root / sizeof root[0]; i++) {
600     printf("%d, %d\n", root[i].num, root[i].offset);
601   }
602   return 0;
603 }
604 ]])
605
606 AT_CHECK([$CC -o test0$EXEEXT test0.c && ./test0$EXEEXT], [0],
607 [[1, 3
608 2, 6
609 0, 0
610 1, 0
611 2, 0
612 0, 0
613 1, 0
614 2, 0
615 0, 0
616 ]])
617
618 AT_DATA([flat.def],
619 [[FLAT
620  a 1
621  b 2
622  c 3
623 @nostrtab
624 ]])
625 AT_CHECK([$AWK -f "$srcdir/scripts/gen-tree.awk" <flat.def >flat.h])
626
627 sed -e 's/tree\.h/flat.h/' -e 's/ROOT/FLAT/' test0.c >test1.c
628 AT_CHECK([$CC -o test1$EXEEXT test1.c && ./test1$EXEEXT], [0],
629 [[1, 0
630 2, 0
631 3, 0
632 0, 0
633 ]])
634
635 AT_CLEANUP
636
637 AT_SETUP([join.awk])
638 AT_KEYWORDS([join awk script scripts])
639
640 JOIN="$AWK -f $srcdir/scripts/join.awk --"
641
642 AT_DATA([a],
643 [[1 a
644 3 a1 x
645 3 a2 x
646 5 a
647 6 a
648 8 a1 x
649 8 a2 x
650 9 a1
651 9 a2
652 9 a3
653 ]])
654
655 AT_DATA([b],
656 [[2 b
657 2 b
658 3 b y
659 4 b
660 6 b1 y
661 6 b2 y
662 7 b
663 8 b1 y
664 8 b2 y
665 ]])
666
667 AT_CHECK([$JOIN a b], [0],
668 [[3 a1 x b y
669 3 a2 x b y
670 6 a b1 y
671 6 a b2 y
672 8 a1 x b1 y
673 8 a1 x b2 y
674 8 a2 x b1 y
675 8 a2 x b2 y
676 ]])
677
678 AT_CHECK([$JOIN -v1 a b], [0],
679 [[1 a
680 5 a
681 9 a1
682 9 a2
683 9 a3
684 ]])
685
686 AT_CHECK([$JOIN -v2 a b], [0],
687 [[2 b
688 2 b
689 4 b
690 7 b
691 ]])
692
693 AT_CHECK([$JOIN -v1 -v2 a b], [0],
694 [[1 a
695 2 b
696 2 b
697 4 b
698 5 a
699 7 b
700 9 a1
701 9 a2
702 9 a3
703 ]])
704
705 AT_CHECK([$JOIN -a1 a b], [0],
706 [[1 a
707 3 a1 x b y
708 3 a2 x b y
709 5 a
710 6 a b1 y
711 6 a b2 y
712 8 a1 x b1 y
713 8 a1 x b2 y
714 8 a2 x b1 y
715 8 a2 x b2 y
716 9 a1
717 9 a2
718 9 a3
719 ]])
720
721 AT_CHECK([$JOIN -a2 a b], [0],
722 [[2 b
723 2 b
724 3 a1 x b y
725 3 a2 x b y
726 4 b
727 6 a b1 y
728 6 a b2 y
729 7 b
730 8 a1 x b1 y
731 8 a1 x b2 y
732 8 a2 x b1 y
733 8 a2 x b2 y
734 ]])
735
736 AT_CHECK([$JOIN -a1 -a2 a b], [0],
737 [[1 a
738 2 b
739 2 b
740 3 a1 x b y
741 3 a2 x b y
742 4 b
743 5 a
744 6 a b1 y
745 6 a b2 y
746 7 b
747 8 a1 x b1 y
748 8 a1 x b2 y
749 8 a2 x b1 y
750 8 a2 x b2 y
751 9 a1
752 9 a2
753 9 a3
754 ]])
755
756 AT_CHECK([$JOIN b a], [0],
757 [[3 b y a1 x
758 3 b y a2 x
759 6 b1 y a
760 6 b2 y a
761 8 b1 y a1 x
762 8 b1 y a2 x
763 8 b2 y a1 x
764 8 b2 y a2 x
765 ]])
766
767 AT_CHECK([$JOIN -v1 b a], [0],
768 [[2 b
769 2 b
770 4 b
771 7 b
772 ]])
773
774 AT_CHECK([$JOIN -v2 b a], [0],
775 [[1 a
776 5 a
777 9 a1
778 9 a2
779 9 a3
780 ]])
781
782 AT_CHECK([$JOIN -v1 -v2 b a], [0],
783 [[1 a
784 2 b
785 2 b
786 4 b
787 5 a
788 7 b
789 9 a1
790 9 a2
791 9 a3
792 ]])
793
794 AT_CHECK([$JOIN -a1 b a], [0],
795 [[2 b
796 2 b
797 3 b y a1 x
798 3 b y a2 x
799 4 b
800 6 b1 y a
801 6 b2 y a
802 7 b
803 8 b1 y a1 x
804 8 b1 y a2 x
805 8 b2 y a1 x
806 8 b2 y a2 x
807 ]])
808
809 AT_CHECK([$JOIN -a2 b a], [0],
810 [[1 a
811 3 b y a1 x
812 3 b y a2 x
813 5 a
814 6 b1 y a
815 6 b2 y a
816 8 b1 y a1 x
817 8 b1 y a2 x
818 8 b2 y a1 x
819 8 b2 y a2 x
820 9 a1
821 9 a2
822 9 a3
823 ]])
824
825 AT_CHECK([$JOIN -a1 -a2 b a], [0],
826 [[1 a
827 2 b
828 2 b
829 3 b y a1 x
830 3 b y a2 x
831 4 b
832 5 a
833 6 b1 y a
834 6 b2 y a
835 7 b
836 8 b1 y a1 x
837 8 b1 y a2 x
838 8 b2 y a1 x
839 8 b2 y a2 x
840 9 a1
841 9 a2
842 9 a3
843 ]])
844
845 AT_CHECK([echo wat | $JOIN -v1 - /dev/null], [0],
846 [[wat
847 ]])
848
849 AT_CLEANUP
850
851 m4_divert_push([PREPARE_TESTS])dnl
852 test_fix_ltdl () {
853   $PERL -e 'my $x = 42; exit $x;'; test $? = 42 || exit 77
854   $PERL -f "$srcdir/scripts/fix-ltdl.pl" "$@"
855 }
856 test_fix_gnulib () {
857   $PERL -e 'my $x = 42; exit $x;'; test $? = 42 || exit 77
858   $PERL -f "$srcdir/scripts/fix-gnulib.pl" "$@"
859 }
860 test_gnulib_mk () {
861   echo;
862   for arg
863   do
864     sed -n -f - "$srcdir/tests/data/gnulib.mk" <<EOF
865 /^## begin gnulib module $arg/,/^## end   gnulib module $arg/p
866 EOF
867   done
868 }
869 m4_divert_pop([PREPARE_TESTS])
870
871 AT_SETUP([fix-gnulib.pl SED_HEADER variables])
872
873 test_gnulib_mk gen-header >test.mk.in
874 AT_CHECK([grep SED_HEADER test.mk.in >expout || exit 99])
875 AT_CHECK([test_fix_gnulib -i test.mk.in -o test.mk || exit
876 grep SED_HEADER test.mk], [0], [expout])
877
878 AT_CLEANUP
879
880 AT_SETUP([fix-gnulib.pl %reldir% substitution])
881
882 test_gnulib_mk sys_types >test.mk.in
883 AT_CHECK([grep '%reldir%' test.mk.in >/dev/null || exit 99])
884
885 sed -n -f - test.mk.in >expout <<'EOF'
886 ${G;p;b}
887 /^## begin gnulib/,/^## end   gnulib/!b
888 /^#/{p;b}
889 s|(srcdir)|(top_srcdir)|
890 s|%reldir%|lib|
891 s|BUILT_SOURCES|gnulib_core_headers|
892 s|sys[[/_]]|lib/&|g
893 /^MOSTLYCLEANFILES/{h;b}
894 p
895 EOF
896
897 AT_CHECK([test_fix_gnulib -i test.mk.in -o test.mk || exit
898 sed -n -e '/^## begin gnulib/,/^## end   gnulib/p' \
899        -e '/CLEANFILES/p' test.mk],
900 [0], [expout])
901
902 AT_CLEANUP
903
904 AT_SETUP([fix-gnulib.pl warning removal])
905
906 AT_DATA([test.mk.in], [[
907 ## test begin
908 noinst_LTLIBRARIES += libgnu.la
909 libgnu_la_CFLAGS = $(AM_CFLAGS) $(GL_CFLAG_GNULIB_WARNINGS)
910 noinst_LIBRARIES += libgnu.a
911 libgnu_a_CFLAGS = $(AM_CFLAGS) $(GL_CFLAG_GNULIB_WARNINGS)
912 ## test end
913 ]])
914 AT_CHECK([test_fix_gnulib -i test.mk.in -o test.mk || exit
915 sed -n '/^## test begin/,/^## test end/p' test.mk], [0], [## test begin
916 EXTRA_LTLIBRARIES += libgnu.la
917 EXTRA_LIBRARIES += libgnu.a
918 ## test end
919 ])
920
921 AT_CLEANUP
922
923 AT_SETUP([fix-gnulib.pl header directory creation])
924
925 AT_DATA([extract.sed],
926 [[/AM_V_GEN/b ok
927 /gl_V_at/b ok
928 s/:.*/:/
929 h
930 b
931 :ok
932 s/'//g
933 x
934 G
935 p
936 n
937 s/[)].*/)/
938 p
939 ]])
940
941 test_gnulib_mk alloca-opt sys_types stddef >test.mk.in
942 AT_CHECK([test_fix_gnulib -i test.mk.in -o test.mk || exit
943 sed -n -f extract.sed test.mk], [0],
944 [[lib/alloca.h:
945         $(AM_V_GEN)$(MKDIR_P) lib
946         $(AM_V_at)
947 lib/sys/types.h:
948         $(AM_V_GEN)$(MKDIR_P) lib/sys
949         $(AM_V_at)
950 lib/stddef.h:
951         $(AM_V_GEN)$(MKDIR_P) lib
952         $(AM_V_at)
953 ]])
954
955 AT_CLEANUP
956
957 dnl TEST_FIND_AUTOMAKE_VER([to-check], [test-action])
958 dnl
959 dnl For each whitespace-separated version token in to-check, check if we can
960 dnl run the programs automake-VER and aclocal-VER.  The special token 'default'
961 dnl also checks the unversioned automake and aclocal (or, if set in the
962 dnl environment, $AUTOMAKE and $ACLOCAL).
963 dnl
964 dnl Then test-action is expanded such that shell variables $ac and $am refer to
965 dnl the aclocal and automake programs, and $amver is the actual version
966 dnl reported by --version.  The action should do nothing if the version is
967 dnl acceptable, or "continue" if the version is unacceptable.
968 dnl
969 dnl If an acceptable version is found, the AUTOMAKE and ACLOCAL environment
970 dnl variables are set accordingly.  Otherwise, the test group is skipped.
971 m4_define([TEST_FIND_AUTOMAKE],
972 [have_am=false
973 for am in $1; do
974   AS_CASE([$am],
975     [default], [ac=${ACLOCAL-aclocal} am=${AUTOMAKE-automake}],
976     [ac=aclocal-$am; am=automake-$am])
977   amver=`$am --version | sed -n '1s/.* //p'`
978   acver=`$ac --version | sed -n '1s/.* //p'`
979   set x $amver $acver; shift; test x"$[]#" = x"2" || continue
980   test x"$amver" = x"$acver" || continue
981   $2
982   have_am=:; break
983 done
984 AT_CHECK([$have_am || exit 77])
985 AUTOMAKE=$am; export AUTOMAKE
986 ACLOCAL=$ac; export ACLOCAL
987 AT_CHECK([$ACLOCAL --version && $AUTOMAKE --version], [0], [stdout])
988 ])
989
990 m4_define([TEST_LTDL_LIBOBJ_MANGLING],
991 [TEST_CONFIGURE_AC([[AM_INIT_AUTOMAKE([foreign subdir-objects])
992 AC_PROG_CC
993 LT_INIT
994 AC_SUBST([ltdl_LTLIBOBJS], [libltdl/test.lo])
995 AC_CONFIG_FILES([Makefile])
996 ]])
997
998 mkdir libltdl
999 AT_DATA([ltdl.mk.in], [[
1000 AM_CPPFLAGS += -DSTRING=\"helloworld\"
1001
1002 noinst_LTLIBRARIES = libltdl/libltdl.la
1003 libltdl_libltdl_la_SOURCES = libltdl/ltdl.c
1004 libltdl_libltdl_la_LIBADD = $(ltdl_LTLIBOBJS)
1005 libltdl_libltdl_la_DEPENDENCIES = $(ltdl_LTLIBOBJS)
1006
1007 EXTRA_DIST += libltdl/test.c
1008 ]])
1009 AT_DATA([Makefile.am], [[AM_CPPFLAGS =
1010 include $(top_srcdir)/ltdl.mk
1011 AM_LIBTOOLFLAGS = --quiet
1012 bin_PROGRAMS = test
1013 test_SOURCES =
1014 test_LDADD = libltdl/libltdl.la
1015 all-local: ; @printf '%s\n' $(AM_CPPFLAGS)
1016 ]])
1017 AT_DATA([libltdl/test.c], [[#include <stdio.h>
1018 int foo(void) { printf("%s\n", STRING); return 0; }
1019 ]])
1020 AT_DATA([libltdl/ltdl.c], [[int foo(void); int main(void) { return foo(); }
1021 ]])
1022
1023 AT_CHECK([test_fix_ltdl -i ltdl.mk.in -o ltdl.mk])
1024 libtoolize; TEST_AUTORECONF
1025 TEST_CONFIGURE([--disable-shared])
1026 AT_CHECK([make -s && ./test], [0], [
1027 helloworld
1028 ])])
1029
1030 AT_SETUP([fix-ltdl.pl LIBOBJ mangling (<automake-1.16)])
1031
1032 TEST_FIND_AUTOMAKE([default 1.10 1.11 1.12 1.13 1.14 1.15],
1033   [AS_VERSION_COMPARE(["$amver"], [1.16], [], [continue], [continue])])
1034 TEST_LTDL_LIBOBJ_MANGLING
1035
1036 AT_CLEANUP
1037
1038 AT_SETUP([fix-ltdl.pl LIBOBJ mangling (>=automake-1.16)])
1039
1040 TEST_FIND_AUTOMAKE([default 1.16 1.17 1.18 1.19],
1041   [AS_VERSION_COMPARE(["$amver"], [1.16], [continue])])
1042 TEST_LTDL_LIBOBJ_MANGLING
1043
1044 AT_CLEANUP