cmake/Source/cmTryRunCommand.cxx

360 lines
12 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 "cmTryRunCommand.h"
2016-07-09 11:21:54 +02:00
2020-02-01 23:06:01 +01:00
#include <cstdio>
2017-07-20 19:35:53 +02:00
#include "cmsys/FStream.hxx"
2017-04-14 19:02:05 +02:00
2018-04-23 21:13:27 +02:00
#include "cmDuration.h"
2017-04-14 19:02:05 +02:00
#include "cmMakefile.h"
2019-11-11 23:01:05 +01:00
#include "cmMessageType.h"
2020-08-30 11:54:41 +02:00
#include "cmProperty.h"
2019-11-11 23:01:05 +01:00
#include "cmRange.h"
2017-04-14 19:02:05 +02:00
#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"
#include "cmake.h"
class cmExecutionStatus;
// cmTryRunCommand
2016-07-09 11:21:54 +02:00
bool cmTryRunCommand::InitialPass(std::vector<std::string> const& argv,
cmExecutionStatus&)
{
2016-07-09 11:21:54 +02:00
if (argv.size() < 4) {
return false;
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (this->Makefile->GetCMakeInstance()->GetWorkingMode() ==
cmake::FIND_PACKAGE_MODE) {
this->Makefile->IssueMessage(
2019-11-11 23:01:05 +01:00
MessageType::FATAL_ERROR,
2016-07-09 11:21:54 +02:00
"The TRY_RUN() command is not supported in --find-package mode.");
2012-02-18 12:40:36 +02:00
return false;
2016-07-09 11:21:54 +02:00
}
2012-02-18 12:40:36 +02:00
// build an arg list for TryCompile and extract the runArgs,
std::vector<std::string> tryCompile;
2018-01-26 17:06:56 +01:00
this->CompileResultVariable.clear();
this->RunResultVariable.clear();
this->OutputVariable.clear();
this->RunOutputVariable.clear();
this->CompileOutputVariable.clear();
std::string runArgs;
unsigned int i;
2016-07-09 11:21:54 +02:00
for (i = 1; i < argv.size(); ++i) {
if (argv[i] == "ARGS") {
++i;
while (i < argv.size() && argv[i] != "COMPILE_DEFINITIONS" &&
2019-11-11 23:01:05 +01:00
argv[i] != "CMAKE_FLAGS" && argv[i] != "LINK_OPTIONS" &&
argv[i] != "LINK_LIBRARIES") {
runArgs += " ";
runArgs += argv[i];
++i;
2016-07-09 11:21:54 +02:00
}
if (i < argv.size()) {
tryCompile.push_back(argv[i]);
2012-02-18 12:40:36 +02:00
}
2016-07-09 11:21:54 +02:00
} else {
if (argv[i] == "OUTPUT_VARIABLE") {
if (argv.size() <= (i + 1)) {
cmSystemTools::Error(
"OUTPUT_VARIABLE specified but there is no variable");
return false;
2016-07-09 11:21:54 +02:00
}
i++;
this->OutputVariable = argv[i];
2016-07-09 11:21:54 +02:00
} else if (argv[i] == "RUN_OUTPUT_VARIABLE") {
if (argv.size() <= (i + 1)) {
cmSystemTools::Error(
"RUN_OUTPUT_VARIABLE specified but there is no variable");
return false;
2016-07-09 11:21:54 +02:00
}
i++;
this->RunOutputVariable = argv[i];
2016-07-09 11:21:54 +02:00
} else if (argv[i] == "COMPILE_OUTPUT_VARIABLE") {
if (argv.size() <= (i + 1)) {
cmSystemTools::Error(
"COMPILE_OUTPUT_VARIABLE specified but there is no variable");
return false;
2016-07-09 11:21:54 +02:00
}
i++;
this->CompileOutputVariable = argv[i];
2016-07-09 11:21:54 +02:00
} else {
tryCompile.push_back(argv[i]);
}
}
2016-07-09 11:21:54 +02:00
}
// although they could be used together, don't allow it, because
// using OUTPUT_VARIABLE makes crosscompiling harder
2017-04-14 19:02:05 +02:00
if (!this->OutputVariable.empty() &&
(!this->RunOutputVariable.empty() ||
!this->CompileOutputVariable.empty())) {
cmSystemTools::Error(
"You cannot use OUTPUT_VARIABLE together with COMPILE_OUTPUT_VARIABLE "
"or RUN_OUTPUT_VARIABLE. Please use only COMPILE_OUTPUT_VARIABLE and/or "
"RUN_OUTPUT_VARIABLE.");
return false;
2016-07-09 11:21:54 +02:00
}
bool captureRunOutput = false;
2016-07-09 11:21:54 +02:00
if (!this->OutputVariable.empty()) {
captureRunOutput = true;
2019-11-11 23:01:05 +01:00
tryCompile.emplace_back("OUTPUT_VARIABLE");
tryCompile.push_back(this->OutputVariable);
2016-07-09 11:21:54 +02:00
}
if (!this->CompileOutputVariable.empty()) {
2019-11-11 23:01:05 +01:00
tryCompile.emplace_back("OUTPUT_VARIABLE");
tryCompile.push_back(this->CompileOutputVariable);
2016-07-09 11:21:54 +02:00
}
if (!this->RunOutputVariable.empty()) {
captureRunOutput = true;
2016-07-09 11:21:54 +02:00
}
this->RunResultVariable = argv[0];
this->CompileResultVariable = argv[1];
// do the try compile
2016-07-09 11:21:54 +02:00
int res = this->TryCompileCode(tryCompile, true);
// now try running the command if it compiled
2016-07-09 11:21:54 +02:00
if (!res) {
if (this->OutputFile.empty()) {
2019-11-11 23:01:05 +01:00
cmSystemTools::Error(this->FindErrorMessage);
2016-07-09 11:21:54 +02:00
} else {
// "run" it and capture the output
std::string runOutputContents;
2015-08-17 11:37:30 +02:00
if (this->Makefile->IsOn("CMAKE_CROSSCOMPILING") &&
2016-07-09 11:21:54 +02:00
!this->Makefile->IsDefinitionSet("CMAKE_CROSSCOMPILING_EMULATOR")) {
2018-01-26 17:06:56 +01:00
this->DoNotRunExecutable(
runArgs, argv[3], captureRunOutput ? &runOutputContents : nullptr);
2016-07-09 11:21:54 +02:00
} else {
this->RunExecutable(runArgs, &runOutputContents);
2016-07-09 11:21:54 +02:00
}
// now put the output into the variables
2016-07-09 11:21:54 +02:00
if (!this->RunOutputVariable.empty()) {
2015-04-27 22:25:09 +02:00
this->Makefile->AddDefinition(this->RunOutputVariable,
2020-02-01 23:06:01 +01:00
runOutputContents);
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (!this->OutputVariable.empty()) {
// if the TryCompileCore saved output in this outputVariable then
// prepend that output to this output
2016-07-09 11:21:54 +02:00
const char* compileOutput =
this->Makefile->GetDefinition(this->OutputVariable);
if (compileOutput) {
2019-11-11 23:01:05 +01:00
runOutputContents = compileOutput + runOutputContents;
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
this->Makefile->AddDefinition(this->OutputVariable, runOutputContents);
}
}
2016-07-09 11:21:54 +02:00
}
// if we created a directory etc, then cleanup after ourselves
2016-07-09 11:21:54 +02:00
if (!this->Makefile->GetCMakeInstance()->GetDebugTryCompile()) {
2018-04-23 21:13:27 +02:00
this->CleanupFiles(this->BinaryDirectory);
2016-07-09 11:21:54 +02:00
}
return true;
}
void cmTryRunCommand::RunExecutable(const std::string& runArgs,
std::string* out)
{
int retVal = -1;
2015-08-17 11:37:30 +02:00
std::string finalCommand;
2019-11-11 23:01:05 +01:00
const std::string& emulator =
2016-07-09 11:21:54 +02:00
this->Makefile->GetSafeDefinition("CMAKE_CROSSCOMPILING_EMULATOR");
if (!emulator.empty()) {
2020-02-01 23:06:01 +01:00
std::vector<std::string> emulatorWithArgs = cmExpandedList(emulator);
2016-07-09 11:21:54 +02:00
finalCommand +=
2019-11-11 23:01:05 +01:00
cmSystemTools::ConvertToRunCommandPath(emulatorWithArgs[0]);
2015-08-17 11:37:30 +02:00
finalCommand += " ";
2019-11-11 23:01:05 +01:00
for (std::string const& arg : cmMakeRange(emulatorWithArgs).advance(1)) {
2015-08-17 11:37:30 +02:00
finalCommand += "\"";
2019-11-11 23:01:05 +01:00
finalCommand += arg;
2015-08-17 11:37:30 +02:00
finalCommand += "\"";
finalCommand += " ";
}
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
finalCommand += cmSystemTools::ConvertToRunCommandPath(this->OutputFile);
2016-07-09 11:21:54 +02:00
if (!runArgs.empty()) {
finalCommand += runArgs;
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
bool worked = cmSystemTools::RunSingleCommand(
2019-11-11 23:01:05 +01:00
finalCommand, out, out, &retVal, nullptr, cmSystemTools::OUTPUT_NONE,
cmDuration::zero());
// set the run var
2018-04-23 21:13:27 +02:00
char retChar[16];
const char* retStr;
2016-07-09 11:21:54 +02:00
if (worked) {
sprintf(retChar, "%i", retVal);
2018-04-23 21:13:27 +02:00
retStr = retChar;
2016-07-09 11:21:54 +02:00
} else {
2018-04-23 21:13:27 +02:00
retStr = "FAILED_TO_RUN";
2016-07-09 11:21:54 +02:00
}
2018-04-23 21:13:27 +02:00
this->Makefile->AddCacheDefinition(this->RunResultVariable, retStr,
2017-04-14 19:02:05 +02:00
"Result of TRY_RUN",
cmStateEnums::INTERNAL);
}
/* This is only used when cross compiling. Instead of running the
executable, two cache variables are created which will hold the results
2012-02-18 12:40:36 +02:00
the executable would have produced.
*/
2012-02-18 12:40:36 +02:00
void cmTryRunCommand::DoNotRunExecutable(const std::string& runArgs,
2016-07-09 11:21:54 +02:00
const std::string& srcFile,
std::string* out)
{
// copy the executable out of the CMakeFiles/ directory, so it is not
// removed at the end of TRY_RUN and the user can run it manually
// on the target platform.
2020-02-01 23:06:01 +01:00
std::string copyDest =
cmStrCat(this->Makefile->GetHomeOutputDirectory(), "/CMakeFiles/",
cmSystemTools::GetFilenameWithoutExtension(this->OutputFile), '-',
this->RunResultVariable,
cmSystemTools::GetFilenameExtension(this->OutputFile));
2015-04-27 22:25:09 +02:00
cmSystemTools::CopyFileAlways(this->OutputFile, copyDest);
2020-02-01 23:06:01 +01:00
std::string resultFileName =
cmStrCat(this->Makefile->GetHomeOutputDirectory(), "/TryRunResults.cmake");
2020-02-01 23:06:01 +01:00
std::string detailsString = cmStrCat("For details see ", resultFileName);
2016-07-09 11:21:54 +02:00
std::string internalRunOutputName =
this->RunResultVariable + "__TRYRUN_OUTPUT";
bool error = false;
2019-11-11 23:01:05 +01:00
if (!this->Makefile->GetDefinition(this->RunResultVariable)) {
// if the variables doesn't exist, create it with a helpful error text
// and mark it as advanced
2020-02-01 23:06:01 +01:00
std::string comment =
cmStrCat("Run result of TRY_RUN(), indicates whether the executable "
"would have been able to run on its target platform.\n",
detailsString);
2015-04-27 22:25:09 +02:00
this->Makefile->AddCacheDefinition(this->RunResultVariable,
"PLEASE_FILL_OUT-FAILED_TO_RUN",
2017-04-14 19:02:05 +02:00
comment.c_str(), cmStateEnums::STRING);
2015-08-17 11:37:30 +02:00
cmState* state = this->Makefile->GetState();
2020-08-30 11:54:41 +02:00
cmProp existingValue = state->GetCacheEntryValue(this->RunResultVariable);
2016-07-09 11:21:54 +02:00
if (existingValue) {
2015-08-17 11:37:30 +02:00
state->SetCacheEntryProperty(this->RunResultVariable, "ADVANCED", "1");
2016-07-09 11:21:54 +02:00
}
error = true;
2016-07-09 11:21:54 +02:00
}
// is the output from the executable used ?
2019-11-11 23:01:05 +01:00
if (out) {
if (!this->Makefile->GetDefinition(internalRunOutputName)) {
// if the variables doesn't exist, create it with a helpful error text
// and mark it as advanced
2020-02-01 23:06:01 +01:00
std::string comment = cmStrCat(
2016-07-09 11:21:54 +02:00
"Output of TRY_RUN(), contains the text, which the executable "
2020-02-01 23:06:01 +01:00
"would have printed on stdout and stderr on its target platform.\n",
detailsString);
2017-04-14 19:02:05 +02:00
this->Makefile->AddCacheDefinition(
internalRunOutputName, "PLEASE_FILL_OUT-NOTFOUND", comment.c_str(),
cmStateEnums::STRING);
2015-08-17 11:37:30 +02:00
cmState* state = this->Makefile->GetState();
2020-08-30 11:54:41 +02:00
cmProp existing = state->GetCacheEntryValue(internalRunOutputName);
2016-07-09 11:21:54 +02:00
if (existing) {
state->SetCacheEntryProperty(internalRunOutputName, "ADVANCED", "1");
}
error = true;
}
2016-07-09 11:21:54 +02:00
}
2016-07-09 11:21:54 +02:00
if (error) {
static bool firstTryRun = true;
2014-08-03 19:52:23 +02:00
cmsys::ofstream file(resultFileName.c_str(),
2016-07-09 11:21:54 +02:00
firstTryRun ? std::ios::out : std::ios::app);
if (file) {
if (firstTryRun) {
/* clang-format off */
file << "# This file was generated by CMake because it detected "
"TRY_RUN() commands\n"
"# in crosscompiling mode. It will be overwritten by the next "
"CMake run.\n"
"# Copy it to a safe location, set the variables to "
"appropriate values\n"
"# and use it then to preset the CMake cache (using -C).\n\n";
2016-07-09 11:21:54 +02:00
/* clang-format on */
}
2020-02-01 23:06:01 +01:00
std::string comment =
cmStrCat('\n', this->RunResultVariable,
"\n indicates whether the executable would have been able "
"to run on its\n"
2020-02-01 23:06:01 +01:00
" target platform. If so, set ",
this->RunResultVariable,
" to\n"
" the exit code (in many cases 0 for success), otherwise "
2020-02-01 23:06:01 +01:00
"enter \"FAILED_TO_RUN\".\n");
2019-11-11 23:01:05 +01:00
if (out) {
comment += internalRunOutputName;
2016-07-09 11:21:54 +02:00
comment +=
"\n contains the text the executable "
"would have printed on stdout and stderr.\n"
" If the executable would not have been able to run, set ";
comment += internalRunOutputName;
comment += " empty.\n"
" Otherwise check if the output is evaluated by the "
"calling CMake code. If so,\n"
" check what the source file would have printed when "
"called with the given arguments.\n";
2016-07-09 11:21:54 +02:00
}
comment += "The ";
comment += this->CompileResultVariable;
comment += " variable holds the build result for this TRY_RUN().\n\n"
"Source file : ";
comment += srcFile + "\n";
comment += "Executable : ";
comment += copyDest + "\n";
comment += "Run arguments : ";
comment += runArgs;
comment += "\n";
2015-08-17 11:37:30 +02:00
comment += " Called from: " + this->Makefile->FormatListFileStack();
cmsys::SystemTools::ReplaceString(comment, "\n", "\n# ");
file << comment << "\n\n";
2014-08-03 19:52:23 +02:00
file << "set( " << this->RunResultVariable << " \n \""
2015-04-27 22:25:09 +02:00
<< this->Makefile->GetDefinition(this->RunResultVariable)
<< "\"\n CACHE STRING \"Result from TRY_RUN\" FORCE)\n\n";
2019-11-11 23:01:05 +01:00
if (out) {
2014-08-03 19:52:23 +02:00
file << "set( " << internalRunOutputName << " \n \""
2015-04-27 22:25:09 +02:00
<< this->Makefile->GetDefinition(internalRunOutputName)
<< "\"\n CACHE STRING \"Output from TRY_RUN\" FORCE)\n\n";
}
2016-07-09 11:21:54 +02:00
file.close();
}
firstTryRun = false;
2020-02-01 23:06:01 +01:00
std::string errorMessage =
cmStrCat("TRY_RUN() invoked in cross-compiling mode, "
"please set the following cache variables "
"appropriately:\n ",
this->RunResultVariable, " (advanced)\n");
2019-11-11 23:01:05 +01:00
if (out) {
errorMessage += " " + internalRunOutputName + " (advanced)\n";
2016-07-09 11:21:54 +02:00
}
errorMessage += detailsString;
2019-11-11 23:01:05 +01:00
cmSystemTools::Error(errorMessage);
return;
2016-07-09 11:21:54 +02:00
}
2019-11-11 23:01:05 +01:00
if (out) {
2015-04-27 22:25:09 +02:00
(*out) = this->Makefile->GetDefinition(internalRunOutputName);
2016-07-09 11:21:54 +02:00
}
}