cmFindProgramCommand.cxx 7.5 KB

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