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);
57 if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"{
58 die "$progname: multiply defined unit in file $infile: $1\n" if (defined($tests{$1}));
64 $unit_source .= $_ if $takeit;
66 close IN || die "$progname: cannot close input file '$infile': $!\n";
70 die "$progname: end of file reached in SIMGRID_TEST block.\n".
71 "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
73 "#endif /* SIMGRID_TEST */\n"
76 die "$progname: no suite defined in $infile\n" unless (length($suite_name));
80 my ($GENERATED)=("/*******************************/\n".
81 "/* GENERATED FILE, DO NOT EDIT */\n".
82 "/*******************************/\n\n");
84 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
86 print OUT "#include <stdio.h>\n";
87 print OUT "#include \"xbt.h\"\n";
89 print OUT "# $beginline \"$infile\" \n";
90 print OUT "$unit_source";
92 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
94 # write the main skeleton if needed
95 if (! -e "simgrid_units_main.c") {
96 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
98 print OUT "#include <stdio.h>\n\n";
99 print OUT "#include \"xbt.h\"\n\n";
100 print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
101 print OUT "/* SGU: BEGIN PROTOTYPES */\n";
102 print OUT "/* SGU: END PROTOTYPES */\n\n";
103 print OUT $GENERATED;
104 # print OUT "# 93 \"sg_unit_extractor.pl\"\n";
106 int main(int argc, char *argv[]) {
107 xbt_test_suite_t suite;
108 char selection[1024];
111 /* SGU: BEGIN SUITES DECLARATION */
112 /* SGU: END SUITES DECLARATION */
114 xbt_init(&argc,argv);
116 /* Search for the tests to do */
118 for (i=1;i<argc;i++) {
119 if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
120 char *p=strchr(argv[i],'=')+1;
121 if (selection[0] == '\\0') {
122 strcpy(selection, p);
124 strcat(selection, \",\");
125 strcat(selection, p);
127 } else if (!strncmp(argv[i],\"--dump-only\",strlen(\"--dump-only\"))||
128 !strncmp(argv[i],\"--dump\", strlen(\"--dump\"))) {
129 xbt_test_dump(selection);
131 } else if (!strncmp(argv[i],\"--help\",strlen(\"--help\"))) {
133 "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
134 "--help: display this help\\n"
135 "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
136 "--tests=selection: Use argument to select which suites/units/tests to run\\n"
137 " --tests can be used more than once, and selection may be a comma\\n"
138 " separated list of directives.\\n\\n"
139 "Directives are of the form:\\n"
140 " [-]suitename[:unitname]\\n\\n"
141 "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
142 "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
143 "it applies on any unit.\\n\\n"
144 "By default, everything is enabled.\\n\\n"
145 "'all' as suite name apply to all suites.\\n\\n"
146 "Example 1: \\"-toto,+toto:tutu\\"\\n"
147 " disables the whole toto testsuite (any unit in it),\\n"
148 " then reenables the tutu unit of the toto test suite.\\n\\n"
149 "Example 2: \\"-all,+toto\\"\\n"
150 " Run nothing but the toto suite.\\n");
153 printf("testall: Unknown option: %s\\n",argv[i]);
157 /* Got all my tests to do */
159 res = xbt_test_run(selection);
165 print OUT $GENERATED;
166 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
169 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
171 my ($name,$func,$title) = @{$_};
172 print " unit $name: func=$func; title=$title\n";
175 #while (my $t = shift @tests) {
177 # add this suite to the main
179 open IN,"simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
183 # print "Look for proto: $_";
184 last if /SGU: BEGIN PROTOTYPES/;
187 # search my prototype
189 # print "Seek protos: $_";
190 last if (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
193 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
195 last if /SGU: END FILE/;
197 $_ = <IN>; # pass extra blank line
199 die "this line should be blank ($_). Did you edit the file?" if /\W/;
203 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
205 my ($name,$func,$title) = @{$_};
206 $newmain .= " void $func(void);\n"
209 $newmain .= " /* SGU: END FILE */\n\n";
210 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
214 # pass remaining prototypes, search declarations
216 $newmain .= $_ unless /SGU: END PROTOTYPES/;
217 last if /SGU: BEGIN SUITES DECLARATION/;
220 ### Done with prototypes. And now, the actual code
222 # search my prototype
224 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
227 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
229 last if /SGU: END FILE/;
231 $_ = <IN>; # pass extra blank line
233 die "this line should be blank ($_). Did you edit the file?" if /\W/;
237 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
238 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
240 my ($name,$func,$title) = @{$_};
241 $newmain .= " xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
244 $newmain .= " /* SGU: END FILE */\n\n";
245 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
253 close IN || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
255 # write it back to main
256 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
258 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
259 } # end if process_one($)