Imported Upstream version 4.4
[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 "gnumake.h"
23
24 char* getenv (const char*);
25
26 int plugin_is_GPL_compatible;
27
28 int testload_gmk_setup (gmk_floc *);
29 int explicit_setup (gmk_floc *);
30
31 int
32 testload_gmk_setup (gmk_floc *pos)
33 {
34     (void)pos;
35     gmk_eval ("TESTLOAD = implicit", 0);
36     if (getenv("TESTAPI_KEEP"))
37       return -1;
38     return 1;
39 }
40
41 int
42 explicit_setup (gmk_floc *pos)
43 {
44     (void)pos;
45     gmk_eval ("TESTLOAD = explicit", 0);
46     if (getenv("TESTAPI_KEEP"))
47       return -1;
48     return 1;
49 }
50 EOF
51 close($F) or die "close: testload.c: $!\n";
52
53 # Make sure we can compile
54
55 my $cppflags = get_config('CPPFLAGS') . ($srcdir ? " -I$srcdir/src" : '');
56 my $cflags = get_config('CFLAGS') . ' -fPIC';
57 my $ldflags = get_config('LDFLAGS') . ' -shared';
58 my $sobuild = "$cc $cppflags $cflags $ldflags -o testload.so testload.c";
59
60 my $clog = `$sobuild 2>&1`;
61 if ($? != 0) {
62     $verbose and print "Failed to build testload.so:\n$sobuild\n$clog";
63     return -1;
64 }
65
66 # TEST 1
67 run_make_test(q!
68 PRE := $(.LOADED)
69 load testload.so
70 POST := $(.LOADED)
71 all: ; @echo pre=$(PRE) post=$(POST) $(TESTLOAD)
72 !,
73               '--warn-undefined-variables', "pre= post=testload.so implicit\n");
74
75 # TEST 2
76 # Load using an explicit function
77 run_make_test(q!
78 PRE := $(.LOADED)
79 load ./testload.so(explicit_setup)
80 POST := $(.LOADED)
81 all: ; @echo pre=$(PRE) post=$(POST) $(TESTLOAD)
82 !,
83               '', "pre= post=testload.so explicit\n");
84
85 # TEST 3
86 # Check multiple loads
87 run_make_test(q!
88 PRE := $(.LOADED)
89 load ./testload.so
90 load testload.so(explicit_setup)
91 POST := $(.LOADED)
92 all: ; @echo pre=$(PRE) post=$(POST) $(TESTLOAD)
93 !,
94               '', "pre= post=testload.so implicit\n");
95
96 # TEST 4
97 # Check auto-rebuild of loaded file that's out of date
98 utouch(-10, 'testload.so');
99 touch('testload.c');
100
101 run_make_test(q!
102 PRE := $(.LOADED)
103 load ./testload.so
104 POST := $(.LOADED)
105 all: ; @echo pre=$(PRE) post=$(POST) $(TESTLOAD)
106 testload.so: testload.c ; @echo "rebuilding $@"; !.$sobuild,
107               '', "rebuilding testload.so\npre= post=testload.so implicit\n");
108
109 # TEST 5
110 # Check auto-rebuild of loaded file when it doesn't exist
111 unlink('testload.so');
112
113 run_make_test(q!
114 PRE := $(.LOADED)
115 -load ./testload.so(explicit_setup)
116 POST := $(.LOADED)
117 all: ; @echo pre=$(PRE) post=$(POST) $(TESTLOAD)
118 %.so: %.c ; @echo "rebuilding $@"; !.$sobuild,
119               '', "rebuilding testload.so\npre= post=testload.so explicit\n");
120
121 # sv 63044.
122 # Test that the loaded shared object is present in .LOADED when the setup
123 # routine returns -1.
124 $ENV{TESTAPI_KEEP} = 1;
125 run_make_test(q!
126 PRE := $(.LOADED)
127 load testload.so
128 POST := $(.LOADED)
129 all: ; @echo pre=$(PRE) post=$(POST) $(TESTLOAD)
130 !,
131               '--warn-undefined-variables', "pre= post=testload.so implicit\n");
132
133 # Check that we don't auto-rebuild of loaded file that's out of date
134 # if we return -1 from the setup
135 utouch(-10, 'testload.so');
136 touch('testload.c');
137
138 $ENV{TESTAPI_KEEP} = 1;
139 run_make_test(q!
140 PRE := $(.LOADED)
141 load ./testload.so
142 POST := $(.LOADED)
143 all: ; @echo pre=$(PRE) post=$(POST) $(TESTLOAD)
144 testload.so: testload.c ; @echo "rebuilding $@"; !.$sobuild,
145               '', "pre= post=testload.so implicit\n");
146
147 unlink(qw(testload.c testload.so)) unless $keep;
148
149 # This tells the test driver that the perl test script executed properly.
150 1;