6 open SELF, "< $0" or die "Cannot open the lock file";
7 if (!flock SELF, LOCK_EX | LOCK_NB) {
8 print STDERR "sg_unit_extractor already running. Cancelling...\n";
12 my $progname="sg_unit_extractor";
14 die "USAGE: $progname infile [infile+]\n"
15 if (scalar @ARGV == 0);
17 map {process_one($_)} @ARGV;
25 $outfile =~ s/\.c$/_unit.c/;
26 $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
30 my ($unit_source,$suite_name,$suite_title)=("","","");
31 my (%tests); # to detect multiple definition
32 my (@tests); # actual content
34 open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
41 if (m/ifdef +SIMGRID_TEST/) {
46 if (m/endif.*SIMGRID_TEST/) {
51 if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*, *(.*?)\);/) { #" {
52 die "$progname: Multiple suites in the same file ($infile) are not supported yet\n" if length($suite_name);
53 ($suite_name,$suite_title)=($1,$2);
54 die "$progname: Empty suite name in $infile" unless length($suite_name);
55 die "$progname: Empty suite title in $infile" unless length($suite_title);
57 } elsif (m/XBT_TEST_SUITE/) {
58 die "$progname: Parse error: This line seem to be a test suite declaration, but failed to parse it\n$_\n";
61 if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"{
62 die "$progname: multiply defined unit in file $infile: $1\n" if (defined($tests{$1}));
67 } elsif (m/XBT_TEST_UNIT/) {
68 die "$progname: Parse error: This line seem to be a test unit, but failed to parse it\n$_\n";
70 $unit_source .= $_ if $takeit;
72 close IN || die "$progname: cannot close input file '$infile': $!\n";
76 die "$progname: end of file reached in SIMGRID_TEST block.\n".
77 "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
79 "#endif /* SIMGRID_TEST */\n"
82 die "$progname: no suite defined in $infile\n" unless (length($suite_name));
86 my ($GENERATED)=("/*******************************/\n".
87 "/* GENERATED FILE, DO NOT EDIT */\n".
88 "/*******************************/\n\n");
90 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
92 print OUT "#include <stdio.h>\n";
93 print OUT "#include \"xbt.h\"\n";
95 print OUT "#line $beginline \"$infile\" \n";
96 print OUT "$unit_source";
98 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
100 # write the main skeleton if needed
101 if (! -e "simgrid_units_main.c") {
102 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
103 print OUT $GENERATED;
104 print OUT "#include <stdio.h>\n\n";
105 print OUT "#include \"xbt.h\"\n\n";
106 print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
107 print OUT "/* SGU: BEGIN PROTOTYPES */\n";
108 print OUT "/* SGU: END PROTOTYPES */\n\n";
109 print OUT $GENERATED;
110 # print OUT "# 93 \"sg_unit_extractor.pl\"\n";
112 int main(int argc, char *argv[]) {
113 xbt_test_suite_t suite;
114 char selection[1024];
117 /* SGU: BEGIN SUITES DECLARATION */
118 /* SGU: END SUITES DECLARATION */
120 xbt_init(&argc,argv);
122 /* Search for the tests to do */
124 for (i=1;i<argc;i++) {
125 if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
126 char *p=strchr(argv[i],'=')+1;
127 if (selection[0] == '\\0') {
128 strcpy(selection, p);
130 strcat(selection, \",\");
131 strcat(selection, p);
133 } else if (!strncmp(argv[i],\"--dump-only\",strlen(\"--dump-only\"))||
134 !strncmp(argv[i],\"--dump\", strlen(\"--dump\"))) {
135 xbt_test_dump(selection);
137 } else if (!strncmp(argv[i],\"--help\",strlen(\"--help\"))) {
139 "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
140 "--help: display this help\\n"
141 "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
142 "--tests=selection: Use argument to select which suites/units/tests to run\\n"
143 " --tests can be used more than once, and selection may be a comma\\n"
144 " separated list of directives.\\n\\n"
145 "Directives are of the form:\\n"
146 " [-]suitename[:unitname]\\n\\n"
147 "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
148 "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
149 "it applies on any unit.\\n\\n"
150 "By default, everything is enabled.\\n\\n"
151 "'all' as suite name apply to all suites.\\n\\n"
152 "Example 1: \\"-toto,+toto:tutu\\"\\n"
153 " disables the whole toto testsuite (any unit in it),\\n"
154 " then reenables the tutu unit of the toto test suite.\\n\\n"
155 "Example 2: \\"-all,+toto\\"\\n"
156 " Run nothing but the toto suite.\\n");
159 printf("testall: Unknown option: %s\\n",argv[i]);
163 /* Got all my tests to do */
165 res = xbt_test_run(selection);
170 print OUT $GENERATED;
171 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
174 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
176 my ($name,$func,$title) = @{$_};
177 print " unit $name: func=$func; title=$title\n";
180 #while (my $t = shift @tests) {
182 # add this suite to the main
184 open IN,"simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
188 # print "Look for proto: $_";
189 last if /SGU: BEGIN PROTOTYPES/;
192 # search my prototype
194 # print "Seek protos: $_";
195 last if (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
198 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
200 last if /SGU: END FILE/;
202 $_ = <IN>; # pass extra blank line
204 die "this line should be blank ($_). Did you edit the file?" if /\W/;
208 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
210 my ($name,$func,$title) = @{$_};
211 $newmain .= " void $func(void);\n"
214 $newmain .= " /* SGU: END FILE */\n\n";
215 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
219 # pass remaining prototypes, search declarations
221 $newmain .= $_ unless /SGU: END PROTOTYPES/;
222 last if /SGU: BEGIN SUITES DECLARATION/;
225 ### Done with prototypes. And now, the actual code
227 # search my prototype
229 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
232 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
234 last if /SGU: END FILE/;
236 $_ = <IN>; # pass extra blank line
238 die "this line should be blank ($_). Did you edit the file?" if /\W/;
242 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
243 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
245 my ($name,$func,$title) = @{$_};
246 $newmain .= " xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
249 $newmain .= " /* SGU: END FILE */\n\n";
250 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
258 close IN || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
260 # write it back to main
261 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
263 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
264 } # end if process_one($)