cmFindProgramCommand.cxx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #include "cmFindProgramCommand.h"
  4. #include <algorithm>
  5. #include <string>
  6. #include <utility>
  7. #include "cmMakefile.h"
  8. #include "cmMessageType.h"
  9. #include "cmPolicies.h"
  10. #include "cmStateTypes.h"
  11. #include "cmStringAlgorithms.h"
  12. #include "cmSystemTools.h"
  13. #include "cmValue.h"
  14. #include "cmWindowsRegistry.h"
  15. class cmExecutionStatus;
  16. #if defined(__APPLE__)
  17. # include <CoreFoundation/CFBundle.h>
  18. # include <CoreFoundation/CFString.h>
  19. # include <CoreFoundation/CFURL.h>
  20. #endif
  21. struct cmFindProgramHelper
  22. {
  23. cmFindProgramHelper(std::string debugName, cmMakefile* makefile,
  24. cmFindBase const* base)
  25. : DebugSearches(std::move(debugName), base)
  26. , Makefile(makefile)
  27. , FindBase(base)
  28. , PolicyCMP0109(makefile->GetPolicyStatus(cmPolicies::CMP0109))
  29. {
  30. #if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)
  31. // Consider platform-specific extensions.
  32. this->Extensions.push_back(".com");
  33. this->Extensions.push_back(".exe");
  34. #endif
  35. // Consider original name with no extensions.
  36. this->Extensions.emplace_back();
  37. }
  38. // List of valid extensions.
  39. std::vector<std::string> Extensions;
  40. // Keep track of the best program file found so far.
  41. std::string BestPath;
  42. // Current names under consideration.
  43. std::vector<std::string> Names;
  44. // Debug state
  45. cmFindBaseDebugState DebugSearches;
  46. cmMakefile* Makefile;
  47. cmFindBase const* FindBase;
  48. cmPolicies::PolicyStatus PolicyCMP0109;
  49. void AddName(std::string const& name) { this->Names.push_back(name); }
  50. void SetName(std::string const& name)
  51. {
  52. this->Names.clear();
  53. this->AddName(name);
  54. }
  55. bool CheckCompoundNames()
  56. {
  57. return std::any_of(this->Names.begin(), this->Names.end(),
  58. [this](std::string const& n) -> bool {
  59. // Only perform search relative to current directory
  60. // if the file name contains a directory separator.
  61. return n.find('/') != std::string::npos &&
  62. this->CheckDirectoryForName("", n);
  63. });
  64. }
  65. bool CheckDirectory(std::string const& path)
  66. {
  67. return std::any_of(this->Names.begin(), this->Names.end(),
  68. [this, &path](std::string const& n) -> bool {
  69. return this->CheckDirectoryForName(path, n);
  70. });
  71. }
  72. bool CheckDirectoryForName(std::string const& path, std::string const& name)
  73. {
  74. return std::any_of(this->Extensions.begin(), this->Extensions.end(),
  75. [this, &path, &name](std::string const& ext) -> bool {
  76. if (!ext.empty() && cmHasSuffix(name, ext)) {
  77. return false;
  78. }
  79. std::string testNameExt = cmStrCat(name, ext);
  80. std::string testPath =
  81. cmSystemTools::CollapseFullPath(testNameExt, path);
  82. if (this->FileIsExecutable(testPath)) {
  83. testPath =
  84. cmSystemTools::ToNormalizedPathOnDisk(testPath);
  85. if (this->FindBase->Validate(testPath)) {
  86. this->BestPath = testPath;
  87. this->DebugSearches.FoundAt(testPath);
  88. return true;
  89. }
  90. }
  91. this->DebugSearches.FailedAt(testPath);
  92. return false;
  93. });
  94. }
  95. bool FileIsExecutable(std::string const& file) const
  96. {
  97. if (!this->FileIsExecutableCMP0109(file)) {
  98. return false;
  99. }
  100. #ifdef _WIN32
  101. // Pretend the Windows "python" app installer alias does not exist.
  102. if (cmSystemTools::LowerCase(file).find("/windowsapps/python") !=
  103. std::string::npos) {
  104. std::string dest;
  105. if (cmSystemTools::ReadSymlink(file, dest) &&
  106. cmHasLiteralSuffix(dest, "\\AppInstallerPythonRedirector.exe")) {
  107. return false;
  108. }
  109. }
  110. #endif
  111. return true;
  112. }
  113. bool FileIsExecutableCMP0109(std::string const& file) const
  114. {
  115. switch (this->PolicyCMP0109) {
  116. case cmPolicies::OLD:
  117. return cmSystemTools::FileExists(file, true);
  118. case cmPolicies::NEW:
  119. return cmSystemTools::FileIsExecutable(file);
  120. default:
  121. break;
  122. }
  123. bool const isExeOld = cmSystemTools::FileExists(file, true);
  124. bool const isExeNew = cmSystemTools::FileIsExecutable(file);
  125. if (isExeNew == isExeOld) {
  126. return isExeNew;
  127. }
  128. if (isExeNew) {
  129. this->Makefile->IssueMessage(
  130. MessageType::AUTHOR_WARNING,
  131. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0109),
  132. "\n"
  133. "The file\n"
  134. " ",
  135. file,
  136. "\n"
  137. "is executable but not readable. "
  138. "CMake is ignoring it for compatibility."));
  139. } else {
  140. this->Makefile->IssueMessage(
  141. MessageType::AUTHOR_WARNING,
  142. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0109),
  143. "\n"
  144. "The file\n"
  145. " ",
  146. file,
  147. "\n"
  148. "is readable but not executable. "
  149. "CMake is using it for compatibility."));
  150. }
  151. return isExeOld;
  152. }
  153. };
  154. cmFindProgramCommand::cmFindProgramCommand(cmExecutionStatus& status)
  155. : cmFindBase("find_program", status)
  156. {
  157. this->NamesPerDirAllowed = true;
  158. this->VariableDocumentation = "Path to a program.";
  159. this->VariableType = cmStateEnums::FILEPATH;
  160. // Windows Registry views
  161. // When policy CMP0134 is not NEW, rely on previous behavior:
  162. if (this->Makefile->GetPolicyStatus(cmPolicies::CMP0134) !=
  163. cmPolicies::NEW) {
  164. if (this->Makefile->GetDefinition("CMAKE_SIZEOF_VOID_P") == "8") {
  165. this->RegistryView = cmWindowsRegistry::View::Reg64_32;
  166. } else {
  167. this->RegistryView = cmWindowsRegistry::View::Reg32_64;
  168. }
  169. } else {
  170. this->RegistryView = cmWindowsRegistry::View::Both;
  171. }
  172. }
  173. // cmFindProgramCommand
  174. bool cmFindProgramCommand::InitialPass(std::vector<std::string> const& argsIn)
  175. {
  176. this->CMakePathName = "PROGRAM";
  177. // call cmFindBase::ParseArguments
  178. if (!this->ParseArguments(argsIn)) {
  179. return false;
  180. }
  181. this->DebugMode = this->ComputeIfDebugModeWanted(this->VariableName);
  182. if (this->AlreadyDefined) {
  183. this->NormalizeFindResult();
  184. return true;
  185. }
  186. std::string const result = this->FindProgram();
  187. this->StoreFindResult(result);
  188. return true;
  189. }
  190. std::string cmFindProgramCommand::FindProgram()
  191. {
  192. std::string program;
  193. if (this->SearchAppBundleFirst || this->SearchAppBundleOnly) {
  194. program = this->FindAppBundle();
  195. }
  196. if (program.empty() && !this->SearchAppBundleOnly) {
  197. program = this->FindNormalProgram();
  198. }
  199. if (program.empty() && this->SearchAppBundleLast) {
  200. program = this->FindAppBundle();
  201. }
  202. return program;
  203. }
  204. std::string cmFindProgramCommand::FindNormalProgram()
  205. {
  206. if (this->NamesPerDir) {
  207. return this->FindNormalProgramNamesPerDir();
  208. }
  209. return this->FindNormalProgramDirsPerName();
  210. }
  211. std::string cmFindProgramCommand::FindNormalProgramNamesPerDir()
  212. {
  213. // Search for all names in each directory.
  214. cmFindProgramHelper helper(this->FindCommandName, this->Makefile, this);
  215. for (std::string const& n : this->Names) {
  216. helper.AddName(n);
  217. }
  218. // Check for the names themselves if they contain a directory separator.
  219. if (helper.CheckCompoundNames()) {
  220. return helper.BestPath;
  221. }
  222. // Search every directory.
  223. for (std::string const& sp : this->SearchPaths) {
  224. if (helper.CheckDirectory(sp)) {
  225. return helper.BestPath;
  226. }
  227. }
  228. // Couldn't find the program.
  229. return "";
  230. }
  231. std::string cmFindProgramCommand::FindNormalProgramDirsPerName()
  232. {
  233. // Search the entire path for each name.
  234. cmFindProgramHelper helper(this->FindCommandName, this->Makefile, this);
  235. for (std::string const& n : this->Names) {
  236. // Switch to searching for this name.
  237. helper.SetName(n);
  238. // Check for the names themselves if they contain a directory separator.
  239. if (helper.CheckCompoundNames()) {
  240. return helper.BestPath;
  241. }
  242. // Search every directory.
  243. for (std::string const& sp : this->SearchPaths) {
  244. if (helper.CheckDirectory(sp)) {
  245. return helper.BestPath;
  246. }
  247. }
  248. }
  249. // Couldn't find the program.
  250. return "";
  251. }
  252. std::string cmFindProgramCommand::FindAppBundle()
  253. {
  254. for (std::string const& name : this->Names) {
  255. std::string appName = name + std::string(".app");
  256. std::string appPath =
  257. cmSystemTools::FindDirectory(appName, this->SearchPaths, true);
  258. if (!appPath.empty()) {
  259. std::string executable = this->GetBundleExecutable(appPath);
  260. if (!executable.empty()) {
  261. return cmSystemTools::CollapseFullPath(executable);
  262. }
  263. }
  264. }
  265. // Couldn't find app bundle
  266. return "";
  267. }
  268. std::string cmFindProgramCommand::GetBundleExecutable(
  269. std::string const& bundlePath)
  270. {
  271. std::string executable;
  272. (void)bundlePath;
  273. #if defined(__APPLE__)
  274. // Started with an example on developer.apple.com about finding bundles
  275. // and modified from that.
  276. // Get a CFString of the app bundle path
  277. // XXX - Is it safe to assume everything is in UTF8?
  278. CFStringRef bundlePathCFS = CFStringCreateWithCString(
  279. kCFAllocatorDefault, bundlePath.c_str(), kCFStringEncodingUTF8);
  280. // Make a CFURLRef from the CFString representation of the
  281. // bundle’s path.
  282. CFURLRef bundleURL = CFURLCreateWithFileSystemPath(
  283. kCFAllocatorDefault, bundlePathCFS, kCFURLPOSIXPathStyle, true);
  284. // Make a bundle instance using the URLRef.
  285. CFBundleRef appBundle = CFBundleCreate(kCFAllocatorDefault, bundleURL);
  286. // returned executableURL is relative to <appbundle>/Contents/MacOS/
  287. CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
  288. if (executableURL) {
  289. int const MAX_OSX_PATH_SIZE = 1024;
  290. UInt8 buffer[MAX_OSX_PATH_SIZE];
  291. if (CFURLGetFileSystemRepresentation(executableURL, false, buffer,
  292. MAX_OSX_PATH_SIZE)) {
  293. executable = bundlePath + "/Contents/MacOS/" +
  294. std::string(reinterpret_cast<char*>(buffer));
  295. }
  296. // Only release CFURLRef if it's not null
  297. CFRelease(executableURL);
  298. }
  299. // Any CF objects returned from functions with "create" or
  300. // "copy" in their names must be released by us!
  301. CFRelease(bundlePathCFS);
  302. CFRelease(bundleURL);
  303. CFRelease(appBundle);
  304. #endif
  305. return executable;
  306. }
  307. bool cmFindProgram(std::vector<std::string> const& args,
  308. cmExecutionStatus& status)
  309. {
  310. return cmFindProgramCommand(status).InitialPass(args);
  311. }