1 /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 file Copyright.txt or https://cmake.org/licensing for details. */
5 #include "cmConfigure.h" // IWYU pragma: keep
11 #include <cm/optional>
13 #include "cmArgumentParser.h"
14 #include "cmArgumentParserTypes.h"
15 #include "cmStateTypes.h"
18 template <typename Iter>
21 /** \class cmCoreTryCompile
22 * \brief Base class for cmTryCompileCommand and cmTryRunCommand
24 * cmCoreTryCompile implements the functionality to build a program.
25 * It is the base class for cmTryCompileCommand and cmTryRunCommand.
27 class cmCoreTryCompile
30 cmCoreTryCompile(cmMakefile* mf)
35 struct Arguments : public ArgumentParser::ParseResult
37 cm::optional<std::string> CompileResultVariable;
38 cm::optional<std::string> BinaryDirectory;
39 cm::optional<std::string> SourceDirectoryOrFile;
40 cm::optional<std::string> ProjectName;
41 cm::optional<std::string> TargetName;
42 cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>> Sources;
43 cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>>
45 cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>>
47 cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>>
49 ArgumentParser::MaybeEmpty<std::vector<std::string>> CMakeFlags{
52 std::vector<std::string> CompileDefs;
53 cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>>
55 ArgumentParser::MaybeEmpty<std::vector<std::string>> LinkOptions;
56 std::map<std::string, std::string> LangProps;
57 std::string CMakeInternal;
58 cm::optional<std::string> OutputVariable;
59 cm::optional<std::string> CopyFileTo;
60 cm::optional<std::string> CopyFileError;
63 // Argument for try_run only.
64 // Keep in sync with warnings in cmCoreTryCompile::ParseArgs.
65 cm::optional<std::string> CompileOutputVariable;
66 cm::optional<std::string> RunOutputVariable;
67 cm::optional<std::string> RunOutputStdOutVariable;
68 cm::optional<std::string> RunOutputStdErrVariable;
69 cm::optional<std::string> RunWorkingDirectory;
70 cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>> RunArgs;
73 Arguments ParseArgs(cmRange<std::vector<std::string>::const_iterator> args,
77 * This is the core code for try compile. It is here so that other commands,
78 * such as TryRun can access the same logic without duplication.
80 * This function requires at least two \p arguments and will crash if given
83 bool TryCompileCode(Arguments& arguments,
84 cmStateEnums::TargetType targetType);
87 * Returns \c true if \p path resides within a CMake temporary directory,
88 * otherwise returns \c false.
90 static bool IsTemporary(std::string const& path);
93 * This deletes all the files created by TryCompileCode.
94 * This way we do not have to rely on the timing and
95 * dependencies of makefiles.
97 void CleanupFiles(std::string const& binDir);
100 * This tries to find the (executable) file created by
101 TryCompileCode. The result is stored in OutputFile. If nothing is found,
102 the error message is stored in FindErrorMessage.
104 void FindOutputFile(const std::string& targetName);
106 std::string BinaryDirectory;
107 std::string OutputFile;
108 std::string FindErrorMessage;
109 bool SrcFileSignature = false;
110 cmMakefile* Makefile;
113 std::string WriteSource(std::string const& name, std::string const& content,
114 char const* command) const;
117 const cmRange<std::vector<std::string>::const_iterator>& args,
118 const cmArgumentParser<Arguments>& parser,
119 std::vector<std::string>& unparsedArguments);