diff --git a/src/lib/Lib/CMakeExecution.cpp b/src/lib/Lib/CMakeExecution.cpp index c589c6497..c0dd1049e 100644 --- a/src/lib/Lib/CMakeExecution.cpp +++ b/src/lib/Lib/CMakeExecution.cpp @@ -9,6 +9,7 @@ // #include "lib/Lib/CMakeExecution.hpp" +#include "lib/Lib/ExecuteAndWaitWithLogging.hpp" #include "lib/Support/Path.hpp" #include @@ -37,7 +38,7 @@ getCmakePath() MRDOCS_CHECK(path, "CMake executable not found"); std::optional const redirects[] = {llvm::StringRef(), llvm::StringRef(), llvm::StringRef()}; std::vector 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; } @@ -51,7 +52,7 @@ executeCmakeHelp(llvm::StringRef cmakePath) MRDOCS_CHECK(errOutputPath, "Failed to create temporary file"); std::optional const redirects[] = {llvm::StringRef(), outputPath.path(), errOutputPath.path()}; std::vector 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()); @@ -89,7 +90,7 @@ executeCmakeSystemInformation(llvm::StringRef cmakePath) MRDOCS_CHECK(errOutputPath, "Failed to create temporary file"); std::optional const redirects[] = {llvm::StringRef(), outputPath.path(), errOutputPath.path()}; std::vector 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()); @@ -518,7 +519,7 @@ executeCmakeExportCompileCommands(llvm::StringRef projectPath, llvm::StringRef c MRDOCS_TRY(auto args, generateCMakeArgs(cmakePath, cmakeArgs, projectPath, buildDir)); std::vector 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")); } diff --git a/src/lib/Lib/ExecuteAndWaitWithLogging.cpp b/src/lib/Lib/ExecuteAndWaitWithLogging.cpp new file mode 100644 index 000000000..a99796f0c --- /dev/null +++ b/src/lib/Lib/ExecuteAndWaitWithLogging.cpp @@ -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 (gennaro.prota@gmail.com) +// +// 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 args, + std::optional> env, + llvm::ArrayRef> redirects, + unsigned secondsToWait, + unsigned memoryLimit, + std::string* errMsg, + bool* executionFailed, + std::optional* procStat, + llvm::BitVector* affinityMask) +{ + MRDOCS_ASSERT(args.size() >= 1); + llvm::SmallString<128> command = args[0]; + for (llvm::ArrayRef::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 diff --git a/src/lib/Lib/ExecuteAndWaitWithLogging.hpp b/src/lib/Lib/ExecuteAndWaitWithLogging.hpp new file mode 100644 index 000000000..ab9d79b21 --- /dev/null +++ b/src/lib/Lib/ExecuteAndWaitWithLogging.hpp @@ -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 (gennaro.prota@gmail.com) +// +// 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 + +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 args, + std::optional> env = std::nullopt, + llvm::ArrayRef> redirects = {}, + unsigned secondsToWait = 0, + unsigned memoryLimit = 0, + std::string* errMsg = nullptr, + bool* executionFailed = nullptr, + std::optional* procStat = nullptr, + llvm::BitVector* affinityMask = nullptr); + +} // clang::mrdocs + +#endif // MRDOCS_LIB_EXECUTE_AND_WAIT_WITH_LOGGING_HPP diff --git a/src/lib/Lib/MrDocsCompilationDatabase.cpp b/src/lib/Lib/MrDocsCompilationDatabase.cpp index dbf64caf4..9f8df357d 100644 --- a/src/lib/Lib/MrDocsCompilationDatabase.cpp +++ b/src/lib/Lib/MrDocsCompilationDatabase.cpp @@ -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 #include @@ -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) { diff --git a/src/test/TestRunner.cpp b/src/test/TestRunner.cpp index 189f8d2c8..53e592f4b 100644 --- a/src/test/TestRunner.cpp +++ b/src/test/TestRunner.cpp @@ -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" @@ -232,7 +233,7 @@ handleFile( path::replace_extension(badPath, gen_->fileExtension()); std::array args { diffCmdPath_.get(), "-u", "--color", expectedPath, badPath }; - llvm::sys::ExecuteAndWait(diffCmdPath_.get(), args); + ExecuteAndWaitWithLogging(diffCmdPath_.get(), args); } } } diff --git a/src/tool/CompilerInfo.cpp b/src/tool/CompilerInfo.cpp index ce5fe99bb..1ebfdc1b0 100644 --- a/src/tool/CompilerInfo.cpp +++ b/src/tool/CompilerInfo.cpp @@ -10,7 +10,7 @@ // #include "CompilerInfo.hpp" - +#include "lib/Lib/ExecuteAndWaitWithLogging.hpp" #include #include @@ -35,7 +35,7 @@ getCompilerVerboseOutput(llvm::StringRef compilerPath) std::optional const redirects[] = {llvm::StringRef(), llvm::StringRef(), outputPath.str()}; std::vector const args = {compilerPath, "-v", "-E", "-x", "c++", "-"}; llvm::ArrayRef 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);