cmUseMangledMesaCommand.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 operatation: ",
  74. tempOutputFile.c_str(), outdir);
  75. return;
  76. }
  77. std::ifstream fin(source);
  78. if(!fin)
  79. {
  80. cmSystemTools::Error("Could not open file for read in copy operatation",
  81. source);
  82. return;
  83. }
  84. // now copy input to output and expand variables in the
  85. // input file at the same time
  86. std::string inLine;
  87. // regular expression for any #include line
  88. cmsys::RegularExpression includeLine("^[ \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 << "/" << includeLine.match(1).c_str() << "\"\n";
  106. }
  107. else
  108. {
  109. fout << inLine << "\n";
  110. }
  111. }
  112. else
  113. {
  114. fout << inLine << "\n";
  115. }
  116. }
  117. // close the files before attempting to copy
  118. fin.close();
  119. fout.close();
  120. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  121. outFile.c_str());
  122. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  123. }