Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmWriteFileCommand.cxx
blob423bec4ec4681a05f402c671ffdb5d224abb04d6
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmWriteFileCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2008/01/23 15:27:59 $
7 Version: $Revision: 1.16 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmWriteFileCommand.h"
19 #include <sys/types.h>
20 #include <sys/stat.h>
22 // cmLibraryCommand
23 bool cmWriteFileCommand
24 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
26 if(args.size() < 2 )
28 this->SetError("called with incorrect number of arguments");
29 return false;
31 std::string message;
32 std::vector<std::string>::const_iterator i = args.begin();
34 std::string fileName = *i;
35 bool overwrite = true;
36 i++;
38 for(;i != args.end(); ++i)
40 if ( *i == "APPEND" )
42 overwrite = false;
44 else
46 message += *i;
50 if ( !this->Makefile->CanIWriteThisFile(fileName.c_str()) )
52 std::string e = "attempted to write a file: " + fileName
53 + " into a source directory.";
54 this->SetError(e.c_str());
55 cmSystemTools::SetFatalErrorOccured();
56 return false;
59 std::string dir = cmSystemTools::GetFilenamePath(fileName);
60 cmSystemTools::MakeDirectory(dir.c_str());
62 mode_t mode =
63 #if defined( _MSC_VER ) || defined( __MINGW32__ )
64 S_IREAD | S_IWRITE
65 #elif defined( __BORLANDC__ )
66 S_IRUSR | S_IWUSR
67 #else
68 S_IRUSR | S_IWUSR |
69 S_IRGRP |
70 S_IROTH
71 #endif
74 // Set permissions to writable
75 if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
77 cmSystemTools::SetPermissions(fileName.c_str(),
78 #if defined( _MSC_VER ) || defined( __MINGW32__ )
79 S_IREAD | S_IWRITE
80 #else
81 S_IRUSR | S_IWUSR
82 #endif
85 // If GetPermissions fails, pretend like it is ok. File open will fail if
86 // the file is not writable
87 std::ofstream file(fileName.c_str(),
88 overwrite?std::ios::out : std::ios::app);
89 if ( !file )
91 std::string error = "Internal CMake error when trying to open file: ";
92 error += fileName.c_str();
93 error += " for writing.";
94 this->SetError(error.c_str());
95 return false;
97 file << message << std::endl;
98 file.close();
99 cmSystemTools::SetPermissions(fileName.c_str(), mode);
100 this->Makefile->AddWrittenFile(fileName.c_str());
102 return true;