summaryrefslogtreecommitdiffstats
path: root/Source/cmTransformDepfile.cxx
diff options
context:
space:
mode:
authorKyle Edwards <kyle.edwards@kitware.com>2021-01-11 19:46:12 (GMT)
committerBrad King <brad.king@kitware.com>2021-01-11 20:00:51 (GMT)
commit9f48a468cd68d69e16302174ff202e9e51035023 (patch)
tree2fe4a4c3ba4b9eb2d6996014536faebd2036e458 /Source/cmTransformDepfile.cxx
parente20560a2dc9668bb9fd55bea79ef7508ab72c746 (diff)
parent06dfa5a7b6c0174bfcb4691a9aaf00809bd7271d (diff)
downloadCMake-9f48a468cd68d69e16302174ff202e9e51035023.zip
CMake-9f48a468cd68d69e16302174ff202e9e51035023.tar.gz
CMake-9f48a468cd68d69e16302174ff202e9e51035023.tar.bz2
Merge branch 'master' into cmake-gui-qrc-fix
Diffstat (limited to 'Source/cmTransformDepfile.cxx')
-rw-r--r--Source/cmTransformDepfile.cxx101
1 files changed, 101 insertions, 0 deletions
diff --git a/Source/cmTransformDepfile.cxx b/Source/cmTransformDepfile.cxx
new file mode 100644
index 0000000..b91e1ce
--- /dev/null
+++ b/Source/cmTransformDepfile.cxx
@@ -0,0 +1,101 @@
+/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
+ file Copyright.txt or https://cmake.org/licensing for details. */
+#include "cmTransformDepfile.h"
+
+#include <string>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include <cm/optional>
+
+#include "cmsys/FStream.hxx"
+
+#include "cmGccDepfileReader.h"
+#include "cmGccDepfileReaderTypes.h"
+#include "cmSystemTools.h"
+
+namespace {
+void WriteFilenameGcc(cmsys::ofstream& fout, const std::string& filename)
+{
+ for (auto c : filename) {
+ switch (c) {
+ case ' ':
+ fout << "\\ ";
+ break;
+ case '\\':
+ fout << "\\\\";
+ break;
+ default:
+ fout << c;
+ break;
+ }
+ }
+}
+
+void WriteGccDepfile(cmsys::ofstream& fout, const cmGccDepfileContent& content)
+{
+ for (auto const& dep : content) {
+ bool first = true;
+ for (auto const& rule : dep.rules) {
+ if (!first) {
+ fout << " \\\n ";
+ }
+ first = false;
+ WriteFilenameGcc(fout, rule);
+ }
+ fout << ':';
+ for (auto const& path : dep.paths) {
+ fout << " \\\n ";
+ WriteFilenameGcc(fout, path);
+ }
+ fout << '\n';
+ }
+}
+
+void WriteVsTlog(cmsys::ofstream& fout, const cmGccDepfileContent& content)
+{
+ for (auto const& dep : content) {
+ fout << '^';
+ bool first = true;
+ for (auto const& rule : dep.rules) {
+ if (!first) {
+ fout << '|';
+ }
+ first = false;
+ fout << cmSystemTools::ConvertToOutputPath(rule);
+ }
+ fout << "\r\n";
+ for (auto const& path : dep.paths) {
+ fout << cmSystemTools::ConvertToOutputPath(path) << "\r\n";
+ }
+ }
+}
+}
+
+bool cmTransformDepfile(cmDepfileFormat format, const std::string& prefix,
+ const std::string& infile, const std::string& outfile)
+{
+ cmGccDepfileContent content;
+ if (cmSystemTools::FileExists(infile)) {
+ auto result = cmReadGccDepfile(infile.c_str(), prefix);
+ if (!result) {
+ return false;
+ }
+ content = *std::move(result);
+ }
+
+ cmsys::ofstream fout(outfile.c_str());
+ if (!fout) {
+ return false;
+ }
+ switch (format) {
+ case cmDepfileFormat::GccDepfile:
+ WriteGccDepfile(fout, content);
+ break;
+ case cmDepfileFormat::VsTlog:
+ WriteVsTlog(fout, content);
+ break;
+ }
+ return true;
+}