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.
13 use Getopt::Long qw(GetOptions);
15 my $progname="sg_unit_extractor";
20 print "USAGE: $progname [--root=part/to/cut] [--outdir=where/to/generate/files] infile [infile+]\n\n";
21 print "This program is in charge of extracting the unit tests out of the SimGrid source code.\n";
22 print "See http://simgrid.gforge.inria.fr/doc/latest/inside_tests.html for more details.\n";
30 Getopt::Long::config('permute','no_getopt_compat', 'no_auto_abbrev');
32 'help|h' => sub {usage(0)},
34 'outdir=s' =>\$outdir) or usage(1);
36 usage(1) if (scalar @ARGV == 0);
38 map {process_one($_)} @ARGV;
44 $infile =~ s|src/|| unless (-e $infile);
47 $outfile =~ s/\.c$/_unit.c/;
48 $outfile =~ s/\.cpp$/_unit.cpp/;
49 $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
50 $outfile = "$outdir$outfile";
52 print "$progname: processing $infile (generating $outfile)...\n";
55 my ($unit_source,$suite_name,$suite_title)=("","","");
56 my (%tests); # to detect multiple definition
57 my (@tests); # actual content
59 open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
60 $infile =~ s|$root|| if defined($root);
67 if (m/ifdef +SIMGRID_TEST/) {
72 if (m/endif.*SIMGRID_TEST/) {
77 if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*, *(.*?)\);/) { #" {
78 die "$progname: Multiple suites in the same file ($infile) are not supported yet\n" if length($suite_name);
79 ($suite_name,$suite_title)=($1,$2);
80 die "$progname: Empty suite name in $infile" unless length($suite_name);
81 die "$progname: Empty suite title in $infile" unless length($suite_title);
83 } elsif (m/XBT_TEST_SUITE/) {
84 die "$progname: Parse error: This line seem to be a test suite declaration, but failed to parse it\n$_\n";
87 if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"{
88 die "$progname: multiply defined unit in file $infile: $1\n" if (defined($tests{$1}));
93 } elsif (m/XBT_TEST_UNIT/) {
94 die "$progname: Parse error: This line seem to be a test unit, but failed to parse it\n$_\n";
96 $unit_source .= $_ if $takeit;
98 close IN || die "$progname: cannot close input file '$infile': $!\n";
102 die "$progname: end of file reached in SIMGRID_TEST block.\n".
103 "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
105 "#endif /* SIMGRID_TEST */\n"
108 die "$progname: no suite defined in $infile\n" unless (length($suite_name));
112 my ($GENERATED)=("/*******************************/\n".
113 "/* GENERATED FILE, DO NOT EDIT */\n".
114 "/*******************************/\n\n");
116 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
117 print OUT $GENERATED;
118 print OUT "#include <stdio.h>\n";
119 print OUT "#include \"xbt.h\"\n";
120 print OUT $GENERATED;
121 print OUT "#line $beginline \"$infile\" \n";
122 print OUT "$unit_source";
123 print OUT $GENERATED;
124 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
126 # write the main skeleton if needed
127 if (! -e "${outdir}simgrid_units_main.c") {
128 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
129 print OUT $GENERATED;
130 print OUT "#include <stdio.h>\n\n";
131 print OUT "#include \"xbt.h\"\n\n";
132 print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
133 print OUT "/* SGU: BEGIN PROTOTYPES */\n";
134 print OUT "/* SGU: END PROTOTYPES */\n\n";
135 print OUT $GENERATED;
136 # print OUT "# 93 \"sg_unit_extractor.pl\"\n";
138 int main(int argc, char *argv[]) {
139 xbt_test_suite_t suite;
140 char selection[1024];
145 /* SGU: BEGIN SUITES DECLARATION */
146 /* SGU: END SUITES DECLARATION */
148 xbt_init(&argc,argv);
150 /* Search for the tests to do */
152 for (i=1;i<argc;i++) {
153 if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
154 char *p=strchr(argv[i],'=')+1;
155 if (selection[0] == '\\0') {
156 strcpy(selection, p);
158 strcat(selection, \",\");
159 strcat(selection, p);
161 } else if (!strcmp(argv[i], \"--verbose\")) {
163 } else if (!strcmp(argv[i], \"--dump-only\")||
164 !strcmp(argv[i], \"--dump\")) {
165 xbt_test_dump(selection);
167 } else if (!strcmp(argv[i], \"--help\")) {
169 "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
170 "--help: display this help\\n"
171 "--verbose: print the name for each running test\\n"
172 "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
173 "--tests=selection: Use argument to select which suites/units/tests to run\\n"
174 " --tests can be used more than once, and selection may be a comma\\n"
175 " separated list of directives.\\n\\n"
176 "Directives are of the form:\\n"
177 " [-]suitename[:unitname]\\n\\n"
178 "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
179 "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
180 "it applies on any unit.\\n\\n"
181 "By default, everything is enabled.\\n\\n"
182 "'all' as suite name apply to all suites.\\n\\n"
183 "Example 1: \\"-toto,+toto:tutu\\"\\n"
184 " disables the whole toto testsuite (any unit in it),\\n"
185 " then reenables the tutu unit of the toto test suite.\\n\\n"
186 "Example 2: \\"-all,+toto\\"\\n"
187 " Run nothing but the toto suite.\\n");
190 printf("testall: Unknown option: %s\\n",argv[i]);
194 /* Got all my tests to do */
196 res = xbt_test_run(selection, verbosity);
201 print OUT $GENERATED;
202 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
205 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
207 my ($name,$func,$title) = @{$_};
208 print " unit $name: func=$func; title=$title\n";
211 #while (my $t = shift @tests) {
213 # add this suite to the main
215 open IN,"${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
219 # print "Look for proto: $_";
220 last if /SGU: BEGIN PROTOTYPES/;
223 # search my prototype
225 # print "Seek protos: $_";
226 last if (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
229 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
231 last if /SGU: END FILE/;
233 $_ = <IN>; # pass extra blank line
235 die "this line should be blank ($_). Did you edit the file?" if /\W/;
239 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
241 my ($name,$func,$title) = @{$_};
242 $newmain .= " void $func(void);\n"
245 $newmain .= " /* SGU: END FILE */\n\n";
246 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
250 # pass remaining prototypes, search declarations
252 $newmain .= $_ unless /SGU: END PROTOTYPES/;
253 last if /SGU: BEGIN SUITES DECLARATION/;
256 ### Done with prototypes. And now, the actual code
258 # search my prototype
260 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
263 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
265 last if /SGU: END FILE/;
267 $_ = <IN>; # pass extra blank line
269 die "this line should be blank ($_). Did you edit the file?" if /\W/;
273 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
274 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
276 my ($name,$func,$title) = @{$_};
277 $newmain .= " xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
280 $newmain .= " /* SGU: END FILE */\n\n";
281 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
289 close IN || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
291 # write it back to main
292 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
294 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
295 } # end if process_one($)