Merge branch 'release-3.29'
[kiteware-cmake.git] / Source / cmUtilitySourceCommand.cxx
blob2805a33bbde34005d04dc5cfe2ec2a96af334c99
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"
5 #include <cstring>
7 #include "cmExecutionStatus.h"
8 #include "cmMakefile.h"
9 #include "cmState.h"
10 #include "cmStateTypes.h"
11 #include "cmStringAlgorithms.h"
12 #include "cmSystemTools.h"
13 #include "cmValue.h"
15 // cmUtilitySourceCommand
16 bool cmUtilitySourceCommand(std::vector<std::string> const& args,
17 cmExecutionStatus& status)
19 if (args.size() < 3) {
20 status.SetError("called with incorrect number of arguments");
21 return false;
24 auto arg = args.begin();
26 // The first argument is the cache entry name.
27 std::string const& cacheEntry = *arg++;
28 cmValue cacheValue = status.GetMakefile().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 std::string const& intDir =
33 status.GetMakefile().GetRequiredDefinition("CMAKE_CFG_INTDIR");
35 bool haveCacheValue = false;
36 if (status.GetMakefile().IsOn("CMAKE_CROSSCOMPILING")) {
37 haveCacheValue = (cacheValue != nullptr);
38 if (!haveCacheValue) {
39 std::string msg = cmStrCat(
40 "UTILITY_SOURCE is used in cross compiling mode for ", cacheEntry,
41 ". 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, "Warning");
46 } else {
47 cmState* state = status.GetMakefile().GetState();
48 haveCacheValue = (cacheValue &&
49 (strstr(cacheValue->c_str(), "(IntDir)") == nullptr ||
50 (intDir == "$(IntDir)")) &&
51 (state->GetCacheMajorVersion() != 0 &&
52 state->GetCacheMinorVersion() != 0));
55 if (haveCacheValue) {
56 return true;
59 // The second argument is the utility's executable name, which will be
60 // needed later.
61 std::string const& utilityName = *arg++;
63 // The third argument specifies the relative directory of the source
64 // of the utility.
65 std::string const& relativeSource = *arg++;
66 std::string utilitySource = status.GetMakefile().GetCurrentSourceDirectory();
67 utilitySource = utilitySource + "/" + relativeSource;
69 // If the directory doesn't exist, the source has not been included.
70 if (!cmSystemTools::FileExists(utilitySource)) {
71 return true;
74 // Make sure all the files exist in the source directory.
75 while (arg != args.end()) {
76 std::string file = utilitySource + "/" + *arg++;
77 if (!cmSystemTools::FileExists(file)) {
78 return true;
82 // The source exists.
83 const std::string& cmakeCFGout =
84 status.GetMakefile().GetRequiredDefinition("CMAKE_CFG_INTDIR");
85 std::string utilityDirectory =
86 status.GetMakefile().GetCurrentBinaryDirectory();
87 std::string exePath;
88 if (cmValue d =
89 status.GetMakefile().GetDefinition("EXECUTABLE_OUTPUT_PATH")) {
90 exePath = *d;
92 if (!exePath.empty()) {
93 utilityDirectory = exePath;
94 } else {
95 utilityDirectory += "/" + relativeSource;
98 // Construct the cache entry for the executable's location.
99 std::string utilityExecutable = utilityDirectory + "/" + cmakeCFGout + "/" +
100 utilityName +
101 *status.GetMakefile().GetDefinition("CMAKE_EXECUTABLE_SUFFIX");
103 // make sure we remove any /./ in the name
104 cmSystemTools::ReplaceString(utilityExecutable, "/./", "/");
106 // Enter the value into the cache.
107 status.GetMakefile().AddCacheDefinition(cacheEntry, utilityExecutable,
108 "Path to an internal program.",
109 cmStateEnums::FILEPATH);
110 // add a value into the cache that maps from the
111 // full path to the name of the project
112 cmSystemTools::ConvertToUnixSlashes(utilityExecutable);
113 status.GetMakefile().AddCacheDefinition(utilityExecutable, utilityName,
114 "Executable to project name.",
115 cmStateEnums::INTERNAL);
117 return true;