Skip to content

Commit

Permalink
Add -MD option
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent db850f3 commit fb5cfe5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
15 changes: 12 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions test/driver.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fb5cfe5

Please sign in to comment.