packaging: Initial packaging
[platform/upstream/cmake.git] / Source / cmSeparateArgumentsCommand.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 "cmSeparateArgumentsCommand.h"
13
14 // cmSeparateArgumentsCommand
15 bool cmSeparateArgumentsCommand
16 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
17 {
18   if(args.empty())
19     {
20     this->SetError("must be given at least one argument.");
21     return false;
22     }
23
24   std::string var;
25   std::string command;
26   enum Mode { ModeOld, ModeUnix, ModeWindows };
27   Mode mode = ModeOld;
28   enum Doing { DoingNone, DoingVariable, DoingMode, DoingCommand };
29   Doing doing = DoingVariable;
30   for(unsigned int i=0; i < args.size(); ++i)
31     {
32     if(doing == DoingVariable)
33       {
34       var = args[i];
35       doing = DoingMode;
36       }
37     else if(doing == DoingMode && args[i] == "UNIX_COMMAND")
38       {
39       mode = ModeUnix;
40       doing = DoingCommand;
41       }
42     else if(doing == DoingMode && args[i] == "WINDOWS_COMMAND")
43       {
44       mode = ModeWindows;
45       doing = DoingCommand;
46       }
47     else if(doing == DoingCommand)
48       {
49       command = args[i];
50       doing = DoingNone;
51       }
52     else
53       {
54       cmOStringStream e;
55       e << "given unknown argument " << args[i];
56       this->SetError(e.str().c_str());
57       return false;
58       }
59     }
60
61   if(mode == ModeOld)
62     {
63     // Original space-replacement version of command.
64     if(const char* def = this->Makefile->GetDefinition(var.c_str()))
65       {
66       std::string value = def;
67       cmSystemTools::ReplaceString(value, " ", ";");
68       this->Makefile->AddDefinition(var.c_str(), value.c_str());
69       }
70     }
71   else
72     {
73     // Parse the command line.
74     std::vector<std::string> vec;
75     if(mode == ModeUnix)
76       {
77       cmSystemTools::ParseUnixCommandLine(command.c_str(), vec);
78       }
79     else // if(mode == ModeWindows)
80       {
81       cmSystemTools::ParseWindowsCommandLine(command.c_str(), vec);
82       }
83
84     // Construct the result list value.
85     std::string value;
86     const char* sep = "";
87     for(std::vector<std::string>::const_iterator vi = vec.begin();
88         vi != vec.end(); ++vi)
89       {
90       // Separate from the previous argument.
91       value += sep;
92       sep = ";";
93
94       // Preserve semicolons.
95       for(std::string::const_iterator si = vi->begin();
96           si != vi->end(); ++si)
97         {
98         if(*si == ';')
99           {
100           value += '\\';
101           }
102         value += *si;
103         }
104       }
105     this->Makefile->AddDefinition(var.c_str(), value.c_str());
106     }
107
108   return true;
109 }