cmFindProgramCommand.cxx 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmFindProgramCommand.h"
  11. #include "cmCacheManager.h"
  12. #include <stdlib.h>
  13. #if defined(__APPLE__)
  14. #include <CoreFoundation/CoreFoundation.h>
  15. #endif
  16. cmFindProgramCommand::cmFindProgramCommand()
  17. {
  18. cmSystemTools::ReplaceString(this->GenericDocumentation,
  19. "FIND_XXX", "find_program");
  20. cmSystemTools::ReplaceString(this->GenericDocumentation,
  21. "CMAKE_XXX_PATH", "CMAKE_PROGRAM_PATH");
  22. cmSystemTools::ReplaceString(this->GenericDocumentation,
  23. "CMAKE_XXX_MAC_PATH",
  24. "CMAKE_APPBUNDLE_PATH");
  25. cmSystemTools::ReplaceString(this->GenericDocumentation,
  26. "CMAKE_SYSTEM_XXX_MAC_PATH",
  27. "CMAKE_SYSTEM_APPBUNDLE_PATH");
  28. cmSystemTools::ReplaceString(this->GenericDocumentation,
  29. "XXX_SYSTEM", "");
  30. cmSystemTools::ReplaceString(this->GenericDocumentation,
  31. "CMAKE_SYSTEM_XXX_PATH",
  32. "CMAKE_SYSTEM_PROGRAM_PATH");
  33. cmSystemTools::ReplaceString(this->GenericDocumentation,
  34. "SEARCH_XXX_DESC", "program");
  35. cmSystemTools::ReplaceString(this->GenericDocumentation,
  36. "SEARCH_XXX", "program");
  37. cmSystemTools::ReplaceString(this->GenericDocumentation,
  38. "XXX_SUBDIR", "[s]bin");
  39. cmSystemTools::ReplaceString(this->GenericDocumentation,
  40. "CMAKE_FIND_ROOT_PATH_MODE_XXX",
  41. "CMAKE_FIND_ROOT_PATH_MODE_PROGRAM");
  42. }
  43. // cmFindProgramCommand
  44. bool cmFindProgramCommand
  45. ::InitialPass(std::vector<std::string> const& argsIn, cmExecutionStatus &)
  46. {
  47. this->VariableDocumentation = "Path to a program.";
  48. this->CMakePathName = "PROGRAM";
  49. // call cmFindBase::ParseArguments
  50. if(!this->ParseArguments(argsIn))
  51. {
  52. return false;
  53. }
  54. if(this->AlreadyInCache)
  55. {
  56. // If the user specifies the entry on the command line without a
  57. // type we should add the type and docstring but keep the original
  58. // value.
  59. if(this->AlreadyInCacheWithoutMetaInfo)
  60. {
  61. this->Makefile->AddCacheDefinition(this->VariableName.c_str(), "",
  62. this->VariableDocumentation.c_str(),
  63. cmCacheManager::FILEPATH);
  64. }
  65. return true;
  66. }
  67. std::string result = FindProgram(this->Names);
  68. if(result != "")
  69. {
  70. // Save the value in the cache
  71. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  72. result.c_str(),
  73. this->VariableDocumentation.c_str(),
  74. cmCacheManager::FILEPATH);
  75. return true;
  76. }
  77. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  78. (this->VariableName + "-NOTFOUND").c_str(),
  79. this->VariableDocumentation.c_str(),
  80. cmCacheManager::FILEPATH);
  81. return true;
  82. }
  83. std::string cmFindProgramCommand::FindProgram(std::vector<std::string> names)
  84. {
  85. std::string program = "";
  86. if(this->SearchAppBundleFirst || this->SearchAppBundleOnly)
  87. {
  88. program = FindAppBundle(names);
  89. }
  90. if(program.empty() && !this->SearchAppBundleOnly)
  91. {
  92. program = cmSystemTools::FindProgram(names, this->SearchPaths, true);
  93. }
  94. if(program.empty() && this->SearchAppBundleLast)
  95. {
  96. program = this->FindAppBundle(names);
  97. }
  98. return program;
  99. }
  100. std::string cmFindProgramCommand
  101. ::FindAppBundle(std::vector<std::string> names)
  102. {
  103. for(std::vector<std::string>::const_iterator name = names.begin();
  104. name != names.end() ; ++name)
  105. {
  106. std::string appName = *name + std::string(".app");
  107. std::string appPath = cmSystemTools::FindDirectory(appName.c_str(),
  108. this->SearchPaths,
  109. true);
  110. if ( !appPath.empty() )
  111. {
  112. std::string executable = GetBundleExecutable(appPath);
  113. if (!executable.empty())
  114. {
  115. return cmSystemTools::CollapseFullPath(executable.c_str());
  116. }
  117. }
  118. }
  119. // Couldn't find app bundle
  120. return "";
  121. }
  122. std::string cmFindProgramCommand::GetBundleExecutable(std::string bundlePath)
  123. {
  124. std::string executable = "";
  125. (void)bundlePath;
  126. #if defined(__APPLE__)
  127. // Started with an example on developer.apple.com about finding bundles
  128. // and modified from that.
  129. // Get a CFString of the app bundle path
  130. // XXX - Is it safe to assume everything is in UTF8?
  131. CFStringRef bundlePathCFS =
  132. CFStringCreateWithCString(kCFAllocatorDefault ,
  133. bundlePath.c_str(), kCFStringEncodingUTF8 );
  134. // Make a CFURLRef from the CFString representation of the
  135. // bundle’s path.
  136. CFURLRef bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
  137. bundlePathCFS,
  138. kCFURLPOSIXPathStyle,
  139. true );
  140. // Make a bundle instance using the URLRef.
  141. CFBundleRef appBundle = CFBundleCreate( kCFAllocatorDefault, bundleURL );
  142. // returned executableURL is relative to <appbundle>/Contents/MacOS/
  143. CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
  144. if (executableURL != NULL)
  145. {
  146. const int MAX_OSX_PATH_SIZE = 1024;
  147. char buffer[MAX_OSX_PATH_SIZE];
  148. // Convert the CFString to a C string
  149. CFStringGetCString( CFURLGetString(executableURL), buffer,
  150. MAX_OSX_PATH_SIZE, kCFStringEncodingUTF8 );
  151. // And finally to a c++ string
  152. executable = bundlePath + "/Contents/MacOS/" + std::string(buffer);
  153. }
  154. // Any CF objects returned from functions with "create" or
  155. // "copy" in their names must be released by us!
  156. CFRelease( bundlePathCFS );
  157. CFRelease( bundleURL );
  158. CFRelease( appBundle );
  159. CFRelease( executableURL );
  160. #endif
  161. return executable;
  162. }