Logo AND Algorithmique Numérique Distribuée

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