From 7aa72e41e6b2703b3f357507252008ebe25dc08d Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Thu, 3 Sep 2020 00:32:47 +0900 Subject: [PATCH] Add -MQ option --- main.c | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index dcbb83162..f744a7d85 100644 --- a/main.c +++ b/main.c @@ -72,6 +72,34 @@ static FileType parse_opt_x(char *s) { error(": 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. @@ -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; @@ -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(); @@ -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) {