[PATCH] D158496: [WIP][clang][modules] module build daemon initial commit

2023-08-30 Thread Connor Sughrue via Phabricator via cfe-commits
cpsughrue added a comment.

In D158496#4618653 , @dblaikie wrote:

> (probably worth linking to the RFC/etc 
> (https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524/47)
>  from the patch description/commit message)

I agree




Comment at: clang/tools/driver/cc1_main.cpp:49
 #include 
+#include 
 

Can be removed


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158496/new/

https://reviews.llvm.org/D158496

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158496: [WIP][clang][modules] module build daemon initial commit

2023-08-30 Thread Connor Sughrue via Phabricator via cfe-commits
cpsughrue updated this revision to Diff 554583.
cpsughrue added a comment.

Remove unnecessary header files


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158496/new/

https://reviews.llvm.org/D158496

Files:
  clang/include/clang/Basic/DiagnosticFrontendKinds.td
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Driver/Options.td
  clang/include/clang/Frontend/FrontendOptions.h
  clang/include/clang/Tooling/ModuleBuildDaemon/Protocol.h
  clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
  clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Tooling/CMakeLists.txt
  clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
  clang/lib/Tooling/ModuleBuildDaemon/Protocol.cpp
  clang/lib/Tooling/ModuleBuildDaemon/SocketMsgSupport.cpp
  clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
  clang/test/Driver/unknown-arg.c
  clang/tools/driver/CMakeLists.txt
  clang/tools/driver/cc1_main.cpp
  clang/tools/driver/cc1modbuildd_main.cpp
  clang/tools/driver/driver.cpp

Index: clang/tools/driver/driver.cpp
===
--- clang/tools/driver/driver.cpp
+++ clang/tools/driver/driver.cpp
@@ -213,6 +213,9 @@
 extern int cc1gen_reproducer_main(ArrayRef Argv,
   const char *Argv0, void *MainAddr,
   const llvm::ToolContext &);
+#if LLVM_ON_UNIX
+extern int cc1modbuildd_main(ArrayRef Argv);
+#endif
 
 static void insertTargetAndModeArgs(const ParsedClangName ,
 SmallVectorImpl ,
@@ -369,9 +372,15 @@
   if (Tool == "-cc1gen-reproducer")
 return cc1gen_reproducer_main(ArrayRef(ArgV).slice(2), ArgV[0],
   GetExecutablePathVP, ToolContext);
-  // Reject unknown tools.
-  llvm::errs() << "error: unknown integrated tool '" << Tool << "'. "
-   << "Valid tools include '-cc1' and '-cc1as'.\n";
+#if LLVM_ON_UNIX
+  if (Tool == "-cc1modbuildd")
+return cc1modbuildd_main(ArrayRef(ArgV).slice(2));
+#endif
+
+  // Reject unknown tools
+  llvm::errs()
+  << "error: unknown integrated tool '" << Tool << "'. "
+  << "Valid tools include '-cc1', '-cc1as', and '-cc1gen-reproducer'.\n";
   return 1;
 }
 
Index: clang/tools/driver/cc1modbuildd_main.cpp
===
--- /dev/null
+++ clang/tools/driver/cc1modbuildd_main.cpp
@@ -0,0 +1,361 @@
+//===--- 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/Basic/DiagnosticCategories.h"
+#include "clang/Basic/DiagnosticFrontend.h"
+#include "clang/Frontend/TextDiagnosticBuffer.h"
+#include "clang/Frontend/TextDiagnosticPrinter.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
+#include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
+#include "clang/Tooling/ModuleBuildDaemon/Protocol.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Config/llvm-config.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 
+
+using namespace llvm;
+using namespace clang;
+using namespace tooling::dependencies;
+
+namespace {
+class ModuleBuildDaemonServer {
+public:
+  SmallString<128> BasePath;
+  SmallString<128> SocketPath;
+  SmallString<128> PidPath;
+
+  ModuleBuildDaemonServer(SmallString<128> Path, ArrayRef Argv)
+  : BasePath(Path), SocketPath(Path),
+Diags(constructDiagnosticsEngine(Argv)) {
+llvm::sys::path::append(SocketPath, SOCKET_FILE_NAME);
+
+if (find(Argv, StringRef("-Rmodule-build-daemon")) != Argv.end())
+  Diags.setSeverityForGroup(diag::Flavor::Remark,
+diag::Group::ModuleBuildDaemon,
+diag::Severity::Remark);
+  }
+
+  ~ModuleBuildDaemonServer() { Shutdown(SIGTERM); }
+
+  int Fork();
+  int Launch();
+  int Listen();
+  static llvm::Error Service(int Client);
+
+  void Shutdown(int signal) {
+unlink(SocketPath.c_str());
+shutdown(ListenSocketFD, SHUT_RD);
+close(ListenSocketFD);
+exit(EXIT_SUCCESS);
+  }
+
+private:
+  // Initializes and returns DiagnosticsEngine
+  static DiagnosticsEngine
+ 

[PATCH] D158496: [WIP][clang][modules] module build daemon initial commit

2023-08-30 Thread Connor Sughrue via Phabricator via cfe-commits
cpsughrue updated this revision to Diff 554582.
cpsughrue edited the summary of this revision.
cpsughrue added a comment.

Addressed feedback:

- Renamed ub_outs() to unbuff_outs()
- Fixed spelling errors
- Added daemon response to handshake request from clang invocation
- Replaced 10 sec sleep with backoff strategy when checking if the daemon has 
finished initializing
- In daemon response to REGISTER, added updated cc1 command line, which 
replaces the clang invocation argv


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158496/new/

https://reviews.llvm.org/D158496

Files:
  clang/include/clang/Basic/DiagnosticFrontendKinds.td
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Driver/Options.td
  clang/include/clang/Frontend/FrontendOptions.h
  clang/include/clang/Tooling/ModuleBuildDaemon/Protocol.h
  clang/include/clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h
  clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Tooling/CMakeLists.txt
  clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
  clang/lib/Tooling/ModuleBuildDaemon/Protocol.cpp
  clang/lib/Tooling/ModuleBuildDaemon/SocketMsgSupport.cpp
  clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
  clang/test/Driver/unknown-arg.c
  clang/tools/driver/CMakeLists.txt
  clang/tools/driver/cc1_main.cpp
  clang/tools/driver/cc1modbuildd_main.cpp
  clang/tools/driver/driver.cpp

Index: clang/tools/driver/driver.cpp
===
--- clang/tools/driver/driver.cpp
+++ clang/tools/driver/driver.cpp
@@ -213,6 +213,9 @@
 extern int cc1gen_reproducer_main(ArrayRef Argv,
   const char *Argv0, void *MainAddr,
   const llvm::ToolContext &);
+#if LLVM_ON_UNIX
+extern int cc1modbuildd_main(ArrayRef Argv);
+#endif
 
 static void insertTargetAndModeArgs(const ParsedClangName ,
 SmallVectorImpl ,
@@ -369,9 +372,15 @@
   if (Tool == "-cc1gen-reproducer")
 return cc1gen_reproducer_main(ArrayRef(ArgV).slice(2), ArgV[0],
   GetExecutablePathVP, ToolContext);
-  // Reject unknown tools.
-  llvm::errs() << "error: unknown integrated tool '" << Tool << "'. "
-   << "Valid tools include '-cc1' and '-cc1as'.\n";
+#if LLVM_ON_UNIX
+  if (Tool == "-cc1modbuildd")
+return cc1modbuildd_main(ArrayRef(ArgV).slice(2));
+#endif
+
+  // Reject unknown tools
+  llvm::errs()
+  << "error: unknown integrated tool '" << Tool << "'. "
+  << "Valid tools include '-cc1', '-cc1as', and '-cc1gen-reproducer'.\n";
   return 1;
 }
 
Index: clang/tools/driver/cc1modbuildd_main.cpp
===
--- /dev/null
+++ clang/tools/driver/cc1modbuildd_main.cpp
@@ -0,0 +1,361 @@
+//===--- 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/Basic/DiagnosticCategories.h"
+#include "clang/Basic/DiagnosticFrontend.h"
+#include "clang/Frontend/TextDiagnosticBuffer.h"
+#include "clang/Frontend/TextDiagnosticPrinter.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
+#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
+#include "clang/Tooling/DependencyScanning/ModuleDepCollector.h"
+#include "clang/Tooling/ModuleBuildDaemon/Protocol.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketMsgSupport.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Config/llvm-config.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 
+
+using namespace llvm;
+using namespace clang;
+using namespace tooling::dependencies;
+
+namespace {
+class ModuleBuildDaemonServer {
+public:
+  SmallString<128> BasePath;
+  SmallString<128> SocketPath;
+  SmallString<128> PidPath;
+
+  ModuleBuildDaemonServer(SmallString<128> Path, ArrayRef Argv)
+  : BasePath(Path), SocketPath(Path),
+Diags(constructDiagnosticsEngine(Argv)) {
+llvm::sys::path::append(SocketPath, SOCKET_FILE_NAME);
+
+if (find(Argv, StringRef("-Rmodule-build-daemon")) != Argv.end())
+  Diags.setSeverityForGroup(diag::Flavor::Remark,
+diag::Group::ModuleBuildDaemon,
+diag::Severity::Remark);
+  }
+
+  

[PATCH] D158496: [WIP][clang][modules] module build daemon initial commit

2023-08-25 Thread David Blaikie via Phabricator via cfe-commits
dblaikie added a comment.

(probably worth linking to the RFC/etc 
(https://discourse.llvm.org/t/rfc-modules-build-daemon-build-system-agnostic-support-for-explicitly-built-modules/71524/47)
 from the patch description/commit message)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D158496/new/

https://reviews.llvm.org/D158496

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D158496: [WIP][clang][modules] module build daemon initial commit

2023-08-22 Thread Connor Sughrue via Phabricator via cfe-commits
cpsughrue created this revision.
cpsughrue added reviewers: jansvoboda11, Bigcheese.
Herald added a project: All.
cpsughrue requested review of this revision.
Herald added subscribers: cfe-commits, MaskRay.
Herald added a project: clang.

Initial commit for module build daemon. The title will be updated soon to 
remove the [WIP] tag once I am done writing tests.

The goal of the commit: A source file that doesn't depend on any modules can 
connect to the daemon, get scanned, find that there are no modules, then resume 
building. If there are any modules in the dependency graph have the daemon emit 
a warning that dependencies were detected but that the daemon cannot yet build 
modules or update the cc1 command line.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158496

Files:
  clang/include/clang/Driver/Options.td
  clang/include/clang/Frontend/FrontendOptions.h
  clang/include/clang/Tooling/ModuleBuildDaemon/Protocol.h
  clang/include/clang/Tooling/ModuleBuildDaemon/SocketSupport.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Tooling/CMakeLists.txt
  clang/lib/Tooling/ModuleBuildDaemon/CMakeLists.txt
  clang/lib/Tooling/ModuleBuildDaemon/Protocol.cpp
  clang/lib/Tooling/ModuleBuildDaemon/SocketSupport.cpp
  clang/tools/driver/CMakeLists.txt
  clang/tools/driver/cc1_main.cpp
  clang/tools/driver/cc1modbuildd_main.cpp
  clang/tools/driver/driver.cpp

Index: clang/tools/driver/driver.cpp
===
--- clang/tools/driver/driver.cpp
+++ clang/tools/driver/driver.cpp
@@ -213,6 +213,9 @@
 extern int cc1gen_reproducer_main(ArrayRef Argv,
   const char *Argv0, void *MainAddr,
   const llvm::ToolContext &);
+#if LLVM_ON_UNIX
+extern int cc1modbuildd_main(ArrayRef Argv);
+#endif
 
 static void insertTargetAndModeArgs(const ParsedClangName ,
 SmallVectorImpl ,
@@ -369,9 +372,20 @@
   if (Tool == "-cc1gen-reproducer")
 return cc1gen_reproducer_main(ArrayRef(ArgV).slice(2), ArgV[0],
   GetExecutablePathVP, ToolContext);
-  // Reject unknown tools.
+#if LLVM_ON_UNIX
+  if (Tool == "-cc1modbuildd")
+return cc1modbuildd_main(ArrayRef(ArgV).slice(2));
+
+  // FIXME: Once cc1modbuildd becomes portable unify llvm::errs messages
   llvm::errs() << "error: unknown integrated tool '" << Tool << "'. "
-   << "Valid tools include '-cc1' and '-cc1as'.\n";
+   << "Valid tools include '-cc1', '-cc1as', '-cc1gen-reproducer', "
+   << "and '-cc1modbuildd'.\n";
+#else
+  // Reject unknown tools
+  llvm::errs()
+  << "error: unknown integrated tool '" << Tool << "'. "
+  << "Valid tools include '-cc1', '-cc1as', and '-cc1gen-reproducer'.\n";
+#endif
   return 1;
 }
 
Index: clang/tools/driver/cc1modbuildd_main.cpp
===
--- /dev/null
+++ clang/tools/driver/cc1modbuildd_main.cpp
@@ -0,0 +1,265 @@
+//===--- 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/Protocol.h"
+#include "clang/Tooling/ModuleBuildDaemon/SocketSupport.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/SmallString.h"
+#include "llvm/Config/llvm-config.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 
+
+using namespace llvm;
+using namespace clang;
+
+namespace {
+class ModuleBuildDaemonServer {
+public:
+  SmallString<128> BasePath;
+  SmallString<128> SocketPath;
+  SmallString<128> PidPath;
+
+  ModuleBuildDaemonServer(SmallString<128> Path)
+  : BasePath(Path), SocketPath(Path) {
+llvm::sys::path::append(SocketPath, SOCKET_FILE_NAME);
+  }
+
+  ~ModuleBuildDaemonServer() { Shutdown(SIGTERM); }
+
+  int Fork();
+  int Launch();
+  int Listen();
+  static llvm::Error Service(int Client);
+
+  void Shutdown(int signal) {
+unlink(SocketPath.c_str());
+shutdown(ListenSocketFD, SHUT_RD);
+close(ListenSocketFD);
+exit(EXIT_SUCCESS);
+  }
+
+private:
+  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->Shutdown(signal);
+  }
+}
+} // namespace
+
+// Forks and detaches process, creating module build daemon
+int