packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmConfigureFileCommand.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
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 "cmConfigureFileCommand.h"
13
14 #include <cmsys/RegularExpression.hxx>
15
16 // cmConfigureFileCommand
17 bool cmConfigureFileCommand
18 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
19 {
20   if(args.size() < 2 )
21     {
22     this->SetError("called with incorrect number of arguments, expected 2");
23     return false;
24     }
25
26   const char* inFile = args[0].c_str();
27   if(!cmSystemTools::FileIsFullPath(inFile))
28     {
29     this->InputFile = this->Makefile->GetCurrentDirectory();
30     this->InputFile += "/";
31     }
32   this->InputFile += inFile;
33
34   // If the input location is a directory, error out.
35   if(cmSystemTools::FileIsDirectory(this->InputFile.c_str()))
36     {
37     cmOStringStream e;
38     e << "input location\n"
39       << "  " << this->InputFile << "\n"
40       << "is a directory but a file was expected.";
41     this->SetError(e.str().c_str());
42     return false;
43     }
44
45   const char* outFile = args[1].c_str();
46   if(!cmSystemTools::FileIsFullPath(outFile))
47     {
48     this->OutputFile = this->Makefile->GetCurrentOutputDirectory();
49     this->OutputFile += "/";
50     }
51   this->OutputFile += outFile;
52
53   // If the output location is already a directory put the file in it.
54   if(cmSystemTools::FileIsDirectory(this->OutputFile.c_str()))
55     {
56     this->OutputFile += "/";
57     this->OutputFile += cmSystemTools::GetFilenameName(inFile);
58     }
59
60   if ( !this->Makefile->CanIWriteThisFile(this->OutputFile.c_str()) )
61     {
62     std::string e = "attempted to configure a file: " + this->OutputFile
63       + " into a source directory.";
64     this->SetError(e.c_str());
65     cmSystemTools::SetFatalErrorOccured();
66     return false;
67     }
68   std::string errorMessage;
69   if (!this->NewLineStyle.ReadFromArguments(args, errorMessage))
70     {
71     this->SetError(errorMessage.c_str());
72     return false;
73     }
74   this->CopyOnly = false;
75   this->EscapeQuotes = false;
76
77   // for CMake 2.0 and earlier CONFIGURE_FILE defaults to the FinalPass,
78   // after 2.0 it only does InitialPass
79   this->Immediate = !this->Makefile->NeedBackwardsCompatibility(2,0);
80
81   this->AtOnly = false;
82   for(unsigned int i=2;i < args.size();++i)
83     {
84     if(args[i] == "COPYONLY")
85       {
86       this->CopyOnly = true;
87       if (this->NewLineStyle.IsValid())
88         {
89         this->SetError("COPYONLY could not be used in combination "
90                        "with NEWLINE_STYLE");
91         return false;
92         }
93       }
94     else if(args[i] == "ESCAPE_QUOTES")
95       {
96       this->EscapeQuotes = true;
97       }
98     else if(args[i] == "@ONLY")
99       {
100       this->AtOnly = true;
101       }
102     else if(args[i] == "IMMEDIATE")
103       {
104       this->Immediate = true;
105       }
106     }
107
108   // If we were told to copy the file immediately, then do it on the
109   // first pass (now).
110   if(this->Immediate)
111     {
112     if ( !this->ConfigureFile() )
113       {
114       this->SetError("Problem configuring file");
115       return false;
116       }
117     }
118
119   return true;
120 }
121
122 void cmConfigureFileCommand::FinalPass()
123 {
124   if(!this->Immediate)
125     {
126     this->ConfigureFile();
127     }
128 }
129
130 int cmConfigureFileCommand::ConfigureFile()
131 {
132   return this->Makefile->ConfigureFile(
133     this->InputFile.c_str(),
134     this->OutputFile.c_str(),
135     this->CopyOnly,
136     this->AtOnly,
137     this->EscapeQuotes,
138     this->NewLineStyle);
139 }
140
141