You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
2.7 KiB

20 years ago
  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. if ( !this->Makefile->CanIWriteThisFile(fileName.c_str()) )
  41. {
  42. std::string e = "attempted to write a file: " + fileName
  43. + " into a source directory.";
  44. this->SetError(e.c_str());
  45. cmSystemTools::SetFatalErrorOccured();
  46. return false;
  47. }
  48. std::string dir = cmSystemTools::GetFilenamePath(fileName);
  49. cmSystemTools::MakeDirectory(dir.c_str());
  50. mode_t mode =
  51. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  52. S_IREAD | S_IWRITE
  53. #elif defined( __BORLANDC__ )
  54. S_IRUSR | S_IWUSR
  55. #else
  56. S_IRUSR | S_IWUSR |
  57. S_IRGRP |
  58. S_IROTH
  59. #endif
  60. ;
  61. // Set permissions to writable
  62. if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
  63. {
  64. cmSystemTools::SetPermissions(fileName.c_str(),
  65. #if defined( _MSC_VER ) || defined( __MINGW32__ )
  66. S_IREAD | S_IWRITE
  67. #else
  68. S_IRUSR | S_IWUSR
  69. #endif
  70. );
  71. }
  72. // If GetPermissions fails, pretend like it is ok. File open will fail if
  73. // the file is not writable
  74. std::ofstream file(fileName.c_str(),
  75. overwrite?std::ios::out : std::ios::app);
  76. if ( !file )
  77. {
  78. std::string error = "Internal CMake error when trying to open file: ";
  79. error += fileName.c_str();
  80. error += " for writing.";
  81. this->SetError(error.c_str());
  82. return false;
  83. }
  84. file << message << std::endl;
  85. file.close();
  86. cmSystemTools::SetPermissions(fileName.c_str(), mode);
  87. this->Makefile->AddWrittenFile(fileName.c_str());
  88. return true;
  89. }