Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to find dependency from parent directory when building exe #734

Closed
samharrison7 opened this issue Aug 28, 2022 · 8 comments · Fixed by #737
Closed

Unable to find dependency from parent directory when building exe #734

samharrison7 opened this issue Aug 28, 2022 · 8 comments · Fixed by #737
Labels
bug Something isn't working

Comments

@samharrison7
Copy link
Member

Description

I am trying to build an executable from a project with the following structure:

src/
  a/
    a_mod.f90
  b_mod.f90
  main.f90

In the code, a_mod uses b_mod, and main uses a_mod. When I build just as a library, it works as expected. However, when I specify to build an executable from main.f90, the build fails with the error <ERROR>*cmd_build*:target error:Unable to find source for module dependency: "b_mod" used by "./src/a/a_mod.f90".

The issue seems to occur when a module depends on a module that is in a parent directory (i.e. a_mod depending on b_mod from its parent directory) and only when build an executable.

I have placed a minimum viable example of the project structure that causes this issue in this repo: https://github.com/samharrison7/fpm-test-exe-issues

Expected Behaviour

I expected fpm to build the executable from this project structure.

Version of fpm

0.6.0, alpha

Platform and Architecture

Ubuntu 20.04

Additional Information

No response

@samharrison7 samharrison7 added the bug Something isn't working label Aug 28, 2022
@zoziha
Copy link
Contributor

zoziha commented Aug 29, 2022

Thanks for your feedback @samharrison7 , it seems that this problem does exist.

In fpm.f90, fpm first parses the main program source files, and labels these source files with FPM_SCOPE_APP, FPM_SCOPE_EXAMPLE, FPM_SCOPE_TEST, then parses the module source files, and labels FPM_SCOPE_LIB, FPM_SCOPE_DEP, and only FPM_SCOPE_LIB, FPM_SCOPE_DEP labelled source files will be queried if provide the module names (see find_module_dependency).

A feasible solution is to parse the dependency source files first, and then parse those main programs. (See #737)

@samharrison7
Copy link
Member Author

Thanks for the quick response and fix @zoziha! I've just tested your patch on my code (including a much more complicated version of the minimum example I posted) and it compiles the exe without problems now.

@LKedward
Copy link
Member

Hi @samharrison7 - this behaviour is currently part of the intended design of fpm. Modules that are not in the main src directory are not part of the library and have a use scope limited to the folder they are in and subfolders. Apologies that this is not documented anywhere for users. It is documented for developers here.

@samharrison7
Copy link
Member Author

Ah I did wonder whether it was a design thing. That seems a logical restriction and is probably a good motivation for me to do some src directory restructuring of my projects!

@samharrison7
Copy link
Member Author

Though I do wonder whether having a flag to override this restriction would be a useful addition for legacy projects where directory restructuring isn't possible/desirable?

@LKedward
Copy link
Member

Though I do wonder whether having a flag to override this restriction would be a useful addition for legacy projects where directory restructuring isn't possible/desirable?

Yes, this is a good point, though it'll have to be an option in the manifest file instead of a flag.

@zoziha
Copy link
Contributor

zoziha commented Aug 29, 2022

@LKedward ,if I'm not mistaken, #737 doesn't seem to change the original intention of the design you mentioned.

  1. Library sources (FPM_SCOPE_LIB) may only use modules also with library scope. This includes library modules from dependencies.
  2. Executable sources (FPM_SCOPE_APP,FPM_SCOPE_TEST) may use library modules (including dependencies) as well as any modules corresponding to source files in the same directory or a subdirectory of the executable source file.
src/
    a/
        a_mod.f90
    b_mod.f90
    main.f90
---
# fpm.toml
[[executable]]
name = "main"
source-dir = "src"
main = "main.f90"

The above demo can be compiled normally in the #737 patch because src defaults to the library file path and a_mod.f90 obeys the rule 1 and can access the parent folder (src), both a_mod.f90 and b_mod.f90 belong to the same FPM_SCOPE_LIB.

app/
    a/
        a_mod.f90
    b_mod.f90
    main.f90
---
# fpm.toml
[[executable]]
name = "main"
source-dir = "app"
main = "main.f90"

The above demo cannot be compiled normally in the #737 patch because the app defaults to the executable program file path. Both a_mod.f90 and b_mod.f90 belong to FPM_SCOPE_APP, and an error will appear:

<ERROR>*cmd_build*:target error:Unable to find source for module dependency: "b_mod" used by "./app/a/a_mod.f90"

The reason is that a_mod.f90 obeys the rule 2 and cannot access the parent folder.

(PS. These 2 rules are really worth documenting in the fpm-doc documentation after we discuss here.)

@LKedward
Copy link
Member

LKedward commented Aug 31, 2022

Thanks for explaining @zoziha, that makes sense to me now.

(PS. These 2 rules are really worth documenting in the fpm-doc documentation after we discuss here.)

Agreed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants