cmConfigureFileCommand.cxx 4.0 KB

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