From 067ba3a2bd1ce168b2867de74d2ea6b944a936fc Mon Sep 17 00:00:00 2001 From: Brad King Date: Fri, 29 Jul 2022 14:56:00 -0400 Subject: 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. --- Source/cmCoreTryCompile.cxx | 23 +---------------------- Source/cmCoreTryCompile.h | 3 ++- Source/cmTryCompileCommand.cxx | 27 ++++++++++++++++++++++++++- Source/cmTryRunCommand.cxx | 2 +- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 44dbb7f..3c749ed 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -236,34 +236,13 @@ std::set const ghs_platform_vars{ } bool cmCoreTryCompile::TryCompileCode(std::vector 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; diff --git a/Source/cmCoreTryCompile.h b/Source/cmCoreTryCompile.h index 1e70293..6ca08e8 100644 --- a/Source/cmCoreTryCompile.h +++ b/Source/cmCoreTryCompile.h @@ -30,7 +30,8 @@ public: * commands, such as TryRun can access the same logic without * duplication. */ - bool TryCompileCode(std::vector const& argv, bool isTryRun); + bool TryCompileCode(std::vector const& argv, + cmStateEnums::TargetType targetType); /** * This deletes all the files created by TryCompileCode. diff --git a/Source/cmTryCompileCommand.cxx b/Source/cmTryCompileCommand.cxx index 7514a23..971dccb 100644 --- a/Source/cmTryCompileCommand.cxx +++ b/Source/cmTryCompileCommand.cxx @@ -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 const& args, @@ -24,8 +28,29 @@ bool cmTryCompileCommand(std::vector 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) { diff --git a/Source/cmTryRunCommand.cxx b/Source/cmTryRunCommand.cxx index b42a6ce..2cf75e2 100644 --- a/Source/cmTryRunCommand.cxx +++ b/Source/cmTryRunCommand.cxx @@ -192,7 +192,7 @@ bool TryRunCommandImpl::TryRunCode(std::vector 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) { -- cgit v0.12