@@ -42,14 +42,13 @@ executeCmakeHelp(llvm::StringRef cmakePath)
42
42
MRDOCS_CHECK (errOutputPath, " Failed to create temporary file" );
43
43
std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef (), outputPath.path (), errOutputPath.path ()};
44
44
std::vector<llvm::StringRef> const args = {cmakePath, " --help" };
45
- llvm::ArrayRef<llvm::StringRef> emptyEnv;
46
- int const result = llvm::sys::ExecuteAndWait (cmakePath, args, emptyEnv, redirects);
45
+ int const result = llvm::sys::ExecuteAndWait (cmakePath, args, std::nullopt, redirects);
47
46
if (result != 0 )
48
47
{
49
48
auto const bufferOrError = llvm::MemoryBuffer::getFile (errOutputPath.path ());
50
- MRDOCS_CHECK (bufferOrError, " CMake execution failed (no error output available)" );
49
+ MRDOCS_CHECK (bufferOrError, " CMake --help execution failed (no error output available)" );
51
50
auto const bufferOrError2 = llvm::MemoryBuffer::getFile (errOutputPath.path ());
52
- MRDOCS_CHECK (bufferOrError2, " CMake execution failed (no error output available)" );
51
+ MRDOCS_CHECK (bufferOrError2, " CMake --help execution failed (no error output available)" );
53
52
// Concatenate both outputs
54
53
std::string output;
55
54
if (bufferOrError.get ()->getBuffer ().str ().empty ())
@@ -67,35 +66,63 @@ executeCmakeHelp(llvm::StringRef cmakePath)
67
66
return Unexpected (Error (" CMake --help execution failed: \n " + output));
68
67
}
69
68
auto const bufferOrError = llvm::MemoryBuffer::getFile (outputPath.path ());
70
- MRDOCS_CHECK (bufferOrError, " Failed to read CMake help output" );
69
+ MRDOCS_CHECK (bufferOrError, " Failed to read CMake -- help output" );
71
70
return bufferOrError.get ()->getBuffer ().str ();
72
71
}
73
72
73
+
74
74
Expected<std::string>
75
- getCmakeDefaultGenerator (llvm::StringRef cmakePath)
75
+ executeCmakeSystemInformation (llvm::StringRef cmakePath)
76
76
{
77
- Expected<std::string> const cmakeHelpExp = executeCmakeHelp (cmakePath);
78
- if (!cmakeHelpExp) {
79
- if (llvm::sys::path::extension (cmakePath) == " .exe" )
77
+ ScopedTempFile const outputPath (" cmake-system-information-out" , " txt" );
78
+ MRDOCS_CHECK (outputPath, " Failed to create temporary file" );
79
+ ScopedTempFile const errOutputPath (" cmake-system-information-err" , " txt" );
80
+ MRDOCS_CHECK (errOutputPath, " Failed to create temporary file" );
81
+ std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef (), outputPath.path (), errOutputPath.path ()};
82
+ std::vector<llvm::StringRef> const args = {cmakePath, " --system-information" };
83
+ int const result = llvm::sys::ExecuteAndWait (cmakePath, args, std::nullopt, redirects);
84
+ if (result != 0 )
85
+ {
86
+ auto const bufferOrError = llvm::MemoryBuffer::getFile (errOutputPath.path ());
87
+ MRDOCS_CHECK (bufferOrError, " CMake --system-information execution failed (no error output available)" );
88
+ auto const bufferOrError2 = llvm::MemoryBuffer::getFile (errOutputPath.path ());
89
+ MRDOCS_CHECK (bufferOrError2, " CMake --system-information execution failed (no error output available)" );
90
+ // Concatenate both outputs
91
+ std::string output;
92
+ if (bufferOrError.get ()->getBuffer ().str ().empty ())
80
93
{
81
- return " Visual Studio 17 2022" ;
94
+ output = bufferOrError2.get ()->getBuffer ().str ();
95
+ }
96
+ else if (bufferOrError2.get ()->getBuffer ().str ().empty ())
97
+ {
98
+ output = bufferOrError.get ()->getBuffer ().str ();
82
99
}
83
100
else
84
101
{
85
- return " Unix Makefiles " ;
102
+ output = bufferOrError. get ()-> getBuffer (). str () + " \n " + bufferOrError2. get ()-> getBuffer (). str () ;
86
103
}
104
+ return Unexpected (Error (" CMake --help execution failed: \n " + output));
87
105
}
106
+ auto const bufferOrError = llvm::MemoryBuffer::getFile (outputPath.path ());
107
+ MRDOCS_CHECK (bufferOrError, " Failed to read CMake --system-information output" );
108
+ return bufferOrError.get ()->getBuffer ().str ();
109
+ }
88
110
89
- std::string const cmakeHelp = *std::move (cmakeHelpExp);
111
+ Expected<std::string>
112
+ parseCmakeHelpOutput (std::string const & cmakeHelp)
113
+ {
90
114
std::istringstream stream (cmakeHelp);
91
115
std::string line;
92
116
std::string defaultGenerator;
93
117
94
- while (std::getline (stream, line)) {
95
- if (line[0 ] == ' *' && line[1 ] == ' ' ) {
118
+ while (std::getline (stream, line))
119
+ {
120
+ if (line[0 ] == ' *' && line[1 ] == ' ' )
121
+ {
96
122
size_t const start = 2 ;
97
123
size_t const end = line.find (" =" , start);
98
- if (end == std::string::npos) {
124
+ if (end == std::string::npos)
125
+ {
99
126
continue ;
100
127
}
101
128
return line.substr (start, end - start);
@@ -104,8 +131,54 @@ getCmakeDefaultGenerator(llvm::StringRef cmakePath)
104
131
return Unexpected (Error (" Default CMake generator not found" ));
105
132
}
106
133
134
+ Expected<std::string>
135
+ parseCmakeSystemInformationOutput (std::string const & cmakeSystemInformation)
136
+ {
137
+ std::istringstream stream (cmakeSystemInformation);
138
+ std::string line;
139
+ std::string defaultGenerator;
140
+
141
+ while (std::getline (stream, line))
142
+ {
143
+ if (line.starts_with (" CMAKE_GENERATOR \" " ))
144
+ {
145
+ size_t const start = 17 ;
146
+ size_t const end = line.find (" \" " , start);
147
+ if (end == std::string::npos)
148
+ {
149
+ continue ;
150
+ }
151
+ return line.substr (start, end - start);
152
+ }
153
+ }
154
+ return Unexpected (Error (" Default CMake generator not found" ));
155
+ }
156
+
157
+ Expected<std::string>
158
+ getCmakeDefaultGenerator (llvm::StringRef cmakePath)
159
+ {
160
+ Expected<std::string> const cmakeHelpExp = executeCmakeHelp (cmakePath);
161
+ if (!cmakeHelpExp)
162
+ {
163
+ Expected<std::string> const cmakeSystemInformationExp = executeCmakeSystemInformation (cmakePath);
164
+ if (!cmakeSystemInformationExp)
165
+ {
166
+ if (llvm::sys::path::extension (cmakePath) == " .exe" )
167
+ {
168
+ return " Visual Studio 17 2022" ;
169
+ }
170
+ return " Unix Makefiles" ;
171
+ }
172
+ std::string const cmakeSystemInformation = *std::move (cmakeSystemInformationExp);
173
+ return parseCmakeSystemInformationOutput (cmakeSystemInformation);
174
+ }
175
+
176
+ std::string const cmakeHelp = *std::move (cmakeHelpExp);
177
+ return parseCmakeHelpOutput (cmakeHelp);
178
+ }
179
+
107
180
Expected<bool >
108
- cmakeDefaultGeneratorIsVisualStudio (llvm::StringRef cmakePath)
181
+ cmakeDefaultGeneratorIsVisualStudio (llvm::StringRef cmakePath)
109
182
{
110
183
MRDOCS_TRY (auto const defaultGenerator, getCmakeDefaultGenerator (cmakePath));
111
184
return defaultGenerator.starts_with (" Visual Studio" );
@@ -145,7 +218,7 @@ parseBashIdentifier(std::string_view str)
145
218
return identifier;
146
219
}
147
220
148
- std::vector<std::string>
221
+ std::vector<std::string>
149
222
parseBashArgs (std::string_view str)
150
223
{
151
224
std::vector<std::string> args;
0 commit comments