cmUseMangledMesaCommand.cxx 4.1 KB

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