cmFindProgramCommand.cxx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*=========================================================================
  2. Program: CMake - Cross-Platform Makefile Generator
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See Copyright.txt or http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #include "cmFindProgramCommand.h"
  14. #include "cmCacheManager.h"
  15. #include <stdlib.h>
  16. #if defined(__APPLE__)
  17. #include <CoreFoundation/CoreFoundation.h>
  18. #endif
  19. cmFindProgramCommand::cmFindProgramCommand()
  20. {
  21. cmSystemTools::ReplaceString(this->GenericDocumentation,
  22. "FIND_XXX", "FIND_PROGRAM");
  23. cmSystemTools::ReplaceString(this->GenericDocumentation,
  24. "CMAKE_XXX_PATH", "CMAKE_PROGRAM_PATH");
  25. cmSystemTools::ReplaceString(this->GenericDocumentation,
  26. "XXX_SYSTEM", "");
  27. cmSystemTools::ReplaceString(this->GenericDocumentation,
  28. "CMAKE_SYSTEM_XXX_PATH",
  29. "CMAKE_SYSTEM_PROGRAM_PATH");
  30. cmSystemTools::ReplaceString(this->GenericDocumentation,
  31. "SEARCH_XXX_DESC", "program");
  32. cmSystemTools::ReplaceString(this->GenericDocumentation,
  33. "SEARCH_XXX", "program");
  34. }
  35. // cmFindProgramCommand
  36. bool cmFindProgramCommand::InitialPass(std::vector<std::string> const& argsIn)
  37. {
  38. this->VariableDocumentation = "Path to a program.";
  39. this->CMakePathName = "PROGRAM";
  40. // call cmFindBase::ParseArguments
  41. if(!this->ParseArguments(argsIn))
  42. {
  43. return false;
  44. }
  45. if(this->AlreadyInCache)
  46. {
  47. return true;
  48. }
  49. std::string result = FindProgram(this->Names);
  50. if(result != "")
  51. {
  52. // Save the value in the cache
  53. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  54. result.c_str(),
  55. this->VariableDocumentation.c_str(),
  56. cmCacheManager::FILEPATH);
  57. return true;
  58. }
  59. this->Makefile->AddCacheDefinition(this->VariableName.c_str(),
  60. (this->VariableName + "-NOTFOUND").c_str(),
  61. this->VariableDocumentation.c_str(),
  62. cmCacheManager::FILEPATH);
  63. return true;
  64. }
  65. std::string cmFindProgramCommand::FindProgram(std::vector<std::string> names)
  66. {
  67. std::string program = "";
  68. // First/last order taken care of in cmFindBase when the paths are setup.
  69. if(this->SearchAppBundleFirst || this->SearchAppBundleLast)
  70. {
  71. program = FindAppBundle(names);
  72. }
  73. if(program.empty() && !this->SearchAppBundleOnly)
  74. {
  75. program = cmSystemTools::FindProgram(names, this->SearchPaths, true);
  76. }
  77. return program;
  78. }
  79. std::string cmFindProgramCommand
  80. ::FindAppBundle(std::vector<std::string> names)
  81. {
  82. for(std::vector<std::string>::const_iterator name = names.begin();
  83. name != names.end() ; ++name)
  84. {
  85. std::string appName = *name + std::string(".app");
  86. std::string appPath = cmSystemTools::FindDirectory(appName.c_str(),
  87. this->SearchPaths,
  88. true);
  89. if ( !appPath.empty() )
  90. {
  91. std::string executable = GetBundleExecutable(appPath);
  92. if (!executable.empty())
  93. {
  94. return cmSystemTools::CollapseFullPath(executable.c_str());
  95. }
  96. }
  97. }
  98. // Couldn't find app bundle
  99. return "";
  100. }
  101. std::string cmFindProgramCommand::GetBundleExecutable(std::string bundlePath)
  102. {
  103. std::string executable = "";
  104. (void)bundlePath;
  105. #if defined(__APPLE__)
  106. // Started with an example on developer.apple.com about finding bundles
  107. // and modified from that.
  108. // Get a CFString of the app bundle path
  109. // XXX - Is it safe to assume everything is in UTF8?
  110. CFStringRef bundlePathCFS =
  111. CFStringCreateWithCString(kCFAllocatorDefault ,
  112. bundlePath.c_str(), kCFStringEncodingUTF8 );
  113. // Make a CFURLRef from the CFString representation of the
  114. // bundle’s path.
  115. CFURLRef bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
  116. bundlePathCFS,
  117. kCFURLPOSIXPathStyle,
  118. true );
  119. // Make a bundle instance using the URLRef.
  120. CFBundleRef appBundle = CFBundleCreate( kCFAllocatorDefault, bundleURL );
  121. // returned executableURL is relative to <appbundle>/Contents/MacOS/
  122. CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
  123. if (executableURL != NULL)
  124. {
  125. const int MAX_OSX_PATH_SIZE = 1024;
  126. char buffer[MAX_OSX_PATH_SIZE];
  127. // Convert the CFString to a C string
  128. CFStringGetCString( CFURLGetString(executableURL), buffer,
  129. MAX_OSX_PATH_SIZE, kCFStringEncodingUTF8 );
  130. // And finally to a c++ string
  131. executable = bundlePath + "/Contents/MacOS/" + std::string(buffer);
  132. }
  133. // Any CF objects returned from functions with "create" or
  134. // "copy" in their names must be released by us!
  135. CFRelease( bundlePathCFS );
  136. CFRelease( bundleURL );
  137. CFRelease( appBundle );
  138. CFRelease( executableURL );
  139. #endif
  140. return executable;
  141. }