cmFindProgramCommand.cxx 7.5 KB

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