cmake/Source/cmUtilitySourceCommand.cxx

119 lines
4.2 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmUtilitySourceCommand.h"
2020-02-01 23:06:01 +01:00
#include <cstring>
2017-04-14 19:02:05 +02:00
2020-02-01 23:06:01 +01:00
#include "cmExecutionStatus.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
#include "cmState.h"
#include "cmStateTypes.h"
2020-02-01 23:06:01 +01:00
#include "cmStringAlgorithms.h"
2017-04-14 19:02:05 +02:00
#include "cmSystemTools.h"
2021-11-20 13:41:27 +01:00
#include "cmValue.h"
2017-04-14 19:02:05 +02:00
// cmUtilitySourceCommand
2020-02-01 23:06:01 +01:00
bool cmUtilitySourceCommand(std::vector<std::string> const& args,
cmExecutionStatus& status)
{
2016-07-09 11:21:54 +02:00
if (args.size() < 3) {
2020-02-01 23:06:01 +01:00
status.SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
auto arg = args.begin();
2013-03-16 19:13:01 +02:00
// The first argument is the cache entry name.
2017-07-20 19:35:53 +02:00
std::string const& cacheEntry = *arg++;
2021-11-20 13:41:27 +01:00
cmValue cacheValue = status.GetMakefile().GetDefinition(cacheEntry);
// If it exists already and appears up to date then we are done. If
// the string contains "(IntDir)" but that is not the
// CMAKE_CFG_INTDIR setting then the value is out of date.
2018-10-28 12:09:07 +01:00
std::string const& intDir =
2020-02-01 23:06:01 +01:00
status.GetMakefile().GetRequiredDefinition("CMAKE_CFG_INTDIR");
bool haveCacheValue = false;
2020-02-01 23:06:01 +01:00
if (status.GetMakefile().IsOn("CMAKE_CROSSCOMPILING")) {
2018-01-26 17:06:56 +01:00
haveCacheValue = (cacheValue != nullptr);
2016-07-09 11:21:54 +02:00
if (!haveCacheValue) {
2020-02-01 23:06:01 +01:00
std::string msg = cmStrCat(
"UTILITY_SOURCE is used in cross compiling mode for ", cacheEntry,
". If your intention is to run this executable, you need to "
"preload the cache with the full path to a version of that "
"program, which runs on this build machine.");
2019-11-11 23:01:05 +01:00
cmSystemTools::Message(msg, "Warning");
}
2016-07-09 11:21:54 +02:00
} else {
2020-02-01 23:06:01 +01:00
cmState* state = status.GetMakefile().GetState();
2018-08-09 18:06:22 +02:00
haveCacheValue = (cacheValue &&
2021-09-14 00:13:48 +02:00
(strstr(cacheValue->c_str(), "(IntDir)") == nullptr ||
2018-10-28 12:09:07 +01:00
(intDir == "$(IntDir)")) &&
2018-08-09 18:06:22 +02:00
(state->GetCacheMajorVersion() != 0 &&
state->GetCacheMinorVersion() != 0));
2016-07-09 11:21:54 +02:00
}
if (haveCacheValue) {
return true;
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// The second argument is the utility's executable name, which will be
// needed later.
2017-07-20 19:35:53 +02:00
std::string const& utilityName = *arg++;
2013-03-16 19:13:01 +02:00
// The third argument specifies the relative directory of the source
// of the utility.
2017-07-20 19:35:53 +02:00
std::string const& relativeSource = *arg++;
2020-02-01 23:06:01 +01:00
std::string utilitySource = status.GetMakefile().GetCurrentSourceDirectory();
2016-07-09 11:21:54 +02:00
utilitySource = utilitySource + "/" + relativeSource;
2013-03-16 19:13:01 +02:00
// If the directory doesn't exist, the source has not been included.
2018-04-23 21:13:27 +02:00
if (!cmSystemTools::FileExists(utilitySource)) {
2016-07-09 11:21:54 +02:00
return true;
}
2013-03-16 19:13:01 +02:00
// Make sure all the files exist in the source directory.
2016-07-09 11:21:54 +02:00
while (arg != args.end()) {
std::string file = utilitySource + "/" + *arg++;
2018-04-23 21:13:27 +02:00
if (!cmSystemTools::FileExists(file)) {
2016-07-09 11:21:54 +02:00
return true;
}
2016-07-09 11:21:54 +02:00
}
2013-03-16 19:13:01 +02:00
// The source exists.
2019-11-11 23:01:05 +01:00
const std::string& cmakeCFGout =
2020-02-01 23:06:01 +01:00
status.GetMakefile().GetRequiredDefinition("CMAKE_CFG_INTDIR");
std::string utilityDirectory =
status.GetMakefile().GetCurrentBinaryDirectory();
std::string exePath;
2021-11-20 13:41:27 +01:00
if (cmValue d =
2021-09-14 00:13:48 +02:00
status.GetMakefile().GetDefinition("EXECUTABLE_OUTPUT_PATH")) {
exePath = *d;
2016-07-09 11:21:54 +02:00
}
if (!exePath.empty()) {
utilityDirectory = exePath;
2016-07-09 11:21:54 +02:00
} else {
utilityDirectory += "/" + relativeSource;
}
2013-03-16 19:13:01 +02:00
// Construct the cache entry for the executable's location.
2016-07-09 11:21:54 +02:00
std::string utilityExecutable = utilityDirectory + "/" + cmakeCFGout + "/" +
2020-02-01 23:06:01 +01:00
utilityName +
2021-09-14 00:13:48 +02:00
*status.GetMakefile().GetDefinition("CMAKE_EXECUTABLE_SUFFIX");
// make sure we remove any /./ in the name
cmSystemTools::ReplaceString(utilityExecutable, "/./", "/");
2013-03-16 19:13:01 +02:00
// Enter the value into the cache.
2020-08-30 11:54:41 +02:00
status.GetMakefile().AddCacheDefinition(cacheEntry, utilityExecutable,
"Path to an internal program.",
cmStateEnums::FILEPATH);
// add a value into the cache that maps from the
// full path to the name of the project
cmSystemTools::ConvertToUnixSlashes(utilityExecutable);
2020-08-30 11:54:41 +02:00
status.GetMakefile().AddCacheDefinition(utilityExecutable, utilityName,
"Executable to project name.",
cmStateEnums::INTERNAL);
2013-03-16 19:13:01 +02:00
return true;
}