cmFindProgramCommand.cxx 8.4 KB

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