From d44871ca085f572b1d6d0a1cb25d7cd4c8f78a5d Mon Sep 17 00:00:00 2001 From: Alexey Panteleev Date: Fri, 11 Mar 2022 17:56:15 -0800 Subject: [PATCH] Escape the characters that Make considers special in the dependency files. --- StandAlone/StandAlone.cpp | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/StandAlone/StandAlone.cpp b/StandAlone/StandAlone.cpp index bda4ee78..f5dd3bb1 100644 --- a/StandAlone/StandAlone.cpp +++ b/StandAlone/StandAlone.cpp @@ -1168,6 +1168,27 @@ struct ShaderCompUnit { } }; +// Writes a string into a depfile, escaping some special characters following the Makefile rules. +static void writeEscapedDepString(std::ofstream& file, const std::string& str) +{ + for (char c : str) { + switch (c) { + case ' ': + case ':': + case '#': + case '[': + case ']': + case '\\': + file << '\\'; + break; + case '$': + file << '$'; + break; + } + file << c; + } +} + // Writes a depfile similar to gcc -MMD foo.c bool writeDepFile(std::string depfile, std::vector& binaryFiles, const std::vector& sources) { @@ -1175,10 +1196,12 @@ bool writeDepFile(std::string depfile, std::vector& binaryFiles, co if (file.fail()) return false; - for (auto it = binaryFiles.begin(); it != binaryFiles.end(); it++) { - file << *it << ":"; - for (auto it = sources.begin(); it != sources.end(); it++) { - file << " " << *it; + for (auto binaryFile = binaryFiles.begin(); binaryFile != binaryFiles.end(); binaryFile++) { + writeEscapedDepString(file, *binaryFile); + file << ":"; + for (auto sourceFile = sources.begin(); sourceFile != sources.end(); sourceFile++) { + file << " "; + writeEscapedDepString(file, *sourceFile); } file << std::endl; }