summaryrefslogtreecommitdiff
path: root/tests/scripts/features/loadapi
blob: 50e5d05fb8aacccf95403a154a241c01500efbdd (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
#                                                                    -*-perl-*-
$description = "Test the shared object load API.";

$details = "Verify the different aspects of the shared object API.";

# Don't do anything if this system doesn't support "load"
exists $FEATURES{load} or return -1;

my $cc = get_config('CC');
if (! $cc) {
    $verbose and print "Skipping load test: no CC defined\n";
    return -1;
}

# First build a shared object
# Provide both a default and non-default load symbol

unlink(qw(testapi.c testapi.so));

open(my $F, '> testapi.c') or die "open: testapi.c: $!\n";
print $F <<'EOF' ;
#include <string.h>
#include <stdio.h>

#include "gnumake.h"

char *getenv (const char*);

int plugin_is_GPL_compatible;

int testapi_gmk_setup (unsigned int abi, const gmk_floc *floc);

static char *
test_eval (const char *buf)
{
    gmk_eval (buf, 0);
    return NULL;
}

static char *
test_expand (const char *val)
{
    return gmk_expand (val);
}

static char *
test_noexpand (const char *val)
{
    char *str = gmk_alloc (strlen (val) + 1);
    strcpy (str, val);
    return str;
}

static char *
func_test (const char *funcname, unsigned int argc, char **argv)
{
    char *mem;

    if (strcmp (funcname, "test-expand") == 0)
        return test_expand (argv[0]);

    if (strcmp (funcname, "test-eval") == 0)
        return test_eval (argv[0]);

    if (strcmp (funcname, "test-noexpand") == 0)
        return test_noexpand (argv[0]);

    mem = gmk_alloc (sizeof ("unknown"));
    strcpy (mem, "unknown");
    return mem;
}

int
testapi_gmk_setup (unsigned int abi, const gmk_floc *floc)
{
    const char *verbose = getenv ("TESTAPI_VERBOSE");

    gmk_add_function ("test-expand", func_test, 1, 1, GMK_FUNC_DEFAULT);
    gmk_add_function ("test-noexpand", func_test, 1, 1, GMK_FUNC_NOEXPAND);
    gmk_add_function ("test-eval", func_test, 1, 1, GMK_FUNC_DEFAULT);
    gmk_add_function ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.", func_test, 0, 0, 0);

    if (verbose)
      {
        printf ("testapi_gmk_setup\n");

        if (verbose[0] == '2')
          printf ("%s:%lu\n", floc->filenm, floc->lineno);
      }

    if (getenv ("TESTAPI_KEEP"))
      return -1;

    return 1;
}

int
alternative_setup ()
{
    gmk_add_function ("test-expand", func_test, 1, 1, GMK_FUNC_DEFAULT);
    gmk_add_function ("test-noexpand", func_test, 1, 1, GMK_FUNC_NOEXPAND);
    gmk_add_function ("test-eval", func_test, 1, 1, GMK_FUNC_DEFAULT);
    gmk_add_function ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.", func_test, 0, 0, 0);

    if (getenv ("TESTAPI_VERBOSE"))
      printf ("alternative_setup\n");

    if (getenv ("TESTAPI_KEEP"))
      return -1;

    return 1;
}

void
testapi_gmk_unload ()
{
    const char *s = getenv ("TESTAPI_VERBOSE");
    if (s && *s == '3')
      printf ("testapi_gmk_unload\n");
}

EOF
close($F) or die "close: testapi.c: $!\n";

# Make sure we can compile

my $cflags = get_config('CFLAGS');
my $cppflags = get_config('CPPFLAGS');
my $ldflags = get_config('LDFLAGS');
my $sobuild = "$cc ".($srcdir? "-I$srcdir/src":'')." $cppflags $cflags -shared -fPIC $ldflags -o testapi.so testapi.c";

my $clog = `$sobuild 2>&1`;
if ($? != 0) {
    $verbose and print "Failed to build testapi.so:\n$sobuild\n$_";
    return -1;
}

# TEST 1
# Check the gmk_expand() function
run_make_test(q!
EXPAND = expansion
all: ; @echo $(test-expand $$(EXPAND))
load testapi.so
!,
              '', "expansion\n");

# TEST 2
# Check the eval operation.  Prove that the argument is expanded only once
run_make_test(q!
load testapi.so
TEST = bye
ASSIGN = VAR = $(TEST) $(shell echo there)
$(test-eval $(value ASSIGN))
TEST = hi
all:;@echo '$(VAR)'
!,
              '', "hi there\n");

# TEST 2
# Check the no-expand capability
run_make_test(q!
load testapi.so
TEST = hi
all:;@echo '$(test-noexpand $(TEST))'
!,
              '', "\$(TEST)\n");


# During all subsequent tests testapi.so exists.
#
my @loads = ('', q!
load testapi.so
load testapi.so
-load testapi.so
-load testapi.so
$(eval load testapi.so)
$(eval -load testapi.so)
!);

for my $extra_loads (@loads) {
my $n = 5;
if ($extra_loads) {
  $n = 12;
}
# sv 63045.
# Test that having unloaded a shared object make loads it again, even if the
# shared object is not updated.
$ENV{TESTAPI_VERBOSE} = 1;
run_make_test("
load testapi.so
$extra_loads
all:; \$(info \$(test-expand hello))
testapi.so: force; \$(info \$@)
force:;
.PHONY: force
", '', "testapi_gmk_setup\ntestapi.so\ntestapi_gmk_setup\nhello\n#MAKE#: 'all' is up to date.\n");

# sv 63045.
# Same as above, but testapi_gmk_setup returned -1.
$ENV{TESTAPI_KEEP} = 1;
$ENV{TESTAPI_VERBOSE} = 1;
run_make_test("
load testapi.so
$extra_loads
all:; \$(info \$(test-expand hello))
testapi.so: force; \$(info \$@)
force:;
.PHONY: force
", '', "testapi_gmk_setup\nhello\n#MAKE#: 'all' is up to date.\n");

# sv 63045.
# Test that make exits, unless make can successfully update an unloaded shared
# object.
$ENV{TESTAPI_VERBOSE} = 1;
run_make_test("
load testapi.so
$extra_loads
all:; \$(info \$(test-expand hello))
testapi.so: force; @#HELPER# fail 1
force:;
.PHONY: force
", '', "testapi_gmk_setup\nfail 1\n#MAKE#: *** [#MAKEFILE#:$n: testapi.so] Error 1\n", 512);

# sv 63045.
# Same as above, but testapi_gmk_setup returned -1.
$ENV{TESTAPI_KEEP} = 1;
$ENV{TESTAPI_VERBOSE} = 1;
run_make_test("
load testapi.so
$extra_loads
all:; \$(info \$(test-expand hello))
testapi.so: force; @#HELPER# fail 1
force:;
.PHONY: force
", '', "testapi_gmk_setup\nhello\n#MAKE#: 'all' is up to date.\n");

# sv 63100.
# Test that make supplies the correct floc when the shared object is loaded
# again.
$ENV{TESTAPI_VERBOSE} = 2;
run_make_test("
load testapi.so
$extra_loads
all:; \$(info \$(test-expand hello))
testapi.so: force; \$(info \$@)
force:;
.PHONY: force
", '', "testapi_gmk_setup\n#MAKEFILE#:2\ntestapi.so\ntestapi_gmk_setup\n#MAKEFILE#:2\nhello\n#MAKE#: 'all' is up to date.\n");
}

my @names  = ('testapi.so', './testapi.so', '#PWD#/testapi.so');

for my $name (@names) {

# Test the make correctly figures out the name of the close function and runs
# the close function.
$ENV{TESTAPI_VERBOSE} = 3;
run_make_test("
load $name
all:; \$(info \$(test-expand hello))
", '', "testapi_gmk_setup\nhello\n#MAKE#: 'all' is up to date.\ntestapi_gmk_unload\n");
}

# Same as above, but the setup function is custom.
@names  = ('testapi.so(alternative_setup)', './testapi.so(alternative_setup)',
           '#PWD#/testapi.so(alternative_setup)');
for my $name (@names) {

# Test the close function.
$ENV{TESTAPI_VERBOSE} = 3;
run_make_test("
load $name
all:; \$(info \$(test-expand hello))
", '', "alternative_setup\nhello\n#MAKE#: 'all' is up to date.\ntestapi_gmk_unload\n");
}

# Test that makes runs the close function on failure.
$ENV{TESTAPI_VERBOSE} = 3;
run_make_test(q!
load testapi.so
all: bad_preqreq; :
!, '', "testapi_gmk_setup\n#MAKE#: *** No rule to make target 'bad_preqreq', needed by 'all'.  Stop.\ntestapi_gmk_unload\n", 512);

# Test that make unloads a shared object, calls the close function, loads
# the plugin again, and then calls the close function again on exit.
&utouch(-10, 'testapi.so');
$ENV{TESTAPI_VERBOSE} = 3;
run_make_test("
load testapi.so
all:; \$(info \$(test-expand hello))
testapi.so: testapi.c; $sobuild
", '', "testapi_gmk_setup\ntestapi_gmk_unload\n$sobuild\ntestapi_gmk_setup\nhello\n#MAKE#: 'all' is up to date.\ntestapi_gmk_unload");

# Test that make unloads a shared object, calls the close function, loads
# the plugin again, and then calls the close function again on exit.
$ENV{TESTAPI_VERBOSE} = 3;
run_make_test(q!
load testapi.so
all:; $(info $(test-expand hello))
testapi.so: force; $(info $@)
force:;
.PHONY: force
!, '', "testapi_gmk_setup\ntestapi_gmk_unload\ntestapi.so\ntestapi_gmk_setup\nhello\n#MAKE#: 'all' is up to date.\ntestapi_gmk_unload\n");

unlink(qw(testapi.c testapi.so)) unless $keep;

# Test that make does not run the close function, unless the shared object
# loaded successfully.
unlink('testapi.so');
$ENV{TESTAPI_VERBOSE} = 3;
run_make_test(q!
load testapi.so
all:; :
!, '', "#MAKEFILE#:2: testapi.so: cannot open shared object file: $ERR_no_such_file\n#MAKEFILE#:2: *** testapi.so: failed to load.  Stop.\n", 512);

# This tells the test driver that the perl test script executed properly.
1;