Nvfortran with fpm

Hi everyone,
I’m trying to build a CUDA Fortran application using fpm (Fortran Package Manager), but I’m running into an error.

Command I’m using:

fpm build --compiler nvfortran --flag “-acc -Minfo=all -cuda” --V

Error message:

BUILD_NAME: build/nvfortran
COMPILER: nvfortran
C COMPILER: nvfortran
CXX COMPILER: nvfortran
COMPILER OPTIONS: -acc -Minfo=all -cuda
C COMPILER OPTIONS:
CXX COMPILER OPTIONS:
LINKER OPTIONS:
INCLUDE DIRECTORIES: [ ]
cmd_build Target error: Unable to find source for module dependency: “cudafor” used by “././src/mod_XXX.f90”
STOP 1

Project structure:

my_project/
├── fpm.toml
├── src/
│ ├── mod_XXX.f90
│ ├── mod_YYY.f90
│ └── mod_ZZZ.f90
└── app/
├── app1.f90
└── app2.f90

I have two executable targets defined in fpm.toml (one for each file in the app/ folder). If I compile manually with nvfortran, everything works fine:

nvfortran -acc -Minfo=all -cuda src/mod_XXX.f90 src/mod_YYY.f90 src/mod_ZZZ.f90 app/app1.f90 -o exe1

So the problem seems to be specific to how fpm is handling the cudafor module dependency.

What do I need to add to the fpm build command (or to fpm.toml) so that fpm can find the cudafor module when using nvfortran?

Any help would be greatly appreciated! Thanks in advance!

A possible workaround:

[build]
external-modules = ["cudafor"]

I do foresee a problem this will not work with other compilers.

The alternative would be to strictly use the CUDA Fortran preprocessor sentinels:

!@cuf use cudafor

and the _CUDA macro for conditional text inclusion. See: CUDA Fortran Conditional Compilation.

Thank you! That workaround solved the issue — it’s now building successfully.