Imported Upstream version 2.8.12.2
[platform/upstream/cmake.git] / Source / cmGetFilenameComponentCommand.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 "cmGetFilenameComponentCommand.h"
13 #include "cmSystemTools.h"
14
15 // cmGetFilenameComponentCommand
16 bool cmGetFilenameComponentCommand
17 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
18 {
19   if(args.size() < 3)
20     {
21     this->SetError("called with incorrect number of arguments");
22     return false;
23     }
24
25   // Check and see if the value has been stored in the cache
26   // already, if so use that value
27   if(args.size() == 4 && args[3] == "CACHE")
28     {
29     const char* cacheValue = this->Makefile->GetDefinition(args[0].c_str());
30     if(cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue))
31       {
32       return true;
33       }
34     }
35
36   std::string result;
37   std::string filename = args[1];
38   if(filename.find("[HKEY") != filename.npos)
39     {
40     // Check the registry as the target application would view it.
41     cmSystemTools::KeyWOW64 view = cmSystemTools::KeyWOW64_32;
42     cmSystemTools::KeyWOW64 other_view = cmSystemTools::KeyWOW64_64;
43     if(this->Makefile->PlatformIs64Bit())
44       {
45       view = cmSystemTools::KeyWOW64_64;
46       other_view = cmSystemTools::KeyWOW64_32;
47       }
48     cmSystemTools::ExpandRegistryValues(filename, view);
49     if(filename.find("/registry") != filename.npos)
50       {
51       std::string other = args[1];
52       cmSystemTools::ExpandRegistryValues(other, other_view);
53       if(other.find("/registry") == other.npos)
54         {
55         filename = other;
56         }
57       }
58     }
59   std::string storeArgs;
60   std::string programArgs;
61   if (args[2] == "DIRECTORY" || args[2] == "PATH")
62     {
63     result = cmSystemTools::GetFilenamePath(filename);
64     }
65   else if (args[2] == "NAME")
66     {
67     result = cmSystemTools::GetFilenameName(filename);
68     }
69   else if (args[2] == "PROGRAM")
70     {
71     for(unsigned int i=2; i < args.size(); ++i)
72       {
73       if(args[i] == "PROGRAM_ARGS")
74         {
75         i++;
76         if(i < args.size())
77           {
78           storeArgs = args[i];
79           }
80         }
81       }
82     cmSystemTools::SplitProgramFromArgs(filename.c_str(),
83                                         result, programArgs);
84     }
85   else if (args[2] == "EXT")
86     {
87     result = cmSystemTools::GetFilenameExtension(filename);
88     }
89   else if (args[2] == "NAME_WE")
90     {
91     result = cmSystemTools::GetFilenameWithoutExtension(filename);
92     }
93   else if (args[2] == "ABSOLUTE" ||
94            args[2] == "REALPATH")
95     {
96     // Collapse the path to its simplest form.
97     // If the path given is relative evaluate it relative to the
98     // current source directory.
99     result = cmSystemTools::CollapseFullPath(
100       filename.c_str(), this->Makefile->GetCurrentDirectory());
101     if(args[2] == "REALPATH")
102       {
103       // Resolve symlinks if possible
104       result = cmSystemTools::GetRealPath(result.c_str());
105       }
106     }
107   else
108     {
109     std::string err = "unknown component " + args[2];
110     this->SetError(err.c_str());
111     return false;
112     }
113
114   if(args.size() == 4 && args[3] == "CACHE")
115     {
116     if(programArgs.size() && storeArgs.size())
117       {
118       this->Makefile->AddCacheDefinition
119         (storeArgs.c_str(), programArgs.c_str(),
120          "", args[2] == "PATH" ? cmCacheManager::FILEPATH
121          : cmCacheManager::STRING);
122       }
123     this->Makefile->AddCacheDefinition
124       (args[0].c_str(), result.c_str(), "",
125        args[2] == "PATH" ? cmCacheManager::FILEPATH
126        : cmCacheManager::STRING);
127     }
128   else
129     {
130     if(programArgs.size() && storeArgs.size())
131       {
132       this->Makefile->AddDefinition(storeArgs.c_str(), programArgs.c_str());
133       }
134     this->Makefile->AddDefinition(args[0].c_str(), result.c_str());
135     }
136
137   return true;
138 }
139