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];
135 /* SGU: BEGIN SUITES DECLARATION */
136 /* SGU: END SUITES DECLARATION */
138 xbt_init(&argc,argv);
140 /* Search for the tests to do */
142 for (i=1;i<argc;i++) {
143 if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
144 char *p=strchr(argv[i],'=')+1;
145 if (selection[0] == '\\0') {
146 strcpy(selection, p);
148 strcat(selection, \",\");
149 strcat(selection, p);
151 } else if (!strncmp(argv[i],\"--dump-only\",strlen(\"--dump-only\"))||
152 !strncmp(argv[i],\"--dump\", strlen(\"--dump\"))) {
153 xbt_test_dump(selection);
155 } else if (!strncmp(argv[i],\"--help\",strlen(\"--help\"))) {
157 "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
158 "--help: display this help\\n"
159 "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
160 "--tests=selection: Use argument to select which suites/units/tests to run\\n"
161 " --tests can be used more than once, and selection may be a comma\\n"
162 " separated list of directives.\\n\\n"
163 "Directives are of the form:\\n"
164 " [-]suitename[:unitname]\\n\\n"
165 "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
166 "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
167 "it applies on any unit.\\n\\n"
168 "By default, everything is enabled.\\n\\n"
169 "'all' as suite name apply to all suites.\\n\\n"
170 "Example 1: \\"-toto,+toto:tutu\\"\\n"
171 " disables the whole toto testsuite (any unit in it),\\n"
172 " then reenables the tutu unit of the toto test suite.\\n\\n"
173 "Example 2: \\"-all,+toto\\"\\n"
174 " Run nothing but the toto suite.\\n");
177 printf("testall: Unknown option: %s\\n",argv[i]);
181 /* Got all my tests to do */
183 res = xbt_test_run(selection);
188 print OUT $GENERATED;
189 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
192 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
194 my ($name,$func,$title) = @{$_};
195 print " unit $name: func=$func; title=$title\n";
198 #while (my $t = shift @tests) {
200 # add this suite to the main
202 open IN,"${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
206 # print "Look for proto: $_";
207 last if /SGU: BEGIN PROTOTYPES/;
210 # search my prototype
212 # print "Seek protos: $_";
213 last if (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
216 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
218 last if /SGU: END FILE/;
220 $_ = <IN>; # pass extra blank line
222 die "this line should be blank ($_). Did you edit the file?" if /\W/;
226 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
228 my ($name,$func,$title) = @{$_};
229 $newmain .= " void $func(void);\n"
232 $newmain .= " /* SGU: END FILE */\n\n";
233 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
237 # pass remaining prototypes, search declarations
239 $newmain .= $_ unless /SGU: END PROTOTYPES/;
240 last if /SGU: BEGIN SUITES DECLARATION/;
243 ### Done with prototypes. And now, the actual code
245 # search my prototype
247 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
250 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
252 last if /SGU: END FILE/;
254 $_ = <IN>; # pass extra blank line
256 die "this line should be blank ($_). Did you edit the file?" if /\W/;
260 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
261 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
263 my ($name,$func,$title) = @{$_};
264 $newmain .= " xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
267 $newmain .= " /* SGU: END FILE */\n\n";
268 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
276 close IN || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
278 # write it back to main
279 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
281 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
282 } # end if process_one($)