[CMake] fortran module name issue

Burlen Loring burlen.loring at gmail.com
Mon Jun 26 19:06:06 EDT 2017


Hi Juan,

I have faced similar challenges when calling Fortran code from C++ 
templates.

In the solution I came up with, I use a marked up Fortran template code 
and Cmake's configure_file command to generate a module for each 
combination of C++ types I need. The markup uses a token for each 
template argument and a decorator that is appended to Fortran symbols 
and is used in the module and file names to differentiate all of the 
CMake generated instantiations.

here is the relevant Cmake code that does all the work, (note: I have 
two template arguments one for coordinates and variables):

    set(teca_alg_f90_srcs)

    set(teca_alg_f90_generics
         gfdl_spline
         gfdl_tc_candidates
         )

    set(f_type real)
    set(c_types float double)
    foreach(generic_src ${teca_alg_f90_generics})
         foreach(c_type_var ${c_types})
             set(iso_c_type_var "${f_type}(c_${c_type_var})")
             string(SUBSTRING ${c_type_var} 0 1 var_name)
             foreach(c_type_coord ${c_types})
                 string(SUBSTRING ${c_type_coord} 0 1 coord_name)
                 set(decorator "c${coord_name}_v${var_name}")
                 set(iso_c_type_coord "${f_type}(c_${c_type_coord})")
                 configure_file(${generic_src}.f90.in
    ${generic_src}_${decorator}.f90 @ONLY)
                 list(APPEND teca_alg_f90_srcs
    ${generic_src}_${decorator}.f90)
             endforeach()
         endforeach()
    endforeach()

    add_library(teca_alg ${teca_alg_cxx_srcs} ${teca_alg_f90_srcs})

And here is a link to an example marked up Fortran template code.

    https://github.com/LBL-EESA/TECA/blob/master/alg/gfdl_tc_candidates.f90.in

 From C++ I use macros to declare decorated Fortran functions and define 
C++ overloads that call them.

    https://github.com/LBL-EESA/TECA/blob/master/alg/gfdl_tc_candidates.h

It's a bit of a heavy handed solution, but I think you can solve your 
problem in a similar way. I'm interested in hearing of other approaches 
that have worked.

Burlen


On 06/26/2017 01:40 PM, Juan E. Sanchez wrote:
> Hi,
>
> It seems like cmake cannot handle the case where the module name is 
> the result of a macro.  I am using this approach to compile the same 
> code for different floating point precision.  Any advice appreciated.  
> This approach would apply to hundreds of files.
>
> The error is:
> Error copying Fortran module "concat".  Tried "CONCAT.mod" and 
> "concat.mod".
> make[2]: *** [CMakeFiles/flib.dir/baz.o.provides.build] Error 1
> make[1]: *** [CMakeFiles/flib.dir/all] Error 2
> make: *** [all] Error 2
>
>
> Regards,
>
> Juan
>
>
> baz.F:
> #include "fmacros.inc"
>
>       module CONCAT(baz)
>       contains
>       subroutine car(t3)
>       end subroutine
>       end module CONCAT(baz)
>
> foo.F:
> #include "fmacros.inc"
>
>       module CONCAT(foo)
>       contains
>       subroutine bar(t1, t2)
>       use CONCAT(baz)
>       implicit none
>       REAL(kind=8) t1
>       REAL(kind=DWIDTH) t2
>       call baz(t2)
>       end subroutine
>       end module CONCAT(foo)
>
> fmacros.inc:
>
> #define CONCAT(a) a/**/_double
> #define DWIDTH 8
>
> main.cc:
> extern "C" {
> void
> }
>
> CMakeLists.txt:
>
> PROJECT(foo)
> ENABLE_LANGUAGE(CXX Fortran)
>
> ADD_LIBRARY(flib foo.F baz.F)
>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20170626/d4e0ccc4/attachment-0001.html>


More information about the CMake mailing list