Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change sg_unit_extractor so that it takes all units to generate on its command line...
[simgrid.git] / tools / sg_unit_extractor.pl
1 #! /usr/bin/perl
2
3 use strict;
4
5 my $progname="sg_unit_extractor";
6 # Get the args 
7 die "USAGE: $progname infile [infile+]\n"
8   if (scalar @ARGV == 0);
9
10 map {process_one($_)} @ARGV;
11
12 sub process_one($) {
13     
14     my $infile = shift;
15     my $outfile;
16     
17     $outfile =  $infile;
18     $outfile =~ s/\.c$/_unit.c/;
19     $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
20     
21     
22     # Get the unit data
23     my ($unit_source,$suite_name,$suite_title)=("","","");
24     my (%tests); # to detect multiple definition
25     my (@tests); # actual content
26     
27     open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
28     
29     my $takeit=0;
30     my $line=0;
31     my $beginline=0;
32     while (<IN>) {
33         $line++;
34         if (m/ifdef +SIMGRID_TEST/) {
35             $beginline = $line;
36             $takeit = 1;
37             next;
38         }
39         if (m/endif.*SIMGRID_TEST/) {
40             $takeit = 0;
41             next
42         }
43         
44         if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*,(.*?)\);/) { #" {
45             die "$progname: Multiple suites in the same file ($infile) are not supported yet\n" if length($suite_name);
46             ($suite_name,$suite_title)=($1,$2);
47             next;
48         } 
49
50         if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"{
51             die "$progname: multiply defined unit in file $infile: $1\n" if (defined($tests{$1}));
52       
53             my @t=($1,$2,$3);
54             push @tests,\@t;
55             $tests{$1} = 1;
56         }
57         $unit_source .= $_ if $takeit;
58     }
59     close IN || die "$progname: cannot close input file '$infile': $!\n";
60
61
62     if ($takeit) {
63         die "$progname: end of file reached in SIMGRID_TEST block.\n".
64           "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
65           "Example:\n".
66           "#endif /* SIMGRID_TEST */\n"
67     }
68
69     die "$progname: no suite defined in $infile\n" unless (length($suite_name));
70   
71     # Write the test
72
73     my ($GENERATED)=("/*******************************/\n".
74                      "/* GENERATED FILE, DO NOT EDIT */\n".
75                      "/*******************************/\n\n");
76     $beginline+=2;
77     open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
78     print OUT $GENERATED;
79     print OUT "#include <stdio.h>\n";
80     print OUT "#include \"xbt.h\"\n";
81     print OUT $GENERATED;
82     print OUT "# $beginline \"$infile\" \n";
83     print OUT "$unit_source";
84     print OUT $GENERATED;
85     close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
86
87     # write the main skeleton if needed
88     if (! -e "simgrid_units_main.c") {
89         open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
90         print OUT $GENERATED;
91         print OUT "#include <stdio.h>\n\n";
92         print OUT "#include \"xbt.h\"\n\n";
93         print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
94         print OUT "/* SGU: BEGIN PROTOTYPES */\n";
95         print OUT "/* SGU: END PROTOTYPES */\n\n";
96         print OUT $GENERATED;
97         #  print OUT "# 93 \"sg_unit_extractor.pl\"\n";
98         print OUT <<EOF;
99 int main(int argc, char *argv[]) {
100   xbt_test_suite_t suite; 
101   char selection[1024];
102   int i;\n
103   int res;\n
104   /* SGU: BEGIN SUITES DECLARATION */
105   /* SGU: END SUITES DECLARATION */
106       
107   xbt_init(&argc,argv);
108     
109   /* Search for the tests to do */
110     selection[0]='\\0';
111     for (i=1;i<argc;i++) {
112       if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
113         char *p=strchr(argv[i],'=')+1;
114         if (selection[0] == '\\0') {
115           strcpy(selection, p);
116         } else {
117           strcat(selection, \",\");
118           strcat(selection, p);
119         }
120       } else if (!strncmp(argv[i],\"--dump-only\",strlen(\"--dump-only\"))||
121                  !strncmp(argv[i],\"--dump\",     strlen(\"--dump\"))) {
122         xbt_test_dump(selection);
123         return 0;
124       } else if (!strncmp(argv[i],\"--help\",strlen(\"--help\"))) {
125           printf(
126               "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
127               "--help: display this help\\n"
128               "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
129               "--tests=selection: Use argument to select which suites/units/tests to run\\n"
130               "                   --tests can be used more than once, and selection may be a comma\\n"
131               "                   separated list of directives.\\n\\n"
132               "Directives are of the form:\\n"
133               "   [-]suitename[:unitname]\\n\\n"
134               "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
135               "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
136               "it applies on any unit.\\n\\n"
137               "By default, everything is enabled.\\n\\n"
138               "'all' as suite name apply to all suites.\\n\\n"
139               "Example 1: \\"-toto,+toto:tutu\\"\\n"
140               "  disables the whole toto testsuite (any unit in it),\\n"
141               "  then reenables the tutu unit of the toto test suite.\\n\\n"
142               "Example 2: \\"-all,+toto\\"\\n"
143               "  Run nothing but the toto suite.\\n");
144           return 0;
145       } else {
146         printf("testall: Unknown option: %s\\n",argv[i]);
147         return 1;
148       }
149     }
150   /* Got all my tests to do */
151       
152   res = xbt_test_run(selection);
153   xbt_test_exit();
154   xbt_exit();
155   return res;
156 }
157 EOF
158         print OUT $GENERATED;
159         close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
160     }
161
162    print "  Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
163    map {
164        my ($name,$func,$title) = @{$_};
165        print "    unit $name: func=$func; title=$title\n";
166    } @tests;
167
168    #while (my $t = shift @tests) {
169
170    # add this suite to the main
171    my $newmain="";
172    open IN,"simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
173     # search prototypes
174        while (<IN>) {
175            $newmain .= $_;
176            #    print "Look for proto: $_";
177            last if /SGU: BEGIN PROTOTYPES/;
178        }
179
180        # search my prototype
181        while (<IN>) {
182            #    print "Seek protos: $_";
183            last if  (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
184            $newmain .= $_;
185        }
186        if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
187            while (<IN>) {
188                last if /SGU: END FILE/;
189            }
190            $_ = <IN>; # pass extra blank line
191            chomp;
192            die "this line should be blank ($_). Did you edit the file?" if /\W/;
193        }
194        my ($old_)=($_);
195        # add my section
196        $newmain .= "  /* SGU: BEGIN FILE $infile */\n";
197        map {
198            my ($name,$func,$title) = @{$_};
199            $newmain .=  "    void $func(void);\n"
200        } @tests;
201        
202        $newmain .= "  /* SGU: END FILE */\n\n";
203        if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
204            $newmain .= $old_;
205        }
206        
207        # pass remaining prototypes, search declarations
208        while (<IN>) {
209            $newmain .= $_ unless /SGU: END PROTOTYPES/;
210            last if /SGU: BEGIN SUITES DECLARATION/;
211        }
212        
213        ### Done with prototypes. And now, the actual code
214        
215        # search my prototype
216        while (<IN>) {
217            last if  (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
218            $newmain .= $_;
219        }
220        if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it    
221            while (<IN>) {
222                last if /SGU: END FILE/;
223            }
224            $_ = <IN>; # pass extra blank line
225            chomp;
226            die "this line should be blank ($_). Did you edit the file?" if /\W/;
227        }
228        my ($old_)=($_);
229        # add my section
230        $newmain .= "    /* SGU: BEGIN FILE $infile */\n";
231        $newmain .= "      suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
232        map {
233            my ($name,$func,$title) = @{$_};
234            $newmain .=  "      xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
235        } @tests;
236        
237        $newmain .= "    /* SGU: END FILE */\n\n";
238        if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
239            $newmain .= $old_;
240        }
241        
242        # pass the remaining 
243        while (<IN>) {
244            $newmain .= $_;
245        }
246        close IN || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
247        
248        # write it back to main
249        open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
250        print OUT $newmain;
251        close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
252 } # end if process_one($)
253
254 0;