cmUseMangledMesaCommand.cxx 3.4 KB

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