Skip to content

Fortran compile fails with module dependencies #4177

Closed

Description

@james-thunes

Describe the bug
scons does not appear to be correctly find dependencies on *.mod files in Fortran files.

My project includes a Fortran library that includes a number of source files and a module including some shared variables. When compiling on windows with ifort, I get the following error when doing a clean build:
error #7002: Error in opening the compiled module file. Check INCLUDE paths.
Subsequent builds complete successfully.

Investigation shows that the above compiler error is seen because the *.mod file required by the source files is not built before the source file compilation. Reordering the list of source files improves the situation, but it still fails periodically when building in parallel. Subsequent attempts to compile work as the *.mod file is created during the first scons attempt and are thus already available.

Per suggestion from discord, I ran scons with --tree=prune to check if the consumers of the module correctly listed the *.mod file as a build dependency. The output below (anonymized) shows the output for one of the source files:

  |   +-<path>\<objName>.staticrt.obj
  |   | +-<relativePath>\<srcFile>.f
  |   | +-C:\Program Files (x86)\IntelSWTools\compilers_and_libraries_2019.5.281\windows\bin\intel64\ifort.EXE

Note that the source file does not list the *.mod file as a dependency.

The project is cross-platform so I also checked the result of --tree=prune on linux (compiled with gfortran). Scons also did not recognize the dependency on the mod file (output was the same as above with the exception of the compiler path). However, the code compiles without issue on linux.

Since the above shows that the source files are not identifying the *.mod file as a build dependency, my assumption is that there is some issue in the way that scons is determining dependencies for Fortran files. I'm not sure why there's not an issue with gfortran on linux.

Required information

  • Link to SCons Users thread discussing your issue.
    discussed on the scons discord (fortran-support channel)
  • Version of SCons
    3.1.2
  • Version of Python
    3.7.13
  • Which python distribution if applicable (python.org, cygwin, anaconda, macports, brew,etc)
    cpython
  • How you installed SCons
    pip install
  • What Platform are you on? (Linux/Windows and which version)
    Windows 10 Enterprise version1909
  • How to reproduce your issue? Please include a small self contained reproducer. Likely a SConstruct should do for most issues.
  • How you invoke scons (The command line you're using "scons --flags some_arguments")
    scons -j8

Activity

  1. james-thunes commented on Jun 15, 2022

    @james-thunes
    Author

    I haven't been able to get a small test case set up yet. I will try to create one and post to this issue

  2. dnwillia-work commented on Jun 15, 2022

    @dnwillia-work

    I'll just add that when I was testing this locally it seems to only be reproducible when running a parallel build with -jN. A serial build would do things in the right order and I never saw that fail, but for parallel it's failing when a dependent source file is compiled concurrently before the module is complete.

  3. bdbaddog commented on Jun 15, 2022

    @bdbaddog
    Contributor

    I'll just add that when I was testing this locally it seems to only be reproducible when running a parallel build with -jN. A serial build would do things in the right order and I never saw that fail, but for parallel it's failing when a dependent source file is compiled concurrently before the module is complete.

    That's just luck... If parallel builds fail due to missing dependencies (for any build system), it means the build system doesn't have all the appropriate dependencies and so isn't building everything needed for the failing build step. I run into this often (though mainly with non-scons based build systems where it's harder to get this right)..

  4. dnwillia-work commented on Jun 15, 2022

    @dnwillia-work

    Yeah, fair enough. Even for the parallel builds I'll add that it would sometimes work, sometimes not.

    I wonder since it seems to be a windows specific thing then is something else going on. eg: the .mod file is still locked by the OS so the dependent module cannot link against it. We have run into this kind of issue with .lib (import library) files on windows parallel builds before as well if an Install() was used to move the .lib file to some other place before it was linked into dependent module despite the fact that we would add Depends() on the install task to the dependent module. We removed the use of Install() targets for this as a result and it got better.

  5. bdbaddog commented on Jun 15, 2022

    @bdbaddog
    Contributor

    Ugh.. the joys of NTFS..

  6. bdbaddog commented on Jun 15, 2022

    @bdbaddog
    Contributor

    How are you creating your Environment()? Specifically are you setting FORTRANSUFFIXES (or any of the F*SUFFIXES env vars)?

  7. bdbaddog commented on Jun 15, 2022

    @bdbaddog
    Contributor

    Try adding this to your Environment()

    FORTRANFILESUFFIXES=['.f90'],

  8. james-thunes commented on Jun 15, 2022

    @james-thunes
    Author

    this particular library has all *.f files, but I can try setting FORTRANFILESUFFIXES=['.f'],

  9. dnwillia-work commented on Jun 15, 2022

    @dnwillia-work

    How are you creating your Environment()? Specifically are you setting FORTRANSUFFIXES (or any of the F*SUFFIXES env vars)?

    We definitely do not configure that particular variable currently. We do configure these variables related to this issue

        env["FORTRANMODDIR"] = "${TARGET.dir}"
        env["F90PATH"] = "${TARGET.dir}"
    

    It's the same for ifort and gfortran.

  10. mwichmann commented on Jun 17, 2022

    @mwichmann
    Collaborator

    Okay, I've located a simple example and don't see the problem with it. Here's the tree dump:

    +-.
      +-SConstruct
      +-list-module.f90
      +-list-module.o
      | +-list-module.f90
      | +-/bin/gfortran
      +-listmodule.mod
      | +-list-module.f90
      | +-/bin/gfortran
      +-main-list
      | +-main-list.o
      | | +-main-list.f90
      | | +-[listmodule.mod]
      | | +-/bin/gfortran
      | +-[list-module.o]
      | +-/bin/gfortran
      +-main-list.f90
      +-[main-list.o]

    main-list.o is shown as depending on its source file, on listmodule.mod, and on the compiler. That looks right to me? What am I missing?

  11. bdbaddog commented on Jun 17, 2022

    @bdbaddog
    Contributor

    @mwichmann - I think this is only an intel fortran issue. And I think it's due to loading default tools, then ifort and having files named .f when ifort looks like it only wants files named .i or .i90?

  12. bdbaddog commented on Jun 17, 2022

    @bdbaddog
    Contributor

    this particular library has all *.f files, but I can try setting FORTRANFILESUFFIXES=['.f'],

    Did that work?

  13. mwichmann commented on Jun 17, 2022

    @mwichmann
    Collaborator

    I still have to look at the .i/.i90 thing, I think it should work just find on ordinary suffixes and I don't know what those actually mean.

  14. james-thunes commented on Jun 17, 2022

    @james-thunes
    Author

    this particular library has all *.f files, but I can try setting FORTRANFILESUFFIXES=['.f'],

    Did that work?

    Sorry, been busy with other tasks. Will try to get to this as soon as I can.

  15. james-thunes commented on Jun 20, 2022

    @james-thunes
    Author

    this particular library has all *.f files, but I can try setting FORTRANFILESUFFIXES=['.f'],

    Did that work?

    Ok, I was able to spend a bit of time with this today. I'm a bit confused to be honest. Setting FORTRANFILESUFFIXES=['.f'], did indeed appear to resolve the issue. I was able to see the module dependencies when looking at the output with --tree=prune. However, removing the flag and doing a clean rebuild of the code I see that the module dependencies is still seen. It definitely wasn't last week...

  16. 45 remaining items

  17. mwichmann commented on Jun 30, 2023

    @mwichmann
    Collaborator

    Okay, a little more light on the non-copying variantdir: sources are absolute paths in this case, but targets shouldn't be so they can be handled by the variant code. But the module that will be generated, as detected by the emitter (not via the scanner, btw, but by using a regex that's supposed to stay in sync with the one in the scanner), has the absolute-path form.

    Fortran emitter: module_one.f90 -> module_one.o module_one.mod
    Fortran emitter: module_two.f90 -> module_two.o module_two.mod
    Fortran emitter: main_prog.f -> main_prog.o
    ...
    Fortran emitter: /home/mats/github/scons/exp/fort-subdir/SConsTests/TestSConscript/module_one.f90 -> module_one.o /home/mats/github/scons/exp/fort-subdir/SConsTests/TestSConscript/module_one.mod
    Fortran emitter: /home/mats/github/scons/exp/fort-subdir/SConsTests/TestSConscript/module_two.f90 -> module_two.o /home/mats/github/scons/exp/fort-subdir/SConsTests/TestSConscript/module_two.mod
    Fortran emitter: /home/mats/github/scons/exp/fort-subdir/SConsTests/TestSConscript/main_prog.f -> main_prog.o
    

    So I presume that this snip from FortranCommon is the wrong way to generate the target node(s) for modules:

        for module in modules:                                                     
            target.append(env.fs.File(module, moddir))
  18. mwichmann commented on Jun 30, 2023

    @mwichmann
    Collaborator

    If it helps to be explicit - it's not just that the module paths here are absolute, it's that they're to what would be considered the "source directory", as in:

    absolute-path-to-topdir/TestSConscript/module_one.mod

    the module file will never appear there... it will either be in the topdir if there's no FORTRANMODDIR, or in the build directory specified by the variantdir if it is set as in the sample.

  19. mwichmann commented on Jul 2, 2023

    @mwichmann
    Collaborator

    Okay, I'm just repeating stuff that's already been said, but module handling seems pretty broken. I set up another small test with a trivial module source file, repeated four times (uniqued for each version):

    1. source in top-level directory
    2. source in src subdirectory with own SConscript
    3. source in vsrc subdirectory, SConscript called as duplicating variantdir build
    4. source in vsrc-no subdirectory, SConscript called as non-duplicating variantdir build-no

    Calling Object() on each, with no setting of FORTRANMODDIR, so the default empty string, which won't expand to any -J option to gfortran, you expect to get four object files and four .mod files, and so we do:

    build-no/test4.o
    build/test3.o
    mod_foo1.mod
    mod_foo2.mod
    mod_foo3.mod
    mod_foo4.mod
    src/test2.o
    test1.o
    

    But that doesn't match what SCons thinks it did, which means we have a problem (the tree is trimmed to show only these eight):

    +-.
      +-build
      | +-build/mod_foo3.mod
      | +-build/test3.o
      +-build-no
      | +-build-no/mod_foo4.mod
      | +-build-no/test4.o
      +-mod_foo1.mod
      +-src
      | +-src/mod_foo2.mod
      | +-src/test2.o
      +-test1.o
    

    It's never good when SCons builds up a picture of what exists, and then that's not what the compiler did. The simplest problem from that is you can't clean, but there's plenty more issues, like rebuilds being horked, etc.

  20. mwichmann commented on Jul 2, 2023

    @mwichmann
    Collaborator

    Now, if we redo the above with the experimental setting of FORTRANMODDIR, so it's force-passed on every build, we at least get consistency:

    f95 -o build/test3.o -c -Jbuild build/test3.f
    f95 -o build-no/test4.o -c -Jbuild-no vsrc-no/test4.f
    f95 -o test1.o -c -J. test1.f
    f95 -o src/test2.o -c -Jsrc src/test2.f
    

    Giving us:

    build/mod_foo3.mod
    build/test3.o
    build-no/mod_foo4.mod
    build-no/test4.o
    mod_foo1.mod
    src/mod_foo2.mod
    src/test2.o
    test1.o
    

    And the tree at least now matches what was built:

    +-.
      +-build
      | +-build/mod_foo3.mod
      | +-build/test3.o
      +-build-no
      | +-build-no/mod_foo4.mod
      | +-build-no/test4.o
      +-mod_foo1.mod
      +-src
      | +-src/mod_foo2.mod
      | +-src/test2.o
      +-test1.o
    

    But that hasn't got into the other case of trying to USE these modules, and whether they're found where they got put so other sources will build.

  21. dnwillia commented on Jul 2, 2023

    @dnwillia

    Yup. At least you can reproduce what I’m also seeing.

  22. mwichmann commented on Jul 2, 2023

    @mwichmann
    Collaborator

    I thought I had a cute fix for the "if there's no FORTRANMODDIR, SCons is out of sync with what the compiler does" problem, but it turns out an extra bit of variantdir cleverness defeats that in one case. Still thinking. This was the idea:

        for module in modules:
            if not moddir:
               target.append(env.fs.File(module, directory='#'))
            else:
               target.append(env.fs.File(module, directory=moddir))
  23. mwichmann commented on Jul 3, 2023

    @mwichmann
    Collaborator

    So the odd bit is recorded, it seems, if you set a relative path for FORTRANMODDIR, and use a non-duplicating variantdir, scons will double the flag usage, emitting one module dir for the target (variant) directory and one for the original source directory, and that's illegal. Like

    env = Environment(FORTRANMODDIR="modules")

    which leads to:

    gfortran -o build-no/test4.o -c -Jbuild-no/modules -Jvsrc-no/modules vsrc-no/test4.f
    Fatal Error: gfortran: Only one '-J' option allowed
    compilation terminated.
    

    I don't think I've caused this....

    Leaving it out (the default is an empty string), or using a top-relative path ("#/modules") does not cause this effect.

  24. mwichmann commented on Jul 4, 2023

    @mwichmann
    Collaborator

    If a relative path is used, then RDirs, the function passed in the _concat call, is the one that makes this mess.

    So this:

    env['_FORTRANMODFLAG'] = '$( ${_concat(FORTRANMODDIRPREFIX, FORTRANMODDIR, FORTRANMODDIRSUFFIX, __env__, RDirs, TARGET, SOURCE)} $)'

    has the prospect of producing something illegal, with Fortran compilers not accepting multiple module-directory options. I think it's better not to apply this particular transformation (i.e., leave out RDirs from the generation of this flag.)

  25. mwichmann commented on Jul 5, 2023

    @mwichmann
    Collaborator

    Hoo boy, this simple little matter might end up being a hair-loss event...

  26. mwichmann commented on Jul 10, 2023

    @mwichmann
    Collaborator

    On a separate note, when the revised gfortran.py from the test branch sets up env["FORTRANMODDIR"] = "${TARGET.dir}", that doesn't get expanded as expected in the emitter when it's called, the emitter does this:

    moddir = env.subst('$FORTRANMODDIR', target=target, source=source)

    and always comes back here with moddir set to .; the expansion works right when the command line is actually generated as can be seen above.

    Okay, this conclusion was just bogus. At SConscript evaluation time, the context is the directory of the SConscript, so it makes sense for the subst here to come back with .. Ignore me...

  27. dnwillia commented on Dec 30, 2024

    @dnwillia

    OK so an update to this finally... Today I tried out all my tests posted at

    https://github.com/dnwillia/SConsTests

    and everything is building properly at release v4.8.1 on MacOS using gfortran. Great progress.

    I'll give it a shot with Linux/windows with both Intel (Windows/Linux) and GNU fortran (Linux) next week. @james-thunes

  28. james-thunes commented on Dec 31, 2024

    @james-thunes
    Author

    great to hear. Thanks for giving it another try. And thanks to the developers for addressing :)

  29. dnwillia commented on Jan 9, 2025

    @dnwillia

    OK so I would say I can confirm that on Linux and MacOS that the code in my test repo compiles with v4.8.1 out of the box with gfortran on Linux and MacOS. It's also working on windows up to the linker step, where linking fails, but that's a separate issue from this one.

    @james-thunes Maybe you can close this issue unless you still see problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Fortranscanner

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions