Skip to content

Commit 939edc8

Browse files
Gennaro Protaalandefreitas
authored andcommitted
Print all external commands
This closes issue #914.
1 parent 7a4e1c3 commit 939edc8

File tree

6 files changed

+92
-8
lines changed

6 files changed

+92
-8
lines changed

src/lib/Lib/CMakeExecution.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//
1010

1111
#include "lib/Lib/CMakeExecution.hpp"
12+
#include "lib/Lib/ExecuteAndWaitWithLogging.hpp"
1213
#include "lib/Support/Path.hpp"
1314

1415
#include <llvm/Support/FileSystem.h>
@@ -37,7 +38,7 @@ getCmakePath()
3738
MRDOCS_CHECK(path, "CMake executable not found");
3839
std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef(), llvm::StringRef(), llvm::StringRef()};
3940
std::vector<llvm::StringRef> const args = {*path, "--version"};
40-
int const result = llvm::sys::ExecuteAndWait(*path, args, std::nullopt, redirects);
41+
int const result = ExecuteAndWaitWithLogging(*path, args, std::nullopt, redirects);
4142
MRDOCS_CHECK(result == 0, "CMake execution failed when checking version");
4243
return *path;
4344
}
@@ -51,7 +52,7 @@ executeCmakeHelp(llvm::StringRef cmakePath)
5152
MRDOCS_CHECK(errOutputPath, "Failed to create temporary file");
5253
std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef(), outputPath.path(), errOutputPath.path()};
5354
std::vector<llvm::StringRef> const args = {cmakePath, "--help"};
54-
int const result = llvm::sys::ExecuteAndWait(cmakePath, args, std::nullopt, redirects);
55+
int const result = ExecuteAndWaitWithLogging(cmakePath, args, std::nullopt, redirects);
5556
if (result != 0)
5657
{
5758
auto const bufferOrError = llvm::MemoryBuffer::getFile(errOutputPath.path());
@@ -89,7 +90,7 @@ executeCmakeSystemInformation(llvm::StringRef cmakePath)
8990
MRDOCS_CHECK(errOutputPath, "Failed to create temporary file");
9091
std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef(), outputPath.path(), errOutputPath.path()};
9192
std::vector<llvm::StringRef> const args = {cmakePath, "--system-information"};
92-
int const result = llvm::sys::ExecuteAndWait(cmakePath, args, std::nullopt, redirects);
93+
int const result = ExecuteAndWaitWithLogging(cmakePath, args, std::nullopt, redirects);
9394
if (result != 0)
9495
{
9596
auto const bufferOrError = llvm::MemoryBuffer::getFile(errOutputPath.path());
@@ -518,7 +519,7 @@ executeCmakeExportCompileCommands(llvm::StringRef projectPath, llvm::StringRef c
518519
MRDOCS_TRY(auto args, generateCMakeArgs(cmakePath, cmakeArgs, projectPath, buildDir));
519520
std::vector<llvm::StringRef> argsRef(args.begin(), args.end());
520521

521-
int const result = llvm::sys::ExecuteAndWait(cmakePath, argsRef, std::nullopt, redirects);
522+
int const result = ExecuteAndWaitWithLogging(cmakePath, argsRef, std::nullopt, redirects);
522523
if (result != 0) {
523524
return Unexpected(Error("CMake execution failed"));
524525
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
// Copyright (c) 2025 Gennaro Prota ([email protected])
7+
//
8+
// Official repository: https://github.com/cppalliance/mrdocs
9+
//
10+
11+
#include "lib/Lib/ExecuteAndWaitWithLogging.hpp"
12+
#include "lib/Support/Report.hpp"
13+
#include "mrdocs/Support/Assert.hpp"
14+
15+
namespace clang::mrdocs {
16+
17+
int ExecuteAndWaitWithLogging(
18+
llvm::StringRef program,
19+
llvm::ArrayRef<llvm::StringRef> args,
20+
std::optional<llvm::ArrayRef<llvm::StringRef>> env,
21+
llvm::ArrayRef<std::optional<llvm::StringRef>> redirects,
22+
unsigned secondsToWait,
23+
unsigned memoryLimit,
24+
std::string* errMsg,
25+
bool* executionFailed,
26+
std::optional<llvm::sys::ProcessStatistics>* procStat,
27+
llvm::BitVector* affinityMask)
28+
{
29+
MRDOCS_ASSERT(args.size() >= 1);
30+
llvm::SmallString<128> command = args[0];
31+
for (llvm::ArrayRef<llvm::StringRef>::size_type i = 1; i < args.size(); ++i) {
32+
command += ' ';
33+
command += args[i];
34+
}
35+
report::info("{}", command);
36+
return llvm::sys::ExecuteAndWait(program, args, env, redirects, secondsToWait, memoryLimit, errMsg, executionFailed, procStat, affinityMask);
37+
}
38+
39+
} // clang::mrdocs
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// Licensed under the Apache License v2.0 with LLVM Exceptions.
3+
// See https://llvm.org/LICENSE.txt for license information.
4+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
//
6+
// Copyright (c) 2025 Gennaro Prota ([email protected])
7+
//
8+
// Official repository: https://github.com/cppalliance/mrdocs
9+
//
10+
11+
#ifndef MRDOCS_LIB_EXECUTE_AND_WAIT_WITH_LOGGING_HPP
12+
#define MRDOCS_LIB_EXECUTE_AND_WAIT_WITH_LOGGING_HPP
13+
14+
#include <llvm/Support/Program.h>
15+
16+
namespace clang::mrdocs {
17+
18+
/**
19+
* A wrapper around llvm::sys::ExecuteAndWait() that prints the command
20+
* being run (with its arguments).
21+
*
22+
* This function has the same parameters, with the same meaning, as
23+
* llvm::sys::ExecuteAndWait().
24+
*
25+
* \par Preconditions
26+
* `args.size() >= 1`.
27+
*/
28+
int ExecuteAndWaitWithLogging(
29+
llvm::StringRef program,
30+
llvm::ArrayRef<llvm::StringRef> args,
31+
std::optional<llvm::ArrayRef<llvm::StringRef>> env = std::nullopt,
32+
llvm::ArrayRef<std::optional<llvm::StringRef>> redirects = {},
33+
unsigned secondsToWait = 0,
34+
unsigned memoryLimit = 0,
35+
std::string* errMsg = nullptr,
36+
bool* executionFailed = nullptr,
37+
std::optional<llvm::sys::ProcessStatistics>* procStat = nullptr,
38+
llvm::BitVector* affinityMask = nullptr);
39+
40+
} // clang::mrdocs
41+
42+
#endif // MRDOCS_LIB_EXECUTE_AND_WAIT_WITH_LOGGING_HPP

src/lib/Lib/MrDocsCompilationDatabase.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "lib/Support/Debug.hpp"
1313
#include "lib/Support/Path.hpp"
1414
#include "lib/Lib/ConfigImpl.hpp"
15+
#include "lib/Lib/ExecuteAndWaitWithLogging.hpp"
1516
#include "lib/Lib/MrDocsCompilationDatabase.hpp"
1617
#include <mrdocs/Support/Report.hpp>
1718
#include <fmt/format.h>
@@ -322,7 +323,7 @@ adjustCommandLine(
322323
outputPath.path(),
323324
llvm::StringRef()
324325
};
325-
int const result = llvm::sys::ExecuteAndWait(
326+
int const result = ExecuteAndWaitWithLogging(
326327
progName, args, std::nullopt, redirects);
327328
if (result != 0)
328329
{

src/test/TestRunner.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "lib/Support/Path.hpp"
1515
#include "lib/Lib/ConfigImpl.hpp"
1616
#include "lib/Lib/CorpusImpl.hpp"
17+
#include "lib/Lib/ExecuteAndWaitWithLogging.hpp"
1718
#include "lib/Lib/MrDocsCompilationDatabase.hpp"
1819
#include "lib/Lib/SingleFileDB.hpp"
1920
#include "lib/Gen/hbs/HandlebarsGenerator.hpp"
@@ -232,7 +233,7 @@ handleFile(
232233
path::replace_extension(badPath, gen_->fileExtension());
233234
std::array<llvm::StringRef, 5u> args {
234235
diffCmdPath_.get(), "-u", "--color", expectedPath, badPath };
235-
llvm::sys::ExecuteAndWait(diffCmdPath_.get(), args);
236+
ExecuteAndWaitWithLogging(diffCmdPath_.get(), args);
236237
}
237238
}
238239
}

src/tool/CompilerInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111

1212
#include "CompilerInfo.hpp"
13-
13+
#include "lib/Lib/ExecuteAndWaitWithLogging.hpp"
1414
#include <mrdocs/Support/Error.hpp>
1515

1616
#include <llvm/Support/Program.h>
@@ -35,7 +35,7 @@ getCompilerVerboseOutput(llvm::StringRef compilerPath)
3535
std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef(), llvm::StringRef(), outputPath.str()};
3636
std::vector<llvm::StringRef> const args = {compilerPath, "-v", "-E", "-x", "c++", "-"};
3737
llvm::ArrayRef<llvm::StringRef> emptyEnv;
38-
int const result = llvm::sys::ExecuteAndWait(compilerPath, args, emptyEnv, redirects);
38+
int const result = ExecuteAndWaitWithLogging(compilerPath, args, emptyEnv, redirects);
3939
if (result != 0)
4040
{
4141
llvm::sys::fs::remove(outputPath);

0 commit comments

Comments
 (0)