Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / tools / smpi / generate_smpi_defines.pl
1 #!/usr/bin/env perl
2 # Copyright 2016 Vincent Danjean <vincent.danjean@inria.fr>
3
4 # Call this script like this:
5 # C/C++  : ./generate_smpi_defines.pl ../../include/smpi/smpi.h
6 # FORTRAN: ./generate_smpi_defines.pl -f ../../include/smpi/smpi.h
7 #
8 # It will generate macros that are useful for adding file and line numbers to
9 # traces without obtaining a backtrace (as this would be very slow and make
10 # the simulation/trace unreliable when compared to a "real" trace as obtained
11 # with MPI+TAU).
12 use strict;
13 use warnings;
14 use Getopt::Std; 
15
16 my %options=();
17 getopts("fc", \%options);
18
19 # $incall denotes whether we are currently parsing a macro or not...
20 my $incall=0;
21
22 my $commentChar="//";
23 if (defined $options{f}) {
24   $commentChar="!"
25 }
26
27 print "$commentChar This file has been automatically generated by the script\n";
28 print "$commentChar in tools/smpi/" . __FILE__ ."\n";
29 print "$commentChar DO NOT EDIT MANUALLY. ALL CHANGES WILL BE OVERWRITTEN!\n";
30
31 # Formatting of the output
32 sub output_macro {
33   my $line = shift;
34   my @parts = split (/\s*,\s*/, $line);
35   my $id = $parts[1];
36
37   # This is a GCC extension. The last statement is the value of the expression
38   # in parentheses.
39   if (defined $options{f}) {
40     print "#define ". uc($id) ." smpi_trace_set_call_location(__FILE__,__LINE__); call ". lc $id ."\n";
41   }
42   else {
43     if ($id eq "MPI_Init") {
44       print "#define MPI_Init(...) ({ smpi_process_init(__VA_ARGS__); smpi_trace_set_call_location(__FILE__,__LINE__); MPI_Init(NULL, NULL); })\n";
45     }
46     else {
47       print "#define $id(...) ({ smpi_trace_set_call_location(__FILE__,__LINE__); $id(__VA_ARGS__); })\n";
48     }
49   }
50 }
51
52 my $wholemacro;
53 my $line;
54 while (defined($line = <>)) {
55   chomp($line);
56   if ($line =~ /^MPI_CALL/) {
57     if ($incall) {
58         output_macro($wholemacro);
59     }
60     $incall=1;
61     $wholemacro = $line;
62   } elsif ($incall && $line =~ /^\s+\S/) { 
63     # Did we already start parsing an MPI_CALL macro? If so, just concatenate
64     $wholemacro .= ' '.$line;
65   } elsif ($incall) {
66     output_macro($wholemacro);
67     $incall=0;
68   }
69 }