cmake/Source/cmCoreTryCompile.h

143 lines
4.4 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. */
2021-09-14 00:13:48 +02:00
#pragma once
2017-07-20 19:35:53 +02:00
#include "cmConfigure.h" // IWYU pragma: keep
2017-04-14 19:02:05 +02:00
2022-11-16 20:14:03 +01:00
#include <map>
2017-04-14 19:02:05 +02:00
#include <string>
#include <vector>
2022-11-16 20:14:03 +01:00
#include <cm/optional>
#include "cmArgumentParser.h"
#include "cmArgumentParserTypes.h"
2023-07-02 19:51:09 +02:00
#include "cmList.h"
2017-04-14 19:02:05 +02:00
#include "cmStateTypes.h"
2023-05-23 16:38:00 +02:00
class cmConfigureLog;
2022-11-16 20:14:03 +01:00
class cmMakefile;
template <typename Iter>
class cmRange;
2023-05-23 16:38:00 +02:00
struct cmTryCompileResult
{
cm::optional<std::string> LogDescription;
std::map<std::string, std::string> 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.
*/
2022-11-16 20:14:03 +01:00
class cmCoreTryCompile
{
public:
2022-11-16 20:14:03 +01:00
cmCoreTryCompile(cmMakefile* mf)
: Makefile(mf)
{
}
struct Arguments : public ArgumentParser::ParseResult
{
cm::optional<std::string> CompileResultVariable;
cm::optional<std::string> BinaryDirectory;
cm::optional<std::string> SourceDirectoryOrFile;
cm::optional<std::string> ProjectName;
cm::optional<std::string> TargetName;
cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> Sources;
cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>>
SourceFromContent;
cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>>
SourceFromVar;
cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>>
SourceFromFile;
ArgumentParser::MaybeEmpty<std::vector<std::string>> CMakeFlags{
1, "CMAKE_FLAGS"
}; // fake argv[0]
2023-07-02 19:51:09 +02:00
cmList CompileDefs;
2022-11-16 20:14:03 +01:00
cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>>
LinkLibraries;
ArgumentParser::MaybeEmpty<std::vector<std::string>> LinkOptions;
std::map<std::string, std::string> LangProps;
std::string CMakeInternal;
cm::optional<std::string> OutputVariable;
cm::optional<std::string> CopyFileTo;
cm::optional<std::string> CopyFileError;
2023-05-23 16:38:00 +02:00
cm::optional<ArgumentParser::NonEmpty<std::string>> LogDescription;
2022-11-16 20:14:03 +01:00
bool NoCache = false;
2023-05-23 16:38:00 +02:00
bool NoLog = false;
2022-11-16 20:14:03 +01:00
// Argument for try_run only.
// Keep in sync with warnings in cmCoreTryCompile::ParseArgs.
cm::optional<std::string> CompileOutputVariable;
cm::optional<std::string> RunOutputVariable;
cm::optional<std::string> RunOutputStdOutVariable;
cm::optional<std::string> RunOutputStdErrVariable;
cm::optional<std::string> RunWorkingDirectory;
cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>> RunArgs;
};
Arguments ParseArgs(cmRange<std::vector<std::string>::const_iterator> args,
bool isTryRun);
/**
2022-11-16 20:14:03 +01:00
* 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.
*/
2023-05-23 16:38:00 +02:00
cm::optional<cmTryCompileResult> TryCompileCode(
Arguments& arguments, cmStateEnums::TargetType targetType);
2022-11-16 20:14:03 +01:00
/**
* Returns \c true if \p path resides within a CMake temporary directory,
* otherwise returns \c false.
*/
static bool IsTemporary(std::string const& path);
2012-02-18 12:40:36 +02:00
/**
* This deletes all the files created by TryCompileCode.
* This way we do not have to rely on the timing and
* dependencies of makefiles.
*/
2018-04-23 21:13:27 +02:00
void CleanupFiles(std::string const& binDir);
2012-02-18 12:40:36 +02:00
/**
* 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.
*/
2022-11-16 20:14:03 +01:00
void FindOutputFile(const std::string& targetName);
2012-02-18 12:40:36 +02:00
2023-05-23 16:38:00 +02:00
static void WriteTryCompileEventFields(
cmConfigureLog& log, cmTryCompileResult const& compileResult);
std::string BinaryDirectory;
std::string OutputFile;
std::string FindErrorMessage;
2018-08-09 18:06:22 +02:00
bool SrcFileSignature = false;
2022-11-16 20:14:03 +01:00
cmMakefile* Makefile;
private:
std::string WriteSource(std::string const& name, std::string const& content,
char const* command) const;
Arguments ParseArgs(
const cmRange<std::vector<std::string>::const_iterator>& args,
const cmArgumentParser<Arguments>& parser,
std::vector<std::string>& unparsedArguments);
};