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
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.
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.
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.
Still present at r206227.
*** Bug 62162 has been marked as a duplicate of this bug. ***
Still present in gcc 12.2.1 This bug still creates problems for meson's fortran support. Please fix.