1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmWriteFileCommand.cxx,v $
6 Date: $Date: 2008-04-30 17:42:39 $
7 Version: $Revision: 1.17 $
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>
23 bool cmWriteFileCommand
24 ::InitialPass(std::vector
<std::string
> const& args
, cmExecutionStatus
&)
28 this->SetError("called with incorrect number of arguments");
32 std::vector
<std::string
>::const_iterator i
= args
.begin();
34 std::string fileName
= *i
;
35 bool overwrite
= true;
38 for(;i
!= args
.end(); ++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();
59 std::string dir
= cmSystemTools::GetFilenamePath(fileName
);
60 cmSystemTools::MakeDirectory(dir
.c_str());
63 #if defined( _MSC_VER ) || defined( __MINGW32__ )
65 #elif defined( __BORLANDC__ )
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__ )
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
);
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());
97 file
<< message
<< std::endl
;
99 cmSystemTools::SetPermissions(fileName
.c_str(), mode
);