[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-21 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,25 @@
+// Check that a clang invocation can spawn and handshake with a module build 
daemon
+
+// RUN: %kill-process "-cc1modbuildd mbd-handshake"
+// RUN: rm -rf mbd-handshake %t
+// RUN: split-file %s %t
+
+//--- main.c
+int main() {return 0;}
+
+// RUN: %clang -fmodule-build-daemon=mbd-handshake -Rmodule-build-daemon 
%t/main.c &> %t/output-new || true

cpsughrue wrote:

That's understandable. To ensure I understand, do you want to see an example 
where the client dies after receiving the handshake response and fails to 
deliver a payload or do you want to see a payload and response sent back and 
forth after the handshake? I think it would be sufficient to carry out a second 
handshake if it's the latter. The only difference between a second handshake 
and a new payload message is the contents of the buffer, which I don't think is 
super important since we mainly care about the mechanisms behind preventing 
zombie processes.

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-21 Thread Connor Sughrue via cfe-commits


@@ -264,6 +264,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon

cpsughrue wrote:

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 of 
`DiagnositcFrontendKinds.td` but in subsequent PRs if the number increases, I 
can pull them out to there own `.td` file

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-20 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,289 @@
+//===--- cc1modbuildd_main.cpp - Clang CC1 Module Build Daemon 
===//
+//
+// 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
+//
+//===--===//
+
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/Utils.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/ThreadPool.h"
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef _WIN32
+#include 
+#else
+#include 
+#endif
+
+using namespace llvm;
+using namespace clang::tooling::cc1modbuildd;
+
+// Create unbuffered STDOUT stream so that any logging done by the module build
+// daemon can be viewed without having to terminate the process
+static raw_fd_ostream _outs() {
+  static raw_fd_ostream S(fileno(stdout), false, true);
+  return S;
+}
+
+static bool LogVerbose = false;
+static void logVerbose(const llvm::Twine ) {
+  if (LogVerbose) {
+unbuff_outs() << message << '\n';
+  }
+}
+
+static void modifySignals(decltype(SIG_DFL) handler) {
+
+  if (std::signal(SIGTERM, handler) == SIG_ERR) {
+errs() << "failed to handle SIGTERM" << '\n';
+exit(EXIT_FAILURE);
+  }
+
+  if (std::signal(SIGINT, handler) == SIG_ERR) {
+errs() << "failed to handle SIGINT" << '\n';
+exit(EXIT_FAILURE);
+  }
+
+#ifdef SIGHUP
+  if (::signal(SIGHUP, SIG_IGN) == SIG_ERR) {
+errs() << "failed to handle SIGHUP" << '\n';
+exit(EXIT_FAILURE);
+  }
+#endif
+}
+
+namespace {
+
+class ModuleBuildDaemonServer {
+public:
+  SmallString<256> SocketPath;
+  SmallString<256> Stderr; // path to stderr
+  SmallString<256> Stdout; // path to stdout
+
+  explicit ModuleBuildDaemonServer(StringRef Path)
+  : SocketPath(Path), Stderr(Path), Stdout(Path) {
+llvm::sys::path::append(SocketPath, SocketFileName);
+llvm::sys::path::append(Stdout, StdoutFileName);
+llvm::sys::path::append(Stderr, StderrFileName);
+  }
+
+  void setupDaemonEnv();
+  void createDaemonSocket();
+  void listenForClients();
+
+  static void handleConnection(std::shared_ptr Connection);
+
+  // TODO: modify so when shutdownDaemon is called the daemon stops accepting
+  // new client connections and waits for all existing client connections to
+  // terminate before closing the file descriptor and exiting
+  void shutdownDaemon() {
+RunServiceLoop = false;
+if (ServerListener.has_value())
+  ServerListener.value().shutdown();
+  }
+
+private:
+  std::atomic RunServiceLoop = true;
+  std::optional ServerListener;
+};
+
+// Used to handle signals
+ModuleBuildDaemonServer *DaemonPtr = nullptr;
+void handleSignal(int) { DaemonPtr->shutdownDaemon(); }
+} // namespace
+
+// Sets up file descriptors and signals for module build daemon
+void ModuleBuildDaemonServer::setupDaemonEnv() {
+
+#ifdef _WIN32
+  if (std::freopen("NUL", "r", stdin) == NULL) {
+#else
+  if (std::freopen("/dev/null", "r", stdin) == NULL) {
+#endif
+llvm::errs() << "Failed to close stdin" << '\n';
+exit(EXIT_FAILURE);
+  }
+
+  if (std::freopen(Stdout.c_str(), "a", stdout) == NULL) {
+llvm::errs() << "Failed to redirect stdout to " << Stdout << '\n';
+exit(EXIT_FAILURE);
+  }
+  if (std::freopen(Stderr.c_str(), "a", stderr) == NULL) {
+llvm::errs() << "Failed to redirect stderr to " << Stderr << '\n';
+exit(EXIT_FAILURE);
+  }
+
+  modifySignals(handleSignal);
+}
+
+// Creates unix socket for IPC with frontends
+void ModuleBuildDaemonServer::createDaemonSocket() {
+
+  while (true) {
+Expected MaybeServerListener =
+llvm::ListeningSocket::createUnix(SocketPath);
+
+if (llvm::Error Err = MaybeServerListener.takeError()) {
+  llvm::handleAllErrors(std::move(Err), [&](const llvm::StringError ) {
+std::error_code EC = SE.convertToErrorCode();
+
+// Exit successfully if the socket address is already in use. When
+// TUs are compiled in parallel, until the socket file is created, all
+// clang invocations will try to spawn a module build daemon.
+#ifdef _WIN32
+if (EC.value() == WSAEADDRINUSE) {
+#else
+if (EC == std::errc::address_in_use) {
+#endif
+  exit(EXIT_SUCCESS);
+} else if (EC == std::errc::file_exists) {
+  if (std::remove(SocketPath.c_str()) != 0) {
+llvm::errs() << "Failed to remove " << SocketPath << ": "
+ << strerror(errno) << '\n';
+exit(EXIT_FAILURE);
+  }
+  // If a previous module build daemon invocation crashes, the socket
+  // file will need to be removed before the address can be binded to
+  logVerbose("Removing ineligible file: " + SocketPath);
+} else {
+   

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-20 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,289 @@
+//===--- cc1modbuildd_main.cpp - Clang CC1 Module Build Daemon 
===//
+//
+// 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
+//
+//===--===//
+
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/Utils.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/ThreadPool.h"
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef _WIN32
+#include 
+#else
+#include 
+#endif
+
+using namespace llvm;
+using namespace clang::tooling::cc1modbuildd;
+
+// Create unbuffered STDOUT stream so that any logging done by the module build
+// daemon can be viewed without having to terminate the process
+static raw_fd_ostream _outs() {
+  static raw_fd_ostream S(fileno(stdout), false, true);
+  return S;
+}
+
+static bool LogVerbose = false;
+static void logVerbose(const llvm::Twine ) {
+  if (LogVerbose) {
+unbuff_outs() << message << '\n';
+  }
+}
+
+static void modifySignals(decltype(SIG_DFL) handler) {
+
+  if (std::signal(SIGTERM, handler) == SIG_ERR) {
+errs() << "failed to handle SIGTERM" << '\n';
+exit(EXIT_FAILURE);
+  }
+
+  if (std::signal(SIGINT, handler) == SIG_ERR) {
+errs() << "failed to handle SIGINT" << '\n';
+exit(EXIT_FAILURE);
+  }
+
+#ifdef SIGHUP
+  if (::signal(SIGHUP, SIG_IGN) == SIG_ERR) {
+errs() << "failed to handle SIGHUP" << '\n';
+exit(EXIT_FAILURE);
+  }
+#endif
+}
+
+namespace {
+
+class ModuleBuildDaemonServer {
+public:
+  SmallString<256> SocketPath;
+  SmallString<256> Stderr; // path to stderr
+  SmallString<256> Stdout; // path to stdout
+
+  explicit ModuleBuildDaemonServer(StringRef Path)
+  : SocketPath(Path), Stderr(Path), Stdout(Path) {
+llvm::sys::path::append(SocketPath, SocketFileName);
+llvm::sys::path::append(Stdout, StdoutFileName);
+llvm::sys::path::append(Stderr, StderrFileName);
+  }
+
+  void setupDaemonEnv();
+  void createDaemonSocket();
+  void listenForClients();
+
+  static void handleConnection(std::shared_ptr Connection);
+
+  // TODO: modify so when shutdownDaemon is called the daemon stops accepting
+  // new client connections and waits for all existing client connections to
+  // terminate before closing the file descriptor and exiting
+  void shutdownDaemon() {
+RunServiceLoop = false;
+if (ServerListener.has_value())
+  ServerListener.value().shutdown();
+  }
+
+private:
+  std::atomic RunServiceLoop = true;
+  std::optional ServerListener;

cpsughrue wrote:

What do you think about moving the signal handler's installation to after 
`ServerListener` is created? The signal handler's purpose is to shut down 
`ListeningSocket` and set `RunServiceLoop` to `false`, which isn't relevant 
until we begin listening for client connections. The `ListeningSocket` should 
be thread-safe, so I won't have to worry about synchronously calling member 
functions. I would move `modifySignals(handleSignal)` to the top of 
`ModuleBuildDaemonServer::listenForClients`

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-17 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,289 @@
+//===--- cc1modbuildd_main.cpp - Clang CC1 Module Build Daemon 
===//
+//
+// 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
+//
+//===--===//
+
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/Utils.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/ThreadPool.h"
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef _WIN32
+#include 
+#else
+#include 
+#endif
+
+using namespace llvm;

cpsughrue wrote:

See if convenient to remove

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-17 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,157 @@
+//=== Frontend.cpp 
===//
+//
+// 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
+//
+//===--===//
+
+#include "clang/Tooling/ModuleBuildDaemon/Frontend.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/FrontendDiagnostic.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/Utils.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/ScopeExit.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/ExponentialBackoff.h"
+#include "llvm/Support/Program.h"
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+using namespace llvm;
+
+namespace clang::tooling::cc1modbuildd {
+
+llvm::Error attemptHandshake(raw_socket_stream ,
+ DiagnosticsEngine ) {
+
+  // Send HandshakeMsg to module build daemon
+  HandshakeMsg Request{ActionType::HANDSHAKE, StatusType::REQUEST};
+  if (llvm::Error Err = writeMsgStructToSocket(Client, Request))
+return Err;
+
+  // Read response from module build daemon
+  Expected MaybeResponse =
+  readMsgStructFromSocket(Client);
+  if (!MaybeResponse) {
+return MaybeResponse.takeError();
+  }
+  HandshakeMsg Response = std::move(*MaybeResponse);
+
+  assert(Response.MsgAction == ActionType::HANDSHAKE &&
+ "The response ActionType should only ever be HANDSHAKE");
+
+  if (Response.MsgStatus == StatusType::SUCCESS) {
+return llvm::Error::success();
+  }
+
+  return llvm::make_error(
+  "Received handshake response 'FAILURE' from module build daemon",
+  std::make_error_code(std::errc::operation_not_permitted));
+}
+
+llvm::Error spawnModuleBuildDaemon(const CompilerInvocation ,
+   const char *Argv0, DiagnosticsEngine ,
+   std::string BasePath) {
+
+  std::vector Args = {Argv0, ModuleBuildDaemonFlag};
+  if (!Clang.getFrontendOpts().ModuleBuildDaemonPath.empty())
+Args.push_back(BasePath.c_str());
+
+  std::string ErrorBuffer;
+  llvm::sys::ExecuteNoWait(Argv0, Args, std::nullopt, {}, 0, ,
+   nullptr, nullptr, /*DetachProcess*/ true);
+
+  // llvm::sys::ExecuteNoWait can fail for a variety of reasons which can't be
+  // generalized to one error code
+  if (!ErrorBuffer.empty())
+return llvm::make_error(ErrorBuffer, 
inconvertibleErrorCode());
+
+  Diag.Report(diag::remark_mbd_spawn);
+  return llvm::Error::success();
+}
+
+Expected>
+getModuleBuildDaemon(const CompilerInvocation , const char *Argv0,
+ DiagnosticsEngine , StringRef BasePath) {
+
+  SmallString<128> SocketPath = BasePath;
+  llvm::sys::path::append(SocketPath, SocketFileName);
+
+  if (llvm::sys::fs::exists(SocketPath)) {
+Expected> MaybeClient =
+raw_socket_stream::createConnectedUnix(SocketPath);
+if (MaybeClient)
+  return std::move(*MaybeClient);
+consumeError(MaybeClient.takeError());
+  }
+
+  if (llvm::Error Err =
+  spawnModuleBuildDaemon(Clang, Argv0, Diag, BasePath.str()))
+return std::move(Err);
+
+  std::chrono::seconds MaxWaitTime(30);
+  ExponentialBackoff Backoff(MaxWaitTime);
+  do {
+if (llvm::sys::fs::exists(SocketPath)) {
+  Expected> MaybeClient =
+  raw_socket_stream::createConnectedUnix(SocketPath);
+  if (MaybeClient) {
+Diag.Report(diag::remark_mbd_connection) << SocketPath;
+return std::move(*MaybeClient);
+  }
+  consumeError(MaybeClient.takeError());
+}
+  } while (Backoff.waitForNextAttempt());
+
+  // After waiting around 30 seconds give up and return an error
+  return llvm::make_error(
+  "Max wait time exceeded: ",

cpsughrue wrote:

Remove ": "

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-17 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 8f2e1a2d02227b14dc70fb6898b37b9e0565b296 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 01/24] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |   7 +
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  13 +-
 clang/utils/kill_process.py   |  86 ++
 llvm/include/llvm/Support/raw_socket_stream.h |   9 +
 llvm/lib/Support/raw_socket_stream.cpp|  24 ++
 25 files changed, 1012 insertions(+), 7 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 14b08d4927ec5e..50e3aab765c7da 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -264,6 +264,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' instead">,
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 47747d8704b6c8..119b79a0315f7c 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ 

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-17 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 8f2e1a2d02227b14dc70fb6898b37b9e0565b296 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 01/24] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |   7 +
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  13 +-
 clang/utils/kill_process.py   |  86 ++
 llvm/include/llvm/Support/raw_socket_stream.h |   9 +
 llvm/lib/Support/raw_socket_stream.cpp|  24 ++
 25 files changed, 1012 insertions(+), 7 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 14b08d4927ec5e..50e3aab765c7da 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -264,6 +264,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' instead">,
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 47747d8704b6c8..119b79a0315f7c 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ 

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-17 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue edited 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-17 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,278 @@
+//===--- cc1modbuildd_main.cpp - Clang CC1 Module Build Daemon 
===//
+//
+// 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
+//
+//===--===//
+
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/Utils.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Signals.h"
+#include "llvm/Support/ThreadPool.h"
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#ifdef _WIN32
+#include 
+#else
+#include 
+#endif
+
+using namespace llvm;
+using namespace clang::tooling::cc1modbuildd;
+
+// Create unbuffered STDOUT stream so that any logging done by the module build
+// daemon can be viewed without having to terminate the process
+static raw_fd_ostream _outs() {
+  static raw_fd_ostream S(fileno(stdout), false, true);
+  return S;
+}
+
+static bool VerboseLog = false;
+static void verboseLog(const llvm::Twine ) {
+  if (VerboseLog) {
+unbuff_outs() << message << '\n';
+  }
+}
+
+static void modifySignals(decltype(SIG_DFL) handler) {
+
+  if (std::signal(SIGTERM, handler) == SIG_ERR) {
+errs() << "failed to handle SIGTERM" << '\n';
+exit(EXIT_FAILURE);
+  }
+
+  if (std::signal(SIGINT, handler) == SIG_ERR) {
+errs() << "failed to handle SIGINT" << '\n';
+exit(EXIT_FAILURE);
+  }
+
+#ifdef SIGHUP
+  if (::signal(SIGHUP, SIG_IGN) == SIG_ERR) {
+errs() << "failed to handle SIGHUP" << '\n';
+exit(EXIT_FAILURE);
+  }
+#endif
+}
+
+namespace {
+
+class ModuleBuildDaemonServer {
+public:
+  SmallString<256> SocketPath;
+  SmallString<256> Stderr; // path to stderr
+  SmallString<256> Stdout; // path to stdout
+
+  ModuleBuildDaemonServer(StringRef Path)
+  : SocketPath(Path), Stderr(Path), Stdout(Path) {
+llvm::sys::path::append(SocketPath, SocketFileName);
+llvm::sys::path::append(Stdout, StdoutFileName);
+llvm::sys::path::append(Stderr, StderrFileName);
+  }
+
+  void setupDaemonEnv();
+  void createDaemonSocket();
+  void listenForClients();
+
+  static void handleConnection(std::shared_ptr Connection);
+
+  // TODO: modify so when shutdownDaemon is called the daemon stops accepting
+  // new client connections and waits for all existing client connections to
+  // terminate before closing the file descriptor and exiting
+  void shutdownDaemon() {
+RunServiceLoop = false;
+if (ServerListener.has_value())
+  ServerListener.value().shutdown();
+  }
+
+private:
+  std::atomic RunServiceLoop = true;
+  std::optional ServerListener;
+};
+
+// Used to handle signals
+ModuleBuildDaemonServer *DaemonPtr = nullptr;
+void handleSignal(int) { DaemonPtr->shutdownDaemon(); }
+} // namespace
+
+// Sets up file descriptors and signals for module build daemon
+void ModuleBuildDaemonServer::setupDaemonEnv() {
+
+#ifdef _WIN32
+  freopen("NUL", "r", stdin);
+#else
+  close(STDIN_FILENO);
+#endif
+
+  freopen(Stdout.c_str(), "a", stdout);
+  freopen(Stderr.c_str(), "a", stderr);
+
+  modifySignals(handleSignal);
+}
+
+// Creates unix socket for IPC with frontends
+void ModuleBuildDaemonServer::createDaemonSocket() {
+
+  while (true) {
+Expected MaybeServerListener =
+llvm::ListeningSocket::createUnix(SocketPath);
+
+if (llvm::Error Err = MaybeServerListener.takeError()) {
+  llvm::handleAllErrors(std::move(Err), [&](const llvm::StringError ) {
+std::error_code EC = SE.convertToErrorCode();
+
+// Exit successfully if the socket address is already in use. When
+// TUs are compiled in parallel, until the socket file is created, all
+// clang invocations will try to spawn a module build daemon.
+#ifdef _WIN32

cpsughrue wrote:

Confirm I can't use std::errc::address_in_use on windows

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-10 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,66 @@
+//=== SocketMsgSupport.cpp 
===//
+//
+// 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
+//
+//===--===//
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/raw_socket_stream.h"
+
+#include 
+#include 
+
+using namespace llvm;
+
+namespace clang::tooling::cc1modbuildd {
+
+Expected>
+connectToSocket(StringRef SocketPath) {
+
+  Expected> MaybeClient =
+  raw_socket_stream::createConnectedUnix(SocketPath);
+  if (!MaybeClient)
+return MaybeClient.takeError();
+
+  return std::move(*MaybeClient);
+}
+
+Expected readBufferFromSocket(raw_socket_stream ) {
+
+  constexpr unsigned short MAX_BUFFER = 4096;
+  char Buffer[MAX_BUFFER];
+  std::string ReturnBuffer;
+
+  ssize_t n = 0;
+  while ((n = Socket.read(Buffer, MAX_BUFFER)) > 0) {
+ReturnBuffer.append(Buffer, n);
+// Read until \n... encountered which is the last line of a YAML document
+if (ReturnBuffer.find("\n...") != std::string::npos)
+  break;
+  }
+
+  if (Socket.has_error()) {
+std::error_code EC = Socket.error();
+Socket.clear_error();
+return make_error("Failed socket read: ", EC);

cpsughrue wrote:

get rid of ": "

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-10 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,66 @@
+//=== SocketMsgSupport.cpp 
===//
+//
+// 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
+//
+//===--===//
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/raw_socket_stream.h"
+
+#include 
+#include 
+
+using namespace llvm;
+
+namespace clang::tooling::cc1modbuildd {
+
+Expected>
+connectToSocket(StringRef SocketPath) {
+
+  Expected> MaybeClient =
+  raw_socket_stream::createConnectedUnix(SocketPath);
+  if (!MaybeClient)
+return MaybeClient.takeError();
+
+  return std::move(*MaybeClient);
+}
+
+Expected readBufferFromSocket(raw_socket_stream ) {
+
+  constexpr unsigned short MAX_BUFFER = 4096;
+  char Buffer[MAX_BUFFER];
+  std::string ReturnBuffer;
+
+  ssize_t n = 0;
+  while ((n = Socket.read(Buffer, MAX_BUFFER)) > 0) {
+ReturnBuffer.append(Buffer, n);
+// Read until \n... encountered which is the last line of a YAML document
+if (ReturnBuffer.find("\n...") != std::string::npos)
+  break;
+  }
+
+  if (Socket.has_error()) {
+std::error_code EC = Socket.error();
+Socket.clear_error();
+return make_error("Failed socket read: ", EC);
+  }
+  return ReturnBuffer;
+}
+
+Error writeBufferToSocket(raw_socket_stream , StringRef Buffer) {
+  Socket << Buffer;
+
+  if (Socket.has_error()) {
+std::error_code EC = Socket.error();
+Socket.clear_error();
+return make_error("Failed socket write: ", EC);

cpsughrue wrote:

Get rid of ": "

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-10 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 92bdc8227423d798101dffc3baec70d2951bb058 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 01/20] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  17 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  13 +-
 clang/utils/kill_process.py   |  86 ++
 llvm/include/llvm/Support/raw_socket_stream.h |   9 +
 llvm/lib/Support/raw_socket_stream.cpp|  24 ++
 25 files changed, 1017 insertions(+), 12 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 14b08d4927ec5e..50e3aab765c7da 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -264,6 +264,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' instead">,
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 47747d8704b6c8..119b79a0315f7c 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ 

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-10 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 92bdc8227423d798101dffc3baec70d2951bb058 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 01/19] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  17 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  13 +-
 clang/utils/kill_process.py   |  86 ++
 llvm/include/llvm/Support/raw_socket_stream.h |   9 +
 llvm/lib/Support/raw_socket_stream.cpp|  24 ++
 25 files changed, 1017 insertions(+), 12 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 14b08d4927ec5e..50e3aab765c7da 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -264,6 +264,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' instead">,
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 47747d8704b6c8..119b79a0315f7c 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ 

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-04-10 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 92bdc8227423d798101dffc3baec70d2951bb058 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 01/18] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  17 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  13 +-
 clang/utils/kill_process.py   |  86 ++
 llvm/include/llvm/Support/raw_socket_stream.h |   9 +
 llvm/lib/Support/raw_socket_stream.cpp|  24 ++
 25 files changed, 1017 insertions(+), 12 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 14b08d4927ec5e2..50e3aab765c7dae 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -264,6 +264,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' instead">,
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td 
b/clang/include/clang/Basic/DiagnosticGroups.td
index 47747d8704b6c85..119b79a0315f7c3 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ 

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-02-12 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 03d3310ca300630a94517fa300858d1f2645e843 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 01/17] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  17 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  10 +-
 clang/utils/kill_process.py   |  86 ++
 llvm/include/llvm/Support/Program.h   |   3 +-
 llvm/include/llvm/Support/raw_socket_stream.h |   8 +-
 llvm/lib/Support/Program.cpp  |   9 +-
 llvm/lib/Support/Unix/Program.inc |  18 +-
 llvm/lib/Support/Windows/Program.inc  |   7 +-
 llvm/lib/Support/raw_socket_stream.cpp|  46 +++-
 29 files changed, 1055 insertions(+), 29 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 85ecfdf9de62d4..a858bc163de0c3 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -262,6 +262,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' 

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-02-12 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 03d3310ca300630a94517fa300858d1f2645e843 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 01/17] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  17 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  10 +-
 clang/utils/kill_process.py   |  86 ++
 llvm/include/llvm/Support/Program.h   |   3 +-
 llvm/include/llvm/Support/raw_socket_stream.h |   8 +-
 llvm/lib/Support/Program.cpp  |   9 +-
 llvm/lib/Support/Unix/Program.inc |  18 +-
 llvm/lib/Support/Windows/Program.inc  |   7 +-
 llvm/lib/Support/raw_socket_stream.cpp|  46 +++-
 29 files changed, 1055 insertions(+), 29 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 85ecfdf9de62d4..a858bc163de0c3 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -262,6 +262,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' 

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-02-10 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 03d3310ca300630a94517fa300858d1f2645e843 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 01/16] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  17 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  10 +-
 clang/utils/kill_process.py   |  86 ++
 llvm/include/llvm/Support/Program.h   |   3 +-
 llvm/include/llvm/Support/raw_socket_stream.h |   8 +-
 llvm/lib/Support/Program.cpp  |   9 +-
 llvm/lib/Support/Unix/Program.inc |  18 +-
 llvm/lib/Support/Windows/Program.inc  |   7 +-
 llvm/lib/Support/raw_socket_stream.cpp|  46 +++-
 29 files changed, 1055 insertions(+), 29 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 85ecfdf9de62d4..a858bc163de0c3 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -262,6 +262,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' 

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-02-07 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 03d3310ca300630a94517fa300858d1f2645e843 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 1/8] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  17 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  10 +-
 clang/utils/kill_process.py   |  86 ++
 llvm/include/llvm/Support/Program.h   |   3 +-
 llvm/include/llvm/Support/raw_socket_stream.h |   8 +-
 llvm/lib/Support/Program.cpp  |   9 +-
 llvm/lib/Support/Unix/Program.inc |  18 +-
 llvm/lib/Support/Windows/Program.inc  |   7 +-
 llvm/lib/Support/raw_socket_stream.cpp|  46 +++-
 29 files changed, 1055 insertions(+), 29 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 85ecfdf9de62d4..a858bc163de0c3 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -262,6 +262,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' 

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-02-06 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 03d3310ca300630a94517fa300858d1f2645e843 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 1/3] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  17 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  10 +-
 clang/utils/kill_process.py   |  86 ++
 llvm/include/llvm/Support/Program.h   |   3 +-
 llvm/include/llvm/Support/raw_socket_stream.h |   8 +-
 llvm/lib/Support/Program.cpp  |   9 +-
 llvm/lib/Support/Unix/Program.inc |  18 +-
 llvm/lib/Support/Windows/Program.inc  |   7 +-
 llvm/lib/Support/raw_socket_stream.cpp|  46 +++-
 29 files changed, 1055 insertions(+), 29 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 85ecfdf9de62d4..a858bc163de0c3 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -262,6 +262,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' 

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-02-06 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 03d3310ca300630a94517fa300858d1f2645e843 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  17 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  10 +-
 clang/utils/kill_process.py   |  86 ++
 llvm/include/llvm/Support/Program.h   |   3 +-
 llvm/include/llvm/Support/raw_socket_stream.h |   8 +-
 llvm/lib/Support/Program.cpp  |   9 +-
 llvm/lib/Support/Unix/Program.inc |  18 +-
 llvm/lib/Support/Windows/Program.inc  |   7 +-
 llvm/lib/Support/raw_socket_stream.cpp|  46 +++-
 29 files changed, 1055 insertions(+), 29 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 85ecfdf9de62d..a858bc163de0c 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -262,6 +262,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' instead">,

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-02-06 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From b47b42eec3752270fa65c3e6ba0b9c78485d811e Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  17 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  10 +-
 clang/utils/kill_process.py   |  88 ++
 llvm/include/llvm/Support/Program.h   |   3 +-
 llvm/include/llvm/Support/raw_socket_stream.h |   8 +-
 llvm/lib/Support/Program.cpp  |   9 +-
 llvm/lib/Support/Unix/Program.inc |  18 +-
 llvm/lib/Support/Windows/Program.inc  |   7 +-
 llvm/lib/Support/raw_socket_stream.cpp|  46 +++-
 29 files changed, 1057 insertions(+), 29 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 85ecfdf9de62d4..a858bc163de0c3 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -262,6 +262,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' 

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-02-06 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 823e05ca073366f54996a74b2a6d6661ed4e575a Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 .../clang/Basic/DiagnosticFrontendKinds.td|  20 ++
 clang/include/clang/Basic/DiagnosticGroups.td |   1 +
 clang/include/clang/Driver/Options.td |  13 +
 .../include/clang/Frontend/FrontendOptions.h  |   8 +
 .../Tooling/ModuleBuildDaemon/Frontend.h  |  36 +++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h | 126 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  58 
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../Tooling/ModuleBuildDaemon/Frontend.cpp| 166 
 .../ModuleBuildDaemon/SocketSupport.cpp   |  66 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  49 
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/Frontend/warning-options.cpp   |   2 +-
 clang/test/ModuleBuildDaemon/daemon-launch.c  |  18 ++
 clang/test/ModuleBuildDaemon/handshake.c  |  22 ++
 clang/test/lit.cfg.py |  11 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  17 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 255 ++
 clang/tools/driver/driver.cpp |  10 +-
 clang/utils/kill_process.py   |  88 ++
 llvm/include/llvm/Support/Program.h   |   3 +-
 llvm/include/llvm/Support/raw_socket_stream.h |   8 +-
 llvm/lib/Support/Program.cpp  |   9 +-
 llvm/lib/Support/Unix/Program.inc |  18 +-
 llvm/lib/Support/Windows/Program.inc  |   7 +-
 llvm/lib/Support/raw_socket_stream.cpp|  48 +++-
 29 files changed, 1059 insertions(+), 29 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Frontend.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Frontend.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/daemon-launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp
 create mode 100644 clang/utils/kill_process.py

diff --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td 
b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 85ecfdf9de62d4..a858bc163de0c3 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -262,6 +262,26 @@ def err_test_module_file_extension_version : Error<
   "test module file extension '%0' has different version (%1.%2) than expected 
"
   "(%3.%4)">;
 
+// Module Build Daemon
+def err_basepath_length :
+  Error<"BasePath '%0' is longer then the max length of %1">,
+  DefaultFatal;
+def err_mbd_handshake :
+  Error<"Attempt to handshake with module build daemon has failed: %0">,
+  DefaultFatal;
+def err_mbd_connect :
+  Error<"Attempt to connect to module build daemon has failed: %0">,
+  DefaultFatal;
+def remark_mbd_spawn :
+  Remark<"Successfully spawned module build daemon">,
+  InGroup;
+def remark_mbd_connection :
+  Remark<"Successfully connected to module build daemon at %0">,
+  InGroup;
+def remark_mbd_handshake :
+  Remark<"Successfully completed handshake with module build daemon">,
+  InGroup;
+
 def warn_eagerly_load_for_standard_cplusplus_modules : Warning<
   "the form '-fmodule-file=' is deprecated for standard C++ named 
modules;"
   "consider to use '-fmodule-file==' 

[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-02-06 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue converted_to_draft 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2024-01-24 Thread Connor Sughrue via cfe-commits

cpsughrue wrote:

> Can you please send separate PRs for the Support changes?

Yes, I'd be happy to

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-11-01 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,135 @@
+//===- 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 "clang/Tooling/ModuleBuildDaemon/Client.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+
+using namespace clang;
+using namespace llvm;
+
+namespace cc1modbuildd {

cpsughrue wrote:

You're right, thanks for pointing it out!

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-11-01 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,41 @@
+//===-- Client.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_CLIENT_H
+#define LLVM_CLANG_TOOLING_MODULEBUILDDAEMON_CLIENT_H
+
+#include "clang/Frontend/CompilerInstance.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/YAMLParser.h"
+#include "llvm/Support/YAMLTraits.h"
+
+using namespace clang;
+using namespace llvm;

cpsughrue wrote:

Thanks!

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-11-01 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue edited 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-11-01 Thread Connor Sughrue via cfe-commits


@@ -3738,6 +3737,19 @@ static bool RenderModulesOptions(Compilation , const 
Driver ,
Std->containsValue("c++latest") || Std->containsValue("gnu++latest"));
   bool HaveModules = HaveStdCXXModules;
 
+  // -fmodule-build-daemon enables module build daemon functionality
+  if (Args.hasArg(options::OPT_fmodule_build_daemon))
+Args.AddLastArg(CmdArgs, options::OPT_fmodule_build_daemon);
+
+  // by default module build daemon socket address and output files are saved
+  // under /tmp/ but that can be overridden by providing the

cpsughrue wrote:

The `/tmp` is provided by `llvm::sys::path::system_temp_directory` which is 
portable

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-10-25 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 5def87462e3b48cfebafdc2526ac929f5cb9cea3 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 1/6] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
 line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 clang/include/clang/Driver/Options.td |  12 +
 .../include/clang/Frontend/FrontendOptions.h  |   7 +
 .../clang/Tooling/ModuleBuildDaemon/Client.h  |  44 +++
 .../ModuleBuildDaemon/SocketMsgSupport.h  | 134 +
 .../Tooling/ModuleBuildDaemon/SocketSupport.h |  31 ++
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  28 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../lib/Tooling/ModuleBuildDaemon/Client.cpp  | 167 +++
 .../ModuleBuildDaemon/SocketSupport.cpp   | 128 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  32 +++
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/ModuleBuildDaemon/handshake.c  |  18 ++
 clang/test/ModuleBuildDaemon/launch.c |  14 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  28 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 267 ++
 clang/tools/driver/driver.cpp |  17 +-
 19 files changed, 947 insertions(+), 9 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Client.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/test/ModuleBuildDaemon/launch.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index efd90942948af27..d31d28255e81a8a 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2836,6 +2836,18 @@ defm declspec : BoolOption<"f", "declspec",
   NegFlag,
   BothFlags<[], [ClangOption, CC1Option],
   " __declspec as a keyword">>, Group;
+
+def fmodule_build_daemon : Flag<["-"], "fmodule-build-daemon">, Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality">,
+  MarshallingInfoFlag>;
+def fmodule_build_daemon_EQ : Joined<["-"], "fmodule-build-daemon=">, 
Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality and defines location of 
output files">,
+  MarshallingInfoString>;
+
 def fmodules_cache_path : Joined<["-"], "fmodules-cache-path=">, 
Group,
   Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
   MetaVarName<"">,
diff --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index 117e35de6f76c4c..8ce97a57d413c0b 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -350,6 +350,9 @@ class FrontendOptions {
   /// Whether to share the FileManager when building modules.
   unsigned ModulesShareFileManager : 1;
 
+  /// Connect to module build daemon
+  unsigned ModuleBuildDaemon : 1;
+
   CodeCompleteOptions CodeCompleteOpts;
 
   /// Specifies the output format of the AST.
@@ -435,6 +438,10 @@ class FrontendOptions {
   /// The output file, if any.
   std::string OutputFile;
 
+  /// If given, the path to the module build daemon's output files and socket
+  /// address
+  std::string 

[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-10-25 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,134 @@
+//===- 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 "clang/Tooling/ModuleBuildDaemon/Client.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+
+using namespace clang;
+using namespace llvm;
+
+namespace 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) {}
+};
+
+template  std::string getBufferFromSocketMsg(T Msg) {
+  static_assert(std::is_base_of::value,
+"T must inherit from cc1modbuildd::BaseMsg");
+
+  std::string Buffer;
+  llvm::raw_string_ostream OS(Buffer);
+  llvm::yaml::Output YamlOut(OS);
+
+  YamlOut << Msg;
+  return Buffer;
+}
+
+template  Expected getSocketMsgFromBuffer(StringRef Buffer) {
+  static_assert(std::is_base_of::value,
+"T must inherit from cc1modbuildd::BaseMsg");
+
+  T ClientRequest;
+  llvm::yaml::Input YamlIn(Buffer);
+  YamlIn >> ClientRequest;
+
+  if (YamlIn.error()) {
+std::string Msg = "Syntax or semantic error during YAML parsing";

cpsughrue wrote:

`YamlIn.error()` dumps an error message describing the syntax or semantic error 
if there is one. I added a comment explaining that

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-10-25 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,211 @@
+//===- Client.cpp 
-===//
+//
+// 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
+//
+//===--===//
+
+#include "clang/Tooling/ModuleBuildDaemon/Client.h"
+#include "clang/Basic/Version.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Frontend/FrontendDiagnostic.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/Utils.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/ScopeExit.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/BLAKE3.h"
+
+// TODO: Make portable
+#if LLVM_ON_UNIX
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+using namespace clang;
+using namespace llvm;
+using namespace cc1modbuildd;
+
+std::string cc1modbuildd::getBasePath() {
+  llvm::BLAKE3 Hash;
+  Hash.update(getClangFullVersion());
+  auto HashResult = Hash.final();
+  uint64_t HashValue =
+  llvm::support::endian::read(
+  HashResult.data());
+  std::string Key = toString(llvm::APInt(64, HashValue), 36, /*Signed*/ false);
+
+  // Set paths
+  SmallString<128> BasePath;
+  llvm::sys::path::system_temp_directory(/*erasedOnReboot*/ true, BasePath);
+  llvm::sys::path::append(BasePath, "clang-" + Key);
+  return BasePath.c_str();
+}
+
+llvm::Error cc1modbuildd::attemptHandshake(int SocketFD,
+   DiagnosticsEngine ) {
+
+  HandshakeMsg Request{ActionType::HANDSHAKE, StatusType::REQUEST};
+  std::string Buffer = getBufferFromSocketMsg(Request);
+
+  // Send HandshakeMsg to module build daemon
+  Diag.Report(diag::remark_module_build_daemon)
+  << "Trying to send HandshakeMsg to module build daemon";
+  if (llvm::Error Err = writeToSocket(Buffer, SocketFD))
+return std::move(Err);
+  Diag.Report(diag::remark_module_build_daemon)
+  << "Successfully sent HandshakeMsg to module build daemon";
+
+  // Receive response from module build daemon
+  Diag.Report(diag::remark_module_build_daemon)
+  << "Waiting to receive module build daemon response";
+  Expected MaybeServerResponse =
+  readSocketMsgFromSocket(SocketFD);
+  if (!MaybeServerResponse)
+return std::move(MaybeServerResponse.takeError());
+  HandshakeMsg ServerResponse = std::move(*MaybeServerResponse);
+
+  assert(ServerResponse.MsgAction == ActionType::HANDSHAKE &&
+ "Response ActionType should only ever be HANDSHAKE");
+
+  if (ServerResponse.MsgStatus == StatusType::SUCCESS) {
+Diag.Report(diag::remark_module_build_daemon)
+<< "Successfully received HandshakeMsg from module build daemon";
+return llvm::Error::success();
+  }
+
+  return llvm::make_error(
+  "Received failed handshake response from module build daemon",
+  inconvertibleErrorCode());
+}
+
+llvm::Error cc1modbuildd::spawnModuleBuildDaemon(StringRef BasePath,
+ const char *Argv0,
+ DiagnosticsEngine ) {
+  std::string BasePathStr = BasePath.str();
+  const char *Args[] = {Argv0, "-cc1modbuildd", BasePathStr.c_str(), nullptr};
+  pid_t pid;
+  Diag.Report(diag::remark_module_build_daemon)
+  << "Trying to spawn module build daemon";
+  int EC = posix_spawn(, Args[0],
+   /*file_actions*/ nullptr,
+   /*spawnattr*/ nullptr, const_cast(Args),
+   /*envp*/ nullptr);
+  if (EC)
+return createStringError(std::error_code(EC, std::generic_category()),
+ "Failed to spawn module build daemon");
+
+  Diag.Report(diag::remark_module_build_daemon)
+  << "Successfully spawned module build daemon";
+  return llvm::Error::success();
+}
+
+Expected cc1modbuildd::getModuleBuildDaemon(const char *Argv0,
+ StringRef BasePath,
+ DiagnosticsEngine ) {
+
+  SmallString<128> SocketPath = BasePath;
+  llvm::sys::path::append(SocketPath, SOCKET_FILE_NAME);
+
+  if (llvm::sys::fs::exists(SocketPath)) {
+Expected MaybeFD = connectToSocket(SocketPath);
+if (MaybeFD) {
+  Diag.Report(diag::remark_module_build_daemon)
+  << "Module build daemon already exists";
+  return std::move(*MaybeFD);
+}
+consumeError(MaybeFD.takeError());
+  }
+
+  if (llvm::Error Err =
+  cc1modbuildd::spawnModuleBuildDaemon(BasePath, Argv0, Diag))
+return std::move(Err);
+
+  const unsigned int MICROSEC_IN_SEC = 100;
+  constexpr unsigned int MAX_WAIT_TIME = 30 * 

[clang] [WIP][clang][MBD] Initial implementation of module build daemon (PR #68498)

2023-10-07 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue created 
https://github.com/llvm/llvm-project/pull/68498

Work in progress implementation of the module build daemon proposed in 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524.

>From 9fe97509277fbce0333c454bb4e2619fed04b189 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 1/2] [WIP][clang][MBD] module build daemon

---
 clang/include/clang/Driver/Options.td |  12 +
 .../include/clang/Frontend/FrontendOptions.h  |   7 +
 .../clang/Tooling/ModuleBuildDaemon/Client.h  |  56 ++
 .../ModuleBuildDaemon/SocketMsgSupport.h  | 166 ++
 .../Tooling/ModuleBuildDaemon/SocketSupport.h |  31 +
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  28 +
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../lib/Tooling/ModuleBuildDaemon/Client.cpp  | 224 +++
 .../ModuleBuildDaemon/SocketSupport.cpp   | 128 
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  32 +
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/ModuleBuildDaemon/launch.c |  10 +
 clang/test/ModuleBuildDaemon/parallel-scan.c  |  31 +
 clang/test/ModuleBuildDaemon/scan.c   |  20 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  74 ++-
 clang/tools/driver/cc1modbuildd_main.cpp  | 553 ++
 clang/tools/driver/driver.cpp |  17 +-
 20 files changed, 1408 insertions(+), 10 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Client.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/launch.c
 create mode 100644 clang/test/ModuleBuildDaemon/parallel-scan.c
 create mode 100644 clang/test/ModuleBuildDaemon/scan.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index 5415b18d3f406df..33c24e182fccca2 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2883,6 +2883,18 @@ defm declspec : BoolOption<"f", "declspec",
   NegFlag,
   BothFlags<[], [ClangOption, CC1Option],
   " __declspec as a keyword">>, Group;
+
+def fmodule_build_daemon : Flag<["-"], "fmodule-build-daemon">, Group,
+  Flags<[NoXarchOption]>, 
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality">,
+  MarshallingInfoFlag>;
+def fmodule_build_daemon_EQ : Joined<["-"], "fmodule-build-daemon=">, 
Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality and defines location of 
output files">,
+  MarshallingInfoString>;
+
 def fmodules_cache_path : Joined<["-"], "fmodules-cache-path=">, 
Group,
   Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
   MetaVarName<"">,
diff --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index 117e35de6f76c4c..8ce97a57d413c0b 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -350,6 +350,9 @@ class FrontendOptions {
   /// Whether to share the FileManager when building modules.
   unsigned ModulesShareFileManager : 1;
 
+  /// Connect to module build daemon
+  unsigned ModuleBuildDaemon : 1;
+
   CodeCompleteOptions CodeCompleteOpts;
 
   /// Specifies the output format of the AST.
@@ -435,6 +438,10 @@ class FrontendOptions {
   /// The output file, if any.
   std::string OutputFile;
 
+  /// If given, the path to the module build daemon's output files and socket
+  /// address
+  std::string ModuleBuildDaemonPath;
+
   /// If given, the new suffix for fix-it rewritten files.
   std::string FixItSuffix;
 
diff --git a/clang/include/clang/Tooling/ModuleBuildDaemon/Client.h 
b/clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
new file mode 100644
index 000..5d5df74743a6a68
--- /dev/null
+++ b/clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
@@ -0,0 +1,56 @@
+//===- Protocol.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: 

[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-10-07 Thread Connor Sughrue via cfe-commits

cpsughrue wrote:

> I haven't read the PR in details but one thing we found difficult to do is 
> how to write a test for daemon spawning logic, because if anything went 
> wrong, you left an unbounded daemon process on CI node.
> 
> The strategy in this PR (try to kill it after the test finishes) definitely 
> doesn't work because 1. if anything failed, you will not hit kill. 2. you are 
> going to kill the daemon from a different test that runs in parallel.

1. Great point, I did not think of that.
2.  The tests are set up so that each one spawns, interacts with, and kills its 
own daemon, so parallel tests shouldn't kill one another's daemon.

> Other things we tried and know didn't work:
> 
> * explicitly start/stop daemon with command: you might not hit stop due to 
> failure
> * have a timeout and let the daemon terminate after that: you can have some 
> infinite loop that prevents daemon cleanup
> 
> What we ended up is to have a server process that spawns clang job that needs 
> to talk to it. We eliminated the run off daemons but we don't really have 
> coverage for how clang spawns a daemon when needed.

Implementing a timeout as part of this patch as a safety measure seems worth 
while too but I'm not sure I understand your final solution. You all spawned a 
clang job that had to communicate with the daemon to make sure one did not 
exist?

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-30 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 5def87462e3b48cfebafdc2526ac929f5cb9cea3 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 1/5] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
 line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 clang/include/clang/Driver/Options.td |  12 +
 .../include/clang/Frontend/FrontendOptions.h  |   7 +
 .../clang/Tooling/ModuleBuildDaemon/Client.h  |  44 +++
 .../ModuleBuildDaemon/SocketMsgSupport.h  | 134 +
 .../Tooling/ModuleBuildDaemon/SocketSupport.h |  31 ++
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  28 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../lib/Tooling/ModuleBuildDaemon/Client.cpp  | 167 +++
 .../ModuleBuildDaemon/SocketSupport.cpp   | 128 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  32 +++
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/ModuleBuildDaemon/handshake.c  |  18 ++
 clang/test/ModuleBuildDaemon/launch.c |  14 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  28 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 267 ++
 clang/tools/driver/driver.cpp |  17 +-
 19 files changed, 947 insertions(+), 9 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Client.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/test/ModuleBuildDaemon/launch.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index efd90942948af27..d31d28255e81a8a 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2836,6 +2836,18 @@ defm declspec : BoolOption<"f", "declspec",
   NegFlag,
   BothFlags<[], [ClangOption, CC1Option],
   " __declspec as a keyword">>, Group;
+
+def fmodule_build_daemon : Flag<["-"], "fmodule-build-daemon">, Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality">,
+  MarshallingInfoFlag>;
+def fmodule_build_daemon_EQ : Joined<["-"], "fmodule-build-daemon=">, 
Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality and defines location of 
output files">,
+  MarshallingInfoString>;
+
 def fmodules_cache_path : Joined<["-"], "fmodules-cache-path=">, 
Group,
   Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
   MetaVarName<"">,
diff --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index 117e35de6f76c4c..8ce97a57d413c0b 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -350,6 +350,9 @@ class FrontendOptions {
   /// Whether to share the FileManager when building modules.
   unsigned ModulesShareFileManager : 1;
 
+  /// Connect to module build daemon
+  unsigned ModuleBuildDaemon : 1;
+
   CodeCompleteOptions CodeCompleteOpts;
 
   /// Specifies the output format of the AST.
@@ -435,6 +438,10 @@ class FrontendOptions {
   /// The output file, if any.
   std::string OutputFile;
 
+  /// If given, the path to the module build daemon's output files and socket
+  /// address
+  std::string 

[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-30 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 5def87462e3b48cfebafdc2526ac929f5cb9cea3 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 1/4] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
 line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 clang/include/clang/Driver/Options.td |  12 +
 .../include/clang/Frontend/FrontendOptions.h  |   7 +
 .../clang/Tooling/ModuleBuildDaemon/Client.h  |  44 +++
 .../ModuleBuildDaemon/SocketMsgSupport.h  | 134 +
 .../Tooling/ModuleBuildDaemon/SocketSupport.h |  31 ++
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  28 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../lib/Tooling/ModuleBuildDaemon/Client.cpp  | 167 +++
 .../ModuleBuildDaemon/SocketSupport.cpp   | 128 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  32 +++
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/ModuleBuildDaemon/handshake.c  |  18 ++
 clang/test/ModuleBuildDaemon/launch.c |  14 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  28 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 267 ++
 clang/tools/driver/driver.cpp |  17 +-
 19 files changed, 947 insertions(+), 9 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Client.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/test/ModuleBuildDaemon/launch.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index efd90942948af27..d31d28255e81a8a 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2836,6 +2836,18 @@ defm declspec : BoolOption<"f", "declspec",
   NegFlag,
   BothFlags<[], [ClangOption, CC1Option],
   " __declspec as a keyword">>, Group;
+
+def fmodule_build_daemon : Flag<["-"], "fmodule-build-daemon">, Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality">,
+  MarshallingInfoFlag>;
+def fmodule_build_daemon_EQ : Joined<["-"], "fmodule-build-daemon=">, 
Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality and defines location of 
output files">,
+  MarshallingInfoString>;
+
 def fmodules_cache_path : Joined<["-"], "fmodules-cache-path=">, 
Group,
   Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
   MetaVarName<"">,
diff --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index 117e35de6f76c4c..8ce97a57d413c0b 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -350,6 +350,9 @@ class FrontendOptions {
   /// Whether to share the FileManager when building modules.
   unsigned ModulesShareFileManager : 1;
 
+  /// Connect to module build daemon
+  unsigned ModuleBuildDaemon : 1;
+
   CodeCompleteOptions CodeCompleteOpts;
 
   /// Specifies the output format of the AST.
@@ -435,6 +438,10 @@ class FrontendOptions {
   /// The output file, if any.
   std::string OutputFile;
 
+  /// If given, the path to the module build daemon's output files and socket
+  /// address
+  std::string 

[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-30 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue edited 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-30 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 5def87462e3b48cfebafdc2526ac929f5cb9cea3 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 1/3] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
 line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 clang/include/clang/Driver/Options.td |  12 +
 .../include/clang/Frontend/FrontendOptions.h  |   7 +
 .../clang/Tooling/ModuleBuildDaemon/Client.h  |  44 +++
 .../ModuleBuildDaemon/SocketMsgSupport.h  | 134 +
 .../Tooling/ModuleBuildDaemon/SocketSupport.h |  31 ++
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  28 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../lib/Tooling/ModuleBuildDaemon/Client.cpp  | 167 +++
 .../ModuleBuildDaemon/SocketSupport.cpp   | 128 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  32 +++
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/ModuleBuildDaemon/handshake.c  |  18 ++
 clang/test/ModuleBuildDaemon/launch.c |  14 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  28 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 267 ++
 clang/tools/driver/driver.cpp |  17 +-
 19 files changed, 947 insertions(+), 9 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Client.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/test/ModuleBuildDaemon/launch.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index efd90942948af27..d31d28255e81a8a 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2836,6 +2836,18 @@ defm declspec : BoolOption<"f", "declspec",
   NegFlag,
   BothFlags<[], [ClangOption, CC1Option],
   " __declspec as a keyword">>, Group;
+
+def fmodule_build_daemon : Flag<["-"], "fmodule-build-daemon">, Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality">,
+  MarshallingInfoFlag>;
+def fmodule_build_daemon_EQ : Joined<["-"], "fmodule-build-daemon=">, 
Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality and defines location of 
output files">,
+  MarshallingInfoString>;
+
 def fmodules_cache_path : Joined<["-"], "fmodules-cache-path=">, 
Group,
   Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
   MetaVarName<"">,
diff --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index 117e35de6f76c4c..8ce97a57d413c0b 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -350,6 +350,9 @@ class FrontendOptions {
   /// Whether to share the FileManager when building modules.
   unsigned ModulesShareFileManager : 1;
 
+  /// Connect to module build daemon
+  unsigned ModuleBuildDaemon : 1;
+
   CodeCompleteOptions CodeCompleteOpts;
 
   /// Specifies the output format of the AST.
@@ -435,6 +438,10 @@ class FrontendOptions {
   /// The output file, if any.
   std::string OutputFile;
 
+  /// If given, the path to the module build daemon's output files and socket
+  /// address
+  std::string 

[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-29 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,18 @@
+// Check that clang invocation can spawn and handshake with module build daemon
+
+// REQUIRES: !system-windows
+
+//  RUN: if pgrep -f "cc1modbuildd mbd-handshake"; then pkill -f "cc1modbuildd 
mbd-handshake"; fi
+//  RUN: rm -rf mbd-handshake %t

cpsughrue wrote:

I could also set up the client to have a max length of 108 too which would 
guarantee they always match.

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-29 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 9c8e6c71084e1f0e7a96987b7aa2d251b02bea48 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 1/3] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
 line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 clang/include/clang/Driver/Options.td |  12 +
 .../include/clang/Frontend/FrontendOptions.h  |   7 +
 .../clang/Tooling/ModuleBuildDaemon/Client.h  |  44 +++
 .../ModuleBuildDaemon/SocketMsgSupport.h  | 134 +
 .../Tooling/ModuleBuildDaemon/SocketSupport.h |  31 ++
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  28 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../lib/Tooling/ModuleBuildDaemon/Client.cpp  | 167 +++
 .../ModuleBuildDaemon/SocketSupport.cpp   | 128 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  32 +++
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/ModuleBuildDaemon/handshake.c  |  18 ++
 clang/test/ModuleBuildDaemon/launch.c |  14 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  28 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 267 ++
 clang/tools/driver/driver.cpp |  17 +-
 19 files changed, 947 insertions(+), 9 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Client.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/test/ModuleBuildDaemon/launch.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index f573f5a06ceb501..3887dad2ef77f58 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2836,6 +2836,18 @@ defm declspec : BoolOption<"f", "declspec",
   NegFlag,
   BothFlags<[], [ClangOption, CC1Option],
   " __declspec as a keyword">>, Group;
+
+def fmodule_build_daemon : Flag<["-"], "fmodule-build-daemon">, Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality">,
+  MarshallingInfoFlag>;
+def fmodule_build_daemon_EQ : Joined<["-"], "fmodule-build-daemon=">, 
Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality and defines location of 
output files">,
+  MarshallingInfoString>;
+
 def fmodules_cache_path : Joined<["-"], "fmodules-cache-path=">, 
Group,
   Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
   MetaVarName<"">,
diff --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index 117e35de6f76c4c..8ce97a57d413c0b 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -350,6 +350,9 @@ class FrontendOptions {
   /// Whether to share the FileManager when building modules.
   unsigned ModulesShareFileManager : 1;
 
+  /// Connect to module build daemon
+  unsigned ModuleBuildDaemon : 1;
+
   CodeCompleteOptions CodeCompleteOpts;
 
   /// Specifies the output format of the AST.
@@ -435,6 +438,10 @@ class FrontendOptions {
   /// The output file, if any.
   std::string OutputFile;
 
+  /// If given, the path to the module build daemon's output files and socket
+  /// address
+  std::string 

[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-29 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 9c8e6c71084e1f0e7a96987b7aa2d251b02bea48 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 1/3] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
 line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 clang/include/clang/Driver/Options.td |  12 +
 .../include/clang/Frontend/FrontendOptions.h  |   7 +
 .../clang/Tooling/ModuleBuildDaemon/Client.h  |  44 +++
 .../ModuleBuildDaemon/SocketMsgSupport.h  | 134 +
 .../Tooling/ModuleBuildDaemon/SocketSupport.h |  31 ++
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  28 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../lib/Tooling/ModuleBuildDaemon/Client.cpp  | 167 +++
 .../ModuleBuildDaemon/SocketSupport.cpp   | 128 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  32 +++
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/ModuleBuildDaemon/handshake.c  |  18 ++
 clang/test/ModuleBuildDaemon/launch.c |  14 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  28 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 267 ++
 clang/tools/driver/driver.cpp |  17 +-
 19 files changed, 947 insertions(+), 9 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Client.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/test/ModuleBuildDaemon/launch.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index f573f5a06ceb501..3887dad2ef77f58 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2836,6 +2836,18 @@ defm declspec : BoolOption<"f", "declspec",
   NegFlag,
   BothFlags<[], [ClangOption, CC1Option],
   " __declspec as a keyword">>, Group;
+
+def fmodule_build_daemon : Flag<["-"], "fmodule-build-daemon">, Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality">,
+  MarshallingInfoFlag>;
+def fmodule_build_daemon_EQ : Joined<["-"], "fmodule-build-daemon=">, 
Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality and defines location of 
output files">,
+  MarshallingInfoString>;
+
 def fmodules_cache_path : Joined<["-"], "fmodules-cache-path=">, 
Group,
   Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
   MetaVarName<"">,
diff --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index 117e35de6f76c4c..8ce97a57d413c0b 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -350,6 +350,9 @@ class FrontendOptions {
   /// Whether to share the FileManager when building modules.
   unsigned ModulesShareFileManager : 1;
 
+  /// Connect to module build daemon
+  unsigned ModuleBuildDaemon : 1;
+
   CodeCompleteOptions CodeCompleteOpts;
 
   /// Specifies the output format of the AST.
@@ -435,6 +438,10 @@ class FrontendOptions {
   /// The output file, if any.
   std::string OutputFile;
 
+  /// If given, the path to the module build daemon's output files and socket
+  /// address
+  std::string 

[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-29 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,128 @@
+//===- SocketSupport.cpp 
--===//
+//
+// 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
+//
+//===--===//
+
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "clang/Basic/Version.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "clang/Tooling/ModuleBuildDaemon/Client.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/ScopeExit.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Config/llvm-config.h"
+#include "llvm/Support/BLAKE3.h"
+
+// TODO: Make portable
+#if LLVM_ON_UNIX
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+Expected cc1modbuildd::createSocket() {
+  int FD;
+  if ((FD = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+std::string Msg = "socket create error: " + std::string(strerror(errno));
+return createStringError(inconvertibleErrorCode(), Msg);

cpsughrue wrote:

I've been trying to write error message that contain the errno description and 
some developer-provided context. For example, `std::perror("Socket bind error: 
");` might print something like `"Socket bind error: No such file or 
directory"` which follows the format `"developer-provided context: errno 
description"`. I think that format does a good job of providing a helpful error 
message.

Is there a way to return developer-provided context alongside the llvm::Error 
formed from errno? When returning a `llvm::Error` with something like `return 
llvm::errorCodeToError(std::error_code(EIO, std::generic_category()));` I have 
not been able to figure out how to return anything other then the error code.


https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue unresolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue unresolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue unresolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue unresolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue unresolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue unresolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue unresolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue unresolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue unresolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue unresolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue resolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue resolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue resolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue resolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue resolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue resolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue resolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue resolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue resolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue resolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,267 @@
+//===--- cc1modbuildd_main.cpp - Clang CC1 Module Build Daemon 
===//
+//
+// 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
+//
+//===--===//
+
+#include "clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/Utils.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/ThreadPool.h"
+#include "llvm/Support/Threading.h"
+#include "llvm/Support/YAMLParser.h"
+#include "llvm/Support/YAMLTraits.h"
+
+// TODO: Make portable
+#if LLVM_ON_UNIX
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+using namespace llvm;
+using namespace clang;
+using namespace cc1modbuildd;
+
+// Create unbuffered STDOUT stream so that any logging done by module build
+// daemon can be viewed without having to terminate the process
+static raw_fd_ostream _outs() {
+  static raw_fd_ostream S(STDOUT_FILENO, false, true);
+  return S;
+}
+
+namespace {
+
+class ModuleBuildDaemonServer {
+public:
+  SmallString<128> BasePath;
+  SmallString<128> SocketPath;
+  SmallString<128> PidPath;
+
+  ModuleBuildDaemonServer(SmallString<128> Path, ArrayRef Argv)

cpsughrue wrote:

Is there an issue with making `Path` a `StringRef`?

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,267 @@
+//===--- cc1modbuildd_main.cpp - Clang CC1 Module Build Daemon 
===//
+//
+// 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
+//
+//===--===//
+
+#include "clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/Utils.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/ThreadPool.h"
+#include "llvm/Support/Threading.h"
+#include "llvm/Support/YAMLParser.h"
+#include "llvm/Support/YAMLTraits.h"
+
+// TODO: Make portable
+#if LLVM_ON_UNIX
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+using namespace llvm;
+using namespace clang;
+using namespace cc1modbuildd;
+
+// Create unbuffered STDOUT stream so that any logging done by module build
+// daemon can be viewed without having to terminate the process
+static raw_fd_ostream _outs() {
+  static raw_fd_ostream S(STDOUT_FILENO, false, true);
+  return S;
+}
+
+namespace {
+
+class ModuleBuildDaemonServer {
+public:
+  SmallString<128> BasePath;
+  SmallString<128> SocketPath;
+  SmallString<128> PidPath;

cpsughrue wrote:

I use those variables with `llvm::sys::path::append(SmallVectorImpl, Twine)` so 
I think they have to be `SmallString`

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue resolved 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 9c8e6c71084e1f0e7a96987b7aa2d251b02bea48 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH 1/2] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
 line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 clang/include/clang/Driver/Options.td |  12 +
 .../include/clang/Frontend/FrontendOptions.h  |   7 +
 .../clang/Tooling/ModuleBuildDaemon/Client.h  |  44 +++
 .../ModuleBuildDaemon/SocketMsgSupport.h  | 134 +
 .../Tooling/ModuleBuildDaemon/SocketSupport.h |  31 ++
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  28 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../lib/Tooling/ModuleBuildDaemon/Client.cpp  | 167 +++
 .../ModuleBuildDaemon/SocketSupport.cpp   | 128 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  32 +++
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/ModuleBuildDaemon/handshake.c  |  18 ++
 clang/test/ModuleBuildDaemon/launch.c |  14 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  28 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 267 ++
 clang/tools/driver/driver.cpp |  17 +-
 19 files changed, 947 insertions(+), 9 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Client.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/test/ModuleBuildDaemon/launch.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index f573f5a06ceb501..3887dad2ef77f58 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2836,6 +2836,18 @@ defm declspec : BoolOption<"f", "declspec",
   NegFlag,
   BothFlags<[], [ClangOption, CC1Option],
   " __declspec as a keyword">>, Group;
+
+def fmodule_build_daemon : Flag<["-"], "fmodule-build-daemon">, Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality">,
+  MarshallingInfoFlag>;
+def fmodule_build_daemon_EQ : Joined<["-"], "fmodule-build-daemon=">, 
Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality and defines location of 
output files">,
+  MarshallingInfoString>;
+
 def fmodules_cache_path : Joined<["-"], "fmodules-cache-path=">, 
Group,
   Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
   MetaVarName<"">,
diff --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index 117e35de6f76c4c..8ce97a57d413c0b 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -350,6 +350,9 @@ class FrontendOptions {
   /// Whether to share the FileManager when building modules.
   unsigned ModulesShareFileManager : 1;
 
+  /// Connect to module build daemon
+  unsigned ModuleBuildDaemon : 1;
+
   CodeCompleteOptions CodeCompleteOpts;
 
   /// Specifies the output format of the AST.
@@ -435,6 +438,10 @@ class FrontendOptions {
   /// The output file, if any.
   std::string OutputFile;
 
+  /// If given, the path to the module build daemon's output files and socket
+  /// address
+  std::string 

[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-28 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,18 @@
+// Check that clang invocation can spawn and handshake with module build daemon
+
+// REQUIRES: !system-windows
+
+//  RUN: if pgrep -f "cc1modbuildd mbd-handshake"; then pkill -f "cc1modbuildd 
mbd-handshake"; fi
+//  RUN: rm -rf mbd-handshake %t

cpsughrue wrote:

Looks like the header file `un.h`, provided by 
glibc-headers-2.34-60.el9.x86_64, sets the max unix socket address length to 
108 characters.

```cpp
// /usr/include/sys/un.h
/* Structure describing the address of an AF_LOCAL (aka AF_UNIX) socket.  */
struct sockaddr_un
  {
__SOCKADDR_COMMON (sun_);
char sun_path[108]; /* Path name.  */
  };
```

`%t` results in a socket address of 
`/home/cpsughrue/repos/llvm-project-fork/build/tools/clang/test/ModuleBuildDaemon/Output/handshake.c.tmp/mbd.sock`
 which is 112 characters. So, in this case, the clang invocation looks to 
communicate over `mbd.sock` under 
`/home/cpsughrue/repos/llvm-project-fork/build/tools/clang/test/ModuleBuildDaemon/Output/handshake.c.tmp`
 but the daemon chops off `.sock` and creates the address `mbd`. 

I can shorten the file name to `hand.c` or use 
`-fmodule-build-daemon=handshake-mbd` which places the socket file at 
`llvm-project/build/tools/clang/test/ModuleBuildDaemon/handshake-mbd/mbd.sock`. 
Any preference?

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-27 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 9c8e6c71084e1f0e7a96987b7aa2d251b02bea48 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
 line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 clang/include/clang/Driver/Options.td |  12 +
 .../include/clang/Frontend/FrontendOptions.h  |   7 +
 .../clang/Tooling/ModuleBuildDaemon/Client.h  |  44 +++
 .../ModuleBuildDaemon/SocketMsgSupport.h  | 134 +
 .../Tooling/ModuleBuildDaemon/SocketSupport.h |  31 ++
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  28 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../lib/Tooling/ModuleBuildDaemon/Client.cpp  | 167 +++
 .../ModuleBuildDaemon/SocketSupport.cpp   | 128 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  32 +++
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/ModuleBuildDaemon/handshake.c  |  18 ++
 clang/test/ModuleBuildDaemon/launch.c |  14 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  28 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 267 ++
 clang/tools/driver/driver.cpp |  17 +-
 19 files changed, 947 insertions(+), 9 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Client.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/test/ModuleBuildDaemon/launch.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index f573f5a06ceb501..3887dad2ef77f58 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2836,6 +2836,18 @@ defm declspec : BoolOption<"f", "declspec",
   NegFlag,
   BothFlags<[], [ClangOption, CC1Option],
   " __declspec as a keyword">>, Group;
+
+def fmodule_build_daemon : Flag<["-"], "fmodule-build-daemon">, Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality">,
+  MarshallingInfoFlag>;
+def fmodule_build_daemon_EQ : Joined<["-"], "fmodule-build-daemon=">, 
Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality and defines location of 
output files">,
+  MarshallingInfoString>;
+
 def fmodules_cache_path : Joined<["-"], "fmodules-cache-path=">, 
Group,
   Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
   MetaVarName<"">,
diff --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index 117e35de6f76c4c..8ce97a57d413c0b 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -350,6 +350,9 @@ class FrontendOptions {
   /// Whether to share the FileManager when building modules.
   unsigned ModulesShareFileManager : 1;
 
+  /// Connect to module build daemon
+  unsigned ModuleBuildDaemon : 1;
+
   CodeCompleteOptions CodeCompleteOpts;
 
   /// Specifies the output format of the AST.
@@ -435,6 +438,10 @@ class FrontendOptions {
   /// The output file, if any.
   std::string OutputFile;
 
+  /// If given, the path to the module build daemon's output files and socket
+  /// address
+  std::string 

[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-27 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From ecea06aeedac75896e4d243420bc590e07891713 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
 line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 clang/include/clang/Driver/Options.td |  12 +
 .../include/clang/Frontend/FrontendOptions.h  |   7 +
 .../clang/Tooling/ModuleBuildDaemon/Client.h  |  44 +++
 .../ModuleBuildDaemon/SocketMsgSupport.h  | 134 +
 .../Tooling/ModuleBuildDaemon/SocketSupport.h |  31 ++
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  28 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../lib/Tooling/ModuleBuildDaemon/Client.cpp  | 167 +++
 .../ModuleBuildDaemon/SocketSupport.cpp   | 128 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  32 +++
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/ModuleBuildDaemon/handshake.c  |  18 ++
 clang/test/ModuleBuildDaemon/launch.c |  14 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  28 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 267 ++
 clang/tools/driver/driver.cpp |  17 +-
 19 files changed, 947 insertions(+), 9 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Client.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/test/ModuleBuildDaemon/launch.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index f573f5a06ceb501..3887dad2ef77f58 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2836,6 +2836,18 @@ defm declspec : BoolOption<"f", "declspec",
   NegFlag,
   BothFlags<[], [ClangOption, CC1Option],
   " __declspec as a keyword">>, Group;
+
+def fmodule_build_daemon : Flag<["-"], "fmodule-build-daemon">, Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality">,
+  MarshallingInfoFlag>;
+def fmodule_build_daemon_EQ : Joined<["-"], "fmodule-build-daemon=">, 
Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality and defines location of 
output files">,
+  MarshallingInfoString>;
+
 def fmodules_cache_path : Joined<["-"], "fmodules-cache-path=">, 
Group,
   Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
   MetaVarName<"">,
diff --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index 117e35de6f76c4c..8ce97a57d413c0b 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -350,6 +350,9 @@ class FrontendOptions {
   /// Whether to share the FileManager when building modules.
   unsigned ModulesShareFileManager : 1;
 
+  /// Connect to module build daemon
+  unsigned ModuleBuildDaemon : 1;
+
   CodeCompleteOptions CodeCompleteOpts;
 
   /// Specifies the output format of the AST.
@@ -435,6 +438,10 @@ class FrontendOptions {
   /// The output file, if any.
   std::string OutputFile;
 
+  /// If given, the path to the module build daemon's output files and socket
+  /// address
+  std::string 

[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-27 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue deleted 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-27 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue deleted 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-27 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue updated 
https://github.com/llvm/llvm-project/pull/67562

>From 4153aeb4e06c6bec6e27dd2c814708a3a8b8c842 Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
 line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 clang/include/clang/Driver/Options.td |  12 +
 .../include/clang/Frontend/FrontendOptions.h  |   7 +
 .../clang/Tooling/ModuleBuildDaemon/Client.h  |  44 +++
 .../ModuleBuildDaemon/SocketMsgSupport.h  | 134 +
 .../Tooling/ModuleBuildDaemon/SocketSupport.h |  31 ++
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  28 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../lib/Tooling/ModuleBuildDaemon/Client.cpp  | 167 +++
 .../ModuleBuildDaemon/SocketSupport.cpp   | 128 +
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  32 +++
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/ModuleBuildDaemon/handshake.c  |  18 ++
 clang/test/ModuleBuildDaemon/launch.c |  14 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  28 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 268 ++
 clang/tools/driver/driver.cpp |  17 +-
 19 files changed, 948 insertions(+), 9 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Client.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/test/ModuleBuildDaemon/launch.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index f573f5a06ceb501..3887dad2ef77f58 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2836,6 +2836,18 @@ defm declspec : BoolOption<"f", "declspec",
   NegFlag,
   BothFlags<[], [ClangOption, CC1Option],
   " __declspec as a keyword">>, Group;
+
+def fmodule_build_daemon : Flag<["-"], "fmodule-build-daemon">, Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality">,
+  MarshallingInfoFlag>;
+def fmodule_build_daemon_EQ : Joined<["-"], "fmodule-build-daemon=">, 
Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality and defines location of 
output files">,
+  MarshallingInfoString>;
+
 def fmodules_cache_path : Joined<["-"], "fmodules-cache-path=">, 
Group,
   Flags<[NoXarchOption]>, Visibility<[ClangOption, CC1Option]>,
   MetaVarName<"">,
diff --git a/clang/include/clang/Frontend/FrontendOptions.h 
b/clang/include/clang/Frontend/FrontendOptions.h
index 117e35de6f76c4c..8ce97a57d413c0b 100644
--- a/clang/include/clang/Frontend/FrontendOptions.h
+++ b/clang/include/clang/Frontend/FrontendOptions.h
@@ -350,6 +350,9 @@ class FrontendOptions {
   /// Whether to share the FileManager when building modules.
   unsigned ModulesShareFileManager : 1;
 
+  /// Connect to module build daemon
+  unsigned ModuleBuildDaemon : 1;
+
   CodeCompleteOptions CodeCompleteOpts;
 
   /// Specifies the output format of the AST.
@@ -435,6 +438,10 @@ class FrontendOptions {
   /// The output file, if any.
   std::string OutputFile;
 
+  /// If given, the path to the module build daemon's output files and socket
+  /// address
+  std::string 

[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-27 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue edited 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-27 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue edited 
https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-27 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,273 @@
+//===--- cc1modbuildd_main.cpp - Clang CC1 Module Build Daemon 
===//
+//
+// 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
+//
+//===--===//
+
+#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
+#include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/Utils.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/ThreadPool.h"
+#include "llvm/Support/Threading.h"
+#include "llvm/Support/YAMLParser.h"
+#include "llvm/Support/YAMLTraits.h"
+
+// TODO: Make portable
+#if LLVM_ON_UNIX
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+using namespace llvm;
+using namespace clang;
+using namespace tooling::dependencies;
+using namespace cc1modbuildd;
+
+// Create unbuffered STDOUT stream so that any logging done by module build
+// daemon can be viewed without having to terminate the process
+static raw_fd_ostream _outs() {
+  static raw_fd_ostream S(STDOUT_FILENO, false, true);
+  return S;
+}
+
+namespace {
+
+class ModuleBuildDaemonServer {
+public:
+  SmallString<128> BasePath;
+  SmallString<128> SocketPath;
+  SmallString<128> PidPath;
+
+  ModuleBuildDaemonServer(SmallString<128> Path, ArrayRef Argv)
+  : BasePath(Path), SocketPath(Path) {
+llvm::sys::path::append(SocketPath, SOCKET_FILE_NAME);
+  }
+
+  ~ModuleBuildDaemonServer() { shutdownDaemon(SIGTERM); }
+
+  int forkDaemon();
+  int launchDaemon();
+  int listenForClients();
+
+  static void handleClient(int Client);
+
+  void shutdownDaemon(int signal) {
+unlink(SocketPath.c_str());
+shutdown(ListenSocketFD, SHUT_RD);
+close(ListenSocketFD);
+exit(EXIT_SUCCESS);
+  }
+
+private:
+  // Initializes and returns DiagnosticsEngine
+  pid_t Pid = -1;
+  int ListenSocketFD = -1;
+};
+
+// Required to handle SIGTERM by calling Shutdown
+ModuleBuildDaemonServer *DaemonPtr = nullptr;
+void handleSignal(int Signal) {
+  if (DaemonPtr != nullptr) {
+DaemonPtr->shutdownDaemon(Signal);
+  }
+}
+} // namespace
+
+static bool verbose = false;
+static void verbose_print(const llvm::Twine ) {
+  if (verbose) {
+unbuff_outs() << message << '\n';
+  }
+}
+
+// Forks and detaches process, creating module build daemon
+int ModuleBuildDaemonServer::forkDaemon() {
+
+  pid_t pid = fork();
+
+  if (pid < 0) {
+exit(EXIT_FAILURE);
+  }
+  if (pid > 0) {
+exit(EXIT_SUCCESS);
+  }
+
+  Pid = getpid();
+
+  close(STDIN_FILENO);
+  close(STDOUT_FILENO);
+  close(STDERR_FILENO);
+
+  SmallString<128> STDOUT = BasePath;
+  llvm::sys::path::append(STDOUT, STDOUT_FILE_NAME);
+  freopen(STDOUT.c_str(), "a", stdout);
+
+  SmallString<128> STDERR = BasePath;
+  llvm::sys::path::append(STDERR, STDERR_FILE_NAME);
+  freopen(STDERR.c_str(), "a", stderr);
+
+  if (signal(SIGTERM, handleSignal) == SIG_ERR) {
+errs() << "failed to handle SIGTERM" << '\n';
+exit(EXIT_FAILURE);
+  }
+  if (signal(SIGHUP, SIG_IGN) == SIG_ERR) {
+errs() << "failed to ignore SIGHUP" << '\n';
+exit(EXIT_FAILURE);
+  }
+  if (setsid() == -1) {
+errs() << "setsid failed" << '\n';
+exit(EXIT_FAILURE);
+  }
+
+  return EXIT_SUCCESS;
+}
+
+// Creates unix socket for IPC with module build daemon
+int ModuleBuildDaemonServer::launchDaemon() {
+
+  // new socket
+  if ((ListenSocketFD = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
+std::perror("Socket create error: ");
+exit(EXIT_FAILURE);
+  }
+
+  struct sockaddr_un addr;
+  memset(, 0, sizeof(struct sockaddr_un));
+  addr.sun_family = AF_UNIX;
+  strncpy(addr.sun_path, SocketPath.c_str(), sizeof(addr.sun_path) - 1);
+
+  // bind to local address
+  if (bind(ListenSocketFD, (struct sockaddr *), sizeof(addr)) == -1) {
+
+// If the socket address is already in use, exit because another module
+// build daemon has successfully launched. When translation units are
+// compiled in parallel, until the socket file is created, all clang
+// invocations will spawn a module build daemon.
+if (errno == EADDRINUSE) {
+  close(ListenSocketFD);
+  exit(EXIT_SUCCESS);
+}
+std::perror("Socket bind error: ");
+exit(EXIT_FAILURE);
+  }
+  verbose_print("mbd created and binded to socket address at: " + SocketPath);
+
+  // set socket to accept incoming connection request
+  unsigned MaxBacklog = llvm::hardware_concurrency().compute_thread_count();
+  if 

[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-27 Thread Connor Sughrue via cfe-commits


@@ -0,0 +1,273 @@
+//===--- cc1modbuildd_main.cpp - Clang CC1 Module Build Daemon 
===//
+//
+// 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
+//
+//===--===//
+
+#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
+#include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"

cpsughrue wrote:

Dependency scanning headers are not needed for the initial module build daemon 
commit

https://github.com/llvm/llvm-project/pull/67562
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [clang][MBD] set up module build daemon infrastructure (PR #67562)

2023-09-27 Thread Connor Sughrue via cfe-commits

https://github.com/cpsughrue created 
https://github.com/llvm/llvm-project/pull/67562

The module build daemon (mbd) will be a cc1 tool that helps convert implicit 
module command lines to explicit module command lines. The basic workflow is as 
follows: A clang invocation will check whether a mbd exists. If not, the 
invocation will spawn one. After the mbd is up and running and a handshake has 
successfully been carried out between the mbd and clang invocation, the clang 
invocation will share its command line with the mbd. The mbd will then scan the 
translation unit and build all modular dependencies before returning a modified 
cc1 command line such that all arguments related to modules are converted from 
the implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to 
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524

>From 0a1c22f0697783f7a3216bb5cbb6cf44e9e3b33d Mon Sep 17 00:00:00 2001
From: cpsughrue 
Date: Sun, 9 Jul 2023 23:19:58 -0400
Subject: [PATCH] [clang][MBD] set up module build daemon infrastructure

The module build daemon (mbd) will serve as a cc1 tool that helps convert
implicit module command lines to explicit module command lines. A clang
invocation will check to see if a mbd exists, if not the invocation will spawn
one. After the mbd is up and running and a handshake has successfully been
carried out between the mbd and clang invocation the clang invocation will
share it's command line with the mbd. The mbd will then scan the translation
unit and build all modular dependencies before returning a modified cc1 command
 line such that all arguments related to modules are converted from the
implicit option to their explicit option.

This commit sets up the basic mbd infrastructure. Including the ability to
spawn a mbd and carry out a handshake between the clang invocation and mbd.

RFC: 
https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524
---
 clang/include/clang/Driver/Options.td |  12 +
 .../include/clang/Frontend/FrontendOptions.h  |   7 +
 .../clang/Tooling/ModuleBuildDaemon/Client.h  |  44 +++
 .../ModuleBuildDaemon/SocketMsgSupport.h  | 134 +
 .../Tooling/ModuleBuildDaemon/SocketSupport.h |  31 ++
 .../clang/Tooling/ModuleBuildDaemon/Utils.h   |  28 ++
 clang/lib/Driver/ToolChains/Clang.cpp |  14 +-
 clang/lib/Tooling/CMakeLists.txt  |   1 +
 .../Tooling/ModuleBuildDaemon/CMakeLists.txt  |   9 +
 .../lib/Tooling/ModuleBuildDaemon/Client.cpp  | 167 +++
 .../ModuleBuildDaemon/SocketSupport.cpp   | 128 
 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp |  32 ++
 clang/test/Driver/unknown-arg.c   |   2 +-
 clang/test/ModuleBuildDaemon/handshake.c  |  18 ++
 clang/test/ModuleBuildDaemon/launch.c |  14 +
 clang/tools/driver/CMakeLists.txt |   3 +
 clang/tools/driver/cc1_main.cpp   |  28 +-
 clang/tools/driver/cc1modbuildd_main.cpp  | 273 ++
 clang/tools/driver/driver.cpp |  17 +-
 19 files changed, 953 insertions(+), 9 deletions(-)
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Client.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
 create mode 100644 
clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
 create mode 100644 clang/include/clang/Tooling/ModuleBuildDaemon/Utils.h
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Client.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
 create mode 100644 clang/lib/Tooling/ModuleBuildDaemon/Utils.cpp
 create mode 100644 clang/test/ModuleBuildDaemon/handshake.c
 create mode 100644 clang/test/ModuleBuildDaemon/launch.c
 create mode 100644 clang/tools/driver/cc1modbuildd_main.cpp

diff --git a/clang/include/clang/Driver/Options.td 
b/clang/include/clang/Driver/Options.td
index f573f5a06ceb501..06575f4b73bd47a 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -2836,6 +2836,18 @@ defm declspec : BoolOption<"f", "declspec",
   NegFlag,
   BothFlags<[], [ClangOption, CC1Option],
   " __declspec as a keyword">>, Group;
+
+def fmodule_build_daemon : Flag<["-"], "fmodule-build-daemon">, Group,
+  Flags<[NoXarchOption]>, 
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality">,
+  MarshallingInfoFlag>;
+def fmodule_build_daemon_EQ : Joined<["-"], "fmodule-build-daemon=">, 
Group,
+  Flags<[NoXarchOption]>,
+  Visibility<[ClangOption, CC1Option]>,
+  HelpText<"Enables module build daemon functionality and defines location of 
output files">,
+  MarshallingInfoString>;
+