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 "cmGetFilenameComponentCommand.h"
13 #include "cmSystemTools.h"
15 // cmGetFilenameComponentCommand
16 bool cmGetFilenameComponentCommand
17 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
21 this->SetError("called with incorrect number of arguments");
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")
29 const char* cacheValue = this->Makefile->GetDefinition(args[0].c_str());
30 if(cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue))
37 std::string filename = args[1];
38 if(filename.find("[HKEY") != filename.npos)
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())
45 view = cmSystemTools::KeyWOW64_64;
46 other_view = cmSystemTools::KeyWOW64_32;
48 cmSystemTools::ExpandRegistryValues(filename, view);
49 if(filename.find("/registry") != filename.npos)
51 std::string other = args[1];
52 cmSystemTools::ExpandRegistryValues(other, other_view);
53 if(other.find("/registry") == other.npos)
59 std::string storeArgs;
60 std::string programArgs;
61 if (args[2] == "DIRECTORY" || args[2] == "PATH")
63 result = cmSystemTools::GetFilenamePath(filename);
65 else if (args[2] == "NAME")
67 result = cmSystemTools::GetFilenameName(filename);
69 else if (args[2] == "PROGRAM")
71 for(unsigned int i=2; i < args.size(); ++i)
73 if(args[i] == "PROGRAM_ARGS")
82 cmSystemTools::SplitProgramFromArgs(filename.c_str(),
85 else if (args[2] == "EXT")
87 result = cmSystemTools::GetFilenameExtension(filename);
89 else if (args[2] == "NAME_WE")
91 result = cmSystemTools::GetFilenameWithoutExtension(filename);
93 else if (args[2] == "ABSOLUTE" ||
94 args[2] == "REALPATH")
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")
103 // Resolve symlinks if possible
104 result = cmSystemTools::GetRealPath(result.c_str());
109 std::string err = "unknown component " + args[2];
110 this->SetError(err.c_str());
114 if(args.size() == 4 && args[3] == "CACHE")
116 if(programArgs.size() && storeArgs.size())
118 this->Makefile->AddCacheDefinition
119 (storeArgs.c_str(), programArgs.c_str(),
120 "", args[2] == "PATH" ? cmCacheManager::FILEPATH
121 : cmCacheManager::STRING);
123 this->Makefile->AddCacheDefinition
124 (args[0].c_str(), result.c_str(), "",
125 args[2] == "PATH" ? cmCacheManager::FILEPATH
126 : cmCacheManager::STRING);
130 if(programArgs.size() && storeArgs.size())
132 this->Makefile->AddDefinition(storeArgs.c_str(), programArgs.c_str());
134 this->Makefile->AddDefinition(args[0].c_str(), result.c_str());