Coverage Report

Created: 2026-02-09 06:05

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