5 my $progname="sg_unit_extractor";
7 die "USAGE: $progname infile [outfile]\n"
8 if (scalar @ARGV == 0 || scalar @ARGV > 2);
9 my ($infile,$outfile) = @ARGV;
11 if (not defined($outfile)) {
13 $outfile =~ s/\.c$/_unit.c/;
14 $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
18 my ($unit_source,$suite_name,$suite_title)=("","","");
19 my (%tests); # to detect multiple definition
20 my (@tests); # actual content
22 open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
29 if (m/ifdef +SIMGRID_TEST/) {
34 if (m/endif.*SIMGRID_TEST/) {
39 if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*,(.*?)\);/) { #"
40 die "$progname: Multiple suites in the same file ($infile) are not supported yet\n"
41 if length($suite_name);
42 ($suite_name,$suite_title)=($1,$2);
46 if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"
47 die "$progname: multiply defined test in file $infile: $1\n"
48 if (defined($tests{$1}));
54 $unit_source .= $_ if $takeit;
56 close IN || die "$progname: cannot close input file '$infile': $!\n";
60 die "$progname: end of file reached in SIMGRID_TEST block.\n".
61 "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
63 "#endif /* SIMGRID_TEST */\n"
66 die "$progname: no suite defined in $infile\n"
67 unless (length($suite_name));
71 my ($GENERATED)=("/*******************************/\n".
72 "/* GENERATED FILE, DO NOT EDIT */\n".
73 "/*******************************/\n\n");
75 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
77 print OUT "#include \"xbt.h\"\n";
79 print OUT "# $beginline \"$infile\" \n";
80 print OUT "$unit_source";
82 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
84 # write the main skeleton if needed
85 if (! -e "simgrid_units_main.c") {
86 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
88 print OUT "#include \"xbt.h\"\n\n";
89 print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
90 print OUT "/* SGU: BEGIN PROTOTYPES */\n";
91 print OUT "/* SGU: END PROTOTYPES */\n\n";
93 print OUT "int main(int argc, char *argv[]) {\n";
94 print OUT " xbt_test_suite_t suite;\n\n";
95 print OUT " /* SGU: BEGIN SUITES DECLARATION */\n";
96 print OUT " /* SGU: END SUITES DECLARATION */\n\n";
97 print OUT " return xbt_test_run();\n";
100 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
103 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
105 my ($name,$func,$title) = @{$_};
106 print " test $name: func=$func; title=$title\n";
109 #while (my $t = shift @tests) {
111 # add this suite to the main
113 open IN,"simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
117 # print "Look for proto: $_";
118 last if /SGU: BEGIN PROTOTYPES/;
121 # search my prototype
123 # print "Seek protos: $_";
124 last if (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
127 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
129 last if /SGU: END FILE/;
131 $_ = <IN>; # pass extra blank line
133 die "this line should be blank ($_). Did you edit the file?" if /\W/;
137 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
139 my ($name,$func,$title) = @{$_};
140 $newmain .= " void $func(void);\n"
143 $newmain .= " /* SGU: END FILE */\n\n";
144 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
148 # pass remaining prototypes, search declarations
150 $newmain .= $_ unless /SGU: END PROTOTYPES/;
151 last if /SGU: BEGIN SUITES DECLARATION/;
154 ### Done with prototypes. And now, the actual code
156 # search my prototype
158 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
161 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
163 last if /SGU: END FILE/;
165 $_ = <IN>; # pass extra blank line
167 die "this line should be blank ($_). Did you edit the file?" if /\W/;
171 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
172 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
174 my ($name,$func,$title) = @{$_};
175 $newmain .= " xbt_test_suite_push(suite, $func, $title);\n";
178 $newmain .= " /* SGU: END FILE */\n\n";
179 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
187 close IN || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
189 # write it back to main
190 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
192 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";