cmFindProgramCommand.cxx 11 KB

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