File: BuildSystemExtensionManager.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (121 lines) | stat: -rw-r--r-- 4,202 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//===-- BuildSystemExtensionManager.cpp -----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "llbuild/BuildSystem/BuildSystemExtensions.h"

#include "llbuild/Basic/Subprocess.h"
#include "llbuild/Basic/PlatformUtility.h"

#include "llvm/Support/FileSystem.h"
#include "llvm/Support/raw_ostream.h"

using namespace llbuild;
using namespace llbuild::basic;
using namespace llbuild::buildsystem;

HandlerState::~HandlerState() {}
ShellCommandHandler::~ShellCommandHandler() {}
BuildSystemExtension::~BuildSystemExtension() {}

#pragma mark - BuildSystemExtensionManager implementation

BuildSystemExtension*
BuildSystemExtensionManager::lookupByCommandPath(StringRef path) {
  std::lock_guard<std::mutex> guard(extensionsLock);

  // Check the cache.
  auto it = extensions.find(path);
  if (it != extensions.end()) return it->second.get();

  // Register negative hit, unless we succeed.
  extensions[path] = nullptr;

  // If missing, look for an extension for this path.
  //
  // Currently, extensions are discovered by expecting that a command has an
  // adjacent "...-for-llbuild" binary which can be queried for info.
  SmallString<256> infoPath{ path };
  infoPath += "-for-llbuild";
  if (!llvm::sys::fs::exists(infoPath)) {
    return {};
  }

  // If the path exists, then query it to find the actual extension library.
  struct CapturingProcessDelegate: ProcessDelegate {
    SmallString<1024> output;
    bool success;
    
    virtual void processStarted(ProcessContext* ctx, ProcessHandle handle,
                                llbuild_pid_t pid) {}

    virtual void processHadError(ProcessContext* ctx, ProcessHandle handle,
                                 const Twine& message) {};

    virtual void processHadOutput(ProcessContext* ctx, ProcessHandle handle,
                                  StringRef data) {
      output += data;
    };

    virtual void processFinished(ProcessContext* ctx, ProcessHandle handle,
                                 const ProcessResult& result) {
      success = (result.status == ProcessStatus::Succeeded &&
                 result.exitCode == 0);
    }
  };
  CapturingProcessDelegate delegate;
  {
    // FIXME: Add a utility for capturing a subprocess infocation.
    ProcessAttributes attr{/*canSafelyInterrupt=*/true};
    ProcessGroup pgrp;
    ProcessHandle handle{0};
    std::vector<StringRef> cmd{infoPath, "--llbuild-extension-version", "0",
        "--extension-path" };
    ProcessReleaseFn releaseFn = [](std::function<void()>&& pwait){ pwait(); };
    ProcessCompletionFn completionFn = [](ProcessResult){};
    spawnProcess(delegate, nullptr, pgrp, handle, cmd, POSIXEnvironment(), attr,
                 std::move(releaseFn), std::move(completionFn));
  }

  // The output is expected to be the exact path to the extension (no extra
  // whitespace, etc.).
  auto extensionPath = delegate.output;
  if (!delegate.success || !llvm::sys::fs::exists(infoPath)) {
    return {};
  }

  // Load the plugin.
  auto handle = sys::OpenLibrary(extensionPath.c_str());
  if (handle == nullptr)
    return {};

  BuildSystemExtension *(*registrationFn)(void) =
      reinterpret_cast<decltype(registrationFn)>(sys::GetSymbolByname(handle,
                                                                      "initialize_llbuild_buildsystem_extension_v0"));
  if (!registrationFn) {
    sys::CloseLibrary(handle);
    return {};
  }

  // For now, we expect the registration to simply allocate and return us a (C)
  // extension instance.
  //
  // FIXME: This needs to be reworked to go through a C API.
  auto *extension = registrationFn();
  if (!extension) {
    sys::CloseLibrary(handle);
    return {};
  }

  extensions[path] = std::unique_ptr<BuildSystemExtension>(extension);
  return extension;
}