cmUseMangledMesaCommand.cxx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2001 Insight Consortium
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright notice,
  14. this list of conditions and the following disclaimer in the documentation
  15. and/or other materials provided with the distribution.
  16. * The name of the Insight Consortium, nor the names of any consortium members,
  17. nor of any contributors, may be used to endorse or promote products derived
  18. from this software without specific prior written permission.
  19. * Modified source versions must be plainly marked as such, and must not be
  20. misrepresented as being the original software.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS IS''
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR
  25. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. =========================================================================*/
  32. #include "cmUseMangledMesaCommand.h"
  33. #include "cmSystemTools.h"
  34. // cmUseMangledMesaCommand
  35. bool cmUseMangledMesaCommand::InitialPass(std::vector<std::string> const& argsIn)
  36. {
  37. // expected two arguments:
  38. // arguement one: the full path to gl_mangle.h
  39. // arguement two : directory for output of edited headers
  40. if(argsIn.size() < 2)
  41. {
  42. this->SetError("called with incorrect number of arguments");
  43. return false;
  44. }
  45. std::vector<std::string> args = argsIn;
  46. m_Makefile->ExpandVariablesInString(args[0]);
  47. m_Makefile->ExpandVariablesInString(args[1]);
  48. const char* inputDir = args[0].c_str();
  49. const char* destDir = args[1].c_str();
  50. std::vector<std::string> files;
  51. cmSystemTools::Glob(inputDir, "\\.h$", files);
  52. if(files.size() == 0)
  53. {
  54. cmSystemTools::Error("Could not open Mesa Directory ", inputDir);
  55. return false;
  56. }
  57. cmSystemTools::MakeDirectory(destDir);
  58. for(std::vector<std::string>::iterator i = files.begin();
  59. i != files.end(); ++i)
  60. {
  61. std::string path = inputDir;
  62. path += "/";
  63. path += *i;
  64. this->CopyAndFullPathMesaHeader(path.c_str(), destDir);
  65. }
  66. return true;
  67. }
  68. void
  69. cmUseMangledMesaCommand::
  70. CopyAndFullPathMesaHeader(const char* source,
  71. const char* outdir)
  72. {
  73. std::string dir, file;
  74. cmSystemTools::SplitProgramPath(source, dir, file);
  75. std::string outFile = outdir;
  76. outFile += "/";
  77. outFile += file;
  78. std::string tempOutputFile = outFile;
  79. tempOutputFile += ".tmp";
  80. std::ofstream fout(tempOutputFile.c_str());
  81. if(!fout)
  82. {
  83. cmSystemTools::Error("Could not open file for write in copy operatation: ",
  84. tempOutputFile.c_str(), outdir);
  85. return;
  86. }
  87. std::ifstream fin(source);
  88. if(!fin)
  89. {
  90. cmSystemTools::Error("Could not open file for read in copy operatation",
  91. source);
  92. return;
  93. }
  94. // now copy input to output and expand varibles in the
  95. // input file at the same time
  96. const int bufSize = 4096;
  97. char buffer[bufSize];
  98. std::string inLine;
  99. // regular expression for any #include line
  100. cmRegularExpression includeLine("^[ \t]*#[ \t]*include[ \t]*[<\"]([^\">]+)[\">]");
  101. // regular expression for gl/ or GL/ in a file (match(1) of above)
  102. cmRegularExpression glDirLine("(gl|GL)(/|\\\\)([^<\"]+)");
  103. // regular expression for gl GL or xmesa in a file (match(1) of above)
  104. cmRegularExpression glLine("(gl|GL|xmesa)");
  105. while(fin)
  106. {
  107. fin.getline(buffer, bufSize);
  108. if(fin)
  109. {
  110. inLine = buffer;
  111. if(includeLine.find(inLine.c_str()))
  112. {
  113. std::string includeFile = includeLine.match(1);
  114. if(glDirLine.find(includeFile.c_str()))
  115. {
  116. std::string file = glDirLine.match(3);
  117. fout << "#include \"" << outdir << "/" << file.c_str() << "\"\n";
  118. }
  119. else if(glLine.find(includeFile.c_str()))
  120. {
  121. fout << "#include \"" << outdir << "/" << includeLine.match(1).c_str() << "\"\n";
  122. }
  123. else
  124. {
  125. fout << inLine << "\n";
  126. }
  127. }
  128. else
  129. {
  130. fout << inLine << "\n";
  131. }
  132. }
  133. }
  134. // close the files before attempting to copy
  135. fin.close();
  136. fout.close();
  137. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  138. outFile.c_str());
  139. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  140. }