1 /*============================================================================
2 CMake - Cross Platform Makefile Generator
3 Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
5 Distributed under the OSI-approved BSD License (the "License");
6 see accompanying file Copyright.txt for details.
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the License for more information.
11 ============================================================================*/
12 #include "cmWriteFileCommand.h"
14 #include <sys/types.h>
18 bool cmWriteFileCommand
19 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
23 this->SetError("called with incorrect number of arguments");
27 std::vector<std::string>::const_iterator i = args.begin();
29 std::string fileName = *i;
30 bool overwrite = true;
33 for(;i != args.end(); ++i)
45 if ( !this->Makefile->CanIWriteThisFile(fileName.c_str()) )
47 std::string e = "attempted to write a file: " + fileName
48 + " into a source directory.";
49 this->SetError(e.c_str());
50 cmSystemTools::SetFatalErrorOccured();
54 std::string dir = cmSystemTools::GetFilenamePath(fileName);
55 cmSystemTools::MakeDirectory(dir.c_str());
59 // Set permissions to writable
60 if ( cmSystemTools::GetPermissions(fileName.c_str(), mode) )
62 cmSystemTools::SetPermissions(fileName.c_str(),
63 #if defined( _MSC_VER ) || defined( __MINGW32__ )
65 #elif defined( __BORLANDC__ )
68 mode | S_IWUSR | S_IWGRP
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);
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());
84 file << message << std::endl;
88 cmSystemTools::SetPermissions(fileName.c_str(), mode);