Bug 47485 - gfortran -M output is incorrect when -MT option is used
Summary: gfortran -M output is incorrect when -MT option is used
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 62162 (view as bug list)
Depends on:
Blocks:
 
Reported: 2011-01-27 06:58 UTC by Thomas Henlich
Modified: 2023-04-27 14:36 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-12-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Henlich 2011-01-27 06:58:10 UTC
When "-MT target" (Change the target of the rule emitted by dependency generation) is specified, the specified target is added as an additional target instead of replacing the default one.

Example test.for:
      program test
      print "(a)", "hello world"
      end program

$ gfortran -cpp -M test.for -MT obj/test.o
test.o obj/test.o: test.for

Expected output:
obj/test.o: test.for
Comment 1 Tobias Burnus 2011-01-27 08:42:07 UTC
The -MT version is added via:

cpp.c's gfc_cpp_init:
      else if (opt->code == OPT_MT || opt->code == OPT_MQ)
        deps_add_target (cpp_get_deps (cpp_in),
                         opt->arg, opt->code == OPT_MQ);

which matches C's handle_deferred_opts in c-family/c-opts.c.
Comment 2 Tobias Burnus 2011-01-30 16:44:58 UTC
The first target - the one which should not appear - is set via

fortran/cpp.c's gfc_cpp_init_0 -> libcpp's cpp_read_main_file; the latter contains:
  if (CPP_OPTION (pfile, deps.style) != DEPS_NONE)
      deps_add_default_target (pfile->deps, fname);

For "gcc" this is called, but "d->ntargets" == 1; in case of gfortran it is 0 and thus the extra "<source>.o" is added.

The d->ntargets is incremented via "deps_add_target".

Thus, the problem seems to be that deps_add_target (for -MT) should be called before libcpp's cpp_read_main_file.
Comment 3 nightstrike 2011-10-18 16:29:43 UTC
I have just now seen this problem for c and not fortran for GCC 4.1.2.  It appears fixed in 4.6.0.  I searched the bug database, and found this:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12448

It looks like the two bugs are related, although I can confirm that the bug still exists for gfortran 4.6.0 using the test case provided in this PR:

$ cat > f.f90
program test
print "(a)", "hello world"
end program

$ gfortran -cpp -M f.f90 -MT aaa
f.o aaa: f.f90


So at the very least, can someone set the bug status to NEW (confirmed) and the See Also to PR12448?

Also, when fixing this, whoever does so, be sure to not run into this snag:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=44076

That should be on the See Also, as well.
Comment 4 Dominique d'Humieres 2013-12-29 13:38:12 UTC
Still present at r206227.
Comment 5 Dominique d'Humieres 2014-08-16 22:42:56 UTC
*** Bug 62162 has been marked as a duplicate of this bug. ***
Comment 6 Volker Weißmann 2023-04-27 14:36:25 UTC
Still present in gcc 12.2.1

This bug still creates problems for meson's fortran support.

Please fix.