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/\.cpp$/_unit.cpp/;
48 $outfile =~ s|.*/([^/]*)$|$1| if $outfile =~ m|/|;
49 $outfile = "$outdir$outfile";
51 print "$progname: processing $infile (generating $outfile)...\n";
54 my ($unit_source,$suite_name,$suite_title)=("","","");
55 my (%tests); # to detect multiple definition
56 my (@tests); # actual content
58 open IN, "$infile" || die "$progname: Cannot open input file '$infile': $!\n";
59 $infile =~ s|$root|| if defined($root);
66 if (m/ifdef +SIMGRID_TEST/) {
71 if (m/endif.*SIMGRID_TEST/) {
76 if (m/XBT_TEST_SUITE\(\w*"([^"]*)"\w*, *(.*?)\);/) { #" {
77 die "$progname: Multiple suites in the same file ($infile) are not supported yet\n" if length($suite_name);
78 ($suite_name,$suite_title)=($1,$2);
79 die "$progname: Empty suite name in $infile" unless length($suite_name);
80 die "$progname: Empty suite title in $infile" unless length($suite_title);
82 } elsif (m/XBT_TEST_SUITE/) {
83 die "$progname: Parse error: This line seem to be a test suite declaration, but failed to parse it\n$_\n";
86 if (m/XBT_TEST_UNIT\(\w*"([^"]*)"\w*,([^,]*),(.*?)\)/) { #"{
87 die "$progname: multiply defined unit in file $infile: $1\n" if (defined($tests{$1}));
92 } elsif (m/XBT_TEST_UNIT/) {
93 die "$progname: Parse error: This line seem to be a test unit, but failed to parse it\n$_\n";
95 $unit_source .= $_ if $takeit;
97 close IN || die "$progname: cannot close input file '$infile': $!\n";
101 die "$progname: end of file reached in SIMGRID_TEST block.\n".
102 "You should end each of the with a line matching: /endif.*SIMGRID_TEST/\n".
104 "#endif /* SIMGRID_TEST */\n"
107 die "$progname: no suite defined in $infile\n" unless (length($suite_name));
111 my ($GENERATED)=("/*******************************/\n".
112 "/* GENERATED FILE, DO NOT EDIT */\n".
113 "/*******************************/\n\n");
115 open OUT,">$outfile" || die "$progname: Cannot open output file '$outfile': $!\n";
116 print OUT $GENERATED;
117 print OUT "#include <stdio.h>\n";
118 print OUT "#include \"xbt.h\"\n";
119 print OUT $GENERATED;
120 print OUT "#line $beginline \"$infile\" \n";
121 print OUT "$unit_source";
122 print OUT $GENERATED;
123 close OUT || die "$progname: Cannot close output file '$outfile': $!\n";
125 # write the main skeleton if needed
126 if (! -e "${outdir}simgrid_units_main.c") {
127 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
128 print OUT $GENERATED;
129 print OUT "#include <stdio.h>\n\n";
130 print OUT "#include \"xbt.h\"\n\n";
131 print OUT "extern xbt_test_unit_t _xbt_current_unit;\n\n";
132 print OUT "/* SGU: BEGIN PROTOTYPES */\n";
133 print OUT "/* SGU: END PROTOTYPES */\n\n";
134 print OUT $GENERATED;
135 # print OUT "# 93 \"sg_unit_extractor.pl\"\n";
137 int main(int argc, char *argv[]) {
138 xbt_test_suite_t suite;
139 char selection[1024];
144 /* SGU: BEGIN SUITES DECLARATION */
145 /* SGU: END SUITES DECLARATION */
147 xbt_init(&argc,argv);
149 /* Search for the tests to do */
151 for (i=1;i<argc;i++) {
152 if (!strncmp(argv[i],\"--tests=\",strlen(\"--tests=\"))) {
153 char *p=strchr(argv[i],'=')+1;
154 if (selection[0] == '\\0') {
155 strcpy(selection, p);
157 strcat(selection, \",\");
158 strcat(selection, p);
160 } else if (!strcmp(argv[i], \"--verbose\")) {
162 } else if (!strcmp(argv[i], \"--dump-only\")||
163 !strcmp(argv[i], \"--dump\")) {
164 xbt_test_dump(selection);
166 } else if (!strcmp(argv[i], \"--help\")) {
168 "Usage: testall [--help] [--tests=selection] [--dump-only]\\n\\n"
169 "--help: display this help\\n"
170 "--verbose: print the name for each running test\\n"
171 "--dump-only: don't run the tests, but display some debuging info about the tests\\n"
172 "--tests=selection: Use argument to select which suites/units/tests to run\\n"
173 " --tests can be used more than once, and selection may be a comma\\n"
174 " separated list of directives.\\n\\n"
175 "Directives are of the form:\\n"
176 " [-]suitename[:unitname]\\n\\n"
177 "If the first char is a '-', the directive disables its argument instead of enabling it\\n"
178 "suitename/unitname is the set of tests to en/disable. If a unitname is not specified,\\n"
179 "it applies on any unit.\\n\\n"
180 "By default, everything is enabled.\\n\\n"
181 "'all' as suite name apply to all suites.\\n\\n"
182 "Example 1: \\"-toto,+toto:tutu\\"\\n"
183 " disables the whole toto testsuite (any unit in it),\\n"
184 " then reenables the tutu unit of the toto test suite.\\n\\n"
185 "Example 2: \\"-all,+toto\\"\\n"
186 " Run nothing but the toto suite.\\n");
189 printf("testall: Unknown option: %s\\n",argv[i]);
193 /* Got all my tests to do */
195 res = xbt_test_run(selection, verbosity);
200 print OUT $GENERATED;
201 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
204 print " Suite $suite_name: $suite_title (".(scalar @tests)." tests)\n";
206 my ($name,$func,$title) = @{$_};
207 print " unit $name: func=$func; title=$title\n";
210 #while (my $t = shift @tests) {
212 # add this suite to the main
214 open IN,"${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
218 # print "Look for proto: $_";
219 last if /SGU: BEGIN PROTOTYPES/;
222 # search my prototype
224 # print "Seek protos: $_";
225 last if (/SGU: END PROTOTYPES/ || /SGU: BEGIN FILE $infile/);
228 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
230 last if /SGU: END FILE/;
232 $_ = <IN>; # pass extra blank line
234 die "this line should be blank ($_). Did you edit the file?" if /\W/;
238 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
240 my ($name,$func,$title) = @{$_};
241 $newmain .= " void $func(void);\n"
244 $newmain .= " /* SGU: END FILE */\n\n";
245 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END PROTOTYPES/) {
249 # pass remaining prototypes, search declarations
251 $newmain .= $_ unless /SGU: END PROTOTYPES/;
252 last if /SGU: BEGIN SUITES DECLARATION/;
255 ### Done with prototypes. And now, the actual code
257 # search my prototype
259 last if (/SGU: END SUITES DECLARATION/ || /SGU: BEGIN FILE $infile/);
262 if (/SGU: BEGIN FILE $infile/) { # found an old section for this file. Kill it
264 last if /SGU: END FILE/;
266 $_ = <IN>; # pass extra blank line
268 die "this line should be blank ($_). Did you edit the file?" if /\W/;
272 $newmain .= " /* SGU: BEGIN FILE $infile */\n";
273 $newmain .= " suite = xbt_test_suite_by_name(\"$suite_name\",$suite_title);\n";
275 my ($name,$func,$title) = @{$_};
276 $newmain .= " xbt_test_suite_push(suite, \"$name\", $func, $title);\n";
279 $newmain .= " /* SGU: END FILE */\n\n";
280 if ($old_ =~ /SGU: BEGIN FILE/ || $old_ =~ /SGU: END SUITES DECLARATION/) {
288 close IN || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
290 # write it back to main
291 open OUT,">${outdir}simgrid_units_main.c" || die "$progname: Cannot open main file '${outdir}simgrid_units_main.c': $!\n";
293 close OUT || die "$progname: Cannot close main file '${outdir}simgrid_units_main.c': $!\n";
294 } # end if process_one($)