cmFindProgramCommand.cxx 11 KB

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