7 use Getopt::Long qw(GetOptions);
9 open SELF, "< $0" or die "Cannot open the lock file";
10 if (!flock SELF, LOCK_EX | LOCK_NB) {
11 print STDERR "sg_unit_extractor already running. Cancelling...\n";
15 my $progname="sg_unit_extractor";
20 print "USAGE: $progname [--root=part/to/cut] [--path=where/to/search NOT WORKING] [--outdir=where/to/generate/files] infile [infile+]\n";
29 Getopt::Long::config('permute','no_getopt_compat', 'no_auto_abbrev');
31 'help|h' => sub {usage(0)},
34 'outdir=s' =>\$outdir) or usage(1);
36 usage(1) if (scalar @ARGV == 0);
38 map {process_one($_)} @ARGV;
44 $infile =~ s|src/|| unless (-e $infile);
47 $outfile =~ s/\.c$/_unit.c/;
48 $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
49 $outfile = "$outdir$outfile";
51 print "$progname: processing $infile (generating $outfile)...\n";
54 my ($unit_source,$suite_name,$suite_title)=("","","");
55 my (%tests); # to detect multiple definition
56 my (@tests); # actual content
58 open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
59 $infile =~ s|$root|| if defined($root);
66 if (m/ifdef +SIMGRID_TEST/) {
71 if (m/endif.*SIMGRID_TEST/) {
76 if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*, *(.*?)\);/) { #" {
77 die "$progname: Multiple suites in the same file ($infile) are not supported yet\n" if length($suite_name);
78 ($suite_name,$suite_title)=($1,$2);
79 die "$progname: Empty suite name in $infile" unless length($suite_name);
80 die "$progname: Empty suite title in $infile" unless length($suite_title);
82 } elsif (m/XBT_TEST_SUITE/) {
83 die "$progname: Parse error: This line seem to be a test suite declaration, but failed to parse it\n$_\n";
86 if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"{
87 die "$progname: multiply defined unit in file $infile: $1\n" if (defined($tests{$1}));
92 } elsif (m/XBT_TEST_UNIT/) {
93 die "$progname: Parse error: This line seem to be a test unit, but failed to parse it\n$_\n";
95 $unit_source .= $_ if $takeit;
97 close IN || die "$progname: cannot close input file '$infile': $!\n";
101 die "$progname: end of file reached in SIMGRID_TEST block.\n".
102 "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
104 "#endif /* SIMGRID_TEST */\n"
107 die "$progname: no suite defined in $infile\n" unless (length($suite_name));
111 my ($GENERATED)=("/*******************************/\n".
112 "/* GENERATED FILE, DO NOT EDIT */\n".
113 "/*******************************/\n\n");
115 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
116 print OUT $GENERATED;
117 print OUT "#include <stdio.h>\n";
118 print OUT "#include \"xbt.h\"\n";
119 print OUT $GENERATED;
120 print OUT "#line $beginline \"$infile\" \n";
121 print OUT "$unit_source";
122 print OUT $GENERATED;
123 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
125 # write the main skeleton if needed
126 if (! -e "${outdir}simgrid_units_main.c") {
127 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
128 print OUT $GENERATED;
129 print OUT "#include <stdio.h>\n\n";
130 print OUT "#include \"xbt.h\"\n\n";
131 print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
132 print OUT "/* SGU: BEGIN PROTOTYPES */\n";
133 print OUT "/* SGU: END PROTOTYPES */\n\n";
134 print OUT $GENERATED;
135 # print OUT "# 93 \"sg_unit_extractor.pl\"\n";
137 int main(int argc, char *argv[]) {
138 xbt_test_suite_t suite;
139 char selection[1024];
142 /* SGU: BEGIN SUITES DECLARATION */
143 /* SGU: END SUITES DECLARATION */
145 xbt_init(&argc,argv);
147 /* Search for the tests to do */
149 for (i=1;i<argc;i++) {
150 if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
151 char *p=strchr(argv[i],'=')+1;
152 if (selection[0] == '\\0') {
153 strcpy(selection, p);
155 strcat(selection, \",\");
156 strcat(selection, p);
158 } else if (!strncmp(argv[i],\"--dump-only\",strlen(\"--dump-only\"))||
159 !strncmp(argv[i],\"--dump\", strlen(\"--dump\"))) {
160 xbt_test_dump(selection);
162 } else if (!strncmp(argv[i],\"--help\",strlen(\"--help\"))) {
164 "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
165 "--help: display this help\\n"
166 "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
167 "--tests=selection: Use argument to select which suites/units/tests to run\\n"
168 " --tests can be used more than once, and selection may be a comma\\n"
169 " separated list of directives.\\n\\n"
170 "Directives are of the form:\\n"
171 " [-]suitename[:unitname]\\n\\n"
172 "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
173 "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
174 "it applies on any unit.\\n\\n"
175 "By default, everything is enabled.\\n\\n"
176 "'all' as suite name apply to all suites.\\n\\n"
177 "Example 1: \\"-toto,+toto:tutu\\"\\n"
178 " disables the whole toto testsuite (any unit in it),\\n"
179 " then reenables the tutu unit of the toto test suite.\\n\\n"
180 "Example 2: \\"-all,+toto\\"\\n"
181 " Run nothing but the toto suite.\\n");
184 printf("testall: Unknown option: %s\\n",argv[i]);
188 /* Got all my tests to do */
190 res = xbt_test_run(selection);
195 print OUT $GENERATED;
196 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
199 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
201 my ($name,$func,$title) = @{$_};
202 print " unit $name: func=$func; title=$title\n";
205 #while (my $t = shift @tests) {
207 # add this suite to the main
209 open IN,"${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
213 # print "Look for proto: $_";
214 last if /SGU: BEGIN PROTOTYPES/;
217 # search my prototype
219 # print "Seek protos: $_";
220 last if (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
223 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
225 last if /SGU: END FILE/;
227 $_ = <IN>; # pass extra blank line
229 die "this line should be blank ($_). Did you edit the file?" if /\W/;
233 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
235 my ($name,$func,$title) = @{$_};
236 $newmain .= " void $func(void);\n"
239 $newmain .= " /* SGU: END FILE */\n\n";
240 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
244 # pass remaining prototypes, search declarations
246 $newmain .= $_ unless /SGU: END PROTOTYPES/;
247 last if /SGU: BEGIN SUITES DECLARATION/;
250 ### Done with prototypes. And now, the actual code
252 # search my prototype
254 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
257 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
259 last if /SGU: END FILE/;
261 $_ = <IN>; # pass extra blank line
263 die "this line should be blank ($_). Did you edit the file?" if /\W/;
267 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
268 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
270 my ($name,$func,$title) = @{$_};
271 $newmain .= " xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
274 $newmain .= " /* SGU: END FILE */\n\n";
275 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
283 close IN || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
285 # write it back to main
286 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
288 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
289 } # end if process_one($)