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 $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
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         if ( $line =~ /^\s*ADD_TEST\(\S+\s+\S*\/tesh\s/ ) {
50             $srcdir     = "";
51             $bindir     = "";
52             $config_var = "";
53             $path       = "";
54             $nb_test++;
55             $tesh_file = "";
56             $name_test = "";
57
58             if ( $line =~ /ADD_TEST\((\S+)/ ) {
59                 $name_test = ($1);
60             }
61             while ( $line =~ /--cfg\s+(\S+)/g ) {
62                 $config_var = "--cfg=$1 $config_var";
63             }
64             while ( $line =~ /--cd\s+(\S+)/g ) {
65                 $path = ($1);
66                 $path =~ s/\"//g;
67             }
68             while ( $line =~ /--setenv\s+(\S+)\=(\S+)/g ) {
69                 my ( $env_var, $value_var ) = ( $1, $2 );
70                 if ( $env_var =~ /srcdir/ ) {
71                     $srcdir = $value_var;
72                 }
73                 elsif ( $env_var =~ /bindir/ ) {
74                     $bindir = $value_var;
75                 }
76             }
77             if ( $line =~ /(\S+)\)$/ ) {
78                 $tesh_file = $1;
79                 $tesh_file =~ s/^[^\/\$]/$path\/$&/;
80                 $tesh_file =~ s/\${CMAKE_HOME_DIRECTORY}/$proj_dir/g;
81                 if ( ! -e "$tesh_file" ) {
82                     print "# tesh_file: $tesh_file does not exist!\n";
83                     print "# $line\n";
84                     next;
85                 }
86             }
87
88             if (0) {
89                 print "test_name = $name_test\n";
90                 print "$config_var\n";
91                 print "path = $path\n";
92                 print "srcdir=$srcdir\n";
93                 print "bindir=$bindir\n";
94                 print "tesh_file = $tesh_file\n";
95                 print "\n\n";
96             }
97
98             my ($count)        = 0;
99             my ($count_first)  = 0;
100             my ($count_second) = 0;
101             open TESH_FILE, $tesh_file or die "Unable to open $tesh_file $!\n";
102             my ($l);
103             while ( defined( $l = <TESH_FILE> ) ) {
104                 chomp $l;
105                 if ( $l =~ /^\$ (.*)$/ ) {
106                     my ($command) = $1;
107                     $command = var_subst($command, "srcdir", $srcdir);
108                     $command = var_subst($command, "bindir", $bindir);
109                     $command = var_subst($command, "EXEEXT", "");
110                     $command = var_subst($command, "SG_TEST_EXENV", "");
111                     $command = var_subst($command, "SG_TEST_ENV", "");
112                     $command = var_subst($command, "SG_EXENV_TEST", "");
113                     $command =~ s/\$@//g;
114 #                    $command =~ s/..\/..\/bin\/smpirun/\${CMAKE_BINARY_DIR\}\/bin\/smpirun/g;
115                     $command =~ s/^\s+//;
116                     $command =~ s/^[^\/\$]\S*\//$path\/$&/;
117                     $command =~ s/^(\S*\/)(?:\.\/)+/$1/g;
118
119                     if ($config_var) {
120                         $command = "$command $config_var";
121                     }
122                     print "ADD_TEST(memcheck-$name_test-$count $command --cd $path\/)\n";
123
124                     #push @test_list, "memcheck-$name_test-$count";
125                     $count++;
126                 }
127                 if ( $l =~ /^\& (.*)$/ ) {
128                     last;
129                 }
130             }
131             close(TESH_FILE);
132         }
133         elsif ( $line =~ /^\s*set_tests_properties/ ) {
134             if ( $line =~ /set_tests_properties\(([\S]+)/ ) {
135                 my ($name_temp) = ($1);
136                 $line =~ s/$name_temp/memcheck-$name_temp-0/g;
137                 print $line. "\n";
138             }
139         }
140         else {
141             print $line. "\n";
142         }
143     }
144 }
145 close(MAKETEST);
146
147 #print "nb_test = $nb_test\n";
148 #print "set(MEMCHECK_LIST\n";
149 #print (join("\n", @test_list));
150 #print ")\n";