[flang-commits] [clang] [flang] [flang][driver] Support Makefile dependency generation (PR #209379)
Thirumalai Shaktivel via flang-commits
flang-commits at lists.llvm.org
Tue Jul 21 04:54:30 PDT 2026
================
@@ -236,6 +236,21 @@ is `ParseSyntaxOnlyAction`, which corresponds to `-fsyntax-only`. In other
words, `flang -fc1 <input-file>` is equivalent to `flang -fc1 -fsyntax-only
<input-file>`.
+## Dependency File Generation
+Flang can emit Makefile-style dependency rules with `-M`, `-MM`, `-MD` and
+`-MMD` (paired with `-MF`, `-MT` and `-MQ` to control the output file and the
+rule target).
+
+There is one behavioural difference to be aware of regarding module
+dependencies (the `.mod` files brought in by `use` statements):
+
+* `-MD` and `-MMD` run a full compilation, so the `.mod` files opened during
+ semantic analysis are recorded and appear in the dependency rule.
+* `-M` and `-MM` run the prescanner only. They report textual dependencies
----------------
Thirumalai-Shaktivel wrote:
Other compilers' behaviour:
**ifx**: Does not support -M/-MM (flag is ambiguous/ignored). Uses -gen-dep instead, which runs through semantics and includes USE module files in the output.
**gfortran**: Supports -M/-MM (requires -cpp), runs full semantics, and includes USE module files in the output — same behaviour as -MD/-MMD.
Passing `-fsyntax-only` instead of `-Eonly` for `-M` should run through semantics and resolve USE statements, emitting .mod files in the dependency output. This deviates from clang's prescan-only behaviour for -M, but might be acceptable — unlike C/C++ #include, Fortran's USE statement is resolved at the semantic stage, not during preprocessing. The change is minimal, and aligns with how gfortran and ifx handle it.
What do you think about this?
I had a requirement to add support for -MD and -MF; in the description of -MF, it had "Write depfile output from -MMD, -MD, -MM, or -M to <file>". So, I planned on supporting all the other options as well.
Regarding the usage, I came across this: https://github.com/mesonbuild/meson/blob/master/mesonbuild/compilers/mixins/gnu.py#L503. Which is using MD, MQ and MF, which seems to be used for building SciPy
> IIUC, the primary difference between `-MD`, and `-M/-MM` is that the latter implies `-E`. Is that correct?
Yes
https://github.com/llvm/llvm-project/pull/209379
More information about the flang-commits
mailing list