cmake/Source/cmFindProgramCommand.cxx

255 lines
7.1 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmFindProgramCommand.h"
2016-07-09 11:21:54 +02:00
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmStateTypes.h"
#include "cmSystemTools.h"
class cmExecutionStatus;
#if defined(__APPLE__)
2018-08-09 18:06:22 +02:00
# include <CoreFoundation/CoreFoundation.h>
#endif
2011-01-16 11:35:12 +01:00
2015-11-17 17:22:37 +01:00
struct cmFindProgramHelper
{
cmFindProgramHelper()
2016-07-09 11:21:54 +02:00
{
#if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)
2015-11-17 17:22:37 +01:00
// Consider platform-specific extensions.
this->Extensions.push_back(".com");
this->Extensions.push_back(".exe");
#endif
// Consider original name with no extensions.
2018-04-23 21:13:27 +02:00
this->Extensions.emplace_back();
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01: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-07-09 11:21:54 +02:00
void AddName(std::string const& name) { this->Names.push_back(name); }
2015-11-17 17:22:37 +01:00
void SetName(std::string const& name)
2016-07-09 11:21:54 +02:00
{
2015-11-17 17:22:37 +01:00
this->Names.clear();
this->AddName(name);
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
bool CheckDirectory(std::string const& path)
2016-07-09 11:21:54 +02:00
{
2018-01-26 17:06:56 +01:00
for (std::string const& n : this->Names) {
if (this->CheckDirectoryForName(path, n)) {
2015-11-17 17:22:37 +01:00
return true;
}
}
2016-07-09 11:21:54 +02:00
return false;
}
2015-11-17 17:22:37 +01:00
bool CheckDirectoryForName(std::string const& path, std::string const& name)
2016-07-09 11:21:54 +02:00
{
2018-01-26 17:06:56 +01:00
for (std::string const& ext : this->Extensions) {
2015-11-17 17:22:37 +01:00
this->TestPath = path;
this->TestPath += name;
2018-01-26 17:06:56 +01:00
if (!ext.empty() && cmSystemTools::StringEndsWith(name, ext.c_str())) {
2015-11-17 17:22:37 +01:00
continue;
2016-07-09 11:21:54 +02:00
}
2018-01-26 17:06:56 +01:00
this->TestPath += ext;
2016-07-09 11:21:54 +02:00
if (cmSystemTools::FileExists(this->TestPath, true)) {
2015-11-17 17:22:37 +01:00
this->BestPath = cmSystemTools::CollapseFullPath(this->TestPath);
return true;
}
}
2016-07-09 11:21:54 +02:00
return false;
}
2015-11-17 17:22:37 +01:00
};
cmFindProgramCommand::cmFindProgramCommand()
{
this->NamesPerDirAllowed = true;
}
// cmFindProgramCommand
2016-07-09 11:21:54 +02:00
bool cmFindProgramCommand::InitialPass(std::vector<std::string> const& argsIn,
cmExecutionStatus&)
{
this->VariableDocumentation = "Path to a program.";
this->CMakePathName = "PROGRAM";
// call cmFindBase::ParseArguments
2016-07-09 11:21:54 +02:00
if (!this->ParseArguments(argsIn)) {
return false;
2016-07-09 11:21:54 +02:00
}
if (this->AlreadyInCache) {
// 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-07-09 11:21:54 +02:00
if (this->AlreadyInCacheWithoutMetaInfo) {
2015-04-27 22:25:09 +02:00
this->Makefile->AddCacheDefinition(this->VariableName, "",
this->VariableDocumentation.c_str(),
2017-04-14 19:02:05 +02:00
cmStateEnums::FILEPATH);
}
2016-07-09 11:21:54 +02:00
return true;
}
2018-01-26 17:06:56 +01:00
std::string const result = FindProgram();
if (!result.empty()) {
// Save the value in the cache
2016-07-09 11:21:54 +02:00
this->Makefile->AddCacheDefinition(this->VariableName, result.c_str(),
this->VariableDocumentation.c_str(),
2017-04-14 19:02:05 +02:00
cmStateEnums::FILEPATH);
2013-03-16 19:13:01 +02:00
return true;
2016-07-09 11:21:54 +02:00
}
this->Makefile->AddCacheDefinition(
this->VariableName, (this->VariableName + "-NOTFOUND").c_str(),
2017-04-14 19:02:05 +02:00
this->VariableDocumentation.c_str(), cmStateEnums::FILEPATH);
return true;
}
2015-11-17 17:22:37 +01:00
std::string cmFindProgramCommand::FindProgram()
{
2017-04-14 19:02:05 +02:00
std::string program;
2016-07-09 11:21:54 +02:00
if (this->SearchAppBundleFirst || this->SearchAppBundleOnly) {
2015-11-17 17:22:37 +01:00
program = FindAppBundle();
2016-07-09 11:21:54 +02:00
}
if (program.empty() && !this->SearchAppBundleOnly) {
2015-11-17 17:22:37 +01:00
program = this->FindNormalProgram();
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (program.empty() && this->SearchAppBundleLast) {
2015-11-17 17:22:37 +01:00
program = this->FindAppBundle();
2016-07-09 11:21:54 +02:00
}
return program;
}
2015-11-17 17:22:37 +01:00
std::string cmFindProgramCommand::FindNormalProgram()
{
2016-07-09 11:21:54 +02:00
if (this->NamesPerDir) {
2015-11-17 17:22:37 +01:00
return this->FindNormalProgramNamesPerDir();
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
return this->FindNormalProgramDirsPerName();
2015-11-17 17:22:37 +01:00
}
std::string cmFindProgramCommand::FindNormalProgramNamesPerDir()
{
// Search for all names in each directory.
cmFindProgramHelper helper;
2018-01-26 17:06:56 +01:00
for (std::string const& n : this->Names) {
helper.AddName(n);
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// Check for the names themselves (e.g. absolute paths).
2016-07-09 11:21:54 +02:00
if (helper.CheckDirectory(std::string())) {
2015-11-17 17:22:37 +01:00
return helper.BestPath;
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// Search every directory.
2018-01-26 17:06:56 +01:00
for (std::string const& sp : this->SearchPaths) {
if (helper.CheckDirectory(sp)) {
2015-11-17 17:22:37 +01:00
return helper.BestPath;
}
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// Couldn't find the program.
return "";
}
std::string cmFindProgramCommand::FindNormalProgramDirsPerName()
{
// Search the entire path for each name.
cmFindProgramHelper helper;
2018-01-26 17:06:56 +01:00
for (std::string const& n : this->Names) {
2015-11-17 17:22:37 +01:00
// Switch to searching for this name.
2018-01-26 17:06:56 +01:00
helper.SetName(n);
2015-11-17 17:22:37 +01:00
// Check for the name by itself (e.g. an absolute path).
2016-07-09 11:21:54 +02:00
if (helper.CheckDirectory(std::string())) {
2015-11-17 17:22:37 +01:00
return helper.BestPath;
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// Search every directory.
2018-01-26 17:06:56 +01:00
for (std::string const& sp : this->SearchPaths) {
if (helper.CheckDirectory(sp)) {
2015-11-17 17:22:37 +01:00
return helper.BestPath;
}
}
2016-07-09 11:21:54 +02:00
}
2015-11-17 17:22:37 +01:00
// Couldn't find the program.
return "";
}
std::string cmFindProgramCommand::FindAppBundle()
{
2018-01-26 17:06:56 +01:00
for (std::string const& name : this->Names) {
2013-03-16 19:13:01 +02:00
2018-01-26 17:06:56 +01:00
std::string appName = name + std::string(".app");
2016-07-09 11:21:54 +02:00
std::string appPath =
cmSystemTools::FindDirectory(appName, this->SearchPaths, true);
2016-07-09 11:21:54 +02:00
if (!appPath.empty()) {
std::string executable = GetBundleExecutable(appPath);
2016-07-09 11:21:54 +02:00
if (!executable.empty()) {
2015-04-27 22:25:09 +02:00
return cmSystemTools::CollapseFullPath(executable);
2013-03-16 19:13:01 +02:00
}
}
2016-07-09 11:21:54 +02:00
}
// Couldn't find app bundle
return "";
}
2017-07-20 19:35:53 +02:00
std::string cmFindProgramCommand::GetBundleExecutable(
std::string const& bundlePath)
{
2017-04-14 19:02:05 +02:00
std::string executable;
(void)bundlePath;
#if defined(__APPLE__)
2013-03-16 19:13:01 +02:00
// Started with an example on developer.apple.com about finding bundles
// and modified from that.
2013-03-16 19:13:01 +02:00
// Get a CFString of the app bundle path
// XXX - Is it safe to assume everything is in UTF8?
2016-07-09 11:21:54 +02:00
CFStringRef bundlePathCFS = CFStringCreateWithCString(
kCFAllocatorDefault, bundlePath.c_str(), kCFStringEncodingUTF8);
2013-03-16 19:13:01 +02:00
// Make a CFURLRef from the CFString representation of the
// bundles path.
2016-07-09 11:21:54 +02:00
CFURLRef bundleURL = CFURLCreateWithFileSystemPath(
kCFAllocatorDefault, bundlePathCFS, kCFURLPOSIXPathStyle, true);
2013-03-16 19:13:01 +02:00
// Make a bundle instance using the URLRef.
2016-07-09 11:21:54 +02:00
CFBundleRef appBundle = CFBundleCreate(kCFAllocatorDefault, bundleURL);
2013-03-16 19:13:01 +02:00
// returned executableURL is relative to <appbundle>/Contents/MacOS/
CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
2013-03-16 19:13:01 +02:00
2018-01-26 17:06:56 +01:00
if (executableURL != nullptr) {
const int MAX_OSX_PATH_SIZE = 1024;
char buffer[MAX_OSX_PATH_SIZE];
2013-03-16 19:13:01 +02:00
// Convert the CFString to a C string
2016-07-09 11:21:54 +02:00
CFStringGetCString(CFURLGetString(executableURL), buffer,
MAX_OSX_PATH_SIZE, kCFStringEncodingUTF8);
2013-03-16 19:13:01 +02:00
// And finally to a c++ string
executable = bundlePath + "/Contents/MacOS/" + std::string(buffer);
2012-04-19 19:04:21 +03:00
// Only release CFURLRef if it's not null
2016-07-09 11:21:54 +02:00
CFRelease(executableURL);
}
2013-03-16 19:13:01 +02:00
// Any CF objects returned from functions with "create" or
// "copy" in their names must be released by us!
2016-07-09 11:21:54 +02:00
CFRelease(bundlePathCFS);
CFRelease(bundleURL);
CFRelease(appBundle);
#endif
return executable;
}