cmUseMangledMesaCommand.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmUseMangledMesaCommand.h"
  14. #include "cmSystemTools.h"
  15. #include <cmsys/RegularExpression.hxx>
  16. // cmUseMangledMesaCommand
  17. bool cmUseMangledMesaCommand::InitialPass(std::vector<std::string> const& args)
  18. {
  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("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  90. // regular expression for gl/ or GL/ in a file (match(1) of above)
  91. cmsys::RegularExpression glDirLine("(gl|GL)(/|\\\\)([^<\"]+)");
  92. // regular expression for gl GL or xmesa in a file (match(1) of above)
  93. cmsys::RegularExpression glLine("(gl|GL|xmesa)");
  94. while(cmSystemTools::GetLineFromStream(fin,inLine))
  95. {
  96. if(includeLine.find(inLine.c_str()))
  97. {
  98. std::string includeFile = includeLine.match(1);
  99. if(glDirLine.find(includeFile.c_str()))
  100. {
  101. std::string gfile = glDirLine.match(3);
  102. fout << "#include \"" << outdir << "/" << gfile.c_str() << "\"\n";
  103. }
  104. else if(glLine.find(includeFile.c_str()))
  105. {
  106. fout << "#include \"" << outdir << "/" << 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. }