Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'mc' into mc++
[simgrid.git] / buildtools / Cmake / Scripts / generate_memcheck_tests.pl
1 #!/usr/bin/perl -w
2
3 # Copyright (c) 2012-2014. The SimGrid Team.
4 # All rights reserved.
5
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
8
9 use strict;
10
11 # input file = AddTest.txt
12
13 if ( $#ARGV != 1 ) {
14     die "Usage: generate_memcheck_tests.pl <CMAKE_HOME_DIRECTORY> AddTests.cmake\n";
15 }
16
17 my ($proj_dir) = $ARGV[0];
18 open MAKETEST, $ARGV[1] or die "Unable to open file: \"$ARGV[1]\". $!\n";
19
20 sub var_subst {
21     my ($text, $name, $value) = @_;
22     if ($value) {
23         $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
24         $text =~ s/\$$name(\W|$)/$value$1/g;
25     }
26     else {
27         $text =~ s/\${$name:=([^}]*)}/$1/g;
28         $text =~ s/\${$name}//g;
29         $text =~ s/\$$name(\W|$)/$1/g;
30     }
31     return $text;
32 }
33
34 my (@test_list) = ();
35 my ($nb_test)   = 0;
36 my ($line);
37 my ($path);
38 my ($dump) = 0;
39 my (%environ);
40 my ($tesh_file);
41 my ($config_var);
42 my ($name_test);
43 my ($indent);
44
45 while ( defined( $line = <MAKETEST> ) ) {
46     chomp $line;
47     if ( $line =~ /BEGIN TESH TESTS/ ) {
48         $dump = 1;
49         next;
50     }
51     if ( $line =~ /END TESH TESTS/ ) {
52         $dump = 0;
53         last;
54     }
55     if ($dump) {
56         $line =~ s/^  //;
57         if ( $line =~ /^\s*ADD_TEST\(\S+\s+\S*TESH\_COMMAND\}\s/ ) {
58             undef %environ;
59             $config_var = "";
60             $path       = "";
61             $nb_test++;
62             $tesh_file = "";
63             $name_test = "";
64             $indent = "";
65
66             if ( $line =~ /^(\s*)ADD_TEST\((\S+)/ ) {
67                 $indent = ($1);
68                 $name_test = ($2);
69             }
70             while ( $line =~ /--cfg\s+(\S+)/g ) {
71                 $config_var = "--cfg=$1 $config_var";
72             }
73             while ( $line =~ /--cd\s+(\S+)/g ) {
74                 $path = ($1);
75                 $path =~ s/\"//g;
76             }
77             while ( $line =~ /--setenv\s+(\S+)\=(\S+)/g ) {
78                 my ( $env_var, $value_var ) = ( $1, $2 );
79                 $environ{$env_var} = $value_var;
80             }
81             if ( $line =~ /(\S+)\s*\)$/ ) {
82                 $tesh_file = $1;
83                 $tesh_file =~ s/^[^\/\$]/$path\/$&/;
84                 $tesh_file =~ s/\${CMAKE_HOME_DIRECTORY}/$proj_dir/g;
85                 if ( ! -e "$tesh_file" ) {
86                     print "# tesh_file: $tesh_file does not exist!\n";
87                     print "# $line\n";
88                     next;
89                 }
90             }
91
92             if (0) {
93                 print "test_name = $name_test\n";
94                 print "config_var = $config_var\n";
95                 print "path = $path\n";
96                 foreach my $key (keys %environ) {
97                     print "$key = $environ{$key}\n";
98                 }
99                 print "tesh_file = $tesh_file\n";
100                 print "\n\n";
101             }
102
103             my ($count)        = 0;
104             my ($count_first)  = 0;
105             my ($count_second) = 0;
106             open TESH_FILE, $tesh_file or die "Unable to open tesh file: \"$tesh_file\". $!\n";
107             my ($input) = "";
108             my ($l);
109             while ( defined( $l = <TESH_FILE> ) ) {
110                 chomp $l;
111                 if ( $l =~ /^< (.*)$/ ) {
112                     $input = $input . $1 . "\n";
113                 }
114                 if ( $l =~ /^\$ (.*)$/ ) {
115                     my ($command) = $1;
116                     foreach my $key (keys %environ) {
117                         $command = var_subst($command, $key, $environ{$key});
118                     }
119                     # substitute remaining known variables, if any
120                     $command = var_subst($command, "srcdir", "");
121                     $command = var_subst($command, "bindir", "");
122                     $command = var_subst($command, "EXEEXT", "");
123                     $command = var_subst($command, "SG_TEST_EXENV", "");
124                     $command = var_subst($command, "SG_TEST_ENV", "");
125                     $command = var_subst($command, "SG_EXENV_TEST", "");
126                     $command =~ s/\$@//g;
127 #                    $command =~ s/..\/..\/bin\/smpirun/\${CMAKE_BINARY_DIR\}\/bin\/smpirun/g;
128                     $command =~ s/^\s+//;
129                     $command =~ s/^[^\/\$]\S*\//$path\/$&/;
130                     $command =~ s/^(\S*\/)(?:\.\/)+/$1/g;
131
132                     if ($config_var) {
133                         $command = "$command $config_var";
134                     }
135                     if ( $command =~ /^mkfile\s+(\S+)/) {
136                         my $file = $1;
137                         # don't ask me to explain why so many backslashes...
138                         $input =~ s/\\/\\\\\\\\\\\\\\\\/g;
139                         $input =~ s/\n/\\\\\\\\n/g;
140                         $input =~ s/"/\\\\\\\\042/g;
141                         $input =~ s/'/\\\\\\\\047/g;
142                         $input =~ s/%/%%/g;
143                         $command = "sh -c \"printf '$input' > $file\"";
144                     }
145                     print "${indent}ADD_TEST(memcheck-$name_test-$count $command --cd $path\/)\n";
146                     $input = "";
147                     #push @test_list, "memcheck-$name_test-$count";
148                     $count++;
149                 }
150                 if ( $l =~ /^\& (.*)$/ ) {
151                     last;
152                 }
153             }
154             close(TESH_FILE);
155         }
156         elsif ( $line =~ /^\s*set_tests_properties/ ) {
157             if ( $line =~ /set_tests_properties\(([\S]+)/ ) {
158                 my ($name_temp) = ($1);
159                 $line =~ s/$name_temp/memcheck-$name_temp-0/g;
160                 print $line. "\n";
161             }
162         }
163         else {
164             print $line. "\n";
165         }
166     }
167 }
168 close(MAKETEST);
169
170 #print "nb_test = $nb_test\n";
171 #print "set(MEMCHECK_LIST\n";
172 #print (join("\n", @test_list));
173 #print ")\n";