cmFindProgramCommand.cxx 7.6 KB

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