Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Now install ruby v1.9.*
[simgrid.git] / src / mk_supernovae.pl
1 #! /usr/bin/perl
2
3 use strict;
4 use Getopt::Long qw(GetOptions);
5
6 #open TMP,">mk_supernovae.pl.args";
7 #map {print TMP "$_ "} @ARGV;
8 #close TMP;
9
10 sub usage($) {
11     my $ret;
12     print "USAGE: mk_supernovae.pl [--fragile=file]* --out=file file1 file2*\n";
13     print "  --help: show this message\n";
14     print "  --fragile=file: specify that file is fragile and shouldn't be supernovaed\n";
15     print "  --out=file: specify the name of the output file\n";
16     print "elements may be separated by semi-columns (;) instead of spaces, too\n";
17     exit $ret;
18 }
19
20 my @fragile_files=undef;
21 my $outfile=undef;
22 my $help;
23
24 Getopt::Long::config('permute','no_getopt_compat', 'no_auto_abbrev');
25 GetOptions(
26     'help|h'                => \$help,
27
28     'fragile=s' =>\@fragile_files,
29     'out=s'     =>\$outfile) or usage(1);
30
31 @fragile_files = split(/;/,join(';',@fragile_files));
32 @fragile_files = split(/ /,join(' ',@fragile_files));
33
34 usage(0) if (defined($help));
35 unless(defined($outfile)) {
36     print "ERROR: No outfile defined.\n";
37     usage(1);
38 }
39
40 #print "mk_supernovae: generate $outfile\n";  
41
42 open OUT, ">$outfile" or die "ERROR: cannot open $outfile: $!\n";
43
44 print OUT <<EOF
45 #define SUPERNOVAE_MODE 1
46 #ifndef _GNU_SOURCE
47 #  define _GNU_SOURCE   /* for getline() with older libc */
48 #endif
49 #include <ctype.h>
50 #include "portable.h"
51 #include "xbt.h"
52   
53 EOF
54   ;
55
56 sub readfile($) {
57     my $filename=shift;
58     open IN,"$filename" || die "ERROR: cannot read $filename: $!\n";
59     my $res;
60     while (<IN>) {
61         $res .= $_;
62     }
63     close IN;  
64     return $res;
65 }
66
67
68 my %fragile;
69 map {$fragile{$_}=1} @fragile_files;
70 my @args = split(/;/,join(';',@ARGV));
71 @args = split(/ /,join(' ',@args));
72 my $nbfile=0;
73 foreach my $file (@args) {
74     if ($fragile{$file}) {
75         print "mk_supernovae: $file is fragile, skip it\n";
76         next;
77     } 
78 #       print "mk_supernovae: process $file\n";
79     $nbfile++;
80
81     my $needundef=1;
82     print OUT "/* file $file */\n";
83     if ($file eq "xbt/log.c") {
84         print OUT "  #define _simgrid_log_category__default &_simgrid_log_category__log\n";
85     } else {
86         my $ctn = readfile($file);
87         if ($ctn =~ m/XBT_LOG_[^ ]*?DEFAULT_[^ ]*?CATEGORY/s) {
88             my $default=$ctn;
89             $default =~ s/.*XBT_LOG_[^ ]*?DEFAULT_[^ ]*?CATEGORY[^(]*\(([^,)]*).*$/$1/s;
90             print OUT "  #define _simgrid_log_category__default &_simgrid_log_category__$default\n";
91         } else {
92             print OUT "  /* no default category in file $file */\n";
93             $needundef = 0;
94         }
95     }
96     print OUT "  #include \"$file\"\n";
97     print OUT "  #undef _simgrid_log_category__default\n" if $needundef;
98     print OUT "\n";
99 }
100 close OUT;
101 print "mk_supernovae: $outfile contains $nbfile files inlined\n";