jameskermode/f90wrap

Does f90wrap ignore preprocessor directives?

wcwitt opened this issue · 2 comments

I am trying to wrap a large-ish project with preprocessor directives, and I keep running into "multiple definition" errors.

Here is a minimal example to demonstrate the issue.

# constants.F90

module constants
implicit none

#ifndef BAD
#define GOOD
#endif

#ifdef GOOD
  real :: pi=3.14159
#endif

#ifdef BAD
  real :: pi=0.0
#endif

end module constants

The command

f90wrap -m pyconstants constants.F90

generates

# f90wrap_constants.f90

! Module constants defined in file constants.F90

subroutine f90wrap_constants__get__pi(f90wrap_pi)
    use constants, only: constants_pi => pi
    implicit none
    real, intent(out) :: f90wrap_pi
    
    f90wrap_pi = constants_pi
end subroutine f90wrap_constants__get__pi

subroutine f90wrap_constants__set__pi(f90wrap_pi)
    use constants, only: constants_pi => pi
    implicit none
    real, intent(in) :: f90wrap_pi
    
    constants_pi = f90wrap_pi
end subroutine f90wrap_constants__set__pi

subroutine f90wrap_constants__get__pi(f90wrap_pi)
    use constants, only: constants_pi => pi
    implicit none
    real, intent(out) :: f90wrap_pi
    
    f90wrap_pi = constants_pi
end subroutine f90wrap_constants__get__pi

subroutine f90wrap_constants__set__pi(f90wrap_pi)
    use constants, only: constants_pi => pi
    implicit none
    real, intent(in) :: f90wrap_pi
    
    constants_pi = f90wrap_pi
end subroutine f90wrap_constants__set__pi

! End of module constants defined in file constants.F90

The pi routines are duplicated, which leads to problems during the f2py step.

Is there an obvious solution that I'm missing? In the examples, I see that preprocessor directives are often commented out in a way that leaves only one option uncommented - is that the best course for now?

You are correct that f90wrap doesn’t understand these directives. You can preprocess the source files before passing to f90wrap, eg with gfortran -E. Here’s an example Makefile which does this: https://github.com/libAtoms/QUIP/blob/public/quippy/Makefile#L82

Neat - that was easy and I learned something new. Thanks!