cmUseMangledMesaCommand.cxx 4.1 KB

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