cmFindProgramCommand.cxx 9.8 KB

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