Chris@10: #!/usr/bin/perl -w Chris@10: # Generate Fortran 2003 interfaces from a sequence of C function declarations Chris@10: # of the form (one per line): Chris@10: # extern (...args...) Chris@10: # extern (...args...) Chris@10: # ... Chris@10: # with no line breaks within a given function. (It's too much work to Chris@10: # write a general parser, since we just have to handle FFTW's header files.) Chris@10: Chris@10: sub canonicalize_type { Chris@10: my($type); Chris@10: ($type) = @_; Chris@10: $type =~ s/ +/ /g; Chris@10: $type =~ s/^ //; Chris@10: $type =~ s/ $//; Chris@10: $type =~ s/([^\* ])\*/$1 \*/g; Chris@10: return $type; Chris@10: } Chris@10: Chris@10: # C->Fortran map of supported return types Chris@10: %return_types = ( Chris@10: "int" => "integer(C_INT)", Chris@10: "ptrdiff_t" => "integer(C_INTPTR_T)", Chris@10: "size_t" => "integer(C_SIZE_T)", Chris@10: "double" => "real(C_DOUBLE)", Chris@10: "float" => "real(C_FLOAT)", Chris@10: "long double" => "real(C_LONG_DOUBLE)", Chris@10: "float128__" => "real(16)", Chris@10: "fftw_plan" => "type(C_PTR)", Chris@10: "fftwf_plan" => "type(C_PTR)", Chris@10: "fftwl_plan" => "type(C_PTR)", Chris@10: "fftwq_plan" => "type(C_PTR)", Chris@10: "void *" => "type(C_PTR)", Chris@10: "char *" => "type(C_PTR)", Chris@10: "double *" => "type(C_PTR)", Chris@10: "float *" => "type(C_PTR)", Chris@10: "long double *" => "type(C_PTR)", Chris@10: "float128__ *" => "type(C_PTR)", Chris@10: "fftw_complex *" => "type(C_PTR)", Chris@10: "fftwf_complex *" => "type(C_PTR)", Chris@10: "fftwl_complex *" => "type(C_PTR)", Chris@10: "fftwq_complex *" => "type(C_PTR)", Chris@10: ); Chris@10: Chris@10: # C->Fortran map of supported argument types Chris@10: %arg_types = ( Chris@10: "int" => "integer(C_INT), value", Chris@10: "unsigned" => "integer(C_INT), value", Chris@10: "size_t" => "integer(C_SIZE_T), value", Chris@10: "ptrdiff_t" => "integer(C_INTPTR_T), value", Chris@10: Chris@10: "fftw_r2r_kind" => "integer(C_FFTW_R2R_KIND), value", Chris@10: "fftwf_r2r_kind" => "integer(C_FFTW_R2R_KIND), value", Chris@10: "fftwl_r2r_kind" => "integer(C_FFTW_R2R_KIND), value", Chris@10: "fftwq_r2r_kind" => "integer(C_FFTW_R2R_KIND), value", Chris@10: Chris@10: "double" => "real(C_DOUBLE), value", Chris@10: "float" => "real(C_FLOAT), value", Chris@10: "long double" => "real(C_LONG_DOUBLE), value", Chris@10: "__float128" => "real(16), value", Chris@10: Chris@10: "fftw_complex" => "complex(C_DOUBLE_COMPLEX), value", Chris@10: "fftwf_complex" => "complex(C_DOUBLE_COMPLEX), value", Chris@10: "fftwl_complex" => "complex(C_LONG_DOUBLE), value", Chris@10: "fftwq_complex" => "complex(16), value", Chris@10: Chris@10: "fftw_plan" => "type(C_PTR), value", Chris@10: "fftwf_plan" => "type(C_PTR), value", Chris@10: "fftwl_plan" => "type(C_PTR), value", Chris@10: "fftwq_plan" => "type(C_PTR), value", Chris@10: "const fftw_plan" => "type(C_PTR), value", Chris@10: "const fftwf_plan" => "type(C_PTR), value", Chris@10: "const fftwl_plan" => "type(C_PTR), value", Chris@10: "const fftwq_plan" => "type(C_PTR), value", Chris@10: Chris@10: "const int *" => "integer(C_INT), dimension(*), intent(in)", Chris@10: "ptrdiff_t *" => "integer(C_INTPTR_T), intent(out)", Chris@10: "const ptrdiff_t *" => "integer(C_INTPTR_T), dimension(*), intent(in)", Chris@10: Chris@10: "const fftw_r2r_kind *" => "integer(C_FFTW_R2R_KIND), dimension(*), intent(in)", Chris@10: "const fftwf_r2r_kind *" => "integer(C_FFTW_R2R_KIND), dimension(*), intent(in)", Chris@10: "const fftwl_r2r_kind *" => "integer(C_FFTW_R2R_KIND), dimension(*), intent(in)", Chris@10: "const fftwq_r2r_kind *" => "integer(C_FFTW_R2R_KIND), dimension(*), intent(in)", Chris@10: Chris@10: "double *" => "real(C_DOUBLE), dimension(*), intent(out)", Chris@10: "float *" => "real(C_FLOAT), dimension(*), intent(out)", Chris@10: "long double *" => "real(C_LONG_DOUBLE), dimension(*), intent(out)", Chris@10: "__float128 *" => "real(16), dimension(*), intent(out)", Chris@10: Chris@10: "fftw_complex *" => "complex(C_DOUBLE_COMPLEX), dimension(*), intent(out)", Chris@10: "fftwf_complex *" => "complex(C_FLOAT_COMPLEX), dimension(*), intent(out)", Chris@10: "fftwl_complex *" => "complex(C_LONG_DOUBLE_COMPLEX), dimension(*), intent(out)", Chris@10: "fftwq_complex *" => "complex(16), dimension(*), intent(out)", Chris@10: Chris@10: "const fftw_iodim *" => "type(fftw_iodim), dimension(*), intent(in)", Chris@10: "const fftwf_iodim *" => "type(fftwf_iodim), dimension(*), intent(in)", Chris@10: "const fftwl_iodim *" => "type(fftwl_iodim), dimension(*), intent(in)", Chris@10: "const fftwq_iodim *" => "type(fftwq_iodim), dimension(*), intent(in)", Chris@10: Chris@10: "const fftw_iodim64 *" => "type(fftw_iodim64), dimension(*), intent(in)", Chris@10: "const fftwf_iodim64 *" => "type(fftwf_iodim64), dimension(*), intent(in)", Chris@10: "const fftwl_iodim64 *" => "type(fftwl_iodim64), dimension(*), intent(in)", Chris@10: "const fftwq_iodim64 *" => "type(fftwq_iodim64), dimension(*), intent(in)", Chris@10: Chris@10: "void *" => "type(C_PTR), value", Chris@10: "FILE *" => "type(C_PTR), value", Chris@10: Chris@10: "const char *" => "character(C_CHAR), dimension(*), intent(in)", Chris@10: Chris@10: "fftw_write_char_func" => "type(C_FUNPTR), value", Chris@10: "fftwf_write_char_func" => "type(C_FUNPTR), value", Chris@10: "fftwl_write_char_func" => "type(C_FUNPTR), value", Chris@10: "fftwq_write_char_func" => "type(C_FUNPTR), value", Chris@10: "fftw_read_char_func" => "type(C_FUNPTR), value", Chris@10: "fftwf_read_char_func" => "type(C_FUNPTR), value", Chris@10: "fftwl_read_char_func" => "type(C_FUNPTR), value", Chris@10: "fftwq_read_char_func" => "type(C_FUNPTR), value", Chris@10: Chris@10: # Although the MPI standard defines this type as simply "integer", Chris@10: # if we use integer without a 'C_' kind in a bind(C) interface then Chris@10: # gfortran complains. Instead, since MPI also requires the C type Chris@10: # MPI_Fint to match Fortran integers, we use the size of this type Chris@10: # (extracted by configure and substituted by the Makefile). Chris@10: "MPI_Comm" => "integer(C_MPI_FINT), value" Chris@10: ); Chris@10: Chris@10: while (<>) { Chris@10: next if /^ *$/; Chris@10: if (/^ *extern +([a-zA-Z_0-9 ]+[ \*]) *([a-zA-Z_0-9]+) *\((.*)\) *$/) { Chris@10: $ret = &canonicalize_type($1); Chris@10: $name = $2; Chris@10: Chris@10: $args = $3; Chris@10: $args =~ s/^ *void *$//; Chris@10: Chris@10: $bad = ($ret ne "void") && !exists($return_types{$ret}); Chris@10: foreach $arg (split(/ *, */, $args)) { Chris@10: $arg =~ /^([a-zA-Z_0-9 ]+[ \*]) *([a-zA-Z_0-9]+) *$/; Chris@10: $argtype = &canonicalize_type($1); Chris@10: $bad = 1 if !exists($arg_types{$argtype}); Chris@10: } Chris@10: if ($bad) { Chris@10: print "! Unable to generate Fortran interface for $name\n"; Chris@10: next; Chris@10: } Chris@10: Chris@10: # any function taking an MPI_Comm arg needs a C wrapper (grr). Chris@10: if ($args =~ /MPI_Comm/) { Chris@10: $cname = $name . "_f03"; Chris@10: } Chris@10: else { Chris@10: $cname = $name; Chris@10: } Chris@10: Chris@10: # Fortran has a 132-character line-length limit by default (grr) Chris@10: $len = 0; Chris@10: Chris@10: print " "; $len = $len + length(" "); Chris@10: if ($ret eq "void") { Chris@10: $kind = "subroutine" Chris@10: } Chris@10: else { Chris@10: print "$return_types{$ret} "; Chris@10: $len = $len + length("$return_types{$ret} "); Chris@10: $kind = "function" Chris@10: } Chris@10: print "$kind $name("; $len = $len + length("$kind $name("); Chris@10: $len0 = $len; Chris@10: Chris@10: $argnames = $args; Chris@10: $argnames =~ s/([a-zA-Z_0-9 ]+[ \*]) *([a-zA-Z_0-9]+) */$2/g; Chris@10: $comma = ""; Chris@10: foreach $argname (split(/ *, */, $argnames)) { Chris@10: if ($len + length("$comma$argname") + 3 > 132) { Chris@10: printf ", &\n%*s", $len0, ""; Chris@10: $len = $len0; Chris@10: $comma = ""; Chris@10: } Chris@10: print "$comma$argname"; Chris@10: $len = $len + length("$comma$argname"); Chris@10: $comma = ","; Chris@10: } Chris@10: print ") "; $len = $len + 2; Chris@10: Chris@10: if ($len + length("bind(C, name='$cname')") > 132) { Chris@10: printf "&\n%*s", $len0 - length("$name("), ""; Chris@10: } Chris@10: print "bind(C, name='$cname')\n"; Chris@10: Chris@10: print " import\n"; Chris@10: foreach $arg (split(/ *, */, $args)) { Chris@10: $arg =~ /^([a-zA-Z_0-9 ]+[ \*]) *([a-zA-Z_0-9]+) *$/; Chris@10: $argtype = &canonicalize_type($1); Chris@10: $argname = $2; Chris@10: $ftype = $arg_types{$argtype}; Chris@10: Chris@10: # Various special cases for argument types: Chris@10: if ($name =~ /_flops$/ && $argtype eq "double *") { Chris@10: $ftype = "real(C_DOUBLE), intent(out)" Chris@10: } Chris@10: if ($name =~ /_execute/ && ($argname eq "ri" || Chris@10: $argname eq "ii" || Chris@10: $argname eq "in")) { Chris@10: $ftype =~ s/intent\(out\)/intent(inout)/; Chris@10: } Chris@10: Chris@10: print " $ftype :: $argname\n" Chris@10: } Chris@10: Chris@10: print " end $kind $name\n"; Chris@10: print " \n"; Chris@10: } Chris@10: }