Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[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 file: \"$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         print "SET( MEMCHECK 1) \n";
42         $dump = 1;
43         next;
44     }
45     if ( $line =~ /END TESH TESTS/ ) {
46         $dump = 0;
47         last;
48     }
49     if ($dump) {
50         $line =~ s/^  //;
51         if ( $line =~ /^\s*ADD_TEST\(\S+\s+\S*\/tesh\s/ ) {
52             undef %environ;
53             $config_var = "";
54             $path       = "";
55             $nb_test++;
56             $tesh_file = "";
57             $name_test = "";
58             $indent = "";
59
60             if ( $line =~ /^(\s*)ADD_TEST\((\S+)/ ) {
61                 $indent = ($1);
62                 $name_test = ($2);
63             }
64             while ( $line =~ /--cfg\s+(\S+)/g ) {
65                 $config_var = "--cfg=$1 $config_var";
66             }
67             while ( $line =~ /--cd\s+(\S+)/g ) {
68                 $path = ($1);
69                 $path =~ s/\"//g;
70             }
71             while ( $line =~ /--setenv\s+(\S+)\=(\S+)/g ) {
72                 my ( $env_var, $value_var ) = ( $1, $2 );
73                 $environ{$env_var} = $value_var;
74             }
75             if ( $line =~ /(\S+)\s*\)$/ ) {
76                 $tesh_file = $1;
77                 $tesh_file =~ s/^[^\/\$]/$path\/$&/;
78                 $tesh_file =~ s/\${CMAKE_HOME_DIRECTORY}/$proj_dir/g;
79                 if ( ! -e "$tesh_file" ) {
80                     print "# tesh_file: $tesh_file does not exist!\n";
81                     print "# $line\n";
82                     next;
83                 }
84             }
85
86             if (0) {
87                 print "test_name = $name_test\n";
88                 print "config_var = $config_var\n";
89                 print "path = $path\n";
90                 foreach my $key (keys %environ) {
91                     print "$key = $environ{$key}\n";
92                 }
93                 print "tesh_file = $tesh_file\n";
94                 print "\n\n";
95             }
96
97             my ($count)        = 0;
98             my ($count_first)  = 0;
99             my ($count_second) = 0;
100             open TESH_FILE, $tesh_file or die "Unable to open tesh file: \"$tesh_file\". $!\n";
101             my ($input) = "";
102             my ($l);
103             while ( defined( $l = <TESH_FILE> ) ) {
104                 chomp $l;
105                 if ( $l =~ /^< (.*)$/ ) {
106                     $input = $input . $1 . "\n";
107                 }
108                 if ( $l =~ /^\$ (.*)$/ ) {
109                     my ($command) = $1;
110                     foreach my $key (keys %environ) {
111                         $command = var_subst($command, $key, $environ{$key});
112                     }
113                     # substitute remaining known variables, if any
114                     $command = var_subst($command, "srcdir", "");
115                     $command = var_subst($command, "bindir", "");
116                     $command = var_subst($command, "EXEEXT", "");
117                     $command = var_subst($command, "SG_TEST_EXENV", "");
118                     $command = var_subst($command, "SG_TEST_ENV", "");
119                     $command = var_subst($command, "SG_EXENV_TEST", "");
120                     $command =~ s/\$@//g;
121 #                    $command =~ s/..\/..\/bin\/smpirun/\${CMAKE_BINARY_DIR\}\/bin\/smpirun/g;
122                     $command =~ s/^\s+//;
123                     $command =~ s/^[^\/\$]\S*\//$path\/$&/;
124                     $command =~ s/^(\S*\/)(?:\.\/)+/$1/g;
125
126                     if ($config_var) {
127                         $command = "$command $config_var";
128                     }
129                     if ( $command =~ /^mkfile\s+(\S+)/) {
130                         my $file = $1;
131                         # don't ask me to explain why so many backslashes...
132                         $input =~ s/\\/\\\\\\\\\\\\\\\\/g;
133                         $input =~ s/\n/\\\\\\\\n/g;
134                         $input =~ s/"/\\\\\\\\042/g;
135                         $input =~ s/'/\\\\\\\\047/g;
136                         $input =~ s/%/%%/g;
137                         $command = "sh -c \"printf '$input' > $file\"";
138                     }
139                     print "${indent}ADD_TEST(memcheck-$name_test-$count $command --cd $path\/)\n";
140                     $input = "";
141                     #push @test_list, "memcheck-$name_test-$count";
142                     $count++;
143                 }
144                 if ( $l =~ /^\& (.*)$/ ) {
145                     last;
146                 }
147             }
148             close(TESH_FILE);
149         }
150         elsif ( $line =~ /^\s*set_tests_properties/ ) {
151             if ( $line =~ /set_tests_properties\(([\S]+)/ ) {
152                 my ($name_temp) = ($1);
153                 $line =~ s/$name_temp/memcheck-$name_temp-0/g;
154                 print $line. "\n";
155             }
156         }
157         else {
158             print $line. "\n";
159         }
160     }
161 }
162 close(MAKETEST);
163
164 #print "nb_test = $nb_test\n";
165 #print "set(MEMCHECK_LIST\n";
166 #print (join("\n", @test_list));
167 #print ")\n";