Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b8528d96499e3abe8ac03055bd750989dc2d5c8a
[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 ($l);
107             while ( defined( $l = <TESH_FILE> ) ) {
108                 chomp $l;
109                 if ( $l =~ /^\$ (.*)$/ ) {
110                     my ($command) = $1;
111                     $command = var_subst($command, "srcdir", $srcdir);
112                     $command = var_subst($command, "bindir", $bindir);
113                     $command = var_subst($command, "EXEEXT", "");
114                     $command = var_subst($command, "SG_TEST_EXENV", "");
115                     $command = var_subst($command, "SG_TEST_ENV", "");
116                     $command = var_subst($command, "SG_EXENV_TEST", "");
117                     $command =~ s/\$@//g;
118 #                    $command =~ s/..\/..\/bin\/smpirun/\${CMAKE_BINARY_DIR\}\/bin\/smpirun/g;
119                     $command =~ s/^\s+//;
120                     $command =~ s/^[^\/\$]\S*\//$path\/$&/;
121                     $command =~ s/^(\S*\/)(?:\.\/)+/$1/g;
122
123                     if ($config_var) {
124                         $command = "$command $config_var";
125                     }
126                     print "${indent}ADD_TEST(memcheck-$name_test-$count $command --cd $path\/)\n";
127
128                     #push @test_list, "memcheck-$name_test-$count";
129                     $count++;
130                 }
131                 if ( $l =~ /^\& (.*)$/ ) {
132                     last;
133                 }
134             }
135             close(TESH_FILE);
136         }
137         elsif ( $line =~ /^\s*set_tests_properties/ ) {
138             if ( $line =~ /set_tests_properties\(([\S]+)/ ) {
139                 my ($name_temp) = ($1);
140                 $line =~ s/$name_temp/memcheck-$name_temp-0/g;
141                 print $line. "\n";
142             }
143         }
144         else {
145             print $line. "\n";
146         }
147     }
148 }
149 close(MAKETEST);
150
151 #print "nb_test = $nb_test\n";
152 #print "set(MEMCHECK_LIST\n";
153 #print (join("\n", @test_list));
154 #print ")\n";