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