Imported Upstream version 4.3
[platform/upstream/make.git] / tests / scripts / features / load
1 #                                                                    -*-perl-*-
2 $description = "Test the load operator.";
3
4 $details = "Test dynamic loading of modules.";
5
6 # Don't do anything if this system doesn't support "load"
7 exists $FEATURES{load} or return -1;
8
9 my $cc = get_config('CC');
10 if (! $cc) {
11     $verbose and print "Skipping load test: no CC defined\n";
12     return -1;
13 }
14
15 # First build a shared object
16 # Provide both a default and non-default load symbol
17
18 unlink(qw(testload.c testload.so));
19
20 open(my $F, '> testload.c') or die "open: testload.c: $!\n";
21 print $F <<'EOF' ;
22 #include <string.h>
23 #include <stdio.h>
24
25 #include "gnumake.h"
26
27 int plugin_is_GPL_compatible;
28
29 int
30 testload_gmk_setup (gmk_floc *pos)
31 {
32     (void)pos;
33     gmk_eval ("TESTLOAD = implicit", 0);
34     return 1;
35 }
36
37 int
38 explicit_setup (gmk_floc *pos)
39 {
40     (void)pos;
41     gmk_eval ("TESTLOAD = explicit", 0);
42     return 1;
43 }
44 EOF
45 close($F) or die "close: testload.c: $!\n";
46
47 # Make sure we can compile
48
49 my $cflags = get_config('CFLAGS');
50 my $cppflags = get_config('CPPFLAGS');
51 my $ldflags = get_config('LDFLAGS');
52 my $sobuild = "$cc ".($srcdir? "-I$srcdir/src":'')." $cppflags $cflags -shared -fPIC $ldflags -o testload.so testload.c";
53
54 my $clog = `$sobuild 2>&1`;
55 if ($? != 0) {
56     $verbose and print "Failed to build testload.so:\n$sobuild\n$_";
57     return -1;
58 }
59
60 # TEST 1
61 run_make_test(q!
62 PRE := $(.LOADED)
63 load testload.so
64 POST := $(.LOADED)
65 all: ; @echo pre=$(PRE) post=$(POST) $(TESTLOAD)
66 !,
67               '--warn-undefined-variables', "pre= post=testload.so implicit\n");
68
69 # TEST 2
70 # Load using an explicit function
71 run_make_test(q!
72 PRE := $(.LOADED)
73 load ./testload.so(explicit_setup)
74 POST := $(.LOADED)
75 all: ; @echo pre=$(PRE) post=$(POST) $(TESTLOAD)
76 !,
77               '', "pre= post=testload.so explicit\n");
78
79 # TEST 4
80 # Check multiple loads
81 run_make_test(q!
82 PRE := $(.LOADED)
83 load ./testload.so
84 load testload.so(explicit_setup)
85 POST := $(.LOADED)
86 all: ; @echo pre=$(PRE) post=$(POST) $(TESTLOAD)
87 !,
88               '', "pre= post=testload.so implicit\n");
89
90 # TEST 5
91 # Check auto-rebuild of loaded file that's out of date
92 utouch(-10, 'testload.so');
93 touch('testload.c');
94
95 run_make_test(q!
96 PRE := $(.LOADED)
97 load ./testload.so
98 POST := $(.LOADED)
99 all: ; @echo pre=$(PRE) post=$(POST) $(TESTLOAD)
100 testload.so: testload.c ; @echo "rebuilding $@"; !.$sobuild,
101               '', "rebuilding testload.so\npre= post=testload.so implicit\n");
102
103 # TEST 5
104 # Check auto-rebuild of loaded file when it doesn't exist
105 unlink('testload.so');
106
107 run_make_test(q!
108 PRE := $(.LOADED)
109 -load ./testload.so(explicit_setup)
110 POST := $(.LOADED)
111 all: ; @echo pre=$(PRE) post=$(POST) $(TESTLOAD)
112 %.so: %.c ; @echo "rebuilding $@"; !.$sobuild,
113               '', "rebuilding testload.so\npre= post=testload.so explicit\n");
114
115 unlink(qw(testload.c testload.so)) unless $keep;
116
117 # This tells the test driver that the perl test script executed properly.
118 1;