cmConfigureFileCommand.cxx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. mode_t perm = 0;
  68. cmSystemTools::GetPermissions(m_InputFile.c_str(), perm);
  69. std::string::size_type pos = m_OuputFile.rfind('/');
  70. if(pos != std::string::npos)
  71. {
  72. std::string path = m_OuputFile.substr(0, pos);
  73. cmSystemTools::MakeDirectory(path.c_str());
  74. }
  75. if(m_CopyOnly)
  76. {
  77. cmSystemTools::CopyFileIfDifferent(m_InputFile.c_str(),
  78. m_OuputFile.c_str());
  79. }
  80. else
  81. {
  82. std::string tempOutputFile = m_OuputFile;
  83. tempOutputFile += ".tmp";
  84. std::ofstream fout(tempOutputFile.c_str());
  85. if(!fout)
  86. {
  87. cmSystemTools::Error("Could not open file for write in copy operatation ",
  88. tempOutputFile.c_str());
  89. return;
  90. }
  91. std::ifstream fin(m_InputFile.c_str());
  92. if(!fin)
  93. {
  94. cmSystemTools::Error("Could not open file for read in copy operatation ",
  95. m_InputFile.c_str());
  96. return;
  97. }
  98. // now copy input to output and expand variables in the
  99. // input file at the same time
  100. std::string inLine;
  101. cmsys::RegularExpression cmdefine("#cmakedefine[ \t]*([A-Za-z_0-9]*)");
  102. while( cmSystemTools::GetLineFromStream(fin, inLine) )
  103. {
  104. m_Makefile->ExpandVariablesInString(inLine, m_EscapeQuotes, m_AtOnly);
  105. m_Makefile->RemoveVariablesInString(inLine, m_AtOnly);
  106. // look for special cmakedefine symbol and handle it
  107. // is the symbol defined
  108. if (cmdefine.find(inLine))
  109. {
  110. const char *def = m_Makefile->GetDefinition(cmdefine.match(1).c_str());
  111. if(!cmSystemTools::IsOff(def))
  112. {
  113. cmSystemTools::ReplaceString(inLine,
  114. "#cmakedefine", "#define");
  115. fout << inLine << "\n";
  116. }
  117. else
  118. {
  119. cmSystemTools::ReplaceString(inLine,
  120. "#cmakedefine", "#undef");
  121. fout << "/* " << inLine << " */\n";
  122. }
  123. }
  124. else
  125. {
  126. fout << inLine << "\n";
  127. }
  128. }
  129. // close the files before attempting to copy
  130. fin.close();
  131. fout.close();
  132. cmSystemTools::CopyFileIfDifferent(tempOutputFile.c_str(),
  133. m_OuputFile.c_str());
  134. cmSystemTools::RemoveFile(tempOutputFile.c_str());
  135. cmSystemTools::SetPermissions(m_OuputFile.c_str(), perm);
  136. }
  137. }