Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
f57f8a274ea9b9bc37b9706f5b719e7579487feb
[simgrid.git] / tools / cmake / scripts / generate_memcheck_tests.pl
1 #!/usr/bin/env perl
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 use warnings;
11
12 # input file = Test.txt
13
14 if ( $#ARGV != 1 ) {
15     die "Usage: generate_memcheck_tests.pl <CMAKE_HOME_DIRECTORY> Tests.cmake\n";
16 }
17
18 my ($proj_dir) = $ARGV[0];
19 open MAKETEST, $ARGV[1] or die "Unable to open file: \"$ARGV[1]\". $!\n";
20
21 sub var_subst {
22     my ($text, $name, $value) = @_;
23     if ($value) {
24         $text =~ s/\${$name(?::[=-][^}]*)?}/$value/g;
25         $text =~ s/\$$name(\W|$)/$value$1/g;
26     }
27     else {
28         $text =~ s/\${$name:=([^}]*)}/$1/g;
29         $text =~ s/\${$name}//g;
30         $text =~ s/\$$name(\W|$)/$1/g;
31     }
32     return $text;
33 }
34
35 my (@test_list) = ();
36 my ($nb_test)   = 0;
37 my ($line);
38 my ($path);
39 my ($dump) = 0;
40 my (%environ);
41 my ($tesh_file);
42 my ($factories);
43 my ($config_var);
44 my ($name_test);
45 my ($indent);
46
47 while ( defined( $line = <MAKETEST> ) ) {
48     chomp $line;
49     if ( $line =~ /BEGIN TESH TESTS/ ) {
50         $dump = 1;
51         next;
52     }
53     if ( $line =~ /END TESH TESTS/ ) {
54         $dump = 0;
55         next;
56     }
57     if ($dump) {
58         $line =~ s/^  //;
59         if ( $line =~ /^\s*(?:ADD_TEST\(\S+\s+\S*TESH\_COMMAND\}\s|ADD_TESH\(|ADD_TESH_FACTORIES)/ ) {
60             undef %environ;
61             $config_var = "";
62             $factories  = "";
63             $path       = "";
64             $nb_test++;
65             $tesh_file = "";
66             $name_test = "";
67             $indent = "";
68             if ( $line =~ /^(\s*)ADD_(?:TEST|TESH)\((\S+)/ ) {
69                 $indent = ($1);
70                 $name_test = ($2);
71             }
72             if ( $line =~ /^(\s*)ADD_TESH_FACTORIES\((\S+)\s+\"(\S+)\"/ ) {
73                 $indent = ($1);
74                 $name_test = ($2);
75                 $factories = ($3);
76             }
77             while ( $line =~ /--cfg\s+(\S+)/g ) {
78                 $config_var = "--cfg=$1 $config_var";
79             }
80             while ( $line =~ /--cd\s+(\S+)/g ) {
81                 $path = ($1);
82                 $path =~ s/\"//g;
83             }
84             while ( $line =~ /--setenv\s+(\S+)\=(\S+)/g ) {
85                 my ( $env_var, $value_var ) = ( $1, $2 );
86                 $environ{$env_var} = $value_var;
87             }
88             if ( $line =~ /(\S+)\s*\)$/ ) {
89                 $tesh_file = $1;
90                 $tesh_file =~ s/^[^\/\$]/$path\/$&/;
91                 $tesh_file =~ s/\${CMAKE_HOME_DIRECTORY}/$proj_dir/g;
92                 if ( ! -e "$tesh_file" ) {
93                     print "# tesh_file: $tesh_file does not exist!\n";
94                     print "# $line\n";
95                     next;
96                 }
97             }
98
99             if (0) {
100                 print "test_name = $name_test\n";
101                 print "config_var = $config_var\n";
102                 print "path = $path\n";
103                 foreach my $key (keys %environ) {
104                     print "$key = $environ{$key}\n";
105                 }
106                 print "tesh_file = $tesh_file\n";
107                 print "\n\n";
108             }
109
110             my ($count)        = 0;
111             my ($close)        = 0;
112             open TESH_FILE, $tesh_file or die "Unable to open tesh file: \"$tesh_file\". $!\n";
113             my ($input) = "";
114             my ($l);
115             while ( defined( $l = <TESH_FILE> ) ) {
116                 chomp $l;
117                 if ( $l =~ /^< (.*)$/ ) {
118                     $input = $input . $1 . "\n";
119                 }
120                 if ( $l =~ /^\$ (.*)$/ ) {
121                     my ($command) = $1;
122                     foreach my $key (keys %environ) {
123                         $command = var_subst($command, $key, $environ{$key});
124                     }
125                     # substitute remaining known variables, if any
126                     $command = var_subst($command, "srcdir", "");
127                     $command = var_subst($command, "bindir", "");
128                     $command = var_subst($command, "EXEEXT", "");
129                     $command = var_subst($command, "SG_TEST_EXENV", "");
130                     $command = var_subst($command, "SG_TEST_ENV", "");
131                     $command = var_subst($command, "SG_EXENV_TEST", "");
132                     $command =~ s/\$@//g;
133 #                    $command =~ s/..\/..\/bin\/smpirun/\${CMAKE_BINARY_DIR\}\/bin\/smpirun/g;
134                     $command =~ s/^\s+//;
135                     $command =~ s/^[^\/\$]\S*\//$path\/$&/;
136                     $command =~ s/^(\S*\/)(?:\.\/)+/$1/g;
137
138                     if ($config_var) {
139                         $command = "$command $config_var";
140                     }
141                     if ( $command =~ /^mkfile\s+(\S+)/) {
142                         my $file = $1;
143                         # don't ask me to explain why so many backslashes...
144                         $input =~ s/\\/\\\\\\\\/g;
145                         $input =~ s/\n/\\\\n/g;
146                         $input =~ s/"/\\\\042/g;
147                         $input =~ s/'/\\\\047/g;
148                         $input =~ s/%/%%/g;
149                         $command = "sh -c \"printf '$input' > $file\"";
150                     }
151                     if ($factories) {
152                       foreach my $factory (split(';', $factories)) {
153                         $close=0;
154                         if( $factory =~ /^raw/) {
155                             print "${indent}if(HAVE_RAWCTX)\n";
156                             $close=1;
157                         }elsif( $factory =~ /^boost/) {
158                             print "${indent}if(HAVE_BOOST_CONTEXT)\n";
159                             $close=1;
160                         }elsif( $factory =~ /^ucontext/) {
161                             print "${indent}if(CONTEXT_UCONTEXT)\n";
162                             $close=1;
163                         }
164                         print "${indent}ADD_TEST(NAME memcheck-$name_test-$factory-$count\n";
165                         print "${indent}         WORKING_DIRECTORY $path\/\n";
166                         print "${indent}         COMMAND $command --cfg=contexts/factory:$factory)\n";
167                         if ($count > 0) {
168                             print "${indent}set_tests_properties(memcheck-$name_test-$factory-$count\n";
169                             print "${indent}                     PROPERTIES DEPENDS memcheck-$name_test-$factory-" . ($count - 1) . ")\n";
170                         }
171                        if ($close > 0){
172                         print "${indent}endif()\n";
173                         }
174                       }
175                     } else {
176                       print "${indent}ADD_TEST(NAME memcheck-$name_test-$count\n";
177                       print "${indent}         WORKING_DIRECTORY $path\/\n";
178                       print "${indent}         COMMAND $command)\n";
179                       if ($count > 0) {
180                           print "${indent}set_tests_properties(memcheck-$name_test-$count\n";
181                           print "${indent}                     PROPERTIES DEPENDS memcheck-$name_test-" . ($count - 1) . ")\n";
182                       }
183                     }
184                     $input = "";
185                     #push @test_list, "memcheck-$name_test-$count";
186                     $count++;
187                 }
188                 if ( $l =~ /^\& (.*)$/ ) {
189                     last;
190                 }
191             }
192             close(TESH_FILE);
193         }
194         elsif ( $line =~ /^\s*SET_TESTS_PROPERTIES/ ) {
195             if ( $line =~ /SET_TESTS_PROPERTIES\(([\S]+)/ ) {
196                 my ($name_temp) = ($1);
197                 $line =~ s/$name_temp/memcheck-$name_temp-0/g;
198                 print $line. "\n";
199             }
200         } elsif ( $line =~ /^(\s*)ADD_TEST\((.*)\)/ ) {
201           print "$1ADD_TEST(memcheck-$2)\n";
202         } else {
203             print $line. "\n";
204         }
205     }
206 }
207 close(MAKETEST);
208
209 #print "nb_test = $nb_test\n";
210 #print "set(MEMCHECK_LIST\n";
211 #print (join("\n", @test_list));
212 #print ")\n";