cmUseMangledMesaCommand.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmUseMangledMesaCommand.h"
  11. #include "cmSystemTools.h"
  12. #include <cmsys/RegularExpression.hxx>
  13. bool cmUseMangledMesaCommand
  14. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  15. {
  16. if(this->Disallowed(cmPolicies::CMP0030,
  17. "The use_mangled_mesa command should not be called; see CMP0030."))
  18. { return true; }
  19. // expected two arguments:
  20. // arguement one: the full path to gl_mangle.h
  21. // arguement two : directory for output of edited headers
  22. if(args.size() != 2)
  23. {
  24. this->SetError("called with incorrect number of arguments");
  25. return false;
  26. }
  27. const char* inputDir = args[0].c_str();
  28. std::string glh = inputDir;
  29. glh += "/";
  30. glh += "gl.h";
  31. if(!cmSystemTools::FileExists(glh.c_str()))
  32. {
  33. std::string e = "Bad path to Mesa, could not find: ";
  34. e += glh;
  35. e += " ";
  36. this->SetError(e.c_str());
  37. return false;
  38. }
  39. const char* destDir = args[1].c_str();
  40. std::vector<std::string> files;
  41. cmSystemTools::Glob(inputDir, "\\.h$", files);
  42. if(files.size() == 0)
  43. {
  44. cmSystemTools::Error("Could not open Mesa Directory ", inputDir);
  45. return false;
  46. }
  47. cmSystemTools::MakeDirectory(destDir);
  48. for(std::vector<std::string>::iterator i = files.begin();
  49. i != files.end(); ++i)
  50. {
  51. std::string path = inputDir;
  52. path += "/";
  53. path += *i;
  54. this->CopyAndFullPathMesaHeader(path.c_str(), destDir);
  55. }
  56. return true;
  57. }
  58. void
  59. cmUseMangledMesaCommand::
  60. CopyAndFullPathMesaHeader(const char* source,
  61. const char* outdir)
  62. {
  63. std::string dir, file;
  64. cmSystemTools::SplitProgramPath(source, dir, file);
  65. std::string outFile = outdir;
  66. outFile += "/";
  67. outFile += file;
  68. std::string tempOutputFile = outFile;
  69. tempOutputFile += ".tmp";
  70. std::ofstream fout(tempOutputFile.c_str());
  71. if(!fout)
  72. {
  73. cmSystemTools::Error("Could not open file for write in copy operation: ",
  74. tempOutputFile.c_str(), outdir);
  75. cmSystemTools::ReportLastSystemError("");
  76. return;
  77. }
  78. std::ifstream fin(source);
  79. if(!fin)
  80. {
  81. cmSystemTools::Error("Could not open file for read in copy operation",
  82. source);
  83. return;
  84. }
  85. // now copy input to output and expand variables in the
  86. // input file at the same time
  87. std::string inLine;
  88. // regular expression for any #include line
  89. cmsys::RegularExpression includeLine(
  90. "^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  91. // regular expression for gl/ or GL/ in a file (match(1) of above)
  92. cmsys::RegularExpression glDirLine("(gl|GL)(/|\\\\)([^<\"]+)");
  93. // regular expression for gl GL or xmesa in a file (match(1) of above)
  94. cmsys::RegularExpression glLine("(gl|GL|xmesa)");
  95. while(cmSystemTools::GetLineFromStream(fin,inLine))
  96. {
  97. if(includeLine.find(inLine.c_str()))
  98. {
  99. std::string includeFile = includeLine.match(1);
  100. if(glDirLine.find(includeFile.c_str()))
  101. {
  102. std::string gfile = glDirLine.match(3);
  103. fout << "#include \"" << outdir << "/" << gfile.c_str() << "\"\n";
  104. }
  105. else if(glLine.find(includeFile.c_str()))
  106. {
  107. fout << "#include \"" << outdir << "/" <<
  108. includeLine.match(1).c_str() << "\"\n";
  109. }
  110. else
  111. {
  112. fout << inLine << "\n";
  113. }
  114. }
  115. else
  116. {
  117. fout << inLine << "\n";
  118. }
  119. }
  120. // close the files before attempting to copy
  121. fin.close();
  122. fout.close();
  123. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  124. outFile.c_str());
  125. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  126. }