Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Source / cmGetFilenameComponentCommand.cxx
blob87f84975ee0fe403ac4bf4fc7d4b589eede88738
1 /*=========================================================================
3 Program: CMake - Cross-Platform Makefile Generator
4 Module: $RCSfile: cmGetFilenameComponentCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2009-02-09 14:23:55 $
7 Version: $Revision: 1.20 $
9 Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
10 See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
12 This software is distributed WITHOUT ANY WARRANTY; without even
13 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 PURPOSE. See the above copyright notices for more information.
16 =========================================================================*/
17 #include "cmGetFilenameComponentCommand.h"
18 #include "cmSystemTools.h"
20 // cmGetFilenameComponentCommand
21 bool cmGetFilenameComponentCommand
22 ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
24 if(args.size() < 3)
26 this->SetError("called with incorrect number of arguments");
27 return false;
30 // Check and see if the value has been stored in the cache
31 // already, if so use that value
32 if(args.size() == 4 && args[3] == "CACHE")
34 const char* cacheValue = this->Makefile->GetDefinition(args[0].c_str());
35 if(cacheValue && !cmSystemTools::IsNOTFOUND(cacheValue))
37 return true;
41 std::string result;
42 std::string filename = args[1];
43 cmSystemTools::ExpandRegistryValues(filename);
44 std::string storeArgs;
45 std::string programArgs;
46 if (args[2] == "PATH")
48 result = cmSystemTools::GetFilenamePath(filename);
50 else if (args[2] == "NAME")
52 result = cmSystemTools::GetFilenameName(filename);
54 else if (args[2] == "PROGRAM")
56 for(unsigned int i=2; i < args.size(); ++i)
58 if(args[i] == "PROGRAM_ARGS")
60 i++;
61 if(i < args.size())
63 storeArgs = args[i];
67 cmSystemTools::SplitProgramFromArgs(filename.c_str(),
68 result, programArgs);
70 else if (args[2] == "EXT")
72 result = cmSystemTools::GetFilenameExtension(filename);
74 else if (args[2] == "NAME_WE")
76 result = cmSystemTools::GetFilenameWithoutExtension(filename);
78 else if (args[2] == "ABSOLUTE" ||
79 args[2] == "REALPATH")
81 // If the path given is relative evaluate it relative to the
82 // current source directory.
83 if(!cmSystemTools::FileIsFullPath(filename.c_str()))
85 std::string fname = this->Makefile->GetCurrentDirectory();
86 if(!fname.empty())
88 fname += "/";
89 fname += filename;
90 filename = fname;
94 // Collapse the path to its simplest form.
95 result = cmSystemTools::CollapseFullPath(filename.c_str());
96 if(args[2] == "REALPATH")
98 // Resolve symlinks if possible
99 result = cmSystemTools::GetRealPath(filename.c_str());
102 else
104 std::string err = "unknown component " + args[2];
105 this->SetError(err.c_str());
106 return false;
109 if(args.size() == 4 && args[3] == "CACHE")
111 if(programArgs.size() && storeArgs.size())
113 this->Makefile->AddCacheDefinition
114 (storeArgs.c_str(), programArgs.c_str(),
115 "", args[2] == "PATH" ? cmCacheManager::FILEPATH
116 : cmCacheManager::STRING);
118 this->Makefile->AddCacheDefinition
119 (args[0].c_str(), result.c_str(), "",
120 args[2] == "PATH" ? cmCacheManager::FILEPATH
121 : cmCacheManager::STRING);
123 else
125 if(programArgs.size() && storeArgs.size())
127 this->Makefile->AddDefinition(storeArgs.c_str(), programArgs.c_str());
129 this->Makefile->AddDefinition(args[0].c_str(), result.c_str());
132 return true;