Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Source / cmCoreTryCompile.h
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #pragma once
4
5 #include "cmConfigure.h" // IWYU pragma: keep
6
7 #include <map>
8 #include <string>
9 #include <vector>
10
11 #include <cm/optional>
12
13 #include "cmArgumentParser.h"
14 #include "cmArgumentParserTypes.h"
15 #include "cmStateTypes.h"
16
17 class cmMakefile;
18 template <typename Iter>
19 class cmRange;
20
21 /** \class cmCoreTryCompile
22  * \brief Base class for cmTryCompileCommand and cmTryRunCommand
23  *
24  * cmCoreTryCompile implements the functionality to build a program.
25  * It is the base class for cmTryCompileCommand and cmTryRunCommand.
26  */
27 class cmCoreTryCompile
28 {
29 public:
30   cmCoreTryCompile(cmMakefile* mf)
31     : Makefile(mf)
32   {
33   }
34
35   struct Arguments : public ArgumentParser::ParseResult
36   {
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>>>
44       SourceFromContent;
45     cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>>
46       SourceFromVar;
47     cm::optional<ArgumentParser::NonEmpty<std::vector<std::string>>>
48       SourceFromFile;
49     ArgumentParser::MaybeEmpty<std::vector<std::string>> CMakeFlags{
50       1, "CMAKE_FLAGS"
51     }; // fake argv[0]
52     std::vector<std::string> CompileDefs;
53     cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>>
54       LinkLibraries;
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;
61     bool NoCache = false;
62
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;
71   };
72
73   Arguments ParseArgs(cmRange<std::vector<std::string>::const_iterator> args,
74                       bool isTryRun);
75
76   /**
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.
79    *
80    * This function requires at least two \p arguments and will crash if given
81    * fewer.
82    */
83   bool TryCompileCode(Arguments& arguments,
84                       cmStateEnums::TargetType targetType);
85
86   /**
87    * Returns \c true if \p path resides within a CMake temporary directory,
88    * otherwise returns \c false.
89    */
90   static bool IsTemporary(std::string const& path);
91
92   /**
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.
96    */
97   void CleanupFiles(std::string const& binDir);
98
99   /**
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.
103    */
104   void FindOutputFile(const std::string& targetName);
105
106   std::string BinaryDirectory;
107   std::string OutputFile;
108   std::string FindErrorMessage;
109   bool SrcFileSignature = false;
110   cmMakefile* Makefile;
111
112 private:
113   std::string WriteSource(std::string const& name, std::string const& content,
114                           char const* command) const;
115
116   Arguments ParseArgs(
117     const cmRange<std::vector<std::string>::const_iterator>& args,
118     const cmArgumentParser<Arguments>& parser,
119     std::vector<std::string>& unparsedArguments);
120 };