Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
hey, figured out how to change vars to pointers without cocci complaining...
[simgrid.git] / src / smpi / smpif2c.in
1 #! /usr/bin/env perl
2
3 use warnings;
4 use strict;
5 use File::Temp;
6 use File::Copy;
7
8 my $include="-I@top_srcdir@/include -I@top_srcdir@/include/smpi -I@includedir@ -I@includedir@/smpi";
9
10 foreach my $fortran (@ARGV) {
11    my $output = $fortran;
12    $output =~ s/.f$/.c/;
13    my $tmp = new File::Temp;
14    $tmp->autoflush(1);
15    `f2c $include -w -a $fortran`;
16    die "F2C failed\n" if $?;
17    open F2C,"<$output";
18    my $started = 0;
19    print $tmp "#ifndef INTEGER_STAR_8\n";
20    print $tmp "#define INTEGER_STAR_8\n";
21    print $tmp "#endif\n";
22    print $tmp "#include <stdlib.h>\n";
23    print $tmp "#include <smpif.h>\n\n";
24    print $tmp "int __attribute__((weak)) __rank(void) {\n  static __thread int rank = -1;\n\n  if(rank < 0) {\n    rank = smpi_global_rank();\n  }\n  return rank;\n}\n\n";
25    while(<F2C>) {
26       chomp;
27       if(/\/\* Common Block Declarations \*\//) {
28          $started = 1;
29       }
30       if($started) {
31          if(/^} (.*?);/) {
32             $_ = "}* __attribute__((weak)) $1 = NULL;\n";
33          } elsif(/^#define\s*(\S*)\s*\(?([^.]*)(\..*?)?\)?$/) {
34             $_ = "#define $1 $2\[__rank()\]";
35             if(defined $3) {
36                $_ .= $3;
37             }
38             $_ .= "\n";
39             $_ .= "\nvoid __attribute__((weak,constructor)) __preinit_$1(void) {\n  if(!$2) $2 = malloc(smpi_global_size() * sizeof(*$2));\n}\n";
40             $_ .= "\nvoid __attribute__((weak,destructor)) __postfini_$1(void) {\n  free($2);\n  $2 = NULL;\n}\n";
41          }
42       }
43       if(/\/\* Table of constant values \*\// || /MAIN__/) {
44          $started = 0;
45       }
46       if(/\/* Main program alias \*\/\s*int\s+.*\s*\(\s*\)\s*{(.*)}/) {
47          $_ = "int smpi_simulated_main(int argc, char** argv) { smpi_process_init(&argc, &argv); $1 }\n";
48       }
49       print $tmp "$_\n";
50    }
51    close F2C;
52    copy($tmp->filename,$output) or die "Copy failed: $!\n";
53 }