Logo AND Algorithmique Numérique Distribuée

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