cmake/Source/cmUtilitySourceCommand.cxx

115 lines
4.1 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"
2017-04-14 19:02:05 +02:00
#include <string.h>
#include "cmMakefile.h"
#include "cmState.h"
#include "cmStateTypes.h"
#include "cmSystemTools.h"
class cmExecutionStatus;
// cmUtilitySourceCommand
2016-07-09 11:21:54 +02:00
bool cmUtilitySourceCommand::InitialPass(std::vector<std::string> const& args,
cmExecutionStatus&)
{
2016-07-09 11:21:54 +02:00
if (args.size() < 3) {
this->SetError("called with incorrect number of arguments");
return false;
2016-07-09 11:21:54 +02:00
}
std::vector<std::string>::const_iterator 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++;
2016-07-09 11:21:54 +02:00
const char* cacheValue = this->Makefile->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.
2013-03-16 19:13:01 +02:00
const char* intDir =
this->Makefile->GetRequiredDefinition("CMAKE_CFG_INTDIR");
bool haveCacheValue = false;
2016-07-09 11:21:54 +02:00
if (this->Makefile->IsOn("CMAKE_CROSSCOMPILING")) {
2018-01-26 17:06:56 +01:00
haveCacheValue = (cacheValue != nullptr);
2016-07-09 11:21:54 +02:00
if (!haveCacheValue) {
std::string msg = "UTILITY_SOURCE is used in cross compiling mode for ";
msg += cacheEntry;
msg += ". If your intention is to run this executable, you need to "
2016-07-09 11:21:54 +02:00
"preload the cache with the full path to a version of that "
"program, which runs on this build machine.";
cmSystemTools::Message(msg.c_str(), "Warning");
}
2016-07-09 11:21:54 +02:00
} else {
cmState* state = this->Makefile->GetState();
2018-08-09 18:06:22 +02:00
haveCacheValue = (cacheValue &&
(strstr(cacheValue, "(IntDir)") == nullptr ||
(intDir && strcmp(intDir, "$(IntDir)") == 0)) &&
(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++;
2015-08-17 11:37:30 +02:00
std::string utilitySource = this->Makefile->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.
2013-03-16 19:13:01 +02:00
std::string cmakeCFGout =
this->Makefile->GetRequiredDefinition("CMAKE_CFG_INTDIR");
2015-08-17 11:37:30 +02:00
std::string utilityDirectory = this->Makefile->GetCurrentBinaryDirectory();
std::string exePath;
2016-07-09 11:21:54 +02:00
if (this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH")) {
exePath = this->Makefile->GetDefinition("EXECUTABLE_OUTPUT_PATH");
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 + "/" +
utilityName + this->Makefile->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.
2016-07-09 11:21:54 +02:00
this->Makefile->AddCacheDefinition(cacheEntry, utilityExecutable.c_str(),
"Path to an internal program.",
2017-04-14 19:02:05 +02:00
cmStateEnums::FILEPATH);
// add a value into the cache that maps from the
// full path to the name of the project
cmSystemTools::ConvertToUnixSlashes(utilityExecutable);
2016-07-09 11:21:54 +02:00
this->Makefile->AddCacheDefinition(utilityExecutable, utilityName.c_str(),
"Executable to project name.",
2017-04-14 19:02:05 +02:00
cmStateEnums::INTERNAL);
2013-03-16 19:13:01 +02:00
return true;
}