6 use Getopt::Long qw(GetOptions);
8 my $progname="sg_unit_extractor";
13 print "USAGE: $progname [--root=part/to/cut] [--path=where/to/search NOT WORKING] [--outdir=where/to/generate/files] infile [infile+]\n";
22 Getopt::Long::config('permute','no_getopt_compat', 'no_auto_abbrev');
24 'help|h' => sub {usage(0)},
27 'outdir=s' =>\$outdir) or usage(1);
29 usage(1) if (scalar @ARGV == 0);
31 map {process_one($_)} @ARGV;
37 $infile =~ s|src/|| unless (-e $infile);
40 $outfile =~ s/\.c$/_unit.c/;
41 $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
42 $outfile = "$outdir$outfile";
44 print "$progname: processing $infile (generating $outfile)...\n";
47 my ($unit_source,$suite_name,$suite_title)=("","","");
48 my (%tests); # to detect multiple definition
49 my (@tests); # actual content
51 open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
52 $infile =~ s|$root|| if defined($root);
59 if (m/ifdef +SIMGRID_TEST/) {
64 if (m/endif.*SIMGRID_TEST/) {
69 if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*, *(.*?)\);/) { #" {
70 die "$progname: Multiple suites in the same file ($infile) are not supported yet\n" if length($suite_name);
71 ($suite_name,$suite_title)=($1,$2);
72 die "$progname: Empty suite name in $infile" unless length($suite_name);
73 die "$progname: Empty suite title in $infile" unless length($suite_title);
75 } elsif (m/XBT_TEST_SUITE/) {
76 die "$progname: Parse error: This line seem to be a test suite declaration, but failed to parse it\n$_\n";
79 if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"{
80 die "$progname: multiply defined unit in file $infile: $1\n" if (defined($tests{$1}));
85 } elsif (m/XBT_TEST_UNIT/) {
86 die "$progname: Parse error: This line seem to be a test unit, but failed to parse it\n$_\n";
88 $unit_source .= $_ if $takeit;
90 close IN || die "$progname: cannot close input file '$infile': $!\n";
94 die "$progname: end of file reached in SIMGRID_TEST block.\n".
95 "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
97 "#endif /* SIMGRID_TEST */\n"
100 die "$progname: no suite defined in $infile\n" unless (length($suite_name));
104 my ($GENERATED)=("/*******************************/\n".
105 "/* GENERATED FILE, DO NOT EDIT */\n".
106 "/*******************************/\n\n");
108 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
109 print OUT $GENERATED;
110 print OUT "#include <stdio.h>\n";
111 print OUT "#include \"xbt.h\"\n";
112 print OUT $GENERATED;
113 print OUT "#line $beginline \"$infile\" \n";
114 print OUT "$unit_source";
115 print OUT $GENERATED;
116 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
118 # write the main skeleton if needed
119 if (! -e "${outdir}simgrid_units_main.c") {
120 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
121 print OUT $GENERATED;
122 print OUT "#include <stdio.h>\n\n";
123 print OUT "#include \"xbt.h\"\n\n";
124 print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
125 print OUT "/* SGU: BEGIN PROTOTYPES */\n";
126 print OUT "/* SGU: END PROTOTYPES */\n\n";
127 print OUT $GENERATED;
128 # print OUT "# 93 \"sg_unit_extractor.pl\"\n";
130 int main(int argc, char *argv[]) {
131 xbt_test_suite_t suite;
132 char selection[1024];
137 /* SGU: BEGIN SUITES DECLARATION */
138 /* SGU: END SUITES DECLARATION */
140 xbt_init(&argc,argv);
142 /* Search for the tests to do */
144 for (i=1;i<argc;i++) {
145 if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
146 char *p=strchr(argv[i],'=')+1;
147 if (selection[0] == '\\0') {
148 strcpy(selection, p);
150 strcat(selection, \",\");
151 strcat(selection, p);
153 } else if (!strcmp(argv[i], \"--verbose\")) {
155 } else if (!strcmp(argv[i], \"--dump-only\")||
156 !strcmp(argv[i], \"--dump\")) {
157 xbt_test_dump(selection);
159 } else if (!strcmp(argv[i], \"--help\")) {
161 "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
162 "--help: display this help\\n"
163 "--verbose: print the name for each running test\\n"
164 "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
165 "--tests=selection: Use argument to select which suites/units/tests to run\\n"
166 " --tests can be used more than once, and selection may be a comma\\n"
167 " separated list of directives.\\n\\n"
168 "Directives are of the form:\\n"
169 " [-]suitename[:unitname]\\n\\n"
170 "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
171 "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
172 "it applies on any unit.\\n\\n"
173 "By default, everything is enabled.\\n\\n"
174 "'all' as suite name apply to all suites.\\n\\n"
175 "Example 1: \\"-toto,+toto:tutu\\"\\n"
176 " disables the whole toto testsuite (any unit in it),\\n"
177 " then reenables the tutu unit of the toto test suite.\\n\\n"
178 "Example 2: \\"-all,+toto\\"\\n"
179 " Run nothing but the toto suite.\\n");
182 printf("testall: Unknown option: %s\\n",argv[i]);
186 /* Got all my tests to do */
188 res = xbt_test_run(selection, verbosity);
193 print OUT $GENERATED;
194 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
197 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
199 my ($name,$func,$title) = @{$_};
200 print " unit $name: func=$func; title=$title\n";
203 #while (my $t = shift @tests) {
205 # add this suite to the main
207 open IN,"${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
211 # print "Look for proto: $_";
212 last if /SGU: BEGIN PROTOTYPES/;
215 # search my prototype
217 # print "Seek protos: $_";
218 last if (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
221 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
223 last if /SGU: END FILE/;
225 $_ = <IN>; # pass extra blank line
227 die "this line should be blank ($_). Did you edit the file?" if /\W/;
231 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
233 my ($name,$func,$title) = @{$_};
234 $newmain .= " void $func(void);\n"
237 $newmain .= " /* SGU: END FILE */\n\n";
238 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
242 # pass remaining prototypes, search declarations
244 $newmain .= $_ unless /SGU: END PROTOTYPES/;
245 last if /SGU: BEGIN SUITES DECLARATION/;
248 ### Done with prototypes. And now, the actual code
250 # search my prototype
252 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
255 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
257 last if /SGU: END FILE/;
259 $_ = <IN>; # pass extra blank line
261 die "this line should be blank ($_). Did you edit the file?" if /\W/;
265 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
266 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
268 my ($name,$func,$title) = @{$_};
269 $newmain .= " xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
272 $newmain .= " /* SGU: END FILE */\n\n";
273 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
281 close IN || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
283 # write it back to main
284 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
286 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
287 } # end if process_one($)