Skip to content

Commit

Permalink
Escape spaces in depfile with backslashes.
Browse files Browse the repository at this point in the history
This matches how Linux escapes spaces in paths.
The same syntax is also used by other build tools that output depfiles,
e.g. https://github.com/rust-lang/cargo/blob/edd36eba5e0d6e0cfcb84bd0cc651ba8bf5e7f83/src/cargo/core/compiler/output_depinfo.rs#L19

Signed-off-by: Colin Finck <[email protected]>
  • Loading branch information
ColinFinck committed Oct 22, 2024
1 parent f9968fa commit 44e7721
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 3 additions & 1 deletion dtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,9 @@ int main(int argc, char *argv[])
if (!depfile)
die("Couldn't open dependency file %s: %s\n", depname,
strerror(errno));
fprintf(depfile, "%s:", outname);

fprint_path_escaped(depfile, outname);
fputc(':', depfile);
}

if (inform == NULL)
Expand Down
6 changes: 4 additions & 2 deletions srcpos.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ FILE *srcfile_relative_open(const char *fname, char **fullnamep)
strerror(errno));
}

if (depfile)
fprintf(depfile, " %s", fullname);
if (depfile) {
fputc(' ', depfile);
fprint_path_escaped(depfile, fullname);
}

if (fullnamep)
*fullnamep = fullname;
Expand Down
16 changes: 16 additions & 0 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@
#include "util.h"
#include "version_gen.h"

void fprint_path_escaped(FILE *fp, const char *path)
{
const char *p = path;

while (*p) {
if (*p == ' ') {
fputc('\\', fp);
fputc(' ', fp);
} else {
fputc(*p, fp);
}

p++;
}
}

char *xstrdup(const char *s)
{
int len = strlen(s) + 1;
Expand Down
5 changes: 5 additions & 0 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ static inline void NORETURN PRINTF(1, 2) die(const char *str, ...)
exit(1);
}

/**
* Writes path to fp, escaping spaces with a backslash.
*/
void fprint_path_escaped(FILE *fp, const char *path);

static inline void *xmalloc(size_t len)
{
void *new = malloc(len);
Expand Down

0 comments on commit 44e7721

Please sign in to comment.