Commit 2caf39c961

Andrew Kelley <andrew@ziglang.org>
2019-03-07 20:30:50
fix .d file processing and use -MV to quote spaces
1 parent 431b3b2
src/cache_hash.cpp
@@ -426,7 +426,7 @@ Error cache_add_dep_file(CacheHash *ch, Buf *dep_file_path, bool verbose) {
         }
         return ErrorReadingDepFile;
     }
-    SplitIterator it = memSplit(buf_to_slice(contents), str("\n"));
+    SplitIterator it = memSplit(buf_to_slice(contents), str("\r\n"));
     // skip first line
     SplitIterator_next(&it);
     for (;;) {
@@ -435,14 +435,44 @@ Error cache_add_dep_file(CacheHash *ch, Buf *dep_file_path, bool verbose) {
             break;
         if (opt_line.value.len == 0)
             continue;
-        SplitIterator line_it = memSplit(opt_line.value, str(" \t"));
-        Slice<uint8_t> filename;
-        if (!SplitIterator_next(&line_it).unwrap(&filename))
+        // skip over indentation
+        while (opt_line.value.len != 0 && (opt_line.value.ptr[0] == ' ' || opt_line.value.ptr[0] == '\t')) {
+            opt_line.value.ptr += 1;
+            opt_line.value.len -= 1;
+        }
+        if (opt_line.value.len == 0)
             continue;
-        Buf *filename_buf = buf_create_from_slice(filename);
+        if (opt_line.value.ptr[0] == '"') {
+            if (opt_line.value.len < 2) {
+                if (verbose) {
+                    fprintf(stderr, "unable to process invalid .d file %s: line too short\n", buf_ptr(dep_file_path));
+                }
+                return ErrorInvalidDepFile;
+            }
+            opt_line.value.ptr += 1;
+            opt_line.value.len -= 2;
+            while (opt_line.value.len != 0 && opt_line.value.ptr[opt_line.value.len] != '"') {
+                opt_line.value.len -= 1;
+            }
+            if (opt_line.value.len == 0) {
+                if (verbose) {
+                    fprintf(stderr, "unable to process invalid .d file %s: missing double quote\n", buf_ptr(dep_file_path));
+                }
+                return ErrorInvalidDepFile;
+            }
+        } else {
+            if (opt_line.value.ptr[opt_line.value.len - 1] == '\\') {
+                opt_line.value.len -= 2; // cut off ` \`
+            }
+            if (opt_line.value.len == 0)
+                continue;
+        }
+
+        Buf *filename_buf = buf_create_from_slice(opt_line.value);
         if ((err = cache_add_file(ch, filename_buf))) {
             if (verbose) {
                 fprintf(stderr, "unable to add %s to cache: %s\n", buf_ptr(filename_buf), err_str(err));
+                fprintf(stderr, "when processing .d file: %s\n", buf_ptr(dep_file_path));
             }
             return err;
         }
src/codegen.cpp
@@ -8367,6 +8367,7 @@ static bool gen_c_object(CodeGen *g, Buf *self_exe_path, CFile *c_file) {
 
         Buf *out_dep_path = buf_sprintf("%s.d", buf_ptr(out_obj_path));
         args.append("-MD");
+        args.append("-MV");
         args.append("-MF");
         args.append(buf_ptr(out_dep_path));
 
src/error.cpp
@@ -38,6 +38,7 @@ const char *err_str(Error err) {
         case ErrorPathTooLong: return "path too long";
         case ErrorCCompilerCannotFindFile: return "C compiler cannot find file";
         case ErrorReadingDepFile: return "failed to read .d file";
+        case ErrorInvalidDepFile: return "invalid .d file";
         case ErrorMissingArchitecture: return "missing architecture";
         case ErrorMissingOperatingSystem: return "missing operating system";
         case ErrorUnknownArchitecture: return "unrecognized architecture";
src/error.hpp
@@ -40,6 +40,7 @@ enum Error {
     ErrorPathTooLong,
     ErrorCCompilerCannotFindFile,
     ErrorReadingDepFile,
+    ErrorInvalidDepFile,
     ErrorMissingArchitecture,
     ErrorMissingOperatingSystem,
     ErrorUnknownArchitecture,
src/translate_c.cpp
@@ -4781,6 +4781,7 @@ Error parse_h_file(ZigType *import, ZigList<ErrorMsg *> *errors, const char *tar
         Buf *prefix = buf_sprintf("%s" OS_SEP, buf_ptr(codegen->cache_dir));
         out_dep_path = os_tmp_filename(prefix, buf_create_from_str(".d"));
         clang_argv.append("-MD");
+        clang_argv.append("-MV");
         clang_argv.append("-MF");
         clang_argv.append(buf_ptr(out_dep_path));
     }