cmFindProgramCommand.cxx 7.5 KB

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