Imported Upstream version 4.4
[platform/upstream/make.git] / tests / scripts / features / loadapi
1 #                                                                    -*-perl-*-
2 $description = "Test the shared object load API.";
3
4 $details = "Verify the different aspects of the shared object API.";
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(testapi.c testapi.so));
19
20 open(my $F, '> testapi.c') or die "open: testapi.c: $!\n";
21 print $F <<'EOF' ;
22 #include <string.h>
23 #include <stdio.h>
24
25 #include "gnumake.h"
26
27 char *getenv (const char*);
28
29 int plugin_is_GPL_compatible;
30
31 int testapi_gmk_setup ();
32
33 static char *
34 test_eval (const char *buf)
35 {
36     gmk_eval (buf, 0);
37     return NULL;
38 }
39
40 static char *
41 test_expand (const char *val)
42 {
43     return gmk_expand (val);
44 }
45
46 static char *
47 test_noexpand (const char *val)
48 {
49     char *str = gmk_alloc (strlen (val) + 1);
50     strcpy (str, val);
51     return str;
52 }
53
54 static char *
55 func_test (const char *funcname, unsigned int argc, char **argv)
56 {
57     char *mem;
58
59     if (strcmp (funcname, "test-expand") == 0)
60         return test_expand (argv[0]);
61
62     if (strcmp (funcname, "test-eval") == 0)
63         return test_eval (argv[0]);
64
65     if (strcmp (funcname, "test-noexpand") == 0)
66         return test_noexpand (argv[0]);
67
68     mem = gmk_alloc (sizeof ("unknown"));
69     strcpy (mem, "unknown");
70     return mem;
71 }
72
73 int
74 testapi_gmk_setup (const gmk_floc *floc)
75 {
76     const char *verbose = getenv ("TESTAPI_VERBOSE");
77
78     gmk_add_function ("test-expand", func_test, 1, 1, GMK_FUNC_DEFAULT);
79     gmk_add_function ("test-noexpand", func_test, 1, 1, GMK_FUNC_NOEXPAND);
80     gmk_add_function ("test-eval", func_test, 1, 1, GMK_FUNC_DEFAULT);
81     gmk_add_function ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.", func_test, 0, 0, 0);
82
83     if (verbose)
84       {
85         printf ("testapi_gmk_setup\n");
86
87         if (verbose[0] == '2')
88           printf ("%s:%lu\n", floc->filenm, floc->lineno);
89       }
90
91     if (getenv ("TESTAPI_KEEP"))
92       return -1;
93
94     return 1;
95 }
96 EOF
97 close($F) or die "close: testapi.c: $!\n";
98
99 # Make sure we can compile
100
101 my $cflags = get_config('CFLAGS');
102 my $cppflags = get_config('CPPFLAGS');
103 my $ldflags = get_config('LDFLAGS');
104 my $sobuild = "$cc ".($srcdir? "-I$srcdir/src":'')." $cppflags $cflags -shared -fPIC $ldflags -o testapi.so testapi.c";
105
106 my $clog = `$sobuild 2>&1`;
107 if ($? != 0) {
108     $verbose and print "Failed to build testapi.so:\n$sobuild\n$_";
109     return -1;
110 }
111
112 # TEST 1
113 # Check the gmk_expand() function
114 run_make_test(q!
115 EXPAND = expansion
116 all: ; @echo $(test-expand $$(EXPAND))
117 load testapi.so
118 !,
119               '', "expansion\n");
120
121 # TEST 2
122 # Check the eval operation.  Prove that the argument is expanded only once
123 run_make_test(q!
124 load testapi.so
125 TEST = bye
126 ASSIGN = VAR = $(TEST) $(shell echo there)
127 $(test-eval $(value ASSIGN))
128 TEST = hi
129 all:;@echo '$(VAR)'
130 !,
131               '', "hi there\n");
132
133 # TEST 2
134 # Check the no-expand capability
135 run_make_test(q!
136 load testapi.so
137 TEST = hi
138 all:;@echo '$(test-noexpand $(TEST))'
139 !,
140               '', "\$(TEST)\n");
141
142
143 # During all subsequent tests testapi.so exists.
144 #
145 my @loads = ('', q!
146 load testapi.so
147 load testapi.so
148 -load testapi.so
149 -load testapi.so
150 $(eval load testapi.so)
151 $(eval -load testapi.so)
152 !);
153
154 for my $extra_loads (@loads) {
155 my $n = 5;
156 if ($extra_loads) {
157   $n = 12;
158 }
159 # sv 63045.
160 # Test that having unloaded a shared object make loads it again, even if the
161 # shared object is not updated.
162 $ENV{TESTAPI_VERBOSE} = 1;
163 run_make_test("
164 load testapi.so
165 $extra_loads
166 all:; \$(info \$(test-expand hello))
167 testapi.so: force; \$(info \$@)
168 force:;
169 .PHONY: force
170 ", '', "testapi_gmk_setup\ntestapi.so\ntestapi_gmk_setup\nhello\n#MAKE#: 'all' is up to date.\n");
171
172 # sv 63045.
173 # Same as above, but testapi_gmk_setup returned -1.
174 $ENV{TESTAPI_KEEP} = 1;
175 $ENV{TESTAPI_VERBOSE} = 1;
176 run_make_test("
177 load testapi.so
178 $extra_loads
179 all:; \$(info \$(test-expand hello))
180 testapi.so: force; \$(info \$@)
181 force:;
182 .PHONY: force
183 ", '', "testapi_gmk_setup\nhello\n#MAKE#: 'all' is up to date.\n");
184
185 # sv 63045.
186 # Test that make exits, unless make can successfully update an unloaded shared
187 # object.
188 $ENV{TESTAPI_VERBOSE} = 1;
189 run_make_test("
190 load testapi.so
191 $extra_loads
192 all:; \$(info \$(test-expand hello))
193 testapi.so: force; @#HELPER# fail 1
194 force:;
195 .PHONY: force
196 ", '', "testapi_gmk_setup\nfail 1\n#MAKE#: *** [#MAKEFILE#:$n: testapi.so] Error 1\n", 512);
197
198 # sv 63045.
199 # Same as above, but testapi_gmk_setup returned -1.
200 $ENV{TESTAPI_KEEP} = 1;
201 $ENV{TESTAPI_VERBOSE} = 1;
202 run_make_test("
203 load testapi.so
204 $extra_loads
205 all:; \$(info \$(test-expand hello))
206 testapi.so: force; @#HELPER# fail 1
207 force:;
208 .PHONY: force
209 ", '', "testapi_gmk_setup\nhello\n#MAKE#: 'all' is up to date.\n");
210
211 # sv 63100.
212 # Test that make supplies the correct floc when the shared object is loaded
213 # again.
214 $ENV{TESTAPI_VERBOSE} = 2;
215 run_make_test("
216 load testapi.so
217 $extra_loads
218 all:; \$(info \$(test-expand hello))
219 testapi.so: force; \$(info \$@)
220 force:;
221 .PHONY: force
222 ", '', "testapi_gmk_setup\n#MAKEFILE#:2\ntestapi.so\ntestapi_gmk_setup\n#MAKEFILE#:2\nhello\n#MAKE#: 'all' is up to date.\n");
223 }
224
225 unlink(qw(testapi.c testapi.so)) unless $keep;
226
227 # This tells the test driver that the perl test script executed properly.
228 1;