Skip to content

fpm not preprocessing main.f90 with --flag -cpp #791

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

Closed
rouson opened this issue Nov 3, 2022 · 4 comments
Closed

fpm not preprocessing main.f90 with --flag -cpp #791

rouson opened this issue Nov 3, 2022 · 4 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists

Comments

@rouson
Copy link
Contributor

rouson commented Nov 3, 2022

Description

Using fpm build --flag -cpp to compile a simple app/main.f90 containing a C preprocessor directive fails:

$ fpm new fpm-issue
$ cd fpm-issue/app
$ vim main.f90
$ cat main.f90 
program main
#ifdef NONEXISTENT
 use nonexistent
#endif
  implicit none
end program
$ fpm build --flag -cpp
$ <ERROR>*cmd_build*:target error:Unable to find source for module dependency: "nonexistent" used by "app/main.f90"
STOP 1
$ fpm --version
Version:     0.6.0, alpha

Compiling with gfortran -cpp main.f90 works.

Expected Behaviour

I expect --flag -cpp to cause fpm to use gfortran -cpp to compile.

Version of fpm

0.6.0

Platform and Architecture

macOS/Intel

Additional Information

No response

@rouson rouson added the bug Something isn't working label Nov 3, 2022
@awvwgk
Copy link
Member

awvwgk commented Nov 3, 2022

Duplicate of #773 and #469

@awvwgk awvwgk marked this as a duplicate of #773 Nov 3, 2022
@awvwgk awvwgk closed this as completed Nov 3, 2022
@awvwgk awvwgk added the duplicate This issue or pull request already exists label Nov 3, 2022
@urbanjost
Copy link
Contributor

urbanjost commented Nov 3, 2022

It is passing the -cpp flag. It is the fact that when scanning the code for module dependencies it looks for USE statements unconditionally. It has actually not gotten to the compilation. The scan for USE does not know anything about cpp directives.

So it is still a problem, but not quite what you describe. If you had something not being parsed for by fpm itself in the #if/#endif region it would work, for example. So #if... works in most cases that are not conditionally selecting a line being scanned for, like USE and INCLUDE statements would be for fpm(1) to automatically determine dependencies.

It would require preprocessing all the input files and only scanning the files afterward to avoid this problem robustly; and the predefined variables the compiler users are not the same in all cases as those used by a straight call to the preprocessor, which would be a harder issue to hit, but also a problem.

Not an elegant solution, but perhaps if a # is found in a file a missing file could be ignored or only be a warning; which except in the rare case where a file starts with a # character that is a continuation of the previous line this would help with cpp (but not all preprocessors, which do not all start directives with a # in column 1).

If the module does not exist because it is external you are in luck. The USE statement will be ignored
if in your fpm.toml file you have

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

then it is assumed the "nonexistent" module is not built by your package, and you want to have that line
in your fpm.toml file anyway.

Another bug is that only "use " and "use::" are looked for, so if you use "use,non-intrinsic:: nonexistent"
or "use,intrinsic:: nonexistent" it would get to the compiler, but also would not potentially build in the right order if the module really is built by the package; so I would not recommend taking advantage of that.

Same for using macros so the "use " string is not part of the line, which would also trick it.

So hopefully, you really are only conditionally using an external module, and using the correct syntax in the fpm.toml file to indicate it is external correctly resolves the problem.

Preprocessing all the files and putting the output files into the build/ directory would be a big change and would require careful consideration of how the compilers search for include files so I do not think that change would be coming any time soon.

@rouson
Copy link
Contributor Author

rouson commented Nov 7, 2022

@urbanjost when you wrote "fpm.module," was that a typographical error? If so, then I think your suggested workaround will work for us because the dependency in question is not built by our project so we'll try putting external-modules = ["nonexistent"] in our fpm.toml file and see if that works around the issue.

@urbanjost
Copy link
Contributor

Yes. should have been "fpm.toml". I cleaned up the original post a bit; I was composing this while it was being closed and did not realize it showed up. I think the discussion linked to above when this was marked as a duplicate basically says to do the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

3 participants