cmConfigureFileCommand.cxx 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "cmConfigureFileCommand.h"
  33. // cmConfigureFileCommand
  34. bool cmConfigureFileCommand::InitialPass(std::vector<std::string> const& args)
  35. {
  36. if(args.size() < 2 )
  37. {
  38. this->SetError("called with incorrect number of arguments, expected 2");
  39. return false;
  40. }
  41. m_InputFile = args[0];
  42. m_OuputFile = args[1];
  43. m_CopyOnly = false;
  44. m_EscapeQuotes = false;
  45. m_Immediate = false;
  46. m_AtOnly = false;
  47. for(unsigned int i=2;i < args.size();++i)
  48. {
  49. if(args[i] == "COPYONLY")
  50. {
  51. m_CopyOnly = true;
  52. }
  53. else if(args[i] == "ESCAPE_QUOTES")
  54. {
  55. m_EscapeQuotes = true;
  56. }
  57. else if(args[i] == "@ONLY")
  58. {
  59. m_AtOnly = true;
  60. }
  61. else if(args[i] == "IMMEDIATE")
  62. {
  63. m_Immediate = true;
  64. }
  65. }
  66. // If we were told to copy the file immediately, then do it on the
  67. // first pass (now).
  68. if(m_Immediate)
  69. {
  70. this->ConfigureFile();
  71. }
  72. return true;
  73. }
  74. void cmConfigureFileCommand::FinalPass()
  75. {
  76. if(!m_Immediate)
  77. {
  78. this->ConfigureFile();
  79. }
  80. }
  81. void cmConfigureFileCommand::ConfigureFile()
  82. {
  83. m_Makefile->ExpandVariablesInString(m_InputFile);
  84. m_Makefile->AddCMakeDependFile(m_InputFile.c_str());
  85. m_Makefile->ExpandVariablesInString(m_OuputFile);
  86. cmSystemTools::ConvertToUnixSlashes(m_OuputFile);
  87. std::string::size_type pos = m_OuputFile.rfind('/');
  88. if(pos != std::string::npos)
  89. {
  90. std::string path = m_OuputFile.substr(0, pos);
  91. cmSystemTools::MakeDirectory(path.c_str());
  92. }
  93. if(m_CopyOnly)
  94. {
  95. cmSystemTools::CopyFileIfDifferent(m_InputFile.c_str(),
  96. m_OuputFile.c_str());
  97. }
  98. else
  99. {
  100. std::string tempOutputFile = m_OuputFile;
  101. tempOutputFile += ".tmp";
  102. std::ofstream fout(tempOutputFile.c_str());
  103. if(!fout)
  104. {
  105. cmSystemTools::Error("Could not open file for write in copy operatation ",
  106. tempOutputFile.c_str());
  107. return;
  108. }
  109. std::ifstream fin(m_InputFile.c_str());
  110. if(!fin)
  111. {
  112. cmSystemTools::Error("Could not open file for read in copy operatation ",
  113. m_InputFile.c_str());
  114. return;
  115. }
  116. // now copy input to output and expand varibles in the
  117. // input file at the same time
  118. const int bufSize = 4096;
  119. char buffer[bufSize];
  120. std::string inLine;
  121. cmRegularExpression cmdefine("#cmakedefine[ \t]*([A-Za-z_0-9]*)");
  122. while(fin)
  123. {
  124. fin.getline(buffer, bufSize);
  125. if(fin)
  126. {
  127. inLine = buffer;
  128. m_Makefile->ExpandVariablesInString(inLine, m_EscapeQuotes, m_AtOnly);
  129. m_Makefile->RemoveVariablesInString(inLine, m_AtOnly);
  130. // look for special cmakedefine symbol and handle it
  131. // is the symbol defined
  132. if (cmdefine.find(inLine))
  133. {
  134. const char *def = m_Makefile->GetDefinition(cmdefine.match(1).c_str());
  135. if(!cmSystemTools::IsOff(def))
  136. {
  137. cmSystemTools::ReplaceString(inLine,
  138. "#cmakedefine", "#define");
  139. fout << inLine << "\n";
  140. }
  141. else
  142. {
  143. cmSystemTools::ReplaceString(inLine,
  144. "#cmakedefine", "#undef");
  145. fout << "/* " << inLine << " */\n";
  146. }
  147. }
  148. else
  149. {
  150. fout << inLine << "\n";
  151. }
  152. }
  153. }
  154. // close the files before attempting to copy
  155. fin.close();
  156. fout.close();
  157. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  158. m_OuputFile.c_str());
  159. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  160. }
  161. }