cmUtilitySourceCommand.cxx 4.2 KB

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