ENH: change the search path order (if several Tcl/Tk are installed, the "current...
[cmake.git] / Source / cmFindProgramCommand.cxx
blob227f69f5c07186d5c32594ee9014ff982c828737
1 /*=========================================================================
3 Program: Insight Segmentation & Registration Toolkit
4 Module: $RCSfile: cmFindProgramCommand.cxx,v $
5 Language: C++
6 Date: $Date: 2002-05-12 15:31:45 $
7 Version: $Revision: 1.23 $
9 Copyright (c) 2002 Insight Consortium. All rights reserved.
10 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 "cmFindProgramCommand.h"
18 #include "cmCacheManager.h"
19 #include <stdlib.h>
20 #include <stdio.h>
23 // cmFindProgramCommand
24 bool cmFindProgramCommand::InitialPass(std::vector<std::string> const& argsIn)
26 if(argsIn.size() < 2 )
28 this->SetError("called with incorrect number of arguments");
29 return false;
31 std::string doc = "Path to a program.";
32 size_t size = argsIn.size();
33 std::vector<std::string> argst;
34 for(unsigned int j = 0; j < size; ++j)
36 if(argsIn[j] != "DOC")
38 argst.push_back(argsIn[j]);
40 else
42 if(j+1 < size)
44 doc = argsIn[j+1];
46 break;
49 std::vector<std::string> args;
50 cmSystemTools::ExpandListArguments(argst, args);
53 std::vector<std::string>::iterator i = args.begin();
54 // Use the first argument as the name of something to be defined
55 const char* define = (*i).c_str();
56 i++; // move iterator to next arg
57 // Now check and see if the value has been stored in the cache
58 // already, if so use that value and don't look for the program
59 const char* cacheValue
60 = m_Makefile->GetDefinition(define);
61 if(cacheValue && strcmp(cacheValue, "NOTFOUND"))
63 return true;
65 if(cacheValue)
67 cmCacheManager::CacheEntry* e =
68 cmCacheManager::GetInstance()->GetCacheEntry(args[0].c_str());
69 if(e)
71 doc = e->m_HelpString;
74 std::vector<std::string> path;
75 std::vector<std::string> names;
76 bool foundName = false;
77 bool foundPath = false;
78 bool doingNames = true;
79 bool no_system_path = false;
80 for (unsigned int j = 1; j < args.size(); ++j)
82 if(args[j] == "NAMES")
84 doingNames = true;
85 foundName = true;
87 else if (args[j] == "PATHS")
89 doingNames = false;
90 foundPath = true;
92 else if (args[j] == "NO_SYSTEM_PATH")
94 no_system_path = true;
96 else
98 if(doingNames)
100 names.push_back(args[j]);
102 else
104 cmSystemTools::ExpandRegistryValues(args[j]);
105 // Glob the entry in case of wildcards.
106 cmSystemTools::GlobDirs(args[j].c_str(), path);
110 // if it is not in the cache, then search the system path
111 // add any user specified paths
112 if(!foundPath && !foundName)
114 path.clear();
115 names.clear();
116 names.push_back(args[1]);
117 for (unsigned int j = 2; j < args.size(); j++)
119 // expand variables
120 std::string exp = args[j];
121 cmSystemTools::ExpandRegistryValues(exp);
123 // Glob the entry in case of wildcards.
124 cmSystemTools::GlobDirs(exp.c_str(), path);
127 for(std::vector<std::string>::iterator i = names.begin();
128 i != names.end() ; ++i)
130 // Try to find the program.
131 std::string result = cmSystemTools::FindProgram(i->c_str(),
132 path,
133 no_system_path);
134 if(result != "")
136 // Save the value in the cache
137 m_Makefile->AddCacheDefinition(define,
138 result.c_str(),
139 doc.c_str(),
140 cmCacheManager::FILEPATH);
142 return true;
145 m_Makefile->AddCacheDefinition(args[0].c_str(),
146 "NOTFOUND",
147 doc.c_str(),
148 cmCacheManager::FILEPATH);
149 return true;