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"
5 #include "cmsys/FStream.hxx"
6 #include "cmsys/RegularExpression.hxx"
8 #include "cmExecutionStatus.h"
9 #include "cmStringAlgorithms.h"
10 #include "cmSystemTools.h"
13 void CopyAndFullPathMesaHeader(const std::string& source,
14 const std::string& outdir);
17 bool cmUseMangledMesaCommand(std::vector<std::string> const& args,
18 cmExecutionStatus& status)
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");
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, ' ');
34 const std::string& destDir = args[1];
35 std::vector<std::string> files;
36 cmSystemTools::Glob(inputDir, "\\.h$", files);
38 cmSystemTools::Error("Could not open Mesa Directory " + inputDir);
41 cmSystemTools::MakeDirectory(destDir);
42 for (std::string const& f : files) {
43 std::string path = cmStrCat(inputDir, '/', f);
44 CopyAndFullPathMesaHeader(path, destDir);
51 void CopyAndFullPathMesaHeader(const std::string& source,
52 const std::string& outdir)
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());
61 cmSystemTools::Error("Could not open file for write in copy operation: " +
62 tempOutputFile + outdir);
63 cmSystemTools::ReportLastSystemError("");
66 cmsys::ifstream fin(source.c_str());
68 cmSystemTools::Error("Could not open file for read in copy operation" +
72 // now copy input to output and expand variables in the
73 // input file at the same time
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)
92 fout << inLine << "\n";
95 fout << inLine << "\n";
98 // close the files before attempting to copy
101 cmSystemTools::MoveFileIfDifferent(tempOutputFile, outFile);