/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying file Copyright.txt or https://cmake.org/licensing for details. */ #pragma once #include "cmConfigure.h" // IWYU pragma: keep #include #include #include #include #include "cmArgumentParser.h" #include "cmArgumentParserTypes.h" #include "cmStateTypes.h" class cmConfigureLog; class cmMakefile; template class cmRange; struct cmTryCompileResult { cm::optional LogDescription; std::map CMakeVariables; std::string SourceDirectory; std::string BinaryDirectory; bool VariableCached = true; std::string Variable; std::string Output; int ExitCode = 1; }; /** \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: cmCoreTryCompile(cmMakefile* mf) : Makefile(mf) { } struct Arguments : public ArgumentParser::ParseResult { cm::optional CompileResultVariable; cm::optional BinaryDirectory; cm::optional SourceDirectoryOrFile; cm::optional ProjectName; cm::optional TargetName; cm::optional>> Sources; cm::optional>> SourceFromContent; cm::optional>> SourceFromVar; cm::optional>> SourceFromFile; ArgumentParser::MaybeEmpty> CMakeFlags{ 1, "CMAKE_FLAGS" }; // fake argv[0] std::vector CompileDefs; cm::optional>> LinkLibraries; ArgumentParser::MaybeEmpty> LinkOptions; std::map LangProps; std::string CMakeInternal; cm::optional OutputVariable; cm::optional CopyFileTo; cm::optional CopyFileError; cm::optional> LogDescription; bool NoCache = false; bool NoLog = false; // Argument for try_run only. // Keep in sync with warnings in cmCoreTryCompile::ParseArgs. cm::optional CompileOutputVariable; cm::optional RunOutputVariable; cm::optional RunOutputStdOutVariable; cm::optional RunOutputStdErrVariable; cm::optional RunWorkingDirectory; cm::optional>> RunArgs; }; Arguments ParseArgs(cmRange::const_iterator> args, bool isTryRun); /** * This is the core code for try compile. It is here so that other commands, * such as TryRun can access the same logic without duplication. * * This function requires at least two \p arguments and will crash if given * fewer. */ cm::optional TryCompileCode( Arguments& arguments, cmStateEnums::TargetType targetType); /** * Returns \c true if \p path resides within a CMake temporary directory, * otherwise returns \c false. */ static bool IsTemporary(std::string const& path); /** * This deletes all the files created by TryCompileCode. * This way we do not have to rely on the timing and * dependencies of makefiles. */ void CleanupFiles(std::string const& binDir); /** * This tries to find the (executable) file created by TryCompileCode. The result is stored in OutputFile. If nothing is found, the error message is stored in FindErrorMessage. */ void FindOutputFile(const std::string& targetName); static void WriteTryCompileEventFields( cmConfigureLog& log, cmTryCompileResult const& compileResult); std::string BinaryDirectory; std::string OutputFile; std::string FindErrorMessage; bool SrcFileSignature = false; cmMakefile* Makefile; private: std::string WriteSource(std::string const& name, std::string const& content, char const* command) const; Arguments ParseArgs( const cmRange::const_iterator>& args, const cmArgumentParser& parser, std::vector& unparsedArguments); };