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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#!/usr/bin/perl -w
# $Id: //info.ravenbrook.com/project/mps/master/test/test/script/clib#9 $
#
# subroutines to compile test libraries, and check whether they
# need to be compiled
#

1;

use Cwd;
use File::Path qw(rmtree);

sub clib {
 my $success = 1;
 my $tlfile;
 my $tlobj;

 &objpurge();
 &mpslibbuild();
 &scrutinize();
 &logcomment("Compiling test libraries.");

 open(MANIFEST, "$testlib_dir/manifest");

 while (defined($tlfile = <MANIFEST>)) {
  unless ($tlfile =~ /^%/) {
   chomp($tlfile);
   $tlfile = $testlib_dir."/".$tlfile;
   $tlobj = $tlfile;
   $tlobj =~ s/\.c/$obj_suffix/;
   $tlobj =~ s/$testlib_dir/$obj_dir/;

   if (&compile($tlfile, $tlobj)) {
   } else {
    $success = 0;
    &logcomment(" failed on $tlfile.");
   }
  }
 }
 close(MANIFEST);
 &record_clib($success);

 return $success;
}


#
# delete everything in the object directory
#

sub objpurge {
 unless (opendir(DIR, $obj_dir)) {
  die "Failed to open object directory $obj_dir.\n";
 }
 &logcomment("Cleaning out old object files.");
 foreach (readdir(DIR)) {
  unless ($_ eq "." || $_ eq ".." || rmtree $obj_dir."/".$_) {
   &logcomment("  ... but failed to delete $_.");
  }
 }
 closedir(DIR);
}

#
# Build the MPS object file.
#

sub mpslibbuild {
 &logcomment("Building MPS library.");
 local $dir = cwd();
 chdir($MPS_INCLUDE_DIR);
 &mysystem($make_command);
 chdir($dir);
}


#
# record information about environment so that when running tests
# we can check the libraries are still applicable
#
# specifically:
#  - MMQA_harness version
#  - values of MPS_INCLUDE_DIR and MPS_LINK_OBJ
#  - latest modification time of a mpsXXX.h files in MPS_INCLUDE_DIR,
#  - or an object in MPS_LINK_OBJ
#  - C-compiler version??

sub record_clib {
 my ($success) = @_;
 unless (open(REC, ">$obj_dir/record")) {
  die "Unable to write clib record.";
 }
 print REC "HARNESS_VERSION $HARNESS_VERSION\n";
 print REC "INCLUDE_DIR $MPS_INCLUDE_DIR\n";
 print REC "LINK_OBJ $MPS_LINK_OBJ\n";
 print REC "SUCCESS $success\n";
# &headertimes and &linkobjtimes have already been called, by &scrutinize
 foreach (sort keys %mps_headers) {
  print REC "HEADER $_ $mps_headers{$_}\n";
 }
 foreach (sort keys %mps_linkobjs) {
  print REC "LINK $_ $mps_linkobjs{$_}\n";
 }
 close(REC);
}

#
# check whether the test libraries correspond to the current
# settings
#

sub test_clib {
 my $err = 0;

 if (!open(REC, "$obj_dir/record")) {
  $err = "no test library description found";
 } elsif (<REC> ne "HARNESS_VERSION $HARNESS_VERSION\n") {
  $err = "libraries were compiled with a different harness version";
 } elsif (<REC> ne "INCLUDE_DIR $MPS_INCLUDE_DIR\n") {
  $err = "MPS_INCLUDE_DIR has changed";
 } elsif (<REC> ne "LINK_OBJ $MPS_LINK_OBJ\n") {
  $err = "MPS_LINK_OBJ has changed";
 } elsif (<REC> ne "SUCCESS 1\n") {
  $err = "previous attempt to compile test libraries failed";
 } else {
  &headertimes();
  &linkobjtimes();
  while (<REC>) {
   if (/^HEADER\s+(\S+)\s+(\S+)/) {
    if (!exists $mps_headers{$1}) {
     $err = "header file $1 disappeared";
    } elsif ($mps_headers{$1} != $2) {
     $err = "I think $1 may have changed"; 
    } else {
     delete $mps_headers{$1};
    }
   } elsif (/^LINK\s+(\S+)\s+(\S+)/) {
    if (!exists $mps_linkobjs{$1}) {
     $err = "link object $1 disappeared";
    } elsif ($mps_linkobjs{$1} != $2) {
     $err = "I think $1 may have changed"; 
    } else {
     delete $mps_linkobjs{$1};
    }
   } else {
    $err = "test library description not understood";
   }
   if ($err) {
    last;
   }
  }
  unless ($err) {
   if (scalar (keys %mps_headers)) {
    ($err) = keys %mps_headers;
    $err = "new header file $err";
   } elsif (scalar (keys %mps_linkobjs)) {
    ($err) = keys %mps_linkobjs;
    $err = "new link object $err";
   } 
  }
 }
 return $err;
}


sub headertimes {
 %mps_headers = ();

 unless (opendir(DIR, $MPS_INCLUDE_DIR)) {
  die "Failed to open directory $MPS_INCLUDE_DIR.\n";
 }
 foreach (readdir(DIR)) {
  if (/^mps.*\.h$/ && ! $ignored_headers{$_}) {
   $mps_headers{$_} = &mod_time("$MPS_INCLUDE_DIR/$_");
  }
 }
 closedir(DIR);
}

sub linkobjtimes {
 %mps_linkobjs = ();
 $_ = $MPS_LINK_OBJ;

 foreach (split) {
  $mps_linkobjs{$_} = &mod_time($_);
 }
}

sub mod_time {
 my ($file, $modtime) = @_;

 unless (open(STAT, $file)) {
  die "Couldn't find $file.\n";
 }
 (undef,undef,undef,undef,undef,
  undef,undef,undef,undef,$modtime) = stat STAT;
 close(STAT);
 return $modtime;
}

#
# root around in MPS_INCLUDE_DIR and find useful-looking header files
#


sub scrutinize {
 my $command;
 my $comobj;

 %mps_symbols = ();
 %mps_linkable = ();

 &logcomment("Checking settings.");
 &headertimes();
 &linkobjtimes();
 &logcomment("Scrutinizing MPS header files.");

 foreach (keys %mps_headers) {
  &scrutfile($_);
 }

# add a dummy symbol to allow us to check that non-defined
# symbols are correctly filtered out

 $mps_symbols{"MPS_MMQA_DUMMY_SYMBOL"} = 1;

 unless (open(SYM, ">$obj_dir/symtest.c")) {
  die "Failed to write symbol test file.\n";
 }
 print SYM "/* THIS FILE IS AUTOMATICALLY GENERATED */\n\n";
 foreach (sort keys %mps_symbols) {
  print SYM "void $_(void);\n";
 }
 print SYM "\n\nint main(void) {\n";
 foreach (sort keys %mps_symbols) {
  print SYM " $_();\n";
 }
 print SYM "\n return 1;\n}\n\n";
 close(SYM);

 $command = "$obj_dir/symtest.c";
 if ($cc_objandexe) {
  $comobj = "$cc_obj$obj_dir/symtest$obj_suffix";
 } else {
  $comobj = "";
 }
 $comout = "$obj_dir/symtest.out";

 if (&mysystem("$cc_command $cc_opts $comobj $cc_exe$obj_dir/symtest"
               . " $obj_dir/symtest.c $MPS_LINK_OBJ $cc_link_opts "
               . sprintf($stdboth_red, $comout))
     == 127) {
  die "Failed link test";
 }

 %mps_linkable = %mps_symbols;

 open(LINKTEST, $comout);
 while (<LINKTEST>) {
  # Clang helpfully annotates its "Undefined symbols" error messages
  # with lines of the form "(maybe you meant: _mps_ap_trip)". To avoid
  # false positives, we must ignore these lines.
  if (!/maybe you meant:/) {
   while (s/((mps|MPS)_\w+)/ /) {
    delete $mps_linkable{$1};
    &debug("Filtering out $1.");
   }
  }
 }
 close(LINKTEST);

 if (exists $mps_linkable{"MPS_MMQA_DUMMY_SYMBOL"}) {
  print "Failed to determine symbols defined in MPS libraries -- exiting.\n";
  die "[Complain to mm-qa about this.]\n";
 } elsif ((scalar(keys %mps_symbols)) == 0) {
  print "Couldn't determine which symbols are defined in MPS libraries -- exiting.\n";
  die "[Complain to mm-qa about this.]\n";
 }
 
 delete $mps_symbols{"MPS_MMQA_DUMMY_SYMBOL"};

 unless (open(SYM, ">$obj_dir/mmqasym.h")) {
  die "Failed to write mmqa symbol file.\n";
 }
 print SYM "/* THIS FILE IS AUTOMATICALLY GENERATED */\n\n";
 print SYM "/* mps header files */\n\n";
 foreach (sort keys %mps_headers) {
  s/\.h$//;
  print SYM "#define MMQA_HEADER_$_\n";
 }
 print SYM "\n\n/* symbols in header files */\n\n";
 foreach (sort keys %mps_symbols) {
  print SYM "#define MMQA_SYMBOL_$_\n";
 }
 print SYM "\n\n/* symbols defined in library */\n\n";
 foreach (sort keys %mps_linkable) {
  print SYM "#define MMQA_DEFINED_$_\n";
 }
 print SYM "\n/* end */\n";
 close(SYM);
}

sub scrutfile {
 my ($infile) = @_;
 my $cmd;

 unless(open(IN, "$MPS_INCLUDE_DIR/$infile")) {
  die "Whoops! Failed to read $infile.\n";
 }
 while (<IN>) {
  chomp;
  while (s/\$//) { $_ = $_.<IN>; chomp; }
  if (/^\s*#\s*define\s*((mps|MPS)_\w+)/) {
   $mps_symbols{$1} = 1;
  }
 }
 close(IN);

 $cmd = &convdirseps("$preprocommand $MPS_INCLUDE_DIR/$infile |");
 &debug("OPEN >>$cmd<<");

 unless(open(IN, $cmd)) {
  die "Failed to preprocess $infile.\n";
 }
 while (<IN>) {
  while (s/((mps|MPS)_\w+)/ /) {
   $mps_symbols{$1} = 1;
  }
 }
 close(IN);
}


sub readSymbols {
 %mps_symbols = ();
 %mps_linkable = ();
 %mps_assumed = ();

 unless (open(SYM, "$obj_dir/mmqasym.h")) {
  die "Couldn't read symbol list -- recompile test libraries (\"qa clib\").\n";
 }

 while (<SYM>) {
  chomp;
  if (/#define MMQA_SYMBOL_(.*)$/) {
   $mps_symbols{$1} = 1;
  } elsif (/#define MMQA_DEFINED_(.*)$/) {
   $mps_linkable{$1} = 1;
  }
 }
 close(SYM);

 unless (open(SYM, "$testlib_dir/assumed")) {
  die "Couldn't read assumed symbol list. Complain to mm-qa.\n";
 }

 while (<SYM>) {
  chomp;
  unless (/^%/) {
   $mps_assumed{$_} = 1;
  }
 }
}
 

#
# make a list of all the things which look like mps symbols
# mentioned in a file
#

sub listFileSymbols {
 my ($infile) = @_;
 my @symbols = ();

 unless (open(IN, $infile)) {
  die "Failed to open $infile.\n";
 }
 while (<IN>) {
  unless (/^\/\*/ .. /\*\/$/) {
   while (/\b((mps|MPS)_\w+\b)/g) {
    push @symbols, $1;
   }
  }
 }
 close(IN);

 return \@symbols;
}


#
# find which symbols in a list are not defined mps symbols
# Return a reference to a list of them
#

sub missingSymbols {
 my ($checklist) = @_;
 my @missing = ();
 
 foreach (@$checklist) {
  unless (exists $mps_symbols{$_} || exists $mps_assumed{$_}) {
   push @missing, $_;
  }
 }

 return \@missing;
}