From 31ee3cd49d3fb27458cec92602e3240066d97c2c Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 15 Jun 2022 14:30:35 -0400 Subject: try_compile: Fail earlier when bindir is not an absolute path If the bindir is not an absolute path, other errors occur later. Fail early with a clear error in this case. --- Source/cmCoreTryCompile.cxx | 16 ++++++++++++++++ Tests/RunCMake/try_compile/BinDirEmpty-result.txt | 1 + Tests/RunCMake/try_compile/BinDirEmpty-stderr.txt | 4 ++++ Tests/RunCMake/try_compile/BinDirEmpty.cmake | 1 + Tests/RunCMake/try_compile/BinDirRelative-result.txt | 1 + Tests/RunCMake/try_compile/BinDirRelative-stderr.txt | 6 ++++++ Tests/RunCMake/try_compile/BinDirRelative.cmake | 1 + Tests/RunCMake/try_compile/RunCMakeTest.cmake | 2 ++ Tests/RunCMake/try_run/BinDirEmpty-result.txt | 1 + Tests/RunCMake/try_run/BinDirEmpty-stderr.txt | 4 ++++ Tests/RunCMake/try_run/BinDirEmpty.cmake | 1 + Tests/RunCMake/try_run/BinDirRelative-result.txt | 1 + Tests/RunCMake/try_run/BinDirRelative-stderr.txt | 6 ++++++ Tests/RunCMake/try_run/BinDirRelative.cmake | 1 + Tests/RunCMake/try_run/RunCMakeTest.cmake | 2 ++ 15 files changed, 48 insertions(+) create mode 100644 Tests/RunCMake/try_compile/BinDirEmpty-result.txt create mode 100644 Tests/RunCMake/try_compile/BinDirEmpty-stderr.txt create mode 100644 Tests/RunCMake/try_compile/BinDirEmpty.cmake create mode 100644 Tests/RunCMake/try_compile/BinDirRelative-result.txt create mode 100644 Tests/RunCMake/try_compile/BinDirRelative-stderr.txt create mode 100644 Tests/RunCMake/try_compile/BinDirRelative.cmake create mode 100644 Tests/RunCMake/try_run/BinDirEmpty-result.txt create mode 100644 Tests/RunCMake/try_run/BinDirEmpty-stderr.txt create mode 100644 Tests/RunCMake/try_run/BinDirEmpty.cmake create mode 100644 Tests/RunCMake/try_run/BinDirRelative-result.txt create mode 100644 Tests/RunCMake/try_run/BinDirRelative-stderr.txt create mode 100644 Tests/RunCMake/try_run/BinDirRelative.cmake diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 45afdd1..1a015be 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -391,6 +391,22 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, } } + if (!this->BinaryDirectory.empty()) { + if (!cmSystemTools::FileIsFullPath(this->BinaryDirectory)) { + this->Makefile->IssueMessage( + MessageType::FATAL_ERROR, + cmStrCat(" is not an absolute path:\n '", + this->BinaryDirectory, "'")); + // Do not try to clean up the ill-specified directory. + this->BinaryDirectory.clear(); + return -1; + } + } else { + this->Makefile->IssueMessage(MessageType::FATAL_ERROR, + "No specified."); + return -1; + } + if (didCopyFile && copyFile.empty()) { this->Makefile->IssueMessage(MessageType::FATAL_ERROR, "COPY_FILE must be followed by a file path"); diff --git a/Tests/RunCMake/try_compile/BinDirEmpty-result.txt b/Tests/RunCMake/try_compile/BinDirEmpty-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/try_compile/BinDirEmpty-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/try_compile/BinDirEmpty-stderr.txt b/Tests/RunCMake/try_compile/BinDirEmpty-stderr.txt new file mode 100644 index 0000000..b1f5ae3 --- /dev/null +++ b/Tests/RunCMake/try_compile/BinDirEmpty-stderr.txt @@ -0,0 +1,4 @@ +^CMake Error at BinDirEmpty.cmake:[0-9]+ \(try_compile\): + No specified. +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]+ \(include\)$ diff --git a/Tests/RunCMake/try_compile/BinDirEmpty.cmake b/Tests/RunCMake/try_compile/BinDirEmpty.cmake new file mode 100644 index 0000000..7bea43d --- /dev/null +++ b/Tests/RunCMake/try_compile/BinDirEmpty.cmake @@ -0,0 +1 @@ +try_compile(resultVar "" ${CMAKE_CURRENT_SOURCE_DIR}/src.c) diff --git a/Tests/RunCMake/try_compile/BinDirRelative-result.txt b/Tests/RunCMake/try_compile/BinDirRelative-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/try_compile/BinDirRelative-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/try_compile/BinDirRelative-stderr.txt b/Tests/RunCMake/try_compile/BinDirRelative-stderr.txt new file mode 100644 index 0000000..a7b6302 --- /dev/null +++ b/Tests/RunCMake/try_compile/BinDirRelative-stderr.txt @@ -0,0 +1,6 @@ +^CMake Error at BinDirRelative.cmake:[0-9]+ \(try_compile\): + is not an absolute path: + + 'bin_dir_relative' +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]+ \(include\)$ diff --git a/Tests/RunCMake/try_compile/BinDirRelative.cmake b/Tests/RunCMake/try_compile/BinDirRelative.cmake new file mode 100644 index 0000000..8deda11 --- /dev/null +++ b/Tests/RunCMake/try_compile/BinDirRelative.cmake @@ -0,0 +1 @@ +try_compile(resultVar bin_dir_relative ${CMAKE_CURRENT_SOURCE_DIR}/src.c) diff --git a/Tests/RunCMake/try_compile/RunCMakeTest.cmake b/Tests/RunCMake/try_compile/RunCMakeTest.cmake index eca7bf4..f755f53 100644 --- a/Tests/RunCMake/try_compile/RunCMakeTest.cmake +++ b/Tests/RunCMake/try_compile/RunCMakeTest.cmake @@ -15,6 +15,8 @@ run_cmake(BadSources1) run_cmake(BadSources2) run_cmake(NonSourceCopyFile) run_cmake(NonSourceCompileDefinitions) +run_cmake(BinDirEmpty) +run_cmake(BinDirRelative) run_cmake(EnvConfig) diff --git a/Tests/RunCMake/try_run/BinDirEmpty-result.txt b/Tests/RunCMake/try_run/BinDirEmpty-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/try_run/BinDirEmpty-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/try_run/BinDirEmpty-stderr.txt b/Tests/RunCMake/try_run/BinDirEmpty-stderr.txt new file mode 100644 index 0000000..def1c22 --- /dev/null +++ b/Tests/RunCMake/try_run/BinDirEmpty-stderr.txt @@ -0,0 +1,4 @@ +^CMake Error at BinDirEmpty.cmake:[0-9]+ \(try_run\): + No specified. +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]+ \(include\)$ diff --git a/Tests/RunCMake/try_run/BinDirEmpty.cmake b/Tests/RunCMake/try_run/BinDirEmpty.cmake new file mode 100644 index 0000000..d4b7ee3 --- /dev/null +++ b/Tests/RunCMake/try_run/BinDirEmpty.cmake @@ -0,0 +1 @@ +try_run(runResultVar compileResultVar "" ${CMAKE_CURRENT_SOURCE_DIR}/src.c) diff --git a/Tests/RunCMake/try_run/BinDirRelative-result.txt b/Tests/RunCMake/try_run/BinDirRelative-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/try_run/BinDirRelative-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/try_run/BinDirRelative-stderr.txt b/Tests/RunCMake/try_run/BinDirRelative-stderr.txt new file mode 100644 index 0000000..54d4e86 --- /dev/null +++ b/Tests/RunCMake/try_run/BinDirRelative-stderr.txt @@ -0,0 +1,6 @@ +^CMake Error at BinDirRelative.cmake:[0-9]+ \(try_run\): + is not an absolute path: + + 'bin_dir_relative' +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]+ \(include\)$ diff --git a/Tests/RunCMake/try_run/BinDirRelative.cmake b/Tests/RunCMake/try_run/BinDirRelative.cmake new file mode 100644 index 0000000..a277403 --- /dev/null +++ b/Tests/RunCMake/try_run/BinDirRelative.cmake @@ -0,0 +1 @@ +try_run(runResultVar compileResultVar bin_dir_relative ${CMAKE_CURRENT_SOURCE_DIR}/src.c) diff --git a/Tests/RunCMake/try_run/RunCMakeTest.cmake b/Tests/RunCMake/try_run/RunCMakeTest.cmake index 5fa5b2f..76c85dd 100644 --- a/Tests/RunCMake/try_run/RunCMakeTest.cmake +++ b/Tests/RunCMake/try_run/RunCMakeTest.cmake @@ -1,6 +1,8 @@ include(RunCMake) run_cmake(BadLinkLibraries) +run_cmake(BinDirEmpty) +run_cmake(BinDirRelative) if (CMAKE_SYSTEM_NAME MATCHES "^(Linux|Darwin|Windows)$" AND CMAKE_C_COMPILER_ID MATCHES "^(MSVC|GNU|LCC|Clang|AppleClang)$") -- cgit v0.12 From 7ba3a3290fb53d2874e8a355ae0a4d12c8191cfb Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 15 Jun 2022 14:22:12 -0400 Subject: try_compile: Refactor positional arg parsing --- Source/cmCoreTryCompile.cxx | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 1a015be..63184a0 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -238,7 +238,7 @@ std::set const ghs_platform_vars{ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, bool isTryRun) { - this->BinaryDirectory = argv[1]; + std::string const& resultVar = argv[0]; this->OutputFile.clear(); // which signature were we called with ? this->SrcFileSignature = true; @@ -264,7 +264,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, } } - std::string sourceDirectory = argv[2]; + std::string sourceDirectory; std::string projectName; std::string targetName; std::vector cmakeFlags(1, "CMAKE_FLAGS"); // fake argv[0] @@ -287,7 +287,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, bool didOutputVariable = false; bool didCopyFile = false; bool didCopyFileError = false; - bool useSources = argv[2] == "SOURCES"; + bool useSources = false; std::vector sources; enum Doing @@ -303,9 +303,12 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, DoingSources, DoingCMakeInternal }; - Doing doing = useSources ? DoingSources : DoingNone; - for (size_t i = 3; i < argv.size(); ++i) { - if (argv[i] == "CMAKE_FLAGS") { + Doing doing = DoingNone; + for (size_t i = 1; i < argv.size(); ++i) { + if (argv[i] == "SOURCES") { + useSources = true; + doing = DoingSources; + } else if (argv[i] == "CMAKE_FLAGS") { doing = DoingCMakeFlags; } else if (argv[i] == "COMPILE_DEFINITIONS") { doing = DoingCompileDefinitions; @@ -379,6 +382,10 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, } else if (doing == DoingCMakeInternal) { cmakeInternal = argv[i]; doing = DoingNone; + } else if (i == 1) { + this->BinaryDirectory = argv[i]; + } else if (i == 2) { + sourceDirectory = argv[i]; } else if (i == 3) { this->SrcFileSignature = false; projectName = argv[i]; @@ -1012,7 +1019,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, } // set the result var to the return value to indicate success or failure - this->Makefile->AddCacheDefinition(argv[0], (res == 0 ? "TRUE" : "FALSE"), + this->Makefile->AddCacheDefinition(resultVar, (res == 0 ? "TRUE" : "FALSE"), "Result of TRY_COMPILE", cmStateEnums::INTERNAL); -- cgit v0.12 From e73c8eaff20b33452db251ce1de1b1162b647178 Mon Sep 17 00:00:00 2001 From: Brad King Date: Wed, 15 Jun 2022 15:07:06 -0400 Subject: cmTry{Compile,Run}Command: Port away from legacy cmCommand Convert the command entry points to free functions. --- Source/cmCommands.cxx | 6 ++-- Source/cmCoreTryCompile.h | 12 +++++-- Source/cmTryCompileCommand.cxx | 27 +++++++------- Source/cmTryCompileCommand.h | 30 ++-------------- Source/cmTryRunCommand.cxx | 82 +++++++++++++++++++++++++++++++----------- Source/cmTryRunCommand.h | 50 ++------------------------ 6 files changed, 90 insertions(+), 117 deletions(-) diff --git a/Source/cmCommands.cxx b/Source/cmCommands.cxx index c6296f9..5e616b3 100644 --- a/Source/cmCommands.cxx +++ b/Source/cmCommands.cxx @@ -20,7 +20,6 @@ #include "cmCMakeMinimumRequired.h" #include "cmCMakePathCommand.h" #include "cmCMakePolicyCommand.h" -#include "cmCommand.h" #include "cmConfigureFileCommand.h" #include "cmContinueCommand.h" #include "cmCreateTestSourceList.h" @@ -264,9 +263,8 @@ void GetProjectCommands(cmState* state) cmTargetLinkLibrariesCommand); state->AddBuiltinCommand("target_link_options", cmTargetLinkOptionsCommand); state->AddBuiltinCommand("target_sources", cmTargetSourcesCommand); - state->AddBuiltinCommand("try_compile", - cm::make_unique()); - state->AddBuiltinCommand("try_run", cm::make_unique()); + state->AddBuiltinCommand("try_compile", cmTryCompileCommand); + state->AddBuiltinCommand("try_run", cmTryRunCommand); state->AddBuiltinCommand("target_precompile_headers", cmTargetPrecompileHeadersCommand); diff --git a/Source/cmCoreTryCompile.h b/Source/cmCoreTryCompile.h index 594fd7f..9d43899 100644 --- a/Source/cmCoreTryCompile.h +++ b/Source/cmCoreTryCompile.h @@ -7,19 +7,24 @@ #include #include -#include "cmCommand.h" #include "cmStateTypes.h" +class cmMakefile; + /** \class cmCoreTryCompile * \brief Base class for cmTryCompileCommand and cmTryRunCommand * * cmCoreTryCompile implements the functionality to build a program. * It is the base class for cmTryCompileCommand and cmTryRunCommand. */ -class cmCoreTryCompile : public cmCommand +class cmCoreTryCompile { public: -protected: + cmCoreTryCompile(cmMakefile* mf) + : Makefile(mf) + { + } + /** * This is the core code for try compile. It is here so that other * commands, such as TryRun can access the same logic without @@ -46,4 +51,5 @@ protected: std::string OutputFile; std::string FindErrorMessage; bool SrcFileSignature = false; + cmMakefile* Makefile; }; diff --git a/Source/cmTryCompileCommand.cxx b/Source/cmTryCompileCommand.cxx index 130c228..7514a23 100644 --- a/Source/cmTryCompileCommand.cxx +++ b/Source/cmTryCompileCommand.cxx @@ -2,34 +2,35 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmTryCompileCommand.h" +#include "cmCoreTryCompile.h" +#include "cmExecutionStatus.h" #include "cmMakefile.h" #include "cmMessageType.h" #include "cmake.h" -class cmExecutionStatus; - -// cmTryCompileCommand -bool cmTryCompileCommand::InitialPass(std::vector const& argv, - cmExecutionStatus&) +bool cmTryCompileCommand(std::vector const& args, + cmExecutionStatus& status) { - if (argv.size() < 3) { + if (args.size() < 3) { return false; } - if (this->Makefile->GetCMakeInstance()->GetWorkingMode() == - cmake::FIND_PACKAGE_MODE) { - this->Makefile->IssueMessage( + cmMakefile& mf = status.GetMakefile(); + + if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) { + mf.IssueMessage( MessageType::FATAL_ERROR, "The try_compile() command is not supported in --find-package mode."); return false; } - this->TryCompileCode(argv, false); + cmCoreTryCompile tc(&mf); + tc.TryCompileCode(args, false); // if They specified clean then we clean up what we can - if (this->SrcFileSignature) { - if (!this->Makefile->GetCMakeInstance()->GetDebugTryCompile()) { - this->CleanupFiles(this->BinaryDirectory); + if (tc.SrcFileSignature) { + if (!mf.GetCMakeInstance()->GetDebugTryCompile()) { + tc.CleanupFiles(tc.BinaryDirectory); } } return true; diff --git a/Source/cmTryCompileCommand.h b/Source/cmTryCompileCommand.h index d8cc16e..6a3430b 100644 --- a/Source/cmTryCompileCommand.h +++ b/Source/cmTryCompileCommand.h @@ -7,33 +7,7 @@ #include #include -#include - -#include "cmCommand.h" -#include "cmCoreTryCompile.h" - class cmExecutionStatus; -/** \class cmTryCompileCommand - * \brief Specifies where to install some files - * - * cmTryCompileCommand is used to test if source code can be compiled - */ -class cmTryCompileCommand : public cmCoreTryCompile -{ -public: - /** - * This is a virtual constructor for the command. - */ - std::unique_ptr Clone() override - { - return cm::make_unique(); - } - - /** - * This is called when the command is first encountered in - * the CMakeLists.txt file. - */ - bool InitialPass(std::vector const& args, - cmExecutionStatus& status) override; -}; +bool cmTryCompileCommand(std::vector const& args, + cmExecutionStatus& status); diff --git a/Source/cmTryRunCommand.cxx b/Source/cmTryRunCommand.cxx index 4cd0adc..98cacdc 100644 --- a/Source/cmTryRunCommand.cxx +++ b/Source/cmTryRunCommand.cxx @@ -6,7 +6,9 @@ #include "cmsys/FStream.hxx" +#include "cmCoreTryCompile.h" #include "cmDuration.h" +#include "cmExecutionStatus.h" #include "cmMakefile.h" #include "cmMessageType.h" #include "cmRange.h" @@ -17,24 +19,40 @@ #include "cmValue.h" #include "cmake.h" -class cmExecutionStatus; +namespace { -// cmTryRunCommand -bool cmTryRunCommand::InitialPass(std::vector const& argv, - cmExecutionStatus&) +class TryRunCommandImpl : public cmCoreTryCompile { - if (argv.size() < 4) { - return false; - } - - if (this->Makefile->GetCMakeInstance()->GetWorkingMode() == - cmake::FIND_PACKAGE_MODE) { - this->Makefile->IssueMessage( - MessageType::FATAL_ERROR, - "The try_run() command is not supported in --find-package mode."); - return false; +public: + TryRunCommandImpl(cmMakefile* mf) + : cmCoreTryCompile(mf) + { } + bool TryRunCode(std::vector const& args); + + void RunExecutable(const std::string& runArgs, + std::string* runOutputContents, + std::string* runOutputStdOutContents, + std::string* runOutputStdErrContents); + void DoNotRunExecutable(const std::string& runArgs, + const std::string& srcFile, + std::string* runOutputContents, + std::string* runOutputStdOutContents, + std::string* runOutputStdErrContents); + + std::string CompileResultVariable; + std::string RunResultVariable; + std::string OutputVariable; + std::string RunOutputVariable; + std::string RunOutputStdOutVariable; + std::string RunOutputStdErrVariable; + std::string CompileOutputVariable; + std::string WorkingDirectory; +}; + +bool TryRunCommandImpl::TryRunCode(std::vector const& argv) +{ // build an arg list for TryCompile and extract the runArgs, std::vector tryCompile; @@ -240,9 +258,9 @@ bool cmTryRunCommand::InitialPass(std::vector const& argv, return true; } -void cmTryRunCommand::RunExecutable(const std::string& runArgs, - std::string* out, std::string* stdOut, - std::string* stdErr) +void TryRunCommandImpl::RunExecutable(const std::string& runArgs, + std::string* out, std::string* stdOut, + std::string* stdErr) { int retVal = -1; @@ -288,10 +306,11 @@ void cmTryRunCommand::RunExecutable(const std::string& runArgs, executable, two cache variables are created which will hold the results the executable would have produced. */ -void cmTryRunCommand::DoNotRunExecutable(const std::string& runArgs, - const std::string& srcFile, - std::string* out, std::string* stdOut, - std::string* stdErr) +void TryRunCommandImpl::DoNotRunExecutable(const std::string& runArgs, + const std::string& srcFile, + std::string* out, + std::string* stdOut, + std::string* stdErr) { // 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 @@ -521,3 +540,24 @@ void cmTryRunCommand::DoNotRunExecutable(const std::string& runArgs, (*out) = *this->Makefile->GetDefinition(internalRunOutputName); } } +} + +bool cmTryRunCommand(std::vector const& args, + cmExecutionStatus& status) +{ + if (args.size() < 4) { + return false; + } + + cmMakefile& mf = status.GetMakefile(); + + if (mf.GetCMakeInstance()->GetWorkingMode() == cmake::FIND_PACKAGE_MODE) { + mf.IssueMessage( + MessageType::FATAL_ERROR, + "The try_run() command is not supported in --find-package mode."); + return false; + } + + TryRunCommandImpl tr(&mf); + return tr.TryRunCode(args); +} diff --git a/Source/cmTryRunCommand.h b/Source/cmTryRunCommand.h index ccf678e..38e3638 100644 --- a/Source/cmTryRunCommand.h +++ b/Source/cmTryRunCommand.h @@ -7,53 +7,7 @@ #include #include -#include - -#include "cmCommand.h" -#include "cmCoreTryCompile.h" - class cmExecutionStatus; -/** \class cmTryRunCommand - * \brief Specifies where to install some files - * - * cmTryRunCommand is used to test if source code can be compiled - */ -class cmTryRunCommand : public cmCoreTryCompile -{ -public: - /** - * This is a virtual constructor for the command. - */ - std::unique_ptr Clone() override - { - return cm::make_unique(); - } - - /** - * This is called when the command is first encountered in - * the CMakeLists.txt file. - */ - bool InitialPass(std::vector const& args, - cmExecutionStatus& status) override; - -private: - void RunExecutable(const std::string& runArgs, - std::string* runOutputContents, - std::string* runOutputStdOutContents, - std::string* runOutputStdErrContents); - void DoNotRunExecutable(const std::string& runArgs, - const std::string& srcFile, - std::string* runOutputContents, - std::string* runOutputStdOutContents, - std::string* runOutputStdErrContents); - - std::string CompileResultVariable; - std::string RunResultVariable; - std::string OutputVariable; - std::string RunOutputVariable; - std::string RunOutputStdOutVariable; - std::string RunOutputStdErrVariable; - std::string CompileOutputVariable; - std::string WorkingDirectory; -}; +bool cmTryRunCommand(std::vector const& args, + cmExecutionStatus& status); -- cgit v0.12 From 8b0ee799e4cefd4aa008e6f251504eb541773a70 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 28 Jun 2022 11:37:19 -0400 Subject: cmCoreTryCompile: Compute src-file signature build directory earlier --- Source/cmCoreTryCompile.cxx | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 63184a0..8d41f00 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -408,6 +408,11 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, this->BinaryDirectory.clear(); return -1; } + // compute the binary dir when TRY_COMPILE is called with a src file + // signature + if (this->SrcFileSignature) { + this->BinaryDirectory += "/CMakeFiles/CMakeTmp"; + } } else { this->Makefile->IssueMessage(MessageType::FATAL_ERROR, "No specified."); @@ -448,6 +453,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, return -1; } + // only valid for srcfile signatures if (!this->SrcFileSignature) { if (!cState.Validate(this->Makefile)) { return -1; @@ -467,14 +473,7 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, if (!objcxxState.Validate(this->Makefile)) { return -1; } - } - // compute the binary dir when TRY_COMPILE is called with a src file - // signature - if (this->SrcFileSignature) { - this->BinaryDirectory += "/CMakeFiles/CMakeTmp"; - } else { - // only valid for srcfile signatures if (!compileDefs.empty()) { this->Makefile->IssueMessage( MessageType::FATAL_ERROR, -- cgit v0.12 From 7a5b1b6010d9f3f2bf979a505eb4c698b994bf26 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 27 Jun 2022 15:17:18 -0400 Subject: cmCoreTryCompile: Select source-file signature project/target names earlier --- Source/cmCoreTryCompile.cxx | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Source/cmCoreTryCompile.cxx b/Source/cmCoreTryCompile.cxx index 8d41f00..d90b574 100644 --- a/Source/cmCoreTryCompile.cxx +++ b/Source/cmCoreTryCompile.cxx @@ -283,7 +283,6 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, std::vector linkOptions; std::string libsToLink = " "; bool useOldLinkLibs = true; - char targetNameBuf[64]; bool didOutputVariable = false; bool didCopyFile = false; bool didCopyFileError = false; @@ -419,6 +418,16 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, return -1; } + if (this->SrcFileSignature) { + projectName = "CMAKE_TRY_COMPILE"; + /* Use a random file name to avoid rapid creation and deletion + of the same executable name (some filesystems fail on that). */ + char targetNameBuf[64]; + snprintf(targetNameBuf, sizeof(targetNameBuf), "cmTC_%05x", + cmSystemTools::RandomSeed() & 0xFFFFF); + targetName = targetNameBuf; + } + if (didCopyFile && copyFile.empty()) { this->Makefile->IssueMessage(MessageType::FATAL_ERROR, "COPY_FILE must be followed by a file path"); @@ -733,12 +742,6 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, cmJoin(compileDefs, "]==] [==[").c_str()); } - /* Use a random file name to avoid rapid creation and deletion - of the same executable name (some filesystems fail on that). */ - snprintf(targetNameBuf, sizeof(targetNameBuf), "cmTC_%05x", - cmSystemTools::RandomSeed() & 0xFFFFF); - targetName = targetNameBuf; - if (!targets.empty()) { std::string fname = "/" + std::string(targetName) + "Targets.cmake"; cmExportTryCompileFileGenerator tcfg(gg, targets, this->Makefile, @@ -895,7 +898,6 @@ int cmCoreTryCompile::TryCompileCode(std::vector const& argv, libsToLink.c_str()); } fclose(fout); - projectName = "CMAKE_TRY_COMPILE"; } // Forward a set of variables to the inner project cache. -- cgit v0.12 From 6a858138068f3b7ce9afc4628ebdd7fb63de6936 Mon Sep 17 00:00:00 2001 From: Brad King Date: Mon, 27 Jun 2022 15:22:12 -0400 Subject: Tests: Add RunCMake.try_compile case covering list arguments with no values --- Tests/RunCMake/try_compile/EmptyListArgs.cmake | 8 ++++++++ Tests/RunCMake/try_compile/RunCMakeTest.cmake | 1 + 2 files changed, 9 insertions(+) create mode 100644 Tests/RunCMake/try_compile/EmptyListArgs.cmake diff --git a/Tests/RunCMake/try_compile/EmptyListArgs.cmake b/Tests/RunCMake/try_compile/EmptyListArgs.cmake new file mode 100644 index 0000000..eed7ee4 --- /dev/null +++ b/Tests/RunCMake/try_compile/EmptyListArgs.cmake @@ -0,0 +1,8 @@ +enable_language(C) + +try_compile(RESULT ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src.c + CMAKE_FLAGS # no values + COMPILE_DEFINITIONS # no values + LINK_LIBRARIES # no values + LINK_OPTIONS # no values + ) diff --git a/Tests/RunCMake/try_compile/RunCMakeTest.cmake b/Tests/RunCMake/try_compile/RunCMakeTest.cmake index f755f53..1467157 100644 --- a/Tests/RunCMake/try_compile/RunCMakeTest.cmake +++ b/Tests/RunCMake/try_compile/RunCMakeTest.cmake @@ -17,6 +17,7 @@ run_cmake(NonSourceCopyFile) run_cmake(NonSourceCompileDefinitions) run_cmake(BinDirEmpty) run_cmake(BinDirRelative) +run_cmake(EmptyListArgs) run_cmake(EnvConfig) -- cgit v0.12 From e1d49847069ff6319bb259bf4eb628111d2a1340 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 28 Jun 2022 11:30:19 -0400 Subject: Tests: Add RunCMake.try_compile case covering empty value arguments --- Tests/RunCMake/try_compile/EmptyValueArgs-result.txt | 1 + Tests/RunCMake/try_compile/EmptyValueArgs-stderr.txt | 9 +++++++++ Tests/RunCMake/try_compile/EmptyValueArgs.cmake | 4 ++++ Tests/RunCMake/try_compile/RunCMakeTest.cmake | 1 + 4 files changed, 15 insertions(+) create mode 100644 Tests/RunCMake/try_compile/EmptyValueArgs-result.txt create mode 100644 Tests/RunCMake/try_compile/EmptyValueArgs-stderr.txt create mode 100644 Tests/RunCMake/try_compile/EmptyValueArgs.cmake diff --git a/Tests/RunCMake/try_compile/EmptyValueArgs-result.txt b/Tests/RunCMake/try_compile/EmptyValueArgs-result.txt new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/Tests/RunCMake/try_compile/EmptyValueArgs-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/try_compile/EmptyValueArgs-stderr.txt b/Tests/RunCMake/try_compile/EmptyValueArgs-stderr.txt new file mode 100644 index 0000000..b1344bd --- /dev/null +++ b/Tests/RunCMake/try_compile/EmptyValueArgs-stderr.txt @@ -0,0 +1,9 @@ +CMake Error at EmptyValueArgs.cmake:[0-9]+ \(try_compile\): + COPY_FILE must be followed by a file path +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]+ \(include\) ++ +CMake Error at EmptyValueArgs.cmake:[0-9]+ \(try_compile\): + COPY_FILE_ERROR must be followed by a variable name +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]+ \(include\) diff --git a/Tests/RunCMake/try_compile/EmptyValueArgs.cmake b/Tests/RunCMake/try_compile/EmptyValueArgs.cmake new file mode 100644 index 0000000..f564abc --- /dev/null +++ b/Tests/RunCMake/try_compile/EmptyValueArgs.cmake @@ -0,0 +1,4 @@ +try_compile(RESULT ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src.c + COPY_FILE "") +try_compile(RESULT ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src.c + COPY_FILE "x" COPY_FILE_ERROR "") diff --git a/Tests/RunCMake/try_compile/RunCMakeTest.cmake b/Tests/RunCMake/try_compile/RunCMakeTest.cmake index 1467157..d377cce 100644 --- a/Tests/RunCMake/try_compile/RunCMakeTest.cmake +++ b/Tests/RunCMake/try_compile/RunCMakeTest.cmake @@ -17,6 +17,7 @@ run_cmake(NonSourceCopyFile) run_cmake(NonSourceCompileDefinitions) run_cmake(BinDirEmpty) run_cmake(BinDirRelative) +run_cmake(EmptyValueArgs) run_cmake(EmptyListArgs) run_cmake(EnvConfig) -- cgit v0.12