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";
26 if (m/ifdef +SIMGRID_TEST/) {
30 if (m/endif.*SIMGRID_TEST/) {
35 if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*,(.*?)\);/) { #"
36 die "$progname: Multiple suites in the same file ($infile) are not supported yet\n"
37 if length($suite_name);
38 ($suite_name,$suite_title)=($1,$2);
42 if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"
43 die "$progname: multiply defined test in file $infile: $1\n"
44 if (defined($tests{$1}));
50 $unit_source .= $_ if $takeit;
52 close IN || die "$progname: cannot close input file '$infile': $!\n";
56 die "$progname: end of file reached in SIMGRID_TEST block.\n".
57 "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
59 "#endif /* SIMGRID_TEST */\n"
62 die "$progname: no suite defined in $infile\n"
63 unless (length($suite_name));
67 my ($GENERATED)=("/*******************************/\n".
68 "/* GENERATED FILE, DO NOT EDIT */\n".
69 "/*******************************/\n\n");
71 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
73 print OUT "#include \"xbt.h\"\n";
75 print OUT "$unit_source";
77 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
79 # write the main skeleton if needed
80 if (! -e "simgrid_units_main.c") {
81 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
83 print OUT "#include \"xbt.h\"\n\n";
84 print OUT "/* SGU: BEGIN PROTOTYPES */\n";
85 print OUT "/* SGU: END PROTOTYPES */\n\n";
87 print OUT "int main(int argc, char *argv[]) {\n";
88 print OUT " xbt_test_suite_t suite;\n\n";
89 print OUT " /* SGU: BEGIN SUITES DECLARATION */\n";
90 print OUT " /* SGU: END SUITES DECLARATION */\n\n";
91 print OUT " return xbt_test_run();\n";
94 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
97 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
99 my ($name,$func,$title) = @{$_};
100 print " test $name: func=$func; title=$title\n";
103 #while (my $t = shift @tests) {
105 # add this suite to the main
107 open IN,"simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
111 # print "Look for proto: $_";
112 last if /SGU: BEGIN PROTOTYPES/;
115 # search my prototype
117 # print "Seek protos: $_";
118 last if (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
121 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
123 last if /SGU: END FILE/;
125 $_ = <IN>; # pass extra blank line
127 die "this line should be blank ($_). Did you edit the file?" if /\W/;
131 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
133 my ($name,$func,$title) = @{$_};
134 $newmain .= " void $func(xbt_test_unit_t _unit);\n"
137 $newmain .= " /* SGU: END FILE */\n\n";
138 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
142 # pass remaining prototypes, search declarations
144 $newmain .= $_ unless /SGU: END PROTOTYPES/;
145 last if /SGU: BEGIN SUITES DECLARATION/;
148 ### Done with prototypes. And now, the actual code
150 # search my prototype
152 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
155 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
157 last if /SGU: END FILE/;
159 $_ = <IN>; # pass extra blank line
161 die "this line should be blank ($_). Did you edit the file?" if /\W/;
165 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
166 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
168 my ($name,$func,$title) = @{$_};
169 $newmain .= " xbt_test_suite_push(suite, $func, $title);\n";
172 $newmain .= " /* SGU: END FILE */\n\n";
173 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
181 close IN || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";
183 # write it back to main
184 open OUT,">simgrid_units_main.c" || die "$progname: Cannot open main file 'simgrid_units_main.c': $!\n";
186 close OUT || die "$progname: Cannot close main file 'simgrid_units_main.c': $!\n";