2009-09-28 19:43:28 +04:00
|
|
|
|
/*============================================================================
|
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
|
Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
|
2001-01-11 22:55:47 +03:00
|
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
|
see accompanying file Copyright.txt for details.
|
2001-01-11 22:55:47 +03:00
|
|
|
|
|
2009-09-28 19:43:28 +04:00
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
See the License for more information.
|
|
|
|
|
============================================================================*/
|
2001-01-18 19:20:24 +03:00
|
|
|
|
#include "cmFindProgramCommand.h"
|
2016-04-29 16:40:20 +03:00
|
|
|
|
|
2001-01-05 19:41:20 +03:00
|
|
|
|
#include <stdlib.h>
|
2006-04-13 19:00:52 +04:00
|
|
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
|
|
|
|
#endif
|
2010-11-12 18:47:28 +03:00
|
|
|
|
|
2015-09-01 20:54:33 +03:00
|
|
|
|
struct cmFindProgramHelper
|
|
|
|
|
{
|
|
|
|
|
cmFindProgramHelper()
|
2016-05-16 17:34:04 +03:00
|
|
|
|
{
|
|
|
|
|
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)
|
2015-09-01 20:54:33 +03:00
|
|
|
|
// Consider platform-specific extensions.
|
|
|
|
|
this->Extensions.push_back(".com");
|
|
|
|
|
this->Extensions.push_back(".exe");
|
|
|
|
|
#endif
|
|
|
|
|
// Consider original name with no extensions.
|
|
|
|
|
this->Extensions.push_back("");
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
2015-09-01 20:54:33 +03:00
|
|
|
|
|
|
|
|
|
// List of valid extensions.
|
|
|
|
|
std::vector<std::string> Extensions;
|
|
|
|
|
|
|
|
|
|
// Keep track of the best program file found so far.
|
|
|
|
|
std::string BestPath;
|
|
|
|
|
|
|
|
|
|
// Current names under consideration.
|
|
|
|
|
std::vector<std::string> Names;
|
|
|
|
|
|
|
|
|
|
// Current full path under consideration.
|
|
|
|
|
std::string TestPath;
|
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
|
void AddName(std::string const& name) { this->Names.push_back(name); }
|
2015-09-01 20:54:33 +03:00
|
|
|
|
void SetName(std::string const& name)
|
2016-05-16 17:34:04 +03:00
|
|
|
|
{
|
2015-09-01 20:54:33 +03:00
|
|
|
|
this->Names.clear();
|
|
|
|
|
this->AddName(name);
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
2015-09-01 20:54:33 +03:00
|
|
|
|
bool CheckDirectory(std::string const& path)
|
2016-05-16 17:34:04 +03:00
|
|
|
|
{
|
2015-09-01 20:54:33 +03:00
|
|
|
|
for (std::vector<std::string>::iterator i = this->Names.begin();
|
2016-05-16 17:34:04 +03:00
|
|
|
|
i != this->Names.end(); ++i) {
|
|
|
|
|
if (this->CheckDirectoryForName(path, *i)) {
|
2015-09-01 20:54:33 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-09-01 20:54:33 +03:00
|
|
|
|
bool CheckDirectoryForName(std::string const& path, std::string const& name)
|
2016-05-16 17:34:04 +03:00
|
|
|
|
{
|
2015-09-01 20:54:33 +03:00
|
|
|
|
for (std::vector<std::string>::iterator ext = this->Extensions.begin();
|
2016-05-16 17:34:04 +03:00
|
|
|
|
ext != this->Extensions.end(); ++ext) {
|
2015-09-01 20:54:33 +03:00
|
|
|
|
this->TestPath = path;
|
|
|
|
|
this->TestPath += name;
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (!ext->empty() && cmSystemTools::StringEndsWith(name, ext->c_str())) {
|
2015-09-01 20:54:33 +03:00
|
|
|
|
continue;
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
2015-09-01 20:54:33 +03:00
|
|
|
|
this->TestPath += *ext;
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (cmSystemTools::FileExists(this->TestPath, true)) {
|
2015-09-01 20:54:33 +03:00
|
|
|
|
this->BestPath = cmSystemTools::CollapseFullPath(this->TestPath);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
|
return false;
|
|
|
|
|
}
|
2015-09-01 20:54:33 +03:00
|
|
|
|
};
|
|
|
|
|
|
2015-09-01 23:25:57 +03:00
|
|
|
|
cmFindProgramCommand::cmFindProgramCommand()
|
|
|
|
|
{
|
|
|
|
|
this->NamesPerDirAllowed = true;
|
|
|
|
|
}
|
|
|
|
|
|
2001-01-18 19:20:24 +03:00
|
|
|
|
// cmFindProgramCommand
|
2016-05-16 17:34:04 +03:00
|
|
|
|
bool cmFindProgramCommand::InitialPass(std::vector<std::string> const& argsIn,
|
|
|
|
|
cmExecutionStatus&)
|
2001-01-05 19:41:20 +03:00
|
|
|
|
{
|
2006-03-02 21:30:22 +03:00
|
|
|
|
this->VariableDocumentation = "Path to a program.";
|
|
|
|
|
this->CMakePathName = "PROGRAM";
|
|
|
|
|
// call cmFindBase::ParseArguments
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (!this->ParseArguments(argsIn)) {
|
2001-01-05 19:41:20 +03:00
|
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
|
|
|
|
if (this->AlreadyInCache) {
|
2006-07-18 23:21:26 +04:00
|
|
|
|
// If the user specifies the entry on the command line without a
|
|
|
|
|
// type we should add the type and docstring but keep the original
|
|
|
|
|
// value.
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (this->AlreadyInCacheWithoutMetaInfo) {
|
2014-03-11 03:04:11 +04:00
|
|
|
|
this->Makefile->AddCacheDefinition(this->VariableName, "",
|
2006-07-18 23:21:26 +04:00
|
|
|
|
this->VariableDocumentation.c_str(),
|
2015-04-07 23:45:54 +03:00
|
|
|
|
cmState::FILEPATH);
|
2001-02-19 23:13:48 +03:00
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2006-04-13 19:00:52 +04:00
|
|
|
|
|
2015-09-01 20:42:14 +03:00
|
|
|
|
std::string result = FindProgram();
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (result != "") {
|
2006-03-02 21:30:22 +03:00
|
|
|
|
// Save the value in the cache
|
2016-05-16 17:34:04 +03:00
|
|
|
|
this->Makefile->AddCacheDefinition(this->VariableName, result.c_str(),
|
2006-03-15 19:02:08 +03:00
|
|
|
|
this->VariableDocumentation.c_str(),
|
2015-04-07 23:45:54 +03:00
|
|
|
|
cmState::FILEPATH);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
2006-03-02 21:30:22 +03:00
|
|
|
|
return true;
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
|
|
|
|
this->Makefile->AddCacheDefinition(
|
|
|
|
|
this->VariableName, (this->VariableName + "-NOTFOUND").c_str(),
|
|
|
|
|
this->VariableDocumentation.c_str(), cmState::FILEPATH);
|
2001-05-04 19:30:46 +04:00
|
|
|
|
return true;
|
2001-01-05 19:41:20 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-01 20:42:14 +03:00
|
|
|
|
std::string cmFindProgramCommand::FindProgram()
|
2006-04-13 19:00:52 +04:00
|
|
|
|
{
|
|
|
|
|
std::string program = "";
|
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (this->SearchAppBundleFirst || this->SearchAppBundleOnly) {
|
2015-09-01 20:42:14 +03:00
|
|
|
|
program = FindAppBundle();
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
|
|
|
|
if (program.empty() && !this->SearchAppBundleOnly) {
|
2015-09-01 20:54:33 +03:00
|
|
|
|
program = this->FindNormalProgram();
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
2006-04-13 19:00:52 +04:00
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (program.empty() && this->SearchAppBundleLast) {
|
2015-09-01 20:42:14 +03:00
|
|
|
|
program = this->FindAppBundle();
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
2006-04-13 19:00:52 +04:00
|
|
|
|
return program;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-01 20:54:33 +03:00
|
|
|
|
std::string cmFindProgramCommand::FindNormalProgram()
|
2015-09-01 23:25:57 +03:00
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (this->NamesPerDir) {
|
2015-09-01 23:25:57 +03:00
|
|
|
|
return this->FindNormalProgramNamesPerDir();
|
2016-05-16 17:34:04 +03:00
|
|
|
|
} else {
|
2015-09-01 23:25:57 +03:00
|
|
|
|
return this->FindNormalProgramDirsPerName();
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
2015-09-01 23:25:57 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string cmFindProgramCommand::FindNormalProgramNamesPerDir()
|
|
|
|
|
{
|
|
|
|
|
// Search for all names in each directory.
|
|
|
|
|
cmFindProgramHelper helper;
|
|
|
|
|
for (std::vector<std::string>::const_iterator ni = this->Names.begin();
|
2016-05-16 17:34:04 +03:00
|
|
|
|
ni != this->Names.end(); ++ni) {
|
2015-09-01 23:25:57 +03:00
|
|
|
|
helper.AddName(*ni);
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
2015-10-30 00:50:19 +03:00
|
|
|
|
|
|
|
|
|
// Check for the names themselves (e.g. absolute paths).
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (helper.CheckDirectory(std::string())) {
|
2015-10-30 00:50:19 +03:00
|
|
|
|
return helper.BestPath;
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
2015-10-30 00:50:19 +03:00
|
|
|
|
|
2015-09-01 23:25:57 +03:00
|
|
|
|
// Search every directory.
|
2016-05-16 17:34:04 +03:00
|
|
|
|
for (std::vector<std::string>::const_iterator p = this->SearchPaths.begin();
|
|
|
|
|
p != this->SearchPaths.end(); ++p) {
|
|
|
|
|
if (helper.CheckDirectory(*p)) {
|
2015-09-01 23:25:57 +03:00
|
|
|
|
return helper.BestPath;
|
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
2015-09-01 23:25:57 +03:00
|
|
|
|
// Couldn't find the program.
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string cmFindProgramCommand::FindNormalProgramDirsPerName()
|
2015-09-01 20:54:33 +03:00
|
|
|
|
{
|
|
|
|
|
// Search the entire path for each name.
|
|
|
|
|
cmFindProgramHelper helper;
|
|
|
|
|
for (std::vector<std::string>::const_iterator ni = this->Names.begin();
|
2016-05-16 17:34:04 +03:00
|
|
|
|
ni != this->Names.end(); ++ni) {
|
2015-09-01 20:54:33 +03:00
|
|
|
|
// Switch to searching for this name.
|
|
|
|
|
helper.SetName(*ni);
|
|
|
|
|
|
2015-10-30 00:50:19 +03:00
|
|
|
|
// Check for the name by itself (e.g. an absolute path).
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (helper.CheckDirectory(std::string())) {
|
2015-10-30 00:50:19 +03:00
|
|
|
|
return helper.BestPath;
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
2015-10-30 00:50:19 +03:00
|
|
|
|
|
2015-09-01 20:54:33 +03:00
|
|
|
|
// Search every directory.
|
2016-05-16 17:34:04 +03:00
|
|
|
|
for (std::vector<std::string>::const_iterator p =
|
|
|
|
|
this->SearchPaths.begin();
|
|
|
|
|
p != this->SearchPaths.end(); ++p) {
|
|
|
|
|
if (helper.CheckDirectory(*p)) {
|
2015-09-01 20:54:33 +03:00
|
|
|
|
return helper.BestPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
2015-09-01 20:54:33 +03:00
|
|
|
|
// Couldn't find the program.
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-01 20:42:14 +03:00
|
|
|
|
std::string cmFindProgramCommand::FindAppBundle()
|
2006-04-13 19:00:52 +04:00
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
|
for (std::vector<std::string>::const_iterator name = this->Names.begin();
|
|
|
|
|
name != this->Names.end(); ++name) {
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
2006-04-13 19:00:52 +04:00
|
|
|
|
std::string appName = *name + std::string(".app");
|
2016-05-16 17:34:04 +03:00
|
|
|
|
std::string appPath =
|
|
|
|
|
cmSystemTools::FindDirectory(appName, this->SearchPaths, true);
|
2006-04-13 19:00:52 +04:00
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (!appPath.empty()) {
|
2006-04-13 19:00:52 +04:00
|
|
|
|
std::string executable = GetBundleExecutable(appPath);
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (!executable.empty()) {
|
2014-10-15 16:54:05 +04:00
|
|
|
|
return cmSystemTools::CollapseFullPath(executable);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
|
}
|
2006-04-13 19:00:52 +04:00
|
|
|
|
|
|
|
|
|
// Couldn't find app bundle
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string cmFindProgramCommand::GetBundleExecutable(std::string bundlePath)
|
|
|
|
|
{
|
|
|
|
|
std::string executable = "";
|
2006-04-14 07:24:09 +04:00
|
|
|
|
(void)bundlePath;
|
2006-04-13 19:00:52 +04:00
|
|
|
|
#if defined(__APPLE__)
|
2012-08-13 21:42:58 +04:00
|
|
|
|
// Started with an example on developer.apple.com about finding bundles
|
2006-04-13 19:00:52 +04:00
|
|
|
|
// and modified from that.
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
2006-04-13 19:00:52 +04:00
|
|
|
|
// Get a CFString of the app bundle path
|
|
|
|
|
// XXX - Is it safe to assume everything is in UTF8?
|
2016-05-16 17:34:04 +03:00
|
|
|
|
CFStringRef bundlePathCFS = CFStringCreateWithCString(
|
|
|
|
|
kCFAllocatorDefault, bundlePath.c_str(), kCFStringEncodingUTF8);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
2006-05-10 23:46:45 +04:00
|
|
|
|
// Make a CFURLRef from the CFString representation of the
|
|
|
|
|
// bundle’s path.
|
2016-05-16 17:34:04 +03:00
|
|
|
|
CFURLRef bundleURL = CFURLCreateWithFileSystemPath(
|
|
|
|
|
kCFAllocatorDefault, bundlePathCFS, kCFURLPOSIXPathStyle, true);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
2006-04-13 19:00:52 +04:00
|
|
|
|
// Make a bundle instance using the URLRef.
|
2016-05-16 17:34:04 +03:00
|
|
|
|
CFBundleRef appBundle = CFBundleCreate(kCFAllocatorDefault, bundleURL);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
2006-04-13 19:00:52 +04:00
|
|
|
|
// returned executableURL is relative to <appbundle>/Contents/MacOS/
|
|
|
|
|
CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
|
if (executableURL != NULL) {
|
2006-04-13 19:00:52 +04:00
|
|
|
|
const int MAX_OSX_PATH_SIZE = 1024;
|
|
|
|
|
char buffer[MAX_OSX_PATH_SIZE];
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
2006-04-13 19:00:52 +04:00
|
|
|
|
// Convert the CFString to a C string
|
2016-05-16 17:34:04 +03:00
|
|
|
|
CFStringGetCString(CFURLGetString(executableURL), buffer,
|
|
|
|
|
MAX_OSX_PATH_SIZE, kCFStringEncodingUTF8);
|
2012-08-13 21:42:58 +04:00
|
|
|
|
|
2006-04-13 19:00:52 +04:00
|
|
|
|
// And finally to a c++ string
|
|
|
|
|
executable = bundlePath + "/Contents/MacOS/" + std::string(buffer);
|
2012-02-25 21:20:36 +04:00
|
|
|
|
// Only release CFURLRef if it's not null
|
2016-05-16 17:34:04 +03:00
|
|
|
|
CFRelease(executableURL);
|
|
|
|
|
}
|
2006-04-13 19:00:52 +04:00
|
|
|
|
|
2012-08-13 21:42:58 +04:00
|
|
|
|
// Any CF objects returned from functions with "create" or
|
2006-04-13 19:00:52 +04:00
|
|
|
|
// "copy" in their names must be released by us!
|
2016-05-16 17:34:04 +03:00
|
|
|
|
CFRelease(bundlePathCFS);
|
|
|
|
|
CFRelease(bundleURL);
|
|
|
|
|
CFRelease(appBundle);
|
2006-04-13 19:00:52 +04:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return executable;
|
|
|
|
|
}
|