cmUtilitySourceCommand.cxx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "cmExecutionStatus.h"
  6. #include "cmMakefile.h"
  7. #include "cmState.h"
  8. #include "cmStateTypes.h"
  9. #include "cmStringAlgorithms.h"
  10. #include "cmSystemTools.h"
  11. // cmUtilitySourceCommand
  12. bool cmUtilitySourceCommand(std::vector<std::string> const& args,
  13. cmExecutionStatus& status)
  14. {
  15. if (args.size() < 3) {
  16. status.SetError("called with incorrect number of arguments");
  17. return false;
  18. }
  19. auto arg = args.begin();
  20. // The first argument is the cache entry name.
  21. std::string const& cacheEntry = *arg++;
  22. const char* cacheValue = status.GetMakefile().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. std::string const& intDir =
  27. status.GetMakefile().GetRequiredDefinition("CMAKE_CFG_INTDIR");
  28. bool haveCacheValue = false;
  29. if (status.GetMakefile().IsOn("CMAKE_CROSSCOMPILING")) {
  30. haveCacheValue = (cacheValue != nullptr);
  31. if (!haveCacheValue) {
  32. std::string msg = cmStrCat(
  33. "UTILITY_SOURCE is used in cross compiling mode for ", cacheEntry,
  34. ". 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, "Warning");
  38. }
  39. } else {
  40. cmState* state = status.GetMakefile().GetState();
  41. haveCacheValue = (cacheValue &&
  42. (strstr(cacheValue, "(IntDir)") == nullptr ||
  43. (intDir == "$(IntDir)")) &&
  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 const& utilityName = *arg++;
  53. // The third argument specifies the relative directory of the source
  54. // of the utility.
  55. std::string const& relativeSource = *arg++;
  56. std::string utilitySource = status.GetMakefile().GetCurrentSourceDirectory();
  57. utilitySource = utilitySource + "/" + relativeSource;
  58. // If the directory doesn't exist, the source has not been included.
  59. if (!cmSystemTools::FileExists(utilitySource)) {
  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)) {
  66. return true;
  67. }
  68. }
  69. // The source exists.
  70. const std::string& cmakeCFGout =
  71. status.GetMakefile().GetRequiredDefinition("CMAKE_CFG_INTDIR");
  72. std::string utilityDirectory =
  73. status.GetMakefile().GetCurrentBinaryDirectory();
  74. std::string exePath;
  75. if (status.GetMakefile().GetDefinition("EXECUTABLE_OUTPUT_PATH")) {
  76. exePath = status.GetMakefile().GetDefinition("EXECUTABLE_OUTPUT_PATH");
  77. }
  78. if (!exePath.empty()) {
  79. utilityDirectory = exePath;
  80. } else {
  81. utilityDirectory += "/" + relativeSource;
  82. }
  83. // Construct the cache entry for the executable's location.
  84. std::string utilityExecutable = utilityDirectory + "/" + cmakeCFGout + "/" +
  85. utilityName +
  86. status.GetMakefile().GetDefinition("CMAKE_EXECUTABLE_SUFFIX");
  87. // make sure we remove any /./ in the name
  88. cmSystemTools::ReplaceString(utilityExecutable, "/./", "/");
  89. // Enter the value into the cache.
  90. status.GetMakefile().AddCacheDefinition(
  91. cacheEntry, utilityExecutable.c_str(), "Path to an internal program.",
  92. cmStateEnums::FILEPATH);
  93. // add a value into the cache that maps from the
  94. // full path to the name of the project
  95. cmSystemTools::ConvertToUnixSlashes(utilityExecutable);
  96. status.GetMakefile().AddCacheDefinition(
  97. utilityExecutable, utilityName.c_str(), "Executable to project name.",
  98. cmStateEnums::INTERNAL);
  99. return true;
  100. }