diff --git a/main.c b/main.c index d991d8a..dcbb831 100644 --- a/main.c +++ b/main.c @@ -11,6 +11,7 @@ static FileType opt_x; static StringArray opt_include; static bool opt_E; static bool opt_M; +static bool opt_MD; static bool opt_MP; static bool opt_S; static bool opt_c; @@ -203,6 +204,11 @@ static void parse_args(int argc, char **argv) { continue; } + if (!strcmp(argv[i], "-MD")) { + opt_MD = true; + continue; + } + if (!strcmp(argv[i], "-cc1-input")) { base_file = argv[++i]; continue; @@ -360,6 +366,8 @@ static void print_dependencies(void) { char *path; if (opt_MF) path = opt_MF; + else if (opt_MD) + path = replace_extn(opt_o ? opt_o : base_file, ".d"); else if (opt_o) path = opt_o; else @@ -422,10 +430,11 @@ static void cc1(void) { tok = append_tokens(tok, tok2); tok = preprocess(tok); - // If -M is given, print file dependencies. - if (opt_M) { + // If -M or -MD are given, print file dependencies. + if (opt_M || opt_MD) { print_dependencies(); - return; + if (opt_M) + return; } // If -E is given, print out preprocessed C code as a result. diff --git a/test/driver.sh b/test/driver.sh index 152e83a..3b783cc 100755 --- a/test/driver.sh +++ b/test/driver.sh @@ -246,4 +246,17 @@ check -MT $chibicc -MT foo -MT bar -M -I$tmp $tmp/out.c | grep -q '^foo bar:' check -MT +# -MD +echo '#include "out2.h"' > $tmp/md2.c +echo '#include "out3.h"' > $tmp/md3.c +(cd $tmp; $OLDPWD/$chibicc -c -MD -I. md2.c md3.c) +grep -q -z '^md2.o:.* md2\.c .* ./out2\.h' $tmp/md2.d +check -MD +grep -q -z '^md3.o:.* md3\.c .* ./out3\.h' $tmp/md3.d +check -MD + +$chibicc -c -MD -MF $tmp/md-mf.d -I. $tmp/md2.c +grep -q -z '^md2.o:.*md2\.c .*/out2\.h' $tmp/md-mf.d +check -MD + echo OK