cmFindProgramCommand.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <stdlib.h>
  12. #if defined(__APPLE__)
  13. #include <CoreFoundation/CoreFoundation.h>
  14. #endif
  15. // cmFindProgramCommand
  16. bool cmFindProgramCommand
  17. ::InitialPass(std::vector<std::string> const& argsIn, cmExecutionStatus &)
  18. {
  19. this->VariableDocumentation = "Path to a program.";
  20. this->CMakePathName = "PROGRAM";
  21. // call cmFindBase::ParseArguments
  22. if(!this->ParseArguments(argsIn))
  23. {
  24. return false;
  25. }
  26. if(this->AlreadyInCache)
  27. {
  28. // If the user specifies the entry on the command line without a
  29. // type we should add the type and docstring but keep the original
  30. // value.
  31. if(this->AlreadyInCacheWithoutMetaInfo)
  32. {
  33. this->Makefile->AddCacheDefinition(this->VariableName, "",
  34. this->VariableDocumentation.c_str(),
  35. cmState::FILEPATH);
  36. }
  37. return true;
  38. }
  39. std::string result = FindProgram(this->Names);
  40. if(result != "")
  41. {
  42. // Save the value in the cache
  43. this->Makefile->AddCacheDefinition(this->VariableName,
  44. result.c_str(),
  45. this->VariableDocumentation.c_str(),
  46. cmState::FILEPATH);
  47. return true;
  48. }
  49. this->Makefile->AddCacheDefinition(this->VariableName,
  50. (this->VariableName + "-NOTFOUND").c_str(),
  51. this->VariableDocumentation.c_str(),
  52. cmState::FILEPATH);
  53. return true;
  54. }
  55. std::string cmFindProgramCommand::FindProgram(std::vector<std::string> names)
  56. {
  57. std::string program = "";
  58. if(this->SearchAppBundleFirst || this->SearchAppBundleOnly)
  59. {
  60. program = FindAppBundle(names);
  61. }
  62. if(program.empty() && !this->SearchAppBundleOnly)
  63. {
  64. program = cmSystemTools::FindProgram(names, this->SearchPaths, true);
  65. }
  66. if(program.empty() && this->SearchAppBundleLast)
  67. {
  68. program = this->FindAppBundle(names);
  69. }
  70. return program;
  71. }
  72. std::string cmFindProgramCommand
  73. ::FindAppBundle(std::vector<std::string> names)
  74. {
  75. for(std::vector<std::string>::const_iterator name = names.begin();
  76. name != names.end() ; ++name)
  77. {
  78. std::string appName = *name + std::string(".app");
  79. std::string appPath = cmSystemTools::FindDirectory(appName,
  80. this->SearchPaths,
  81. true);
  82. if ( !appPath.empty() )
  83. {
  84. std::string executable = GetBundleExecutable(appPath);
  85. if (!executable.empty())
  86. {
  87. return cmSystemTools::CollapseFullPath(executable);
  88. }
  89. }
  90. }
  91. // Couldn't find app bundle
  92. return "";
  93. }
  94. std::string cmFindProgramCommand::GetBundleExecutable(std::string bundlePath)
  95. {
  96. std::string executable = "";
  97. (void)bundlePath;
  98. #if defined(__APPLE__)
  99. // Started with an example on developer.apple.com about finding bundles
  100. // and modified from that.
  101. // Get a CFString of the app bundle path
  102. // XXX - Is it safe to assume everything is in UTF8?
  103. CFStringRef bundlePathCFS =
  104. CFStringCreateWithCString(kCFAllocatorDefault ,
  105. bundlePath.c_str(), kCFStringEncodingUTF8 );
  106. // Make a CFURLRef from the CFString representation of the
  107. // bundle’s path.
  108. CFURLRef bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
  109. bundlePathCFS,
  110. kCFURLPOSIXPathStyle,
  111. true );
  112. // Make a bundle instance using the URLRef.
  113. CFBundleRef appBundle = CFBundleCreate( kCFAllocatorDefault, bundleURL );
  114. // returned executableURL is relative to <appbundle>/Contents/MacOS/
  115. CFURLRef executableURL = CFBundleCopyExecutableURL(appBundle);
  116. if (executableURL != NULL)
  117. {
  118. const int MAX_OSX_PATH_SIZE = 1024;
  119. char buffer[MAX_OSX_PATH_SIZE];
  120. // Convert the CFString to a C string
  121. CFStringGetCString( CFURLGetString(executableURL), buffer,
  122. MAX_OSX_PATH_SIZE, kCFStringEncodingUTF8 );
  123. // And finally to a c++ string
  124. executable = bundlePath + "/Contents/MacOS/" + std::string(buffer);
  125. // Only release CFURLRef if it's not null
  126. CFRelease( executableURL );
  127. }
  128. // Any CF objects returned from functions with "create" or
  129. // "copy" in their names must be released by us!
  130. CFRelease( bundlePathCFS );
  131. CFRelease( bundleURL );
  132. CFRelease( appBundle );
  133. #endif
  134. return executable;
  135. }