Skip to content

Improve support for include statements #358

@LKedward

Description

@LKedward
Member

fpm currently has limited support for source file includes which I notice are used a lot in existing packages and are fundamental to c libraries. Specifically:

  • fpm cannot yet track module dependencies in included files;
  • fpm incremental rebuilds do not track changes in included files and;
  • included file cannot have the .f90 suffix

I am currently working on an update to fpm to address these shortcomings.

Activity

self-assigned this
on Feb 15, 2021
jbdv-no

jbdv-no commented on Mar 3, 2021

@jbdv-no

While trying to use fpm (incredible effort by all the developers btw, thank you!) in one of my
projects, I have encountered a problem when using include statements with the nagfor compiler.

Nagfor fails to compile since it cannot locate include files (named *.inc, located along with
all other *.f90 source files in the ./src directory). The very same directory structure works
as expected when using gfortran or ifort to compile.

Searching through fpm's documentation and issues for a solution, lead me to this particular issue,
and since I could not find particular mention being made of nagfor in this context before, thought
it could be useful to share this very simple reproducer. (Assuming of course that I am using fpm as
intended, very possible that a naming/other mistake on my side is causing the trouble)

Reproducing the problem

  • Create a new fpm project:

    fpm new fpm_inc --app --src
    
  • Put the subroutine say_hello from src/fpm_inc.f90 into a separate file
    src/say_hello.inc and modify src/fpm_inc.f90 to include this file:

    ! src/fpm_inc.f90
    module fpm_inc
      implicit none
      private
    
      public :: say_hello
    contains
      include 'say_hello.inc'
    end module fpm_inc
      ! src/say_hello.inc
      subroutine say_hello
        print *, "Hello, fpm_inc!"
      end subroutine say_hello
  • This project builds successfully with gfortran (default) and ifort:

    fpm run
    
    + build/gfortran_debug/app/fpm_inc 
    Hello, fpm_inc!
    
    fpm run --compiler ifort
    
    + build/ifort_debug/app/fpm_inc 
    Hello, fpm_inc!
    
  • However, it fails with nagfor, which complains that it cannot open the include file

    fpm run --compiler nagfor
    + nagfor -c ./src/fpm_inc.f90 -g -C=all -O0 -gline -PIC  -mdir build/nagfor_debug/fpm_inc -I build/nagfor_debug/fpm_inc -o build/nagfor_debug/fpm_inc/src_fpm_inc.f90.o
    NAG Fortran Compiler Release 6.2(Chiyoda) Build 6207
    Error: ./src/fpm_inc.f90, line 7: Cannot open INCLUDE file "say_hello.inc": No such file or directory
    Error: ./src/fpm_inc.f90, line 8: Implicit type for SAY_HELLO
    [NAG Fortran Compiler pass 1 error termination, 2 errors]
     Command failed
    ERROR STOP
    

    In this trivial case, the include is easy to avoid. However, in my actual use case the include
    file implements an algorithm using kind specifiers inherited from the module scope (so a form of
    "poor man's templates"), with different modules defined for different kinds.

    According to nagfor's documentation:

    Non-intrinsic modules, INCLUDE files and #include files are expected to exist in the current working directory or in a directory named by an -I option.

    Which would imply that an additional -I ./src can solve the problem in this particular case? But
    this is surely not the general solution.

    PS: You may notice from the code snippet, that I built fpm from source (using a binary release)
    and removed -coarray=single from the compiler flags. Since this is a feature (introduced in
    release 7.0) not supported by the release of nagfor that I have access to (Release 6.2).

LKedward

LKedward commented on Mar 3, 2021

@LKedward
MemberAuthor

Many thanks for the detailed report @jbdv-no, it looks like nagfor behaves slightly differently to gfortran and ifort which first search in the directory of the current source file. The solution for this issue will make the include directory explicit and so should fix your issue also.

removed their assignment
on Mar 7, 2023
self-assigned this
on Mar 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

mediumDifficulty level is medium

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @ivan-pi@LKedward@jbdv-no

      Issue actions

        Improve support for include statements · Issue #358 · fortran-lang/fpm