cmUseMangledMesaCommand.cxx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 char* inputDir = args[0].c_str();
  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 char* destDir = args[1].c_str();
  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.c_str(), destDir);
  42. }
  43. return true;
  44. }
  45. void cmUseMangledMesaCommand::CopyAndFullPathMesaHeader(const char* source,
  46. const char* outdir)
  47. {
  48. std::string dir, file;
  49. cmSystemTools::SplitProgramPath(source, dir, file);
  50. std::string outFile = outdir;
  51. outFile += "/";
  52. outFile += file;
  53. std::string tempOutputFile = outFile;
  54. tempOutputFile += ".tmp";
  55. cmsys::ofstream fout(tempOutputFile.c_str());
  56. if (!fout) {
  57. cmSystemTools::Error("Could not open file for write in copy operation: ",
  58. tempOutputFile.c_str(), outdir);
  59. cmSystemTools::ReportLastSystemError("");
  60. return;
  61. }
  62. cmsys::ifstream fin(source);
  63. if (!fin) {
  64. cmSystemTools::Error("Could not open file for read in copy operation",
  65. source);
  66. return;
  67. }
  68. // now copy input to output and expand variables in the
  69. // input file at the same time
  70. std::string inLine;
  71. // regular expression for any #include line
  72. cmsys::RegularExpression includeLine(
  73. "^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  74. // regular expression for gl/ or GL/ in a file (match(1) of above)
  75. cmsys::RegularExpression glDirLine("(gl|GL)(/|\\\\)([^<\"]+)");
  76. // regular expression for gl GL or xmesa in a file (match(1) of above)
  77. cmsys::RegularExpression glLine("(gl|GL|xmesa)");
  78. while (cmSystemTools::GetLineFromStream(fin, inLine)) {
  79. if (includeLine.find(inLine.c_str())) {
  80. std::string includeFile = includeLine.match(1);
  81. if (glDirLine.find(includeFile.c_str())) {
  82. std::string gfile = glDirLine.match(3);
  83. fout << "#include \"" << outdir << "/" << gfile << "\"\n";
  84. } else if (glLine.find(includeFile.c_str())) {
  85. fout << "#include \"" << outdir << "/" << includeLine.match(1)
  86. << "\"\n";
  87. } else {
  88. fout << inLine << "\n";
  89. }
  90. } else {
  91. fout << inLine << "\n";
  92. }
  93. }
  94. // close the files before attempting to copy
  95. fin.close();
  96. fout.close();
  97. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(), outFile.c_str());
  98. cmSystemTools::RemoveFile(tempOutputFile);
  99. }