5 my $progname="sg_unit_extractor";
7 die "USAGE: $progname infile [infile+]\n"
8 if (scalar @ARGV == 0);
10 map {process_one($_)} @ARGV;
18 $outfile =~ s/\.c$/_unit.c/;
19 $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
23 my ($unit_source,$suite_name,$suite_title)=("","","");
24 my (%tests); # to detect multiple definition
25 my (@tests); # actual content
27 open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
34 if (m/ifdef +SIMGRID_TEST/) {
39 if (m/endif.*SIMGRID_TEST/) {
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);
50 if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"{
51 die "$progname: multiply defined unit in file $infile: $1\n" if (defined($tests{$1}));
57 $unit_source .= $_ if $takeit;
59 close IN || die "$progname: cannot close input file '$infile': $!\n";
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".
66 "#endif /* SIMGRID_TEST */\n"
69 die "$progname: no suite defined in $infile\n" unless (length($suite_name));
73 my ($GENERATED)=("/*******************************/\n".
74 "/* GENERATED FILE, DO NOT EDIT */\n".
75 "/*******************************/\n\n");
77 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
79 print OUT "#include <stdio.h>\n";
80 print OUT "#include \"xbt.h\"\n";
82 print OUT "# $beginline \"$infile\" \n";
83 print OUT "$unit_source";
85 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
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";
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";
97 # print OUT "# 93 \"sg_unit_extractor.pl\"\n";
99 int main(int argc, char *argv[]) {
100 xbt_test_suite_t suite;
101 char selection[1024];
104 /* SGU: BEGIN SUITES DECLARATION */
105 /* SGU: END SUITES DECLARATION */
107 xbt_init(&argc,argv);
109 /* Search for the tests to do */
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);
117 strcat(selection, \",\");
118 strcat(selection, p);
120 } else if (!strncmp(argv[i],\"--dump-only\",strlen(\"--dump-only\"))||
121 !strncmp(argv[i],\"--dump\", strlen(\"--dump\"))) {
122 xbt_test_dump(selection);
124 } else if (!strncmp(argv[i],\"--help\",strlen(\"--help\"))) {
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");
146 printf("testall: Unknown option: %s\\n",argv[i]);
150 /* Got all my tests to do */
152 res = xbt_test_run(selection);
158 print OUT $GENERATED;
159 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
162 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
164 my ($name,$func,$title) = @{$_};
165 print " unit $name: func=$func; title=$title\n";
168 #while (my $t = shift @tests) {
170 # add this suite to the main
172 open IN,"simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
176 # print "Look for proto: $_";
177 last if /SGU: BEGIN PROTOTYPES/;
180 # search my prototype
182 # print "Seek protos: $_";
183 last if (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
186 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
188 last if /SGU: END FILE/;
190 $_ = <IN>; # pass extra blank line
192 die "this line should be blank ($_). Did you edit the file?" if /\W/;
196 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
198 my ($name,$func,$title) = @{$_};
199 $newmain .= " void $func(void);\n"
202 $newmain .= " /* SGU: END FILE */\n\n";
203 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
207 # pass remaining prototypes, search declarations
209 $newmain .= $_ unless /SGU: END PROTOTYPES/;
210 last if /SGU: BEGIN SUITES DECLARATION/;
213 ### Done with prototypes. And now, the actual code
215 # search my prototype
217 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
220 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
222 last if /SGU: END FILE/;
224 $_ = <IN>; # pass extra blank line
226 die "this line should be blank ($_). Did you edit the file?" if /\W/;
230 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
231 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
233 my ($name,$func,$title) = @{$_};
234 $newmain .= " xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
237 $newmain .= " /* SGU: END FILE */\n\n";
238 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
246 close IN || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
248 # write it back to main
249 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
251 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
252 } # end if process_one($)