Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Incorporate simgrid-java in simgrid-java/.
[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 ($srcdir);
33 my ($bindir);
34 my ($tesh_file);
35 my ($config_var);
36 my ($name_test);
37 my ($indent);
38
39 while ( defined( $line = <MAKETEST> ) ) {
40     chomp $line;
41     if ( $line =~ /BEGIN TESH TESTS/ ) {
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             $srcdir     = "";
53             $bindir     = "";
54             $config_var = "";
55             $path       = "";
56             $nb_test++;
57             $tesh_file = "";
58             $name_test = "";
59             $indent = "";
60
61             if ( $line =~ /^(\s*)ADD_TEST\((\S+)/ ) {
62                 $indent = ($1);
63                 $name_test = ($2);
64             }
65             while ( $line =~ /--cfg\s+(\S+)/g ) {
66                 $config_var = "--cfg=$1 $config_var";
67             }
68             while ( $line =~ /--cd\s+(\S+)/g ) {
69                 $path = ($1);
70                 $path =~ s/\"//g;
71             }
72             while ( $line =~ /--setenv\s+(\S+)\=(\S+)/g ) {
73                 my ( $env_var, $value_var ) = ( $1, $2 );
74                 if ( $env_var =~ /srcdir/ ) {
75                     $srcdir = $value_var;
76                 }
77                 elsif ( $env_var =~ /bindir/ ) {
78                     $bindir = $value_var;
79                 }
80             }
81             if ( $line =~ /(\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\n";
95                 print "path = $path\n";
96                 print "srcdir=$srcdir\n";
97                 print "bindir=$bindir\n";
98                 print "tesh_file = $tesh_file\n";
99                 print "\n\n";
100             }
101
102             my ($count)        = 0;
103             my ($count_first)  = 0;
104             my ($count_second) = 0;
105             open TESH_FILE, $tesh_file or die "Unable to open $tesh_file $!\n";
106             my ($input) = "";
107             my ($l);
108             while ( defined( $l = <TESH_FILE> ) ) {
109                 chomp $l;
110                 if ( $l =~ /^< (.*)$/ ) {
111                     $input = $input . $1 . "\n";
112                 }
113                 if ( $l =~ /^\$ (.*)$/ ) {
114                     my ($command) = $1;
115                     $command = var_subst($command, "srcdir", $srcdir);
116                     $command = var_subst($command, "bindir", $bindir);
117                     $command = var_subst($command, "EXEEXT", "");
118                     $command = var_subst($command, "SG_TEST_EXENV", "");
119                     $command = var_subst($command, "SG_TEST_ENV", "");
120                     $command = var_subst($command, "SG_EXENV_TEST", "");
121                     $command =~ s/\$@//g;
122 #                    $command =~ s/..\/..\/bin\/smpirun/\${CMAKE_BINARY_DIR\}\/bin\/smpirun/g;
123                     $command =~ s/^\s+//;
124                     $command =~ s/^[^\/\$]\S*\//$path\/$&/;
125                     $command =~ s/^(\S*\/)(?:\.\/)+/$1/g;
126
127                     if ($config_var) {
128                         $command = "$command $config_var";
129                     }
130                     if ( $command =~ /^mkfile\s+(\S+)/) {
131                         my $file = $1;
132                         # don't ask me to explain why so many backslashes...
133                         $input =~ s/\\/\\\\\\\\\\\\\\\\/g;
134                         $input =~ s/\n/\\\\\\\\n/g;
135                         $input =~ s/"/\\\\\\\\042/g;
136                         $input =~ s/'/\\\\\\\\047/g;
137                         $input =~ s/%/%%/g;
138                         $command = "sh -c \"printf '$input' > $file\"";
139                     }
140                     print "${indent}ADD_TEST(memcheck-$name_test-$count $command --cd $path\/)\n";
141                     $input = "";
142                     #push @test_list, "memcheck-$name_test-$count";
143                     $count++;
144                 }
145                 if ( $l =~ /^\& (.*)$/ ) {
146                     last;
147                 }
148             }
149             close(TESH_FILE);
150         }
151         elsif ( $line =~ /^\s*set_tests_properties/ ) {
152             if ( $line =~ /set_tests_properties\(([\S]+)/ ) {
153                 my ($name_temp) = ($1);
154                 $line =~ s/$name_temp/memcheck-$name_temp-0/g;
155                 print $line. "\n";
156             }
157         }
158         else {
159             print $line. "\n";
160         }
161     }
162 }
163 close(MAKETEST);
164
165 #print "nb_test = $nb_test\n";
166 #print "set(MEMCHECK_LIST\n";
167 #print (join("\n", @test_list));
168 #print ")\n";