cmFindProgramCommand.cxx 7.5 KB

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