3 # Copyright (c) 2005-2012, 2014. The SimGrid Team. All rights reserved.
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the license (GNU LGPL) which comes with this package.
9 use Getopt::Long qw(GetOptions);
11 my $progname="sg_unit_extractor";
16 print "USAGE: $progname [--root=part/to/cut] [--outdir=where/to/generate/files] infile [infile+]\n\n";
17 print "This program is in charge of extracting the unit tests out of the SimGrid source code.\n";
18 print "See http://simgrid.gforge.inria.fr/doc/latest/inside_tests.html for more details.\n";
26 Getopt::Long::config('permute','no_getopt_compat', 'no_auto_abbrev');
28 'help|h' => sub {usage(0)},
30 'outdir=s' =>\$outdir) or usage(1);
32 usage(1) if (scalar @ARGV == 0);
34 map {process_one($_)} @ARGV;
40 $infile =~ s|src/|| unless (-e $infile);
43 $outfile =~ s/\.c$/_unit.c/;
44 $outfile =~ s/\.cpp$/_unit.cpp/;
45 $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
46 $outfile = "$outdir$outfile";
48 print "$progname: processing $infile (generating $outfile)...\n";
51 my ($unit_source,$suite_name,$suite_title)=("","","");
52 my (%tests); # to detect multiple definition
53 my (@tests); # actual content
55 open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
56 $infile =~ s|$root|| if defined($root);
63 if (m/ifdef +SIMGRID_TEST/) {
68 if (m/endif.*SIMGRID_TEST/) {
73 if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*, *(.*?)\);/) { #" {
74 die "$progname: Multiple suites in the same file ($infile) are not supported yet\n" if length($suite_name);
75 ($suite_name,$suite_title)=($1,$2);
76 die "$progname: Empty suite name in $infile" unless length($suite_name);
77 die "$progname: Empty suite title in $infile" unless length($suite_title);
79 } elsif (m/XBT_TEST_SUITE/) {
80 die "$progname: Parse error: This line seem to be a test suite declaration, but failed to parse it\n$_\n";
83 if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"{
84 die "$progname: multiply defined unit in file $infile: $1\n" if (defined($tests{$1}));
89 } elsif (m/XBT_TEST_UNIT/) {
90 die "$progname: Parse error: This line seem to be a test unit, but failed to parse it\n$_\n";
92 $unit_source .= $_ if $takeit;
94 close IN || die "$progname: cannot close input file '$infile': $!\n";
98 die "$progname: end of file reached in SIMGRID_TEST block.\n".
99 "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
101 "#endif /* SIMGRID_TEST */\n"
104 die "$progname: no suite defined in $infile\n" unless (length($suite_name));
108 my ($GENERATED)=("/*******************************/\n".
109 "/* GENERATED FILE, DO NOT EDIT */\n".
110 "/*******************************/\n\n");
112 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
113 print OUT $GENERATED;
114 print OUT "#include <stdio.h>\n";
115 print OUT "#include \"xbt.h\"\n";
116 print OUT $GENERATED;
117 print OUT "#line $beginline \"$infile\" \n";
118 print OUT "$unit_source";
119 print OUT $GENERATED;
120 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
122 # write the main skeleton if needed
123 if (! -e "${outdir}simgrid_units_main.c") {
124 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
125 print OUT $GENERATED;
126 print OUT "#include <stdio.h>\n\n";
127 print OUT "#include \"xbt.h\"\n\n";
128 print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
129 print OUT "/* SGU: BEGIN PROTOTYPES */\n";
130 print OUT "/* SGU: END PROTOTYPES */\n\n";
131 print OUT $GENERATED;
132 # print OUT "# 93 \"sg_unit_extractor.pl\"\n";
134 int main(int argc, char *argv[]) {
135 xbt_test_suite_t suite;
136 char selection[1024];
141 /* SGU: BEGIN SUITES DECLARATION */
142 /* SGU: END SUITES DECLARATION */
144 xbt_init(&argc,argv);
146 /* Search for the tests to do */
148 for (i=1;i<argc;i++) {
149 if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
150 char *p=strchr(argv[i],'=')+1;
151 if (selection[0] == '\\0') {
152 strncpy(selection,p,1024);
154 strncat(selection, \",\",1);
155 strncat(selection, p, 1023);
157 } else if (!strcmp(argv[i], \"--verbose\")) {
159 } else if (!strcmp(argv[i], \"--dump-only\")||
160 !strcmp(argv[i], \"--dump\")) {
161 xbt_test_dump(selection);
163 } else if (!strcmp(argv[i], \"--help\")) {
165 "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
166 "--help: display this help\\n"
167 "--verbose: print the name for each running test\\n"
168 "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
169 "--tests=selection: Use argument to select which suites/units/tests to run\\n"
170 " --tests can be used more than once, and selection may be a comma\\n"
171 " separated list of directives.\\n\\n"
172 "Directives are of the form:\\n"
173 " [-]suitename[:unitname]\\n\\n"
174 "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
175 "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
176 "it applies on any unit.\\n\\n"
177 "By default, everything is enabled.\\n\\n"
178 "'all' as suite name apply to all suites.\\n\\n"
179 "Example 1: \\"-toto,+toto:tutu\\"\\n"
180 " disables the whole toto testsuite (any unit in it),\\n"
181 " then reenables the tutu unit of the toto test suite.\\n\\n"
182 "Example 2: \\"-all,+toto\\"\\n"
183 " Run nothing but the toto suite.\\n");
186 printf("testall: Unknown option: %s\\n",argv[i]);
190 /* Got all my tests to do */
192 res = xbt_test_run(selection, verbosity);
197 print OUT $GENERATED;
198 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
201 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
203 my ($name,$func,$title) = @{$_};
204 print " unit $name: func=$func; title=$title\n";
207 #while (my $t = shift @tests) {
209 # add this suite to the main
211 open IN,"${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
215 # print "Look for proto: $_";
216 last if /SGU: BEGIN PROTOTYPES/;
219 # search my prototype
221 # print "Seek protos: $_";
222 last if (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
225 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
227 last if /SGU: END FILE/;
229 $_ = <IN>; # pass extra blank line
231 die "this line should be blank ($_). Did you edit the file?" if /\W/;
235 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
237 my ($name,$func,$title) = @{$_};
238 $newmain .= " void $func(void);\n"
241 $newmain .= " /* SGU: END FILE */\n\n";
242 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
246 # pass remaining prototypes, search declarations
248 $newmain .= $_ unless /SGU: END PROTOTYPES/;
249 last if /SGU: BEGIN SUITES DECLARATION/;
252 ### Done with prototypes. And now, the actual code
254 # search my prototype
256 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
259 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
261 last if /SGU: END FILE/;
263 $_ = <IN>; # pass extra blank line
265 die "this line should be blank ($_). Did you edit the file?" if /\W/;
269 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
270 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
272 my ($name,$func,$title) = @{$_};
273 $newmain .= " xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
276 $newmain .= " /* SGU: END FILE */\n\n";
277 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
285 close IN || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
287 # write it back to main
288 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
290 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
291 } # end if process_one($)