cmCoreTryCompile: Move target type selection logic to try_compile

This is specific to `try_compile` since `try_run` always needs an
executable.  Move the logic out of the common code path.
This commit is contained in:
Brad King
2022-07-29 14:56:00 -04:00
parent 781e1b191a
commit 067ba3a2bd
4 changed files with 30 additions and 25 deletions

View File

@@ -236,34 +236,13 @@ std::set<std::string> const ghs_platform_vars{
}
bool cmCoreTryCompile::TryCompileCode(std::vector<std::string> const& argv,
bool isTryRun)
cmStateEnums::TargetType targetType)
{
std::string const& resultVar = argv[0];
this->OutputFile.clear();
// which signature were we called with ?
this->SrcFileSignature = true;
cmStateEnums::TargetType targetType = cmStateEnums::EXECUTABLE;
cmValue tt = this->Makefile->GetDefinition("CMAKE_TRY_COMPILE_TARGET_TYPE");
if (!isTryRun && cmNonempty(tt)) {
if (*tt == cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE)) {
targetType = cmStateEnums::EXECUTABLE;
} else if (*tt ==
cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY)) {
targetType = cmStateEnums::STATIC_LIBRARY;
} else {
this->Makefile->IssueMessage(
MessageType::FATAL_ERROR,
cmStrCat("Invalid value '", *tt,
"' for CMAKE_TRY_COMPILE_TARGET_TYPE. Only '",
cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE),
"' and '",
cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY),
"' are allowed."));
return false;
}
}
std::string sourceDirectory;
std::string projectName;
std::string targetName;

View File

@@ -30,7 +30,8 @@ public:
* commands, such as TryRun can access the same logic without
* duplication.
*/
bool TryCompileCode(std::vector<std::string> const& argv, bool isTryRun);
bool TryCompileCode(std::vector<std::string> const& argv,
cmStateEnums::TargetType targetType);
/**
* This deletes all the files created by TryCompileCode.

View File

@@ -6,6 +6,10 @@
#include "cmExecutionStatus.h"
#include "cmMakefile.h"
#include "cmMessageType.h"
#include "cmState.h"
#include "cmStateTypes.h"
#include "cmStringAlgorithms.h"
#include "cmValue.h"
#include "cmake.h"
bool cmTryCompileCommand(std::vector<std::string> const& args,
@@ -24,8 +28,29 @@ bool cmTryCompileCommand(std::vector<std::string> const& args,
return false;
}
cmStateEnums::TargetType targetType = cmStateEnums::EXECUTABLE;
cmValue tt = mf.GetDefinition("CMAKE_TRY_COMPILE_TARGET_TYPE");
if (cmNonempty(tt)) {
if (*tt == cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE)) {
targetType = cmStateEnums::EXECUTABLE;
} else if (*tt ==
cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY)) {
targetType = cmStateEnums::STATIC_LIBRARY;
} else {
mf.IssueMessage(
MessageType::FATAL_ERROR,
cmStrCat("Invalid value '", *tt,
"' for CMAKE_TRY_COMPILE_TARGET_TYPE. Only '",
cmState::GetTargetTypeName(cmStateEnums::EXECUTABLE),
"' and '",
cmState::GetTargetTypeName(cmStateEnums::STATIC_LIBRARY),
"' are allowed."));
return false;
}
}
cmCoreTryCompile tc(&mf);
tc.TryCompileCode(args, false);
tc.TryCompileCode(args, targetType);
// if They specified clean then we clean up what we can
if (tc.SrcFileSignature) {

View File

@@ -192,7 +192,7 @@ bool TryRunCommandImpl::TryRunCode(std::vector<std::string> const& argv)
this->CompileResultVariable = argv[1];
// do the try compile
bool compiled = this->TryCompileCode(tryCompile, true);
bool compiled = this->TryCompileCode(tryCompile, cmStateEnums::EXECUTABLE);
// now try running the command if it compiled
if (compiled) {