# Module dependencies

**URL:** <https://fortran-lang.discourse.group/t/module-dependencies/1194>\
**Category:** Uncategorized\
**Created:** [May 9, 2021, 10:03pm UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194 "2021-05-09T22:03:56Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [May 9, 2021, 10:03pm UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/1 "2021-05-09T22:03:56Z")

</div>

Do people have tools to update make files (or another build system) when a new module is to be USEd in a program? That module may depend on other source files, so its dependencies need to be added in the proper order.

For example, I can create an executable with

`gfortran kind.f90 first_last_true.f90 print_array.f90 error.f90 get_unit.f90 cholesky.f90 util.f90 correlation.f90 statistics.f90 random_normal.f90 random_mv_normal.f90 acf.f90 var_sim.f90 pythag.f90 svbksb.f90 svdcmp.f90 reg_stats.f90 var_fit.f90 xvar_fit_print.f90`

and also with

`gfortran kind.f90 statistics.f90 qsort.f90 date_mdy.f90 util.f90 qsort_good.f90 open_file.f90 get_unit.f90 replace_string.f90 error.f90 read_mdy_alloc.f90 data_frame_full.f90 data_frame_full_stats.f90 xvar_data_frame.f90`

The file `var_fit.f90` contains a module `var_fit_mod` that I want to use in `xvar_data_frame.f90`, so the question is which files in addition to `var_fit.f90` do I need to add to the second compilation command above. A script tells me that the files in the first compilation list but not the second are

`first_last_true.f90 print_array.f90 cholesky.f90 correlation.f90 random_normal.f90 random_mv_normal.f90 acf.f90 var_sim.f90 pythag.f90 svbksb.f90 svdcmp.f90 reg_stats.f90 var_fit.f90 xvar_fit_print.f90`

I need to add a subset of this list of files to the second compilation list, but which ones and in what order? I created a main program `dummy_var_fit.f90` that is just

```
use var_fit_mod
end

```

and ascertained that

`gfortran kind.f90 error.f90 util.f90 print_array.f90 first_last_true.f90 acf.f90 svbksb.f90 pythag.f90 svdcmp.f90 reg_stats.f90 var_fit.f90 dummy_var_fit.f90`

compiles. That narrows the list of files I need to add to

`print_array.f90 first_last_true.f90 acf.f90 svbksb.f90 pythag.f90 svdcmp.f90 reg_stats.f90 var_fit.f90 dummy_var_fit.f90`.

After some experimentation I find that

`gfortran kind.f90 print_array.f90 first_last_true.f90 acf.f90 svbksb.f90 pythag.f90 svdcmp.f90 statistics.f90 qsort.f90 date_mdy.f90 util.f90 qsort_good.f90 open_file.f90 get_unit.f90 replace_string.f90 error.f90 read_mdy_alloc.f90 reg_stats.f90 data_frame_full.f90 data_frame_full_stats.f90 var_fit.f90 xvar_data_frame.f90`

compiles. Creating dummy main programs and associated make files for each module can clarify what dependencies are, but I would like to automate the process of creating compilation comands.

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [May 10, 2021, 1:43am UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/2 "2021-05-10T01:43:56Z")

</div>

Once `fpm` can generate Makefile and CMake build system, you can just rerun `fpm` and it would update your make files. There are also other tools that can generate the dependencies in a makefile form.

---

<div class="post-metadata">

**Author:** ![Euler-37](https://avatars.discourse-cdn.com/v4/letter/e/b19c9b/32.png) [@Euler-37](https://fortran-lang.discourse.group/u/Euler-37)\
**Post date:** [May 10, 2021, 2:40am UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/3 "2021-05-10T02:40:16Z")

</div>

`makedepf90` is a choice. [makedepf90(1) - Linux man page (die.net)](https://linux.die.net/man/1/makedepf90)

---

<div class="post-metadata">

**Author:** ![certik](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/certik/32/4_2.png) [@certik](https://fortran-lang.discourse.group/u/certik)\
**Post date:** [May 10, 2021, 4:15am UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/4 "2021-05-10T04:15:48Z")

</div>

Thank you @Euler-37 for the tip and welcome!

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [May 10, 2021, 8:35am UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/5 "2021-05-10T08:35:38Z")

</div>

I grabbed a version from [GitHub - outpaddling/makedepf90: Generate make dependencies for Fortran code](https://github.com/outpaddling/makedepf90)

---

<div class="post-metadata">

**Author:** ![epagone](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/epagone/32/252_2.png) [@epagone](https://fortran-lang.discourse.group/u/epagone)\
**Post date:** [May 10, 2021, 10:37am UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/6 "2021-05-10T10:37:11Z")

</div>

I’m not sure if it’s suitable for your needs, but **long** time ago, before switching to SCons and (then) CMake, I used to use [mkmf](https://extranet.gfdl.noaa.gov/~vb/mkmf.html). It might worth checking it out until fpm matures (as suggested).

---

<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:** [May 11, 2021, 12:15am UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/8 "2021-05-11T00:15:33Z")

</div>

I once worked with the developer of [fake](https://sourceforge.net/projects/fortranmake/). I wasn’t a fan, but it might suit your needs well.

---

<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:** [May 11, 2021, 10:43am UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/9 "2021-05-11T10:43:31Z")

</div>

> [@Beliavsky](#):
>
> but I would like to automate the process of creating compilation comands.

Sounds like what `fpm` is for.

Here are a few more relevant threads and tutorials:

- [49149 – Dependency autogeneration with `-M` rendered useless by requiring .mod files](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49149)
- [Writing Makefiles for Modern Fortran](https://aoterodelaroza.github.io/devnotes/modern-fortran-makefiles/) (see section “Automatic dependency generation”)
- [Automatically generated dependencies](https://fortran-lang.org/learn/building_programs/project_make#automatically-generated-dependencies)(in [Building programs](https://fortran-lang.org/learn/building_programs) Minibook at [fortran-lang.org](http://fortran-lang.org))

The Fortran Wiki also has a [list of tools](http://fortranwiki.org/fortran/show/Build+tools). I believe some of these tools probably haven’t kept up with language features like block statements and submodules. The Fortran Wiki list highlights one of the problems of Fortran, which is the amount of reinventing the wheel that goes on. If we could join forces and build a robust tool that given a list of source files outputs module dependencies in a chosen format, it could benefit all build systems.

The [Ninja](https://ninja-build.org/) build sytem manual contains a short section about [Fortran modules](https://ninja-build.org/manual.html#_fortran_modules), and introduces a _“hypothetical `fscan` tool [that] scans the source files”_ and writes an output file with the dynamic dependency information.

I guess all of the tools actually follow the same logic, which would be nice to summarize in pseudo-code:

```plaintext
Input: List of Fortran source files
Output: Fortran dependency graph

For File in List of Source Files:
  Process(File)...

```

If we could agree on a good notation for the dependency graph, than any existing or future build tool could use it. The logic `fpm` uses to resolve dependencies currently is contained in [`fpm_targets.f90`](https://github.com/fortran-lang/fpm/blob/master/src/fpm_targets.f90#L329), while the derived type describing source files are defined in [`fpm_model.f90`](https://github.com/fortran-lang/fpm/blob/master/src/fpm_model.f90). The actual parsing is defined in [`fpm_source_parsing.f90`](https://github.com/fortran-lang/fpm/blob/master/src/fpm_source_parsing.f90#L74).

It would be great to discuss what are the possible _caveats_ in building a module dependency graph (cyclic dependencies, preprocessing,…) and how to resolve each of them?

Another important question for a scalable tool is how to update the dependency graph given a previous version, and a list of modified files? The author of the [tup](http://gittup.org/tup/index.html) build system, wrote an interesting document about [Build System Rules and Algorithms](http://gittup.org/tup/build_system_rules_and_algorithms.pdf) which I believe are relevant also to the design of `fpm`.

---

<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:** [May 28, 2021, 2:35pm UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/10 "2021-05-28T14:35:37Z")

</div>

Browsing through the C++ 2020 proposals I discovered:

> [Format for describing dependencies of source files](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p1689r1.html)

Unsurprisingly the authors are from Kitware (company behind CMake).

Maybe it is worth taking a look at to get some ideas for having fpm emit such a dependency file and in combination with a Universal Fortran compiler interface.

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [July 1, 2021, 1:25pm UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/11 "2021-07-01T13:25:56Z")

</div>

I see that Intel Fortran has a `-gen-dep` option to generate dependency information. For module files it shows the source file dependencies, and for object files it shows the source file and module file dependencies. Here is an example of its use:

```auto
c:\fortran\2015>ifort -nologo -gen-dep kind.f90 qsort.f90 date_mdy.f90 util.f90 qsort_good.f90 open_file.f90 get_unit.f90 replace_string.f90 error.f90 read_mdy_alloc.f90 data_frame_full.f90 xxdata_frame_full.f90
kind_mod.mod : \
  kind.f90

kind.obj : \
  kind.f90

qsort_mod.mod : \
  qsort.f90

qsort.obj : \
  qsort.f90 kind_mod.mod

date_mdy_mod.mod : \
  date_mdy.f90

date_mdy.obj : \
  date_mdy.f90

util.f90(4962): remark #8291: Recommended relationship between field width 'W' and the number of fractional digits 'D' in this edit descriptor is 'W>=D+7'.
   write (outu_,"('#>=thresh, total, thresh, abs_min_ =',2(1x,i0),g12.6)") count(xx(:,1) > abs_min_),n,abs_min_
-------------------------------------------------------------------^
util_mod.mod : \
  util.f90

util.obj : \
  util.f90 kind_mod.mod

qsort_good_mod.mod : \
  qsort_good.f90

qsort_good.obj : \
  qsort_good.f90 qsort_mod.mod util_mod.mod kind_mod.mod

open_file_mod.mod : \
  open_file.f90

open_file.obj : \
  open_file.f90

get_unit_mod.mod : \
  get_unit.f90

get_unit.obj : \
  get_unit.f90

replace_string_mod.mod : \
  replace_string.f90

replace_string.obj : \
  replace_string.f90

error_mod.mod : \
  error.f90

error.obj : \
  error.f90

read_mdy_alloc_mod.mod : \
  read_mdy_alloc.f90

read_mdy_alloc.obj : \
  read_mdy_alloc.f90 error_mod.mod util_mod.mod replace_string_mod.mod open_file_mod.mod \
  kind_mod.mod get_unit_mod.mod date_mdy_mod.mod

data_frame_full_mod.mod : \
  data_frame_full.f90

data_frame_full.obj : \
  data_frame_full.f90 get_unit_mod.mod util_mod.mod kind_mod.mod

xxdata_frame_full.obj : \
  xxdata_frame_full.f90 data_frame_full_mod.mod

```

---

<div class="post-metadata">

**Author:** ![Beliavsky](https://avatars.discourse-cdn.com/v4/letter/b/ba8739/32.png) [@Beliavsky](https://fortran-lang.discourse.group/u/Beliavsky)\
**Post date:** [July 5, 2021, 3:52pm UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/12 "2021-07-05T15:52:49Z")

</div>

The Python tool [run-fortran](https://github.com/lycantropos/run-fortran) works on some programs I tried, except for codes that use intrinsic modules with statements such as `use iso_fortran_env`. Sample usage is just

`python3 path_to_script\run-fortran.py run`

in the directory of the Fortran source files. It lists the files in a proper order for compilation.

---

<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 8, 2021, 1:32pm UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/13 "2021-07-08T13:32:02Z")

</div>

I’ve found one more tool which is not listed on the Fortran Wiki pages:

> [fdep](https://gitlab.mpcdf.mpg.de/elpa/elpa/-/tree/master/fdep) is a small set of scripts to teach autoconf/automake (using GNU make)  
> about the additional dependencies in Fortran 90 files due to modules.

It appears to be developed within the [ELPA](https://elpa.mpcdf.mpg.de/#) (Eigenvalue soLvers for Petaflop Applications) project.

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [July 8, 2021, 9:20pm UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/14 "2021-07-08T21:20:29Z")

</div>

> **[The DOT Language](https://graphviz.org/doc/info/lang.html)**
>
> The following is an abstract grammar defining the DOT language. Terminals are shown in bold font and nonterminals in italics. Literal characters are given in single quotes. Parentheses ( and ) indicate grouping when needed. Square brackets \[and \]...

Every compiler should have a mode for producing dependency information. The traditional format to describe graphs is a “dot” file. It seems fairly simple to derive a to-be-included make fragment from this.

---

<div class="post-metadata">

**Author:** ![vmagnin](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/vmagnin/32/28_2.png) [@vmagnin](https://fortran-lang.discourse.group/u/vmagnin)\
**Post date:** [March 18, 2022, 9:00am UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/15 "2022-03-18T09:00:17Z")

</div>

> [@Beliavsky](#):
>
> I see that Intel Fortran has a `-gen-dep` option to generate dependency information. For module files it shows the source file dependencies, and for object files it shows the source file and module file dependencies.

Thanks for that information.

By default, dependencies are printed on standard output but you can also specify a filename. And you can also list the intrinsic modules with `-gen-depshow=intr`. For example:

```bash
$ ifx -gen-dep=dependencies.txt -gen-depshow=intr main.f90

```

Strangely, if you use several times that option, the output is appended to the text file, instead of being overwritten.

---

<div class="post-metadata">

**Author:** ![gnikit](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/gnikit/32/1213_2.png) [@gnikit](https://fortran-lang.discourse.group/u/gnikit)\
**Post date:** [November 29, 2022, 12:14pm UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/16 "2022-11-29T12:14:59Z")

</div>

I am looking for something similar to initialise the linter of VS Code. When fpm is fully supported we can just run `fpm build` with some additional settings, but for the time being I am looking for something that can give me a list with the order the compilation is supposed to happen, the main limitations is that it needs to be cross platform and easy to install.

I have been eyeing @zedthree’s [GitHub - ZedThree/fort\_depend.py: A python script to generate dependencies for Fortran projects](https://github.com/ZedThree/fort_depend.py) although I would have to heavily modify it for be usable by vscode. Is there anything else we could potentially use?

---

<div class="post-metadata">

**Author:** ![zedthree](https://avatars.discourse-cdn.com/v4/letter/z/f4b2a3/32.png) [@zedthree](https://fortran-lang.discourse.group/u/zedthree)\
**Post date:** [November 29, 2022, 1:10pm UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/17 "2022-11-29T13:10:22Z")

</div>

CMake has a built-in dependency tree generator. I’m not sure how easy it is to expose that information directly though

---

<div class="post-metadata">

**Author:** ![PaulM](https://avatars.discourse-cdn.com/v4/letter/p/839c29/32.png) [@PaulM](https://fortran-lang.discourse.group/u/PaulM)\
**Post date:** [February 13, 2023, 7:44pm UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/18 "2023-02-13T19:44:13Z")

</div>

A tool for obtaining file dependency information is FF08Depends.  
It is available for download at:

[https://www.megms.com.au/ff08depends.htm](https://www.megms.com.au/ff08depends.htm)

\<Quote  
FF08Depends is a command line tool for obtaining file dependency information for Fortran 2008 source files.  
The tool itself is written in Fortran 2003. A zip archive of the latest source release (revision 2960 from 2020-04-17), available under an Apache 2.0 license, can be found at [www.megms.com.au/download/FF08Depends.zip](http://www.megms.com.au/download/FF08Depends.zip).

-t or --tree:  
Display the dependency tree rather than the dependency order. The optional argument specifies the format for the tree. The default format is make, which is a format that is suitable for many “ **make** ” programs. The other available format is dot, which specifies a format suitable as input into the dot graph tool (graphviz).  
/\>

The announcement post is here:

> **[Dependency analysis of mild insanity in Fortran](https://community.intel.com/t5/Intel-Fortran-Compiler/Dependency-analysis-of-mild-insanity-in-Fortran/m-p/919119)**
>
> For those who want to have a bit of a giggle... A while back, as a bit of a F2003 learning exercise I started writing a little Fortran parser in Fortran that lets you do dependency analysis - "tell me what order I should compile my files".  If...

FF08Depends author is IanH:

> **[User IanH](https://stackoverflow.com/users/1234550/ianh)**
>
> Stack Overflow | The World’s Largest Online Community for Developers

The archive contains other utilities written in Fortran, in particular a Generic command line parsing module (CmdLine.f90)

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [February 14, 2023, 6:21am UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/19 "2023-02-14T06:21:41Z")

</div>

This is a very useful tool. Thank you for sharing. Do you know if it can also detect and analyze included files via processor `#include`?

---

<div class="post-metadata">

**Author:** ![shahmoradi](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/shahmoradi/32/3151_2.png) [@shahmoradi](https://fortran-lang.discourse.group/u/shahmoradi)\
**Post date:** [February 14, 2023, 6:55am UTC](https://fortran-lang.discourse.group/t/module-dependencies/1194/20 "2023-02-14T06:55:21Z")

</div>

The answer to my question is given in the Intel forum link you shared.

> But if you are doing anything outside the remit of the standard (like using the C preprocessor to #define or #include stuff) then the utility will probably just get confused. Note the qualifiers “should”, “perhaps”, etc are all in there for good reasons - this might also just waste your time.
