cmWriteFileCommand.cxx 2.4 KB

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