Logo AND Algorithmique Numérique Distribuée

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