cmUtilitySourceCommand.cxx 4.6 KB

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