cmWriteFileCommand.cxx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "cmWriteFileCommand.h"
  14. // cmLibraryCommand
  15. bool cmWriteFileCommand::InitialPass(std::vector<std::string> const& args)
  16. {
  17. if(args.size() < 2 )
  18. {
  19. this->SetError("called with incorrect number of arguments");
  20. return false;
  21. }
  22. std::string message;
  23. std::vector<std::string>::const_iterator i = args.begin();
  24. std::string fileName = *i;
  25. bool overwrite = true;
  26. i++;
  27. for(;i != args.end(); ++i)
  28. {
  29. if ( *i == "APPEND" )
  30. {
  31. overwrite = false;
  32. }
  33. else
  34. {
  35. message += *i;
  36. }
  37. }
  38. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  39. cmSystemTools::MakeDirectory(dir.c_str());
  40. mode_t mode =
  41. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  42. S_IREAD | S_IWRITE
  43. #elif defined( __BORLANDC__ )
  44. S_IRUSR | S_IWUSR
  45. #else
  46. S_IRUSR | S_IWUSR |
  47. S_IRGRP |
  48. S_IROTH
  49. #endif
  50. ;
  51. // Set permissions to writable
  52. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  53. {
  54. cmSystemTools::SetPermissions(fileName.c_str(),
  55. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  56. S_IREAD | S_IWRITE
  57. #else
  58. S_IRUSR | S_IWUSR
  59. #endif
  60. );
  61. }
  62. // If GetPermissions fails, pretend like it is ok. File open will fail if
  63. // the file is not writable
  64. std::ofstream file(fileName.c_str(), overwrite?std::ios::out : std::ios::app);
  65. if ( !file )
  66. {
  67. std::string error = "Internal CMake error when trying to open file: ";
  68. error += fileName.c_str();
  69. error += " for writting.";
  70. this->SetError(error.c_str());
  71. return false;
  72. }
  73. file << message << std::endl;
  74. file.close();
  75. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  76. m_Makefile->AddWrittenFile(fileName.c_str());
  77. return true;
  78. }