cmFindProgramCommand.cxx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. class cmExecutionStatus;
  14. #if defined(__APPLE__)
  15. # include <CoreFoundation/CoreFoundation.h>
  16. #endif
  17. struct cmFindProgramHelper
  18. {
  19. cmFindProgramHelper(std::string debugName, cmMakefile* makefile,
  20. cmFindBase const* base)
  21. : DebugSearches(std::move(debugName), base)
  22. , Makefile(makefile)
  23. , PolicyCMP0109(makefile->GetPolicyStatus(cmPolicies::CMP0109))
  24. {
  25. #if defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__)
  26. // Consider platform-specific extensions.
  27. this->Extensions.push_back(".com");
  28. this->Extensions.push_back(".exe");
  29. #endif
  30. // Consider original name with no extensions.
  31. this->Extensions.emplace_back();
  32. }
  33. // List of valid extensions.
  34. std::vector<std::string> Extensions;
  35. // Keep track of the best program file found so far.
  36. std::string BestPath;
  37. // Current names under consideration.
  38. std::vector<std::string> Names;
  39. // Current name with extension under consideration.
  40. std::string TestNameExt;
  41. // Current full path under consideration.
  42. std::string TestPath;
  43. // Debug state
  44. cmFindBaseDebugState DebugSearches;
  45. cmMakefile* Makefile;
  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. // Only perform search relative to current directory
  68. // if the file name contains a directory separator.
  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. this->TestNameExt = cmStrCat(name, ext);
  80. this->TestPath = cmSystemTools::CollapseFullPath(
  81. this->TestNameExt, path);
  82. bool exists = this->FileIsExecutable(this->TestPath);
  83. exists ? this->DebugSearches.FoundAt(this->TestPath)
  84. : this->DebugSearches.FailedAt(this->TestPath);
  85. if (exists) {
  86. this->BestPath = this->TestPath;
  87. return true;
  88. }
  89. return false;
  90. });
  91. }
  92. bool FileIsExecutable(std::string const& file) const
  93. {
  94. switch (this->PolicyCMP0109) {
  95. case cmPolicies::OLD:
  96. return cmSystemTools::FileExists(file, true);
  97. case cmPolicies::NEW:
  98. case cmPolicies::REQUIRED_ALWAYS:
  99. case cmPolicies::REQUIRED_IF_USED:
  100. return cmSystemTools::FileIsExecutable(file);
  101. default:
  102. break;
  103. }
  104. bool const isExeOld = cmSystemTools::FileExists(file, true);
  105. bool const isExeNew = cmSystemTools::FileIsExecutable(file);
  106. if (isExeNew == isExeOld) {
  107. return isExeNew;
  108. }
  109. if (isExeNew) {
  110. this->Makefile->IssueMessage(
  111. MessageType::AUTHOR_WARNING,
  112. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0109),
  113. "\n"
  114. "The file\n"
  115. " ",
  116. file,
  117. "\n"
  118. "is executable but not readable. "
  119. "CMake is ignoring it for compatibility."));
  120. } else {
  121. this->Makefile->IssueMessage(
  122. MessageType::AUTHOR_WARNING,
  123. cmStrCat(cmPolicies::GetPolicyWarning(cmPolicies::CMP0109),
  124. "\n"
  125. "The file\n"
  126. " ",
  127. file,
  128. "\n"
  129. "is readable but not executable. "
  130. "CMake is using it for compatibility."));
  131. }
  132. return isExeOld;
  133. }
  134. };
  135. cmFindProgramCommand::cmFindProgramCommand(cmExecutionStatus& status)
  136. : cmFindBase("find_program", status)
  137. {
  138. this->NamesPerDirAllowed = true;
  139. this->VariableDocumentation = "Path to a program.";
  140. this->VariableType = cmStateEnums::FILEPATH;
  141. }
  142. // cmFindProgramCommand
  143. bool cmFindProgramCommand::InitialPass(std::vector<std::string> const& argsIn)
  144. {
  145. this->DebugMode = this->ComputeIfDebugModeWanted();
  146. this->CMakePathName = "PROGRAM";
  147. // call cmFindBase::ParseArguments
  148. if (!this->ParseArguments(argsIn)) {
  149. return false;
  150. }
  151. if (this->AlreadyInCache) {
  152. this->NormalizeFindResult();
  153. return true;
  154. }
  155. std::string const result = this->FindProgram();
  156. this->StoreFindResult(result);
  157. return true;
  158. }
  159. std::string cmFindProgramCommand::FindProgram()
  160. {
  161. std::string program;
  162. if (this->SearchAppBundleFirst || this->SearchAppBundleOnly) {
  163. program = this->FindAppBundle();
  164. }
  165. if (program.empty() && !this->SearchAppBundleOnly) {
  166. program = this->FindNormalProgram();
  167. }
  168. if (program.empty() && this->SearchAppBundleLast) {
  169. program = this->FindAppBundle();
  170. }
  171. return program;
  172. }
  173. std::string cmFindProgramCommand::FindNormalProgram()
  174. {
  175. if (this->NamesPerDir) {
  176. return this->FindNormalProgramNamesPerDir();
  177. }
  178. return this->FindNormalProgramDirsPerName();
  179. }
  180. std::string cmFindProgramCommand::FindNormalProgramNamesPerDir()
  181. {
  182. // Search for all names in each directory.
  183. cmFindProgramHelper helper(this->FindCommandName, this->Makefile, this);
  184. for (std::string const& n : this->Names) {
  185. helper.AddName(n);
  186. }
  187. // Check for the names themselves if they contain a directory separator.
  188. if (helper.CheckCompoundNames()) {
  189. return helper.BestPath;
  190. }
  191. // Search every directory.
  192. for (std::string const& sp : this->SearchPaths) {
  193. if (helper.CheckDirectory(sp)) {
  194. return helper.BestPath;
  195. }
  196. }
  197. // Couldn't find the program.
  198. return "";
  199. }
  200. std::string cmFindProgramCommand::FindNormalProgramDirsPerName()
  201. {
  202. // Search the entire path for each name.
  203. cmFindProgramHelper helper(this->FindCommandName, this->Makefile, this);
  204. for (std::string const& n : this->Names) {
  205. // Switch to searching for this name.
  206. helper.SetName(n);
  207. // Check for the names themselves if they contain a directory separator.
  208. if (helper.CheckCompoundNames()) {
  209. return helper.BestPath;
  210. }
  211. // Search every directory.
  212. for (std::string const& sp : this->SearchPaths) {
  213. if (helper.CheckDirectory(sp)) {
  214. return helper.BestPath;
  215. }
  216. }
  217. }
  218. // Couldn't find the program.
  219. return "";
  220. }
  221. std::string cmFindProgramCommand::FindAppBundle()
  222. {
  223. for (std::string const& name : this->Names) {
  224. std::string appName = name + std::string(".app");
  225. std::string appPath =
  226. cmSystemTools::FindDirectory(appName, this->SearchPaths, true);
  227. if (!appPath.empty()) {
  228. std::string executable = this->GetBundleExecutable(appPath);
  229. if (!executable.empty()) {
  230. return cmSystemTools::CollapseFullPath(executable);
  231. }
  232. }
  233. }
  234. // Couldn't find app bundle
  235. return "";
  236. }
  237. std::string cmFindProgramCommand::GetBundleExecutable(
  238. std::string const& bundlePath)
  239. {
  240. std::string executable;
  241. (void)bundlePath;
  242. #if defined(__APPLE__)
  243. // Started with an example on developer.apple.com about finding bundles
  244. // and modified from that.
  245. // Get a CFString of the app bundle path
  246. // XXX - Is it safe to assume everything is in UTF8?
  247. CFStringRef bundlePathCFS = CFStringCreateWithCString(
  248. kCFAllocatorDefault, bundlePath.c_str(), kCFStringEncodingUTF8);
  249. // Make a CFURLRef from the CFString representation of the
  250. // bundle’s path.
  251. CFURLRef bundleURL = CFURLCreateWithFileSystemPath(
  252. kCFAllocatorDefault, bundlePathCFS, kCFURLPOSIXPathStyle, true);
  253. // Make a bundle instance using the URLRef.
  254. CFBundleRef appBundle = CFBundleCreate(kCFAllocatorDefault, bundleURL);
  255. // returned executableURL is relative to <appbundle>/Contents/MacOS/
  256. CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
  257. if (executableURL != nullptr) {
  258. const int MAX_OSX_PATH_SIZE = 1024;
  259. UInt8 buffer[MAX_OSX_PATH_SIZE];
  260. if (CFURLGetFileSystemRepresentation(executableURL, false, buffer,
  261. MAX_OSX_PATH_SIZE)) {
  262. executable = bundlePath + "/Contents/MacOS/" +
  263. std::string(reinterpret_cast<char*>(buffer));
  264. }
  265. // Only release CFURLRef if it's not null
  266. CFRelease(executableURL);
  267. }
  268. // Any CF objects returned from functions with "create" or
  269. // "copy" in their names must be released by us!
  270. CFRelease(bundlePathCFS);
  271. CFRelease(bundleURL);
  272. CFRelease(appBundle);
  273. #endif
  274. return executable;
  275. }
  276. bool cmFindProgram(std::vector<std::string> const& args,
  277. cmExecutionStatus& status)
  278. {
  279. return cmFindProgramCommand(status).InitialPass(args);
  280. }