cmFindProgramCommand.cxx 8.1 KB

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