3 # Copyright (c) 2005-2012, 2014. The SimGrid Team.
6 # This program is free software; you can redistribute it and/or modify it
7 # under the terms of the license (GNU LGPL) which comes with this package.
12 use Getopt::Long qw(GetOptions);
14 my $progname="sg_unit_extractor";
19 print "USAGE: $progname [--root=part/to/cut] [--path=where/to/search NOT WORKING] [--outdir=where/to/generate/files] infile [infile+]\n";
28 Getopt::Long::config('permute','no_getopt_compat', 'no_auto_abbrev');
30 'help|h' => sub {usage(0)},
33 'outdir=s' =>\$outdir) or usage(1);
35 usage(1) if (scalar @ARGV == 0);
37 map {process_one($_)} @ARGV;
43 $infile =~ s|src/|| unless (-e $infile);
46 $outfile =~ s/\.c$/_unit.c/;
47 $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
48 $outfile = "$outdir$outfile";
50 print "$progname: processing $infile (generating $outfile)...\n";
53 my ($unit_source,$suite_name,$suite_title)=("","","");
54 my (%tests); # to detect multiple definition
55 my (@tests); # actual content
57 open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
58 $infile =~ s|$root|| if defined($root);
65 if (m/ifdef +SIMGRID_TEST/) {
70 if (m/endif.*SIMGRID_TEST/) {
75 if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*, *(.*?)\);/) { #" {
76 die "$progname: Multiple suites in the same file ($infile) are not supported yet\n" if length($suite_name);
77 ($suite_name,$suite_title)=($1,$2);
78 die "$progname: Empty suite name in $infile" unless length($suite_name);
79 die "$progname: Empty suite title in $infile" unless length($suite_title);
81 } elsif (m/XBT_TEST_SUITE/) {
82 die "$progname: Parse error: This line seem to be a test suite declaration, but failed to parse it\n$_\n";
85 if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"{
86 die "$progname: multiply defined unit in file $infile: $1\n" if (defined($tests{$1}));
91 } elsif (m/XBT_TEST_UNIT/) {
92 die "$progname: Parse error: This line seem to be a test unit, but failed to parse it\n$_\n";
94 $unit_source .= $_ if $takeit;
96 close IN || die "$progname: cannot close input file '$infile': $!\n";
100 die "$progname: end of file reached in SIMGRID_TEST block.\n".
101 "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
103 "#endif /* SIMGRID_TEST */\n"
106 die "$progname: no suite defined in $infile\n" unless (length($suite_name));
110 my ($GENERATED)=("/*******************************/\n".
111 "/* GENERATED FILE, DO NOT EDIT */\n".
112 "/*******************************/\n\n");
114 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
115 print OUT $GENERATED;
116 print OUT "#include <stdio.h>\n";
117 print OUT "#include \"xbt.h\"\n";
118 print OUT $GENERATED;
119 print OUT "#line $beginline \"$infile\" \n";
120 print OUT "$unit_source";
121 print OUT $GENERATED;
122 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
124 # write the main skeleton if needed
125 if (! -e "${outdir}simgrid_units_main.c") {
126 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
127 print OUT $GENERATED;
128 print OUT "#include <stdio.h>\n\n";
129 print OUT "#include \"xbt.h\"\n\n";
130 print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
131 print OUT "/* SGU: BEGIN PROTOTYPES */\n";
132 print OUT "/* SGU: END PROTOTYPES */\n\n";
133 print OUT $GENERATED;
134 # print OUT "# 93 \"sg_unit_extractor.pl\"\n";
136 int main(int argc, char *argv[]) {
137 xbt_test_suite_t suite;
138 char selection[1024];
143 /* SGU: BEGIN SUITES DECLARATION */
144 /* SGU: END SUITES DECLARATION */
146 xbt_init(&argc,argv);
148 /* Search for the tests to do */
150 for (i=1;i<argc;i++) {
151 if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
152 char *p=strchr(argv[i],'=')+1;
153 if (selection[0] == '\\0') {
154 strcpy(selection, p);
156 strcat(selection, \",\");
157 strcat(selection, p);
159 } else if (!strcmp(argv[i], \"--verbose\")) {
161 } else if (!strcmp(argv[i], \"--dump-only\")||
162 !strcmp(argv[i], \"--dump\")) {
163 xbt_test_dump(selection);
165 } else if (!strcmp(argv[i], \"--help\")) {
167 "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
168 "--help: display this help\\n"
169 "--verbose: print the name for each running test\\n"
170 "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
171 "--tests=selection: Use argument to select which suites/units/tests to run\\n"
172 " --tests can be used more than once, and selection may be a comma\\n"
173 " separated list of directives.\\n\\n"
174 "Directives are of the form:\\n"
175 " [-]suitename[:unitname]\\n\\n"
176 "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
177 "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
178 "it applies on any unit.\\n\\n"
179 "By default, everything is enabled.\\n\\n"
180 "'all' as suite name apply to all suites.\\n\\n"
181 "Example 1: \\"-toto,+toto:tutu\\"\\n"
182 " disables the whole toto testsuite (any unit in it),\\n"
183 " then reenables the tutu unit of the toto test suite.\\n\\n"
184 "Example 2: \\"-all,+toto\\"\\n"
185 " Run nothing but the toto suite.\\n");
188 printf("testall: Unknown option: %s\\n",argv[i]);
192 /* Got all my tests to do */
194 res = xbt_test_run(selection, verbosity);
199 print OUT $GENERATED;
200 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
203 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
205 my ($name,$func,$title) = @{$_};
206 print " unit $name: func=$func; title=$title\n";
209 #while (my $t = shift @tests) {
211 # add this suite to the main
213 open IN,"${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
217 # print "Look for proto: $_";
218 last if /SGU: BEGIN PROTOTYPES/;
221 # search my prototype
223 # print "Seek protos: $_";
224 last if (/SGU: END PROTOTYPES/ || /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";
239 my ($name,$func,$title) = @{$_};
240 $newmain .= " void $func(void);\n"
243 $newmain .= " /* SGU: END FILE */\n\n";
244 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
248 # pass remaining prototypes, search declarations
250 $newmain .= $_ unless /SGU: END PROTOTYPES/;
251 last if /SGU: BEGIN SUITES DECLARATION/;
254 ### Done with prototypes. And now, the actual code
256 # search my prototype
258 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
261 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
263 last if /SGU: END FILE/;
265 $_ = <IN>; # pass extra blank line
267 die "this line should be blank ($_). Did you edit the file?" if /\W/;
271 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
272 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
274 my ($name,$func,$title) = @{$_};
275 $newmain .= " xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
278 $newmain .= " /* SGU: END FILE */\n\n";
279 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
287 close IN || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
289 # write it back to main
290 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
292 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
293 } # end if process_one($)