cmConfigureFileCommand.cxx 4.3 KB

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