resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmUseMangledMesaCommand.cxx
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #include "cmUseMangledMesaCommand.h"
4
5 #include "cmsys/FStream.hxx"
6 #include "cmsys/RegularExpression.hxx"
7
8 #include "cmExecutionStatus.h"
9 #include "cmStringAlgorithms.h"
10 #include "cmSystemTools.h"
11
12 namespace {
13 void CopyAndFullPathMesaHeader(const std::string& source,
14                                const std::string& outdir);
15 }
16
17 bool cmUseMangledMesaCommand(std::vector<std::string> const& args,
18                              cmExecutionStatus& status)
19 {
20   // expected two arguments:
21   // argument one: the full path to gl_mangle.h
22   // argument two : directory for output of edited headers
23   if (args.size() != 2) {
24     status.SetError("called with incorrect number of arguments");
25     return false;
26   }
27   const std::string& inputDir = args[0];
28   std::string glh = cmStrCat(inputDir, "/gl.h");
29   if (!cmSystemTools::FileExists(glh)) {
30     std::string e = cmStrCat("Bad path to Mesa, could not find: ", glh, ' ');
31     status.SetError(e);
32     return false;
33   }
34   const std::string& destDir = args[1];
35   std::vector<std::string> files;
36   cmSystemTools::Glob(inputDir, "\\.h$", files);
37   if (files.empty()) {
38     cmSystemTools::Error("Could not open Mesa Directory " + inputDir);
39     return false;
40   }
41   cmSystemTools::MakeDirectory(destDir);
42   for (std::string const& f : files) {
43     std::string path = cmStrCat(inputDir, '/', f);
44     CopyAndFullPathMesaHeader(path, destDir);
45   }
46
47   return true;
48 }
49
50 namespace {
51 void CopyAndFullPathMesaHeader(const std::string& source,
52                                const std::string& outdir)
53 {
54   std::string dir;
55   std::string file;
56   cmSystemTools::SplitProgramPath(source, dir, file);
57   std::string outFile = cmStrCat(outdir, '/', file);
58   std::string tempOutputFile = cmStrCat(outFile, ".tmp");
59   cmsys::ofstream fout(tempOutputFile.c_str());
60   if (!fout) {
61     cmSystemTools::Error("Could not open file for write in copy operation: " +
62                          tempOutputFile + outdir);
63     cmSystemTools::ReportLastSystemError("");
64     return;
65   }
66   cmsys::ifstream fin(source.c_str());
67   if (!fin) {
68     cmSystemTools::Error("Could not open file for read in copy operation" +
69                          source);
70     return;
71   }
72   // now copy input to output and expand variables in the
73   // input file at the same time
74   std::string inLine;
75   // regular expression for any #include line
76   cmsys::RegularExpression includeLine(
77     "^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
78   // regular expression for gl/ or GL/ in a file (match(1) of above)
79   cmsys::RegularExpression glDirLine(R"((gl|GL)(/|\\)([^<"]+))");
80   // regular expression for gl GL or xmesa in a file (match(1) of above)
81   cmsys::RegularExpression glLine("(gl|GL|xmesa)");
82   while (cmSystemTools::GetLineFromStream(fin, inLine)) {
83     if (includeLine.find(inLine)) {
84       std::string includeFile = includeLine.match(1);
85       if (glDirLine.find(includeFile)) {
86         std::string gfile = glDirLine.match(3);
87         fout << "#include \"" << outdir << "/" << gfile << "\"\n";
88       } else if (glLine.find(includeFile)) {
89         fout << "#include \"" << outdir << "/" << includeLine.match(1)
90              << "\"\n";
91       } else {
92         fout << inLine << "\n";
93       }
94     } else {
95       fout << inLine << "\n";
96     }
97   }
98   // close the files before attempting to copy
99   fin.close();
100   fout.close();
101   cmSystemTools::MoveFileIfDifferent(tempOutputFile, outFile);
102 }
103 }