cmFindProgramCommand.cxx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 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::string const& n : this->Names) {
  40. if (this->CheckDirectoryForName(path, n)) {
  41. return true;
  42. }
  43. }
  44. return false;
  45. }
  46. bool CheckDirectoryForName(std::string const& path, std::string const& name)
  47. {
  48. for (std::string const& ext : this->Extensions) {
  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 const result = FindProgram();
  89. if (!result.empty()) {
  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::string const& n : this->Names) {
  127. helper.AddName(n);
  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::string const& sp : this->SearchPaths) {
  135. if (helper.CheckDirectory(sp)) {
  136. return helper.BestPath;
  137. }
  138. }
  139. // Couldn't find the program.
  140. return "";
  141. }
  142. std::string cmFindProgramCommand::FindNormalProgramDirsPerName()
  143. {
  144. // Search the entire path for each name.
  145. cmFindProgramHelper helper;
  146. for (std::string const& n : this->Names) {
  147. // Switch to searching for this name.
  148. helper.SetName(n);
  149. // Check for the name by itself (e.g. an absolute path).
  150. if (helper.CheckDirectory(std::string())) {
  151. return helper.BestPath;
  152. }
  153. // Search every directory.
  154. for (std::string const& sp : this->SearchPaths) {
  155. if (helper.CheckDirectory(sp)) {
  156. return helper.BestPath;
  157. }
  158. }
  159. }
  160. // Couldn't find the program.
  161. return "";
  162. }
  163. std::string cmFindProgramCommand::FindAppBundle()
  164. {
  165. for (std::string const& name : this->Names) {
  166. std::string appName = name + std::string(".app");
  167. std::string appPath =
  168. cmSystemTools::FindDirectory(appName, this->SearchPaths, true);
  169. if (!appPath.empty()) {
  170. std::string executable = GetBundleExecutable(appPath);
  171. if (!executable.empty()) {
  172. return cmSystemTools::CollapseFullPath(executable);
  173. }
  174. }
  175. }
  176. // Couldn't find app bundle
  177. return "";
  178. }
  179. std::string cmFindProgramCommand::GetBundleExecutable(
  180. std::string const& bundlePath)
  181. {
  182. std::string executable;
  183. (void)bundlePath;
  184. #if defined(__APPLE__)
  185. // Started with an example on developer.apple.com about finding bundles
  186. // and modified from that.
  187. // Get a CFString of the app bundle path
  188. // XXX - Is it safe to assume everything is in UTF8?
  189. CFStringRef bundlePathCFS = CFStringCreateWithCString(
  190. kCFAllocatorDefault, bundlePath.c_str(), kCFStringEncodingUTF8);
  191. // Make a CFURLRef from the CFString representation of the
  192. // bundle’s path.
  193. CFURLRef bundleURL = CFURLCreateWithFileSystemPath(
  194. kCFAllocatorDefault, bundlePathCFS, kCFURLPOSIXPathStyle, true);
  195. // Make a bundle instance using the URLRef.
  196. CFBundleRef appBundle = CFBundleCreate(kCFAllocatorDefault, bundleURL);
  197. // returned executableURL is relative to <appbundle>/Contents/MacOS/
  198. CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
  199. if (executableURL != nullptr) {
  200. const int MAX_OSX_PATH_SIZE = 1024;
  201. char buffer[MAX_OSX_PATH_SIZE];
  202. // Convert the CFString to a C string
  203. CFStringGetCString(CFURLGetString(executableURL), buffer,
  204. MAX_OSX_PATH_SIZE, kCFStringEncodingUTF8);
  205. // And finally to a c++ string
  206. executable = bundlePath + "/Contents/MacOS/" + std::string(buffer);
  207. // Only release CFURLRef if it's not null
  208. CFRelease(executableURL);
  209. }
  210. // Any CF objects returned from functions with "create" or
  211. // "copy" in their names must be released by us!
  212. CFRelease(bundlePathCFS);
  213. CFRelease(bundleURL);
  214. CFRelease(appBundle);
  215. #endif
  216. return executable;
  217. }