cmUseMangledMesaCommand.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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& argsIn)
  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(argsIn.size() < 2)
  22. {
  23. this->SetError("called with incorrect number of arguments");
  24. return false;
  25. }
  26. std::vector<std::string> args = argsIn;
  27. m_Makefile->ExpandVariablesInString(args[0]);
  28. m_Makefile->ExpandVariablesInString(args[1]);
  29. const char* inputDir = args[0].c_str();
  30. const char* destDir = args[1].c_str();
  31. std::vector<std::string> files;
  32. cmSystemTools::Glob(inputDir, "\\.h$", files);
  33. if(files.size() == 0)
  34. {
  35. cmSystemTools::Error("Could not open Mesa Directory ", inputDir);
  36. return false;
  37. }
  38. cmSystemTools::MakeDirectory(destDir);
  39. for(std::vector<std::string>::iterator i = files.begin();
  40. i != files.end(); ++i)
  41. {
  42. std::string path = inputDir;
  43. path += "/";
  44. path += *i;
  45. this->CopyAndFullPathMesaHeader(path.c_str(), destDir);
  46. }
  47. return true;
  48. }
  49. void
  50. cmUseMangledMesaCommand::
  51. CopyAndFullPathMesaHeader(const char* source,
  52. const char* outdir)
  53. {
  54. std::string dir, file;
  55. cmSystemTools::SplitProgramPath(source, dir, file);
  56. std::string outFile = outdir;
  57. outFile += "/";
  58. outFile += file;
  59. std::string tempOutputFile = outFile;
  60. tempOutputFile += ".tmp";
  61. std::ofstream fout(tempOutputFile.c_str());
  62. if(!fout)
  63. {
  64. cmSystemTools::Error("Could not open file for write in copy operatation: ",
  65. tempOutputFile.c_str(), outdir);
  66. return;
  67. }
  68. std::ifstream fin(source);
  69. if(!fin)
  70. {
  71. cmSystemTools::Error("Could not open file for read in copy operatation",
  72. source);
  73. return;
  74. }
  75. // now copy input to output and expand varibles in the
  76. // input file at the same time
  77. const int bufSize = 4096;
  78. char buffer[bufSize];
  79. std::string inLine;
  80. // regular expression for any #include line
  81. cmRegularExpression includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  82. // regular expression for gl/ or GL/ in a file (match(1) of above)
  83. cmRegularExpression glDirLine("(gl|GL)(/|\\\\)([^<\"]+)");
  84. // regular expression for gl GL or xmesa in a file (match(1) of above)
  85. cmRegularExpression glLine("(gl|GL|xmesa)");
  86. while(fin)
  87. {
  88. fin.getline(buffer, bufSize);
  89. if(fin)
  90. {
  91. inLine = buffer;
  92. if(includeLine.find(inLine.c_str()))
  93. {
  94. std::string includeFile = includeLine.match(1);
  95. if(glDirLine.find(includeFile.c_str()))
  96. {
  97. std::string file = glDirLine.match(3);
  98. fout << "#include \"" << outdir << "/" << file.c_str() << "\"\n";
  99. }
  100. else if(glLine.find(includeFile.c_str()))
  101. {
  102. fout << "#include \"" << outdir << "/" << includeLine.match(1).c_str() << "\"\n";
  103. }
  104. else
  105. {
  106. fout << inLine << "\n";
  107. }
  108. }
  109. else
  110. {
  111. fout << inLine << "\n";
  112. }
  113. }
  114. }
  115. // close the files before attempting to copy
  116. fin.close();
  117. fout.close();
  118. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  119. outFile.c_str());
  120. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  121. }