cmUtilitySourceCommand.cxx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 "cmUtilitySourceCommand.h"
  11. // cmUtilitySourceCommand
  12. bool cmUtilitySourceCommand
  13. ::InitialPass(std::vector<std::string> const& args, cmExecutionStatus &)
  14. {
  15. if(this->Disallowed(cmPolicies::CMP0034,
  16. "The utility_source command should not be called; see CMP0034."))
  17. { return true; }
  18. if(args.size() < 3)
  19. {
  20. this->SetError("called with incorrect number of arguments");
  21. return false;
  22. }
  23. std::vector<std::string>::const_iterator arg = args.begin();
  24. // The first argument is the cache entry name.
  25. std::string cacheEntry = *arg++;
  26. const char* cacheValue =
  27. this->Makefile->GetDefinition(cacheEntry);
  28. // If it exists already and appears up to date then we are done. If
  29. // the string contains "(IntDir)" but that is not the
  30. // CMAKE_CFG_INTDIR setting then the value is out of date.
  31. const char* intDir =
  32. this->Makefile->GetRequiredDefinition("CMAKE_CFG_INTDIR");
  33. bool haveCacheValue = false;
  34. if (this->Makefile->IsOn("CMAKE_CROSSCOMPILING"))
  35. {
  36. haveCacheValue = (cacheValue != 0);
  37. if (!haveCacheValue)
  38. {
  39. std::string msg = "UTILITY_SOURCE is used in cross compiling mode for ";
  40. msg += cacheEntry;
  41. msg += ". If your intention is to run this executable, you need to "
  42. "preload the cache with the full path to a version of that "
  43. "program, which runs on this build machine.";
  44. cmSystemTools::Message(msg.c_str() ,"Warning");
  45. }
  46. }
  47. else
  48. {
  49. cmCacheManager *manager =
  50. this->Makefile->GetCMakeInstance()->GetCacheManager();
  51. haveCacheValue = (cacheValue &&
  52. (strstr(cacheValue, "(IntDir)") == 0 ||
  53. (intDir && strcmp(intDir, "$(IntDir)") == 0)) &&
  54. (manager->GetCacheMajorVersion() != 0 &&
  55. manager->GetCacheMinorVersion() != 0 ));
  56. }
  57. if(haveCacheValue)
  58. {
  59. return true;
  60. }
  61. // The second argument is the utility's executable name, which will be
  62. // needed later.
  63. std::string utilityName = *arg++;
  64. // The third argument specifies the relative directory of the source
  65. // of the utility.
  66. std::string relativeSource = *arg++;
  67. std::string utilitySource = this->Makefile->GetCurrentDirectory();
  68. utilitySource = utilitySource+"/"+relativeSource;
  69. // If the directory doesn't exist, the source has not been included.
  70. if(!cmSystemTools::FileExists(utilitySource.c_str()))
  71. { return true; }
  72. // Make sure all the files exist in the source directory.
  73. while(arg != args.end())
  74. {
  75. std::string file = utilitySource+"/"+*arg++;
  76. if(!cmSystemTools::FileExists(file.c_str()))
  77. { return true; }
  78. }
  79. // The source exists.
  80. std::string cmakeCFGout =
  81. this->Makefile->GetRequiredDefinition("CMAKE_CFG_INTDIR");
  82. std::string utilityDirectory = this->Makefile->GetCurrentOutputDirectory();
  83. std::string exePath;
  84. if (this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH"))
  85. {
  86. exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
  87. }
  88. if(!exePath.empty())
  89. {
  90. utilityDirectory = exePath;
  91. }
  92. else
  93. {
  94. utilityDirectory += "/"+relativeSource;
  95. }
  96. // Construct the cache entry for the executable's location.
  97. std::string utilityExecutable =
  98. utilityDirectory+"/"+cmakeCFGout+"/"
  99. +utilityName+this->Makefile->GetDefinition("CMAKE_EXECUTABLE_SUFFIX");
  100. // make sure we remove any /./ in the name
  101. cmSystemTools::ReplaceString(utilityExecutable, "/./", "/");
  102. // Enter the value into the cache.
  103. this->Makefile->AddCacheDefinition(cacheEntry,
  104. utilityExecutable.c_str(),
  105. "Path to an internal program.",
  106. cmCacheManager::FILEPATH);
  107. // add a value into the cache that maps from the
  108. // full path to the name of the project
  109. cmSystemTools::ConvertToUnixSlashes(utilityExecutable);
  110. this->Makefile->AddCacheDefinition(utilityExecutable,
  111. utilityName.c_str(),
  112. "Executable to project name.",
  113. cmCacheManager::INTERNAL);
  114. return true;
  115. }