Skip to content

Commit

Permalink
Add -MQ option
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Dec 7, 2020
1 parent fb5cfe5 commit 7aa72e4
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,34 @@ static FileType parse_opt_x(char *s) {
error("<command line>: unknown argument for -x: %s", s);
}

static char *quote_makefile(char *s) {
char *buf = calloc(1, strlen(s) * 2 + 1);

for (int i = 0, j = 0; s[i]; i++) {
switch (s[i]) {
case '$':
buf[j++] = '$';
buf[j++] = '$';
break;
case '#':
buf[j++] = '\\';
buf[j++] = '#';
break;
case ' ':
case '\t':
for (int k = i - 1; k >= 0 && s[k] == '\\'; k--)
buf[j++] = '\\';
buf[j++] = '\\';
buf[j++] = s[i];
break;
default:
buf[j++] = s[i];
break;
}
}
return buf;
}

static void parse_args(int argc, char **argv) {
// Make sure that all command line options that take an argument
// have an argument.
Expand Down Expand Up @@ -209,6 +237,14 @@ static void parse_args(int argc, char **argv) {
continue;
}

if (!strcmp(argv[i], "-MQ")) {
if (opt_MT == NULL)
opt_MT = quote_makefile(argv[++i]);
else
opt_MT = format("%s %s", opt_MT, quote_makefile(argv[++i]));
continue;
}

if (!strcmp(argv[i], "-cc1-input")) {
base_file = argv[++i];
continue;
Expand Down Expand Up @@ -374,7 +410,10 @@ static void print_dependencies(void) {
path = "-";

FILE *out = open_file(path);
fprintf(out, "%s:", opt_MT ? opt_MT : replace_extn(base_file, ".o"));
if (opt_MT)
fprintf(out, "%s:", opt_MT);
else
fprintf(out, "%s:", quote_makefile(replace_extn(base_file, ".o")));

File **files = get_input_files();

Expand All @@ -384,7 +423,7 @@ static void print_dependencies(void) {

if (opt_MP)
for (int i = 1; files[i]; i++)
fprintf(out, "%s:\n\n", files[i]->name);
fprintf(out, "%s:\n\n", quote_makefile(files[i]->name));
}

static Token *must_tokenize_file(char *path) {
Expand Down

0 comments on commit 7aa72e4

Please sign in to comment.