Logo AND Algorithmique Numérique Distribuée

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