cmUtilitySourceCommand.cxx 4.1 KB

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