cmUseMangledMesaCommand.cxx 4.0 KB

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