-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[clang][MBD] set up module build daemon infrastructure #67562
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
Draft
cpsughrue
wants to merge
33
commits into
llvm:main
Choose a base branch
from
cpsughrue:MBD_handshake
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
aca58d3
[clang][MBD] set up module build daemon infrastructure
cpsughrue 4257079
Set ModuleBuildDaemonServer member functions to return void
cpsughrue 5efeff9
Modify ListeningServer::accept to take std::optional<std::chrono::mic…
cpsughrue cc21148
Make raw_socket_stream FD atomic
cpsughrue 9a46ae1
Remove exit in signal handler
cpsughrue 04ef24e
Create setupSignal functionality so I can set signals back to their d…
cpsughrue f17a145
Make blacker happy
cpsughrue 15c5933
Fix bug where if daemon crashes and does not unlink socket file the n…
cpsughrue f73d5ca
Remove std::move(s) preventing copy elision -Wpessimizing-move
cpsughrue 0e47580
Remove exit in shutdownDeamon because ListeningSocket destructor will…
cpsughrue 601bb4d
Take advantage of ability to convert between std::seconds and std::mi…
cpsughrue 1792d90
Switch to break statement to get rid of state whn calling createUnix
cpsughrue bf58d5d
Fix the style of a few variable names
cpsughrue 6f408ae
After ModuleBuildDaemonServer deconstructs ignore signals
cpsughrue 145f565
Switch over to using non-deprecated version of close and unlink for w…
cpsughrue 87caa38
fix windows build and formatting issues
cpsughrue cfb4a7f
Remove check that mbd.sock was removed - fails on windows due to how …
cpsughrue 21efa0d
Write test to crash situation where MBD crashes
cpsughrue e6f9329
Build fixes after rebase
cpsughrue 5ce60ce
Use llvm support ExponentialBackoff function
cpsughrue 4a5a708
Remove llvm Support changes from PR
cpsughrue f24a7e8
Remove OBE socket function that was just serving as a pass through
cpsughrue 691d4d6
Move where signals begin to be ignored
cpsughrue 860230c
Update error code handling with new and improved ListeningSocket::accept
cpsughrue fb86101
cppcheck fixes
cpsughrue 96ad3fd
Change how we close stdin on linux
cpsughrue c8057b2
Remove blank lines after function declaration and remove instances of
cpsughrue 873a4c5
Remove redundant return
cpsughrue c137f76
Move location of signal handler so that I don't have to wory about sy…
cpsughrue ed0b755
Remove keyword from header file
cpsughrue 1352a40
Add test with multiple TUs in one command line
cpsughrue 565fdc4
Changes to after rebase
cpsughrue 5b42777
Merge branch 'main' into MBD_handshake
cpsughrue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===----------------------------- Frontend.h -----------------------------===// | ||
// | ||
// Part of the LLVM Project, 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLING_MODULEBUILDDAEMON_FRONTEND_H | ||
#define LLVM_CLANG_TOOLING_MODULEBUILDDAEMON_FRONTEND_H | ||
|
||
#include "clang/Frontend/CompilerInvocation.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/raw_socket_stream.h" | ||
|
||
namespace clang::tooling::cc1modbuildd { | ||
|
||
llvm::Error attemptHandshake(llvm::raw_socket_stream &Client, | ||
clang::DiagnosticsEngine &Diag); | ||
|
||
llvm::Error spawnModuleBuildDaemon(const clang::CompilerInvocation &Clang, | ||
const char *Argv0, | ||
clang::DiagnosticsEngine &Diag, | ||
std::string BasePath); | ||
|
||
llvm::Expected<std::unique_ptr<llvm::raw_socket_stream>> | ||
getModuleBuildDaemon(const clang::CompilerInvocation &Clang, const char *Argv0, | ||
clang::DiagnosticsEngine &Diag, llvm::StringRef BasePath); | ||
|
||
void spawnModuleBuildDaemonAndHandshake(const clang::CompilerInvocation &Clang, | ||
const char *Argv0, | ||
clang::DiagnosticsEngine &Diag); | ||
|
||
} // namespace clang::tooling::cc1modbuildd | ||
|
||
#endif // LLVM_CLANG_TOOLING_MODULEBUILDDAEMON_CLIENT_H |
131 changes: 131 additions & 0 deletions
131
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
//===------------------------- SocketMsgSupport.h -------------------------===// | ||
// | ||
// Part of the LLVM Project, 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLING_MODULEBUILDDAEMON_SOCKETMSGSUPPORT_H | ||
#define LLVM_CLANG_TOOLING_MODULEBUILDDAEMON_SOCKETMSGSUPPORT_H | ||
|
||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/Error.h" | ||
#include "llvm/Support/YAMLParser.h" | ||
#include "llvm/Support/YAMLTraits.h" | ||
#include "llvm/Support/raw_socket_stream.h" | ||
|
||
namespace clang::tooling::cc1modbuildd { | ||
|
||
enum class ActionType { HANDSHAKE }; | ||
enum class StatusType { REQUEST, SUCCESS, FAILURE }; | ||
|
||
struct BaseMsg { | ||
ActionType MsgAction; | ||
StatusType MsgStatus; | ||
|
||
BaseMsg() = default; | ||
BaseMsg(ActionType Action, StatusType Status) | ||
: MsgAction(Action), MsgStatus(Status) {} | ||
}; | ||
|
||
struct HandshakeMsg : public BaseMsg { | ||
HandshakeMsg() = default; | ||
HandshakeMsg(ActionType Action, StatusType Status) | ||
: BaseMsg(Action, Status) {} | ||
}; | ||
|
||
llvm::Expected<std::string> | ||
readBufferFromSocket(llvm::raw_socket_stream &Socket); | ||
llvm::Error writeBufferToSocket(llvm::raw_socket_stream &Socket, | ||
llvm::StringRef Buffer); | ||
|
||
template <typename T> std::string convertMsgStructToBuffer(T MsgStruct) { | ||
static_assert(std::is_base_of<cc1modbuildd::BaseMsg, T>::value); | ||
|
||
std::string Buffer; | ||
llvm::raw_string_ostream OS(Buffer); | ||
llvm::yaml::Output YamlOut(OS); | ||
|
||
// TODO confirm yaml::Output does not have any error messages | ||
YamlOut << MsgStruct; | ||
|
||
return Buffer; | ||
} | ||
|
||
template <typename T> | ||
llvm::Expected<T> convertBufferToMsgStruct(llvm::StringRef Buffer) { | ||
static_assert(std::is_base_of<cc1modbuildd::BaseMsg, T>::value); | ||
|
||
T MsgStruct; | ||
llvm::yaml::Input YamlIn(Buffer); | ||
YamlIn >> MsgStruct; | ||
|
||
// YamlIn.error() dumps an error message if there is one | ||
if (YamlIn.error()) | ||
return llvm::make_error<llvm::StringError>( | ||
"Syntax or semantic error during YAML parsing", | ||
llvm::inconvertibleErrorCode()); | ||
|
||
return MsgStruct; | ||
} | ||
|
||
template <typename T> | ||
llvm::Expected<T> readMsgStructFromSocket(llvm::raw_socket_stream &Socket) { | ||
static_assert(std::is_base_of<cc1modbuildd::BaseMsg, T>::value); | ||
|
||
llvm::Expected<std::string> MaybeBuffer = readBufferFromSocket(Socket); | ||
if (!MaybeBuffer) | ||
return std::move(MaybeBuffer.takeError()); | ||
std::string Buffer = std::move(*MaybeBuffer); | ||
|
||
llvm::Expected<T> MaybeMsgStruct = convertBufferToMsgStruct<T>(Buffer); | ||
if (!MaybeMsgStruct) | ||
return std::move(MaybeMsgStruct.takeError()); | ||
return std::move(*MaybeMsgStruct); | ||
} | ||
|
||
template <typename T> | ||
llvm::Error writeMsgStructToSocket(llvm::raw_socket_stream &Socket, | ||
T MsgStruct) { | ||
static_assert(std::is_base_of<cc1modbuildd::BaseMsg, T>::value); | ||
|
||
std::string Buffer = convertMsgStructToBuffer(MsgStruct); | ||
if (llvm::Error Err = writeBufferToSocket(Socket, Buffer)) | ||
return Err; | ||
return llvm::Error::success(); | ||
} | ||
|
||
} // namespace clang::tooling::cc1modbuildd | ||
|
||
namespace llvm { | ||
namespace yaml { | ||
template <> | ||
struct ScalarEnumerationTraits<clang::tooling::cc1modbuildd::StatusType> { | ||
static void enumeration(IO &Io, clang::tooling::cc1modbuildd::StatusType &S) { | ||
Io.enumCase(S, "REQUEST", | ||
clang::tooling::cc1modbuildd::StatusType::REQUEST); | ||
Io.enumCase(S, "SUCCESS", | ||
clang::tooling::cc1modbuildd::StatusType::SUCCESS); | ||
Io.enumCase(S, "FAILURE", | ||
clang::tooling::cc1modbuildd::StatusType::FAILURE); | ||
} | ||
}; | ||
|
||
template <> | ||
struct ScalarEnumerationTraits<clang::tooling::cc1modbuildd::ActionType> { | ||
static void enumeration(IO &Io, clang::tooling::cc1modbuildd::ActionType &A) { | ||
Io.enumCase(A, "HANDSHAKE", | ||
clang::tooling::cc1modbuildd::ActionType::HANDSHAKE); | ||
} | ||
}; | ||
|
||
template <> struct MappingTraits<clang::tooling::cc1modbuildd::HandshakeMsg> { | ||
static void mapping(IO &Io, clang::tooling::cc1modbuildd::HandshakeMsg &H) { | ||
Io.mapRequired("Action", H.MsgAction); | ||
Io.mapRequired("Status", H.MsgStatus); | ||
} | ||
}; | ||
} // namespace yaml | ||
} // namespace llvm | ||
#endif // LLVM_CLANG_TOOLING_MODULEBUILDDAEMON_SOCKETMSGSUPPORT_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//===------------------------------ Utils.h -------------------------------===// | ||
// | ||
// Part of the LLVM Project, 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Functions required by both the frontend and the module build daemon | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_CLANG_TOOLING_MODULEBUILDDAEMON_UTILS_H | ||
#define LLVM_CLANG_TOOLING_MODULEBUILDDAEMON_UTILS_H | ||
|
||
#include "llvm/Support/Error.h" | ||
|
||
#include <chrono> | ||
#include <string> | ||
|
||
#ifdef _WIN32 | ||
#ifndef NOMINMAX | ||
#define NOMINMAX | ||
#endif | ||
// winsock2.h must be included before afunix.h | ||
// clang-format off | ||
#include <winsock2.h> | ||
#include <afunix.h> | ||
// clang-format on | ||
#else | ||
#include <sys/un.h> | ||
#endif | ||
|
||
namespace clang::tooling::cc1modbuildd { | ||
|
||
constexpr std::string_view SocketFileName = "mbd.sock"; | ||
constexpr std::string_view StdoutFileName = "mbd.out"; | ||
constexpr std::string_view StderrFileName = "mbd.err"; | ||
constexpr std::string_view ModuleBuildDaemonFlag = "-cc1modbuildd"; | ||
|
||
// A llvm::raw_socket_stream uses sockaddr_un | ||
constexpr size_t SocketAddrMaxLength = sizeof(sockaddr_un::sun_path); | ||
|
||
constexpr size_t BasePathMaxLength = | ||
SocketAddrMaxLength - SocketFileName.length(); | ||
|
||
// Get a temprary location where the daemon can store log files and a socket | ||
// address. Of the format /tmp/clang-<BLAKE3HashOfClangFullVersion>/ | ||
std::string getBasePath(); | ||
|
||
// Check if the user provided BasePath is short enough | ||
bool validBasePathLength(llvm::StringRef); | ||
|
||
} // namespace clang::tooling::cc1modbuildd | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
set(LLVM_LINK_COMPONENTS | ||
Support | ||
) | ||
|
||
add_clang_library(clangModuleBuildDaemon | ||
Frontend.cpp | ||
SocketSupport.cpp | ||
Utils.cpp | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this deserves a new
DiagnosticBuildDaemonKind.td
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think that's a bad idea. There are currently only 6 options in the
ModuleBuildDaemon
group so for now I'd vote to keep them a part ofDiagnositcFrontendKinds.td
but in subsequent PRs if the number increases, I can pull them out to there own.td
file