# Fpm issue with preprocessor-defined use statements

**URL:** <https://fortran-lang.discourse.group/t/fpm-issue-with-preprocessor-defined-use-statements/8371>\
**Category:** fpm\
**Created:** [July 21, 2024, 12:48pm UTC](https://fortran-lang.discourse.group/t/fpm-issue-with-preprocessor-defined-use-statements/8371 "2024-07-21T12:48:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nedanator](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nedanator/32/4416_2.png) [@nedanator](https://fortran-lang.discourse.group/u/nedanator)\
**Post date:** [July 21, 2024, 12:48pm UTC](https://fortran-lang.discourse.group/t/fpm-issue-with-preprocessor-defined-use-statements/8371/1 "2024-07-21T12:48:31Z")

</div>

Hi all.

I am trying to set up something like the following (which works fine in cmake makefiles) using `fpm`. Depending on the preprocessor flag/macro, I want different modules to be imported in the Fortran code.

My main.F90 is:

```auto
program main
#ifdef AAA
  use aaa
#endif
  use fpm_test, only: say_hello
  implicit none

  call say_hello()
end program main

```

My fpm.toml is:

```auto
name = "fpm-test"
version = "0.1.0"
license = "license"
author = "nedanator"
maintainer = "aaa@bbb.ccc"
copyright = "Copyright 2024, nedanator"
[build]
auto-executables = true
auto-tests = true
auto-examples = true
module-naming = false
[install]
library = false
[fortran]
implicit-typing = false
implicit-external = false
source-form = "free"
[preprocess]
[preprocess.cpp]

```

When I use

```auto
fpm build

```

I get the following error:

```auto
<ERROR> *cmd_build* Target error: Unable to find source for module dependency: "aaa" used by "app/main.F90"
STOP 1

```

I feel like I’m missing something obvious, but I haven’t been able to get it to work after a bit of playing around. Adding and removing the following line from the `fpm.toml` seems to do nothing.

```auto
macros = ["AAA=Off"]

```

Is this something with `fpm` needing to look at the files and dependencies before it even runs through the preprocessor, so it looks at all `use` statements (and, hence, the preprocessor can’t doesn’t get a chance to turn it off)?

Thanks in advance for any help.  
Ned

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [July 22, 2024, 1:27pm UTC](https://fortran-lang.discourse.group/t/fpm-issue-with-preprocessor-defined-use-statements/8371/2 "2024-07-22T13:27:17Z")

</div>

> [@nedanator](#):
>
> Is this something with `fpm` needing to look at the files and dependencies before it even runs through the preprocessor, so it looks at all `use` statements (and, hence, the preprocessor can’t doesn’t get a chance to turn it off)?

This is exactly it. `fpm` doesn’t run the preprocessor before looking for use statements to determine the file dependencies. I don’t really know how cmake manages to accomplish this.

---

<div class="post-metadata">

**Author:** ![nedanator](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nedanator/32/4416_2.png) [@nedanator](https://fortran-lang.discourse.group/u/nedanator)\
**Post date:** [July 22, 2024, 1:42pm UTC](https://fortran-lang.discourse.group/t/fpm-issue-with-preprocessor-defined-use-statements/8371/3 "2024-07-22T13:42:24Z")

</div>

Thanks @everythingfunctional. This means I can stop looking at this idea. 🙂

From what I understand, cmake does no work itself in terms of the compilation of the actual Fortran files. So it just calls the compiler with the preprocessor flags and, at which point, the file will have that section removed (if the option is off) before it is passed to the Fortran compiler properly.

I guess, with `fpm`’s easy dependency installation capabilities, I probably don’t actually need this option here, and can just always have it install the dependency `aaa`, regardless of whether or not the code will actually use it (and then I just need to find a way to pass that option flag through `fpm` to get to the compiler preprocessor). That way, it’ll always be able to find that library for the `use` statement, regardless of whether it’ll use it later on.

---

<div class="post-metadata">

**Author:** ![everythingfunctional](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/everythingfunctional/32/176_2.png) [@everythingfunctional](https://fortran-lang.discourse.group/u/everythingfunctional)\
**Post date:** [July 22, 2024, 2:07pm UTC](https://fortran-lang.discourse.group/t/fpm-issue-with-preprocessor-defined-use-statements/8371/4 "2024-07-22T14:07:11Z")

</div>

> [@nedanator](#):
>
> That way, it’ll always be able to find that library for the `use` statement, regardless of whether it’ll use it later on.

That’s what I would suggest, at least for now.

> [@nedanator](#):
>
> From what I understand, cmake does no work itself in terms of the compilation of the actual Fortran files. So it just calls the compiler with the preprocessor flags and, at which point, the file will have that section removed (if the option is off) before it is passed to the Fortran compiler properly.

But then how does it know which order to compile the files? You cannot compile a file that `use`s a module before the file that contains the module. Surely it does something to respect that dependency, but maybe I’m assuming too much.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [July 22, 2024, 2:12pm UTC](https://fortran-lang.discourse.group/t/fpm-issue-with-preprocessor-defined-use-statements/8371/5 "2024-07-22T14:12:49Z")

</div>

> [@everythingfunctional](#):
>
> But then how does it know which order to compile the files? You cannot compile a file that `use`s a module before the file that contains the module. Surely it does something to respect that dependency, but maybe I’m assuming too much.

CMake has its own module dependency scanner. You can find more information in a previous post: [CMake FortranCInterface - #8 by ivanpribec](https://fortran-lang.discourse.group/t/cmake-fortrancinterface/7573/8)

---

<div class="post-metadata">

**Author:** ![nedanator](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nedanator/32/4416_2.png) [@nedanator](https://fortran-lang.discourse.group/u/nedanator)\
**Post date:** [July 22, 2024, 3:19pm UTC](https://fortran-lang.discourse.group/t/fpm-issue-with-preprocessor-defined-use-statements/8371/6 "2024-07-22T15:19:59Z")

</div>

> [@everythingfunctional](#):
>
> But then how does it know which order to compile the files? You cannot compile a file that `use`s a module before the file that contains the module. Surely it does something to respect that dependency, but maybe I’m assuming too much.

The `use` statement was for importing an external library. So it would be using linking flags in the compiler command to link to something already built that didn’t have dependencies the other way.

---

<div class="post-metadata">

**Author:** ![nedanator](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/nedanator/32/4416_2.png) [@nedanator](https://fortran-lang.discourse.group/u/nedanator)\
**Post date:** [July 22, 2024, 3:20pm UTC](https://fortran-lang.discourse.group/t/fpm-issue-with-preprocessor-defined-use-statements/8371/7 "2024-07-22T15:20:16Z")

</div>

Oh, that’s cool. Thanks @ivanpribec, I didn’t know that! 😃

---

<div class="post-metadata">

**Author:** ![urbanjost](https://avatars.discourse-cdn.com/v4/letter/u/0ea827/32.png) [@urbanjost](https://fortran-lang.discourse.group/u/urbanjost)\
**Post date:** [July 22, 2024, 3:26pm UTC](https://fortran-lang.discourse.group/t/fpm-issue-with-preprocessor-defined-use-statements/8371/8 "2024-07-22T15:26:15Z")

</div>

See also:

[Can you make fpm ignore a USE statement inside a preprocessor #ifdef #endif block · Issue #773 · fortran-lang/fpm · GitHub](https://github.com/fortran-lang/fpm/issues/773)

until resolved in a more robust way, perhaps a note on how to work around this should be added to the documentation.
