/src/CMake/Source/cmCoreTryCompile.h
Line | Count | Source |
1 | | /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying |
2 | | file LICENSE.rst 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 <utility> |
10 | | #include <vector> |
11 | | |
12 | | #include <cm/optional> |
13 | | #include <cm/string_view> |
14 | | |
15 | | #include "cmArgumentParser.h" |
16 | | #include "cmArgumentParserTypes.h" |
17 | | #include "cmList.h" |
18 | | |
19 | | class cmConfigureLog; |
20 | | class cmMakefile; |
21 | | template <typename Iter> |
22 | | class cmRange; |
23 | | |
24 | | namespace cm { |
25 | | enum class TargetType; |
26 | | } // namespace cm |
27 | | |
28 | | struct cmTryCompileResult |
29 | | { |
30 | | cm::optional<std::string> LogDescription; |
31 | | std::map<std::string, std::string> CMakeVariables; |
32 | | |
33 | | std::string SourceDirectory; |
34 | | std::string BinaryDirectory; |
35 | | |
36 | | bool VariableCached = true; |
37 | | std::string Variable; |
38 | | |
39 | | std::string Output; |
40 | | int ExitCode = 1; |
41 | | }; |
42 | | |
43 | | /** \class cmCoreTryCompile |
44 | | * \brief Base class for cmTryCompileCommand and cmTryRunCommand |
45 | | * |
46 | | * cmCoreTryCompile implements the functionality to build a program. |
47 | | * It is the base class for cmTryCompileCommand and cmTryRunCommand. |
48 | | */ |
49 | | class cmCoreTryCompile |
50 | | { |
51 | | public: |
52 | | cmCoreTryCompile(cmMakefile* mf) |
53 | 0 | : Makefile(mf) |
54 | 0 | { |
55 | 0 | } |
56 | | |
57 | | struct Arguments : public ArgumentParser::ParseResult |
58 | | { |
59 | | Arguments(cmMakefile const* mf) |
60 | 0 | : Makefile(mf) |
61 | 0 | { |
62 | 0 | } |
63 | | |
64 | | cmMakefile const* Makefile; |
65 | | |
66 | | enum class SourceType |
67 | | { |
68 | | Normal, |
69 | | CxxModule, |
70 | | Directory, |
71 | | }; |
72 | | |
73 | | cm::optional<std::string> CompileResultVariable; |
74 | | cm::optional<std::string> BinaryDirectory; |
75 | | cm::optional<std::string> SourceDirectoryOrFile; |
76 | | cm::optional<std::string> ProjectName; |
77 | | cm::optional<std::string> TargetName; |
78 | | cm::optional<ArgumentParser::NonEmpty< |
79 | | std::vector<std::pair<std::string, SourceType>>>> |
80 | | Sources; |
81 | | cm::optional<ArgumentParser::NonEmpty< |
82 | | std::vector<std::pair<std::string, SourceType>>>> |
83 | | SourceFromContent; |
84 | | cm::optional<ArgumentParser::NonEmpty< |
85 | | std::vector<std::pair<std::string, SourceType>>>> |
86 | | SourceFromVar; |
87 | | cm::optional<ArgumentParser::NonEmpty< |
88 | | std::vector<std::pair<std::string, SourceType>>>> |
89 | | SourceFromFile; |
90 | | ArgumentParser::MaybeEmpty<std::vector<std::string>> CMakeFlags{ |
91 | | 1, "CMAKE_FLAGS" |
92 | | }; // fake argv[0] |
93 | | cmList CompileDefs; |
94 | | cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>> |
95 | | LinkLibraries; |
96 | | ArgumentParser::MaybeEmpty<std::vector<std::string>> LinkOptions; |
97 | | cm::optional<std::string> LinkerLanguage; |
98 | | std::map<std::string, std::string> LangProps; |
99 | | std::string CMakeInternal; |
100 | | cm::optional<std::string> OutputVariable; |
101 | | cm::optional<std::string> CopyFileTo; |
102 | | cm::optional<std::string> CopyFileError; |
103 | | cm::optional<ArgumentParser::NonEmpty<std::string>> LogDescription; |
104 | | bool NoCache = false; |
105 | | bool NoLog = false; |
106 | | |
107 | | ArgumentParser::Continue SetSourceType(cm::string_view sourceType); |
108 | | SourceType SourceTypeContext = SourceType::Normal; |
109 | | std::string SourceTypeError; |
110 | | |
111 | | // Argument for try_run only. |
112 | | // Keep in sync with warnings in cmCoreTryCompile::ParseArgs. |
113 | | cm::optional<std::string> CompileOutputVariable; |
114 | | cm::optional<std::string> RunOutputVariable; |
115 | | cm::optional<std::string> RunOutputStdOutVariable; |
116 | | cm::optional<std::string> RunOutputStdErrVariable; |
117 | | cm::optional<std::string> RunWorkingDirectory; |
118 | | cm::optional<ArgumentParser::MaybeEmpty<std::vector<std::string>>> RunArgs; |
119 | | }; |
120 | | |
121 | | Arguments ParseArgs(cmRange<std::vector<std::string>::const_iterator> args, |
122 | | bool isTryRun); |
123 | | |
124 | | /** |
125 | | * This is the core code for try compile. It is here so that other commands, |
126 | | * such as TryRun can access the same logic without duplication. |
127 | | * |
128 | | * This function requires at least two \p arguments and will crash if given |
129 | | * fewer. |
130 | | */ |
131 | | cm::optional<cmTryCompileResult> TryCompileCode(Arguments& arguments, |
132 | | cm::TargetType targetType); |
133 | | |
134 | | /** |
135 | | * Returns \c true if \p path resides within a CMake temporary directory, |
136 | | * otherwise returns \c false. |
137 | | */ |
138 | | static bool IsTemporary(std::string const& path); |
139 | | |
140 | | /** |
141 | | * This deletes all the files created by TryCompileCode. |
142 | | * This way we do not have to rely on the timing and |
143 | | * dependencies of makefiles. |
144 | | */ |
145 | | void CleanupFiles(std::string const& binDir); |
146 | | |
147 | | /** |
148 | | * This tries to find the (executable) file created by |
149 | | TryCompileCode. The result is stored in OutputFile. If nothing is found, |
150 | | the error message is stored in FindErrorMessage. |
151 | | */ |
152 | | void FindOutputFile(std::string const& targetName); |
153 | | |
154 | | static void WriteTryCompileEventFields( |
155 | | cmConfigureLog& log, cmTryCompileResult const& compileResult); |
156 | | |
157 | | std::string BinaryDirectory; |
158 | | std::string OutputFile; |
159 | | std::string FindErrorMessage; |
160 | | bool SrcFileSignature = false; |
161 | | cmMakefile* Makefile; |
162 | | |
163 | | private: |
164 | | std::string WriteSource(std::string const& name, std::string const& content, |
165 | | char const* command) const; |
166 | | |
167 | | Arguments ParseArgs(cmRange<std::vector<std::string>::const_iterator> args, |
168 | | cmArgumentParser<Arguments> const& parser, |
169 | | std::vector<std::string>& unparsedArguments); |
170 | | }; |