Skip to content

Print all external commands #918

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/lib/Lib/CMakeExecution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//

#include "lib/Lib/CMakeExecution.hpp"
#include "lib/Lib/ExecuteAndWaitWithLogging.hpp"
#include "lib/Support/Path.hpp"

#include <llvm/Support/FileSystem.h>
Expand Down Expand Up @@ -37,7 +38,7 @@ getCmakePath()
MRDOCS_CHECK(path, "CMake executable not found");
std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef(), llvm::StringRef(), llvm::StringRef()};
std::vector<llvm::StringRef> const args = {*path, "--version"};
int const result = llvm::sys::ExecuteAndWait(*path, args, std::nullopt, redirects);
int const result = ExecuteAndWaitWithLogging(*path, args, std::nullopt, redirects);
MRDOCS_CHECK(result == 0, "CMake execution failed when checking version");
return *path;
}
Expand All @@ -51,7 +52,7 @@ executeCmakeHelp(llvm::StringRef cmakePath)
MRDOCS_CHECK(errOutputPath, "Failed to create temporary file");
std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef(), outputPath.path(), errOutputPath.path()};
std::vector<llvm::StringRef> const args = {cmakePath, "--help"};
int const result = llvm::sys::ExecuteAndWait(cmakePath, args, std::nullopt, redirects);
int const result = ExecuteAndWaitWithLogging(cmakePath, args, std::nullopt, redirects);
if (result != 0)
{
auto const bufferOrError = llvm::MemoryBuffer::getFile(errOutputPath.path());
Expand Down Expand Up @@ -89,7 +90,7 @@ executeCmakeSystemInformation(llvm::StringRef cmakePath)
MRDOCS_CHECK(errOutputPath, "Failed to create temporary file");
std::optional<llvm::StringRef> const redirects[] = {llvm::StringRef(), outputPath.path(), errOutputPath.path()};
std::vector<llvm::StringRef> const args = {cmakePath, "--system-information"};
int const result = llvm::sys::ExecuteAndWait(cmakePath, args, std::nullopt, redirects);
int const result = ExecuteAndWaitWithLogging(cmakePath, args, std::nullopt, redirects);
if (result != 0)
{
auto const bufferOrError = llvm::MemoryBuffer::getFile(errOutputPath.path());
Expand Down Expand Up @@ -518,7 +519,7 @@ executeCmakeExportCompileCommands(llvm::StringRef projectPath, llvm::StringRef c
MRDOCS_TRY(auto args, generateCMakeArgs(cmakePath, cmakeArgs, projectPath, buildDir));
std::vector<llvm::StringRef> argsRef(args.begin(), args.end());

int const result = llvm::sys::ExecuteAndWait(cmakePath, argsRef, std::nullopt, redirects);
int const result = ExecuteAndWaitWithLogging(cmakePath, argsRef, std::nullopt, redirects);
if (result != 0) {
return Unexpected(Error("CMake execution failed"));
}
Expand Down
39 changes: 39 additions & 0 deletions src/lib/Lib/ExecuteAndWaitWithLogging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (c) 2025 Gennaro Prota ([email protected])
//
// Official repository: https://github.com/cppalliance/mrdocs
//

#include "lib/Lib/ExecuteAndWaitWithLogging.hpp"
#include "lib/Support/Report.hpp"
#include "mrdocs/Support/Assert.hpp"

namespace clang::mrdocs {

int ExecuteAndWaitWithLogging(
llvm::StringRef program,
llvm::ArrayRef<llvm::StringRef> args,
std::optional<llvm::ArrayRef<llvm::StringRef>> env,
llvm::ArrayRef<std::optional<llvm::StringRef>> redirects,
unsigned secondsToWait,
unsigned memoryLimit,
std::string* errMsg,
bool* executionFailed,
std::optional<llvm::sys::ProcessStatistics>* procStat,
llvm::BitVector* affinityMask)
{
MRDOCS_ASSERT(args.size() >= 1);
llvm::SmallString<128> command = args[0];
for (llvm::ArrayRef<llvm::StringRef>::size_type i = 1; i < args.size(); ++i) {
command += ' ';
command += args[i];
}
report::info("{}", command);
return llvm::sys::ExecuteAndWait(program, args, env, redirects, secondsToWait, memoryLimit, errMsg, executionFailed, procStat, affinityMask);
}

} // clang::mrdocs
42 changes: 42 additions & 0 deletions src/lib/Lib/ExecuteAndWaitWithLogging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//
// Licensed under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Copyright (c) 2025 Gennaro Prota ([email protected])
//
// Official repository: https://github.com/cppalliance/mrdocs
//

#ifndef MRDOCS_LIB_EXECUTE_AND_WAIT_WITH_LOGGING_HPP
#define MRDOCS_LIB_EXECUTE_AND_WAIT_WITH_LOGGING_HPP

#include <llvm/Support/Program.h>

namespace clang::mrdocs {

/**
* A wrapper around llvm::sys::ExecuteAndWait() that prints the command
* being run (with its arguments).
*
* This function has the same parameters, with the same meaning, as
* llvm::sys::ExecuteAndWait().
*
* \par Preconditions
* `args.size() >= 1`.
*/
int ExecuteAndWaitWithLogging(
llvm::StringRef program,
llvm::ArrayRef<llvm::StringRef> args,
std::optional<llvm::ArrayRef<llvm::StringRef>> env = std::nullopt,
llvm::ArrayRef<std::optional<llvm::StringRef>> redirects = {},
unsigned secondsToWait = 0,
unsigned memoryLimit = 0,
std::string* errMsg = nullptr,
bool* executionFailed = nullptr,
std::optional<llvm::sys::ProcessStatistics>* procStat = nullptr,
llvm::BitVector* affinityMask = nullptr);

} // clang::mrdocs

#endif // MRDOCS_LIB_EXECUTE_AND_WAIT_WITH_LOGGING_HPP
3 changes: 2 additions & 1 deletion src/lib/Lib/MrDocsCompilationDatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "lib/Support/Debug.hpp"
#include "lib/Support/Path.hpp"
#include "lib/Lib/ConfigImpl.hpp"
#include "lib/Lib/ExecuteAndWaitWithLogging.hpp"
#include "lib/Lib/MrDocsCompilationDatabase.hpp"
#include <mrdocs/Support/Report.hpp>
#include <fmt/format.h>
Expand Down Expand Up @@ -322,7 +323,7 @@ adjustCommandLine(
outputPath.path(),
llvm::StringRef()
};
int const result = llvm::sys::ExecuteAndWait(
int const result = ExecuteAndWaitWithLogging(
progName, args, std::nullopt, redirects);
if (result != 0)
{
Expand Down
3 changes: 2 additions & 1 deletion src/test/TestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "lib/Support/Path.hpp"
#include "lib/Lib/ConfigImpl.hpp"
#include "lib/Lib/CorpusImpl.hpp"
#include "lib/Lib/ExecuteAndWaitWithLogging.hpp"
#include "lib/Lib/MrDocsCompilationDatabase.hpp"
#include "lib/Lib/SingleFileDB.hpp"
#include "lib/Gen/hbs/HandlebarsGenerator.hpp"
Expand Down Expand Up @@ -232,7 +233,7 @@ handleFile(
path::replace_extension(badPath, gen_->fileExtension());
std::array<llvm::StringRef, 5u> args {
diffCmdPath_.get(), "-u", "--color", expectedPath, badPath };
llvm::sys::ExecuteAndWait(diffCmdPath_.get(), args);
ExecuteAndWaitWithLogging(diffCmdPath_.get(), args);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/tool/CompilerInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//

#include "CompilerInfo.hpp"

#include "lib/Lib/ExecuteAndWaitWithLogging.hpp"
#include <mrdocs/Support/Error.hpp>

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