cmConfigureFileCommand.cxx 4.2 KB

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