From ad16628c9eb5af5314ac26a6fd96637483dbd715 Mon Sep 17 00:00:00 2001
From: Riccardo Schirone <sirmy15@gmail.com>
Date: Sat, 30 Jan 2021 00:50:55 +0100
Subject: [PATCH] Make sure to escape paths in generated make dependencies

If spaces are not escaped when generating the make dependencies file,
then if one of the dependencies has a space it would be interpreted as
two separate targets by Make, instead of just one.
---
 tcctools.c | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/tcctools.c b/tcctools.c
index 7b6988c0..96e5e12e 100644
--- a/tcctools.c
+++ b/tcctools.c
@@ -515,10 +515,23 @@ int _dowildcard = 1;
 /* -------------------------------------------------------------- */
 /* generate xxx.d file */
 
+static char *escape_target_dep(const char *s) {
+    char *res = tcc_malloc(strlen(s) * 2 + 1);
+    int j;
+    for (j = 0; *s; s++, j++) {
+        if (is_space(*s)) {
+            res[j++] = '\\';
+        }
+        res[j] = *s;
+    }
+    res[j] = '\0';
+    return res;
+}
+
 ST_FUNC void gen_makedeps(TCCState *s1, const char *target, const char *filename)
 {
     FILE *depout;
-    char buf[1024];
+    char buf[1024], *escaped_target;
     int i, k;
 
     if (!filename) {
@@ -540,7 +553,9 @@ ST_FUNC void gen_makedeps(TCCState *s1, const char *target, const char *filename
         for (k = 0; k < i; ++k)
             if (0 == strcmp(s1->target_deps[i], s1->target_deps[k]))
                 goto next;
-        fprintf(depout, " \\\n  %s", s1->target_deps[i]);
+        escaped_target = escape_target_dep(s1->target_deps[i]);
+        fprintf(depout, " \\\n  %s", escaped_target);
+        tcc_free(escaped_target);
     next:;
     }
     fprintf(depout, "\n");