Skip to content

Commit 5cd4df5

Browse files
authored
Merge pull request #731 from bnbarham/add-ninja-c-api
Add a C API and corresponding Swift interface for the Ninja component
2 parents 9f581f6 + 92d9fd7 commit 5cd4df5

25 files changed

+940
-789
lines changed

Diff for: Package.swift

+2-17
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,6 @@ let package = Package(
2828
.library(
2929
name: "llbuildAnalysis",
3030
targets: ["llbuildAnalysis"]),
31-
32-
// Swift library for accessing [Ninja](ninjabuild.org) files.
33-
.library(
34-
name: "Ninja",
35-
targets: ["Ninja"])
3631
],
3732
targets: [
3833
// MARK: Products
@@ -64,7 +59,7 @@ let package = Package(
6459
/// The public llbuild C API.
6560
.target(
6661
name: "libllbuild",
67-
dependencies: ["llbuildCore", "llbuildBuildSystem"],
62+
dependencies: ["llbuildCore", "llbuildBuildSystem", "llbuildNinja"],
6863
path: "products/libllbuild"
6964
),
7065

@@ -76,16 +71,6 @@ let package = Package(
7671
exclude: []
7772
),
7873

79-
/// The public Swift Ninja API.
80-
.target(
81-
name: "Ninja",
82-
dependencies: ["llbuild"],
83-
path: "products/swift-Ninja"),
84-
.testTarget(
85-
name: "SwiftNinjaTests",
86-
dependencies: ["llbuildTestSupport", "Ninja"],
87-
path: "unittests/swift-Ninja"),
88-
8974
// MARK: Components
9075

9176
.target(
@@ -173,7 +158,7 @@ let package = Package(
173158
.linkedLibrary("dl", .when(platforms: [.linux])),
174159
.linkedLibrary("pthread", .when(platforms: [.linux]))]),
175160

176-
.testTarget(
161+
.target(
177162
name: "llbuildTestSupport",
178163
path: "unittests/TestSupport"),
179164

Diff for: include/llbuild/Core/MakefileDepsParser.h

+10-19
Original file line numberDiff line numberDiff line change
@@ -32,49 +32,40 @@ class MakefileDepsParser {
3232

3333
/// Called if an error is encountered in parsing the input.
3434
///
35-
/// \param message A C-string text message including information on the
36-
/// error.
35+
/// \param message A message including information on the error.
3736
///
3837
/// \param position The approximate position of the error in the input
3938
/// buffer.
40-
virtual void error(const char* message, uint64_t position) = 0;
39+
virtual void error(StringRef message, uint64_t position) = 0;
4140

4241
/// Called when a new rule is encountered.
4342
///
44-
/// \param name - A pointer to the rule name string start.
45-
///
46-
/// \param length - The raw length of the rule name string, including escape
47-
/// sequences.
43+
/// \param name - The rule name.
4844
///
4945
/// \param unescapedWord - An unescaped version of the name.
50-
virtual void actOnRuleStart(const char* name, uint64_t length,
51-
const StringRef unescapedWord) = 0;
46+
virtual void actOnRuleStart(StringRef name, StringRef unescapedWord) = 0;
5247

5348
/// Called when a new dependency is found for the current rule.
5449
///
5550
/// This is only called between paired calls to \see actOnRuleStart() and
5651
/// \see actOnRuleEnd().
5752
///
58-
/// \param dependency - A pointer to the dependency string start.
59-
///
60-
/// \param length - The raw length of the dependency string, including
61-
/// escape sequences.
53+
/// \param dependency - The dependency string start.
6254
///
6355
/// \param unescapedWord - An unescaped version of the dependency.
64-
virtual void actOnRuleDependency(const char* dependency, uint64_t length,
65-
const StringRef unescapedWord) = 0;
56+
virtual void actOnRuleDependency(StringRef dependency,
57+
StringRef unescapedWord) = 0;
6658

6759
/// Called when a rule is complete.
6860
virtual void actOnRuleEnd() = 0;
6961
};
7062

71-
const char* data;
72-
uint64_t length;
63+
StringRef data;
7364
ParseActions& actions;
7465

7566
public:
76-
MakefileDepsParser(const char* data, uint64_t length, ParseActions& actions)
77-
: data(data), length(length), actions(actions) {}
67+
MakefileDepsParser(StringRef data, ParseActions& actions)
68+
: data(data), actions(actions) {}
7869

7970
void parse();
8071
};

Diff for: include/llbuild/Ninja/ManifestLoader.h

+22-19
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@
1313
#ifndef LLBUILD_NINJA_MANIFESTLOADER_H
1414
#define LLBUILD_NINJA_MANIFESTLOADER_H
1515

16+
#include "llbuild/Basic/LLVM.h"
1617
#include "llbuild/Ninja/Manifest.h"
1718

18-
#include <string>
19-
#include <utility>
19+
#include "llvm/ADT/StringRef.h"
20+
21+
#include <memory>
22+
23+
namespace llvm {
24+
class MemoryBuffer;
25+
}
2026

2127
namespace llbuild {
2228
namespace ninja {
@@ -34,35 +40,32 @@ class ManifestLoaderActions {
3440
/// Called at the beginning of loading, to register the manifest loader.
3541
virtual void initialize(ManifestLoader* loader) = 0;
3642

37-
virtual void error(std::string filename, std::string message,
38-
const Token& at) = 0;
43+
virtual void error(StringRef filename, StringRef message, const Token& at) = 0;
3944

4045
/// Called by the loader to request the contents of a manifest file be loaded.
4146
///
42-
/// \param filename The name of the file to load.
43-
///
44-
/// \param forToken If non-null, the token triggering the file load, for use
45-
/// in diagnostics.
47+
/// \param path Absolute path of the file to load.
4648
///
47-
/// \param data_out On success, the contents of the file.
49+
/// \param forFilename If non-empty, the name of the file triggering the file
50+
/// load (for use in diagnostics).
4851
///
49-
/// \param length_out On success, the length of the data in the file.
52+
/// \param forToken If non-null, the token triggering the file load (for use
53+
/// in diagnostics).
5054
///
51-
/// \returns True on success. On failure, the action is assumed to have
52-
/// produced an appropriate error.
53-
virtual bool readFileContents(const std::string& fromFilename,
54-
const std::string& filename,
55-
const Token* forToken,
56-
std::unique_ptr<char[]> *data_out,
57-
uint64_t *length_out) = 0;
55+
/// \returns The loaded file on success, or a nullptr. On failure, the action
56+
/// is assumed to have produced an appropriate error.
57+
virtual std::unique_ptr<llvm::MemoryBuffer> readFile(
58+
StringRef path, StringRef forFilename, const Token* forToken) = 0;
5859
};
5960

6061
/// Interface for loading Ninja build manifests.
6162
class ManifestLoader {
62-
void *impl;
63+
class ManifestLoaderImpl;
64+
std::unique_ptr<ManifestLoaderImpl> impl;
6365

6466
public:
65-
ManifestLoader(StringRef workingDirectory, StringRef mainFilename, ManifestLoaderActions& actions);
67+
ManifestLoader(StringRef workingDirectory, StringRef mainFilename,
68+
ManifestLoaderActions& actions);
6669
~ManifestLoader();
6770

6871
/// Load the manifest.

Diff for: include/llbuild/Ninja/Parser.h

+9-7
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
#ifndef LLBUILD_NINJA_PARSER_H
1414
#define LLBUILD_NINJA_PARSER_H
1515

16-
#include "llvm/ADT/ArrayRef.h"
17-
1816
#include "llbuild/Basic/LLVM.h"
1917
#include "llbuild/Ninja/Lexer.h"
2018

21-
#include <string>
19+
#include "llvm/ADT/ArrayRef.h"
20+
#include "llvm/ADT/StringRef.h"
21+
22+
#include <memory>
2223

2324
namespace llbuild {
2425
namespace ninja {
@@ -35,13 +36,13 @@ class ParseActions {
3536

3637
virtual ~ParseActions();
3738

38-
virtual void error(std::string message, const Token& at) = 0;
39+
virtual void error(StringRef message, const Token& at) = 0;
3940

4041
/// Called at the beginning of parsing, to register the parser.
4142
virtual void initialize(Parser* parser) = 0;
4243

4344
/// Called at the beginning of parsing a particular manifest.
44-
virtual void actOnBeginManifest(std::string name) = 0;
45+
virtual void actOnBeginManifest(StringRef name) = 0;
4546
/// Called at the end of parsing the current manifest.
4647
virtual void actOnEndManifest() = 0;
4748

@@ -164,10 +165,11 @@ class ParseActions {
164165

165166
/// Interface for parsing a Ninja build manifest.
166167
class Parser {
167-
void *impl;
168+
class ParserImpl;
169+
std::unique_ptr<ParserImpl> impl;
168170

169171
public:
170-
Parser(const char* data, uint64_t length, ParseActions& actions);
172+
Parser(StringRef data, ParseActions &actions);
171173
~Parser();
172174

173175
void parse();

Diff for: lib/BuildSystem/BuildSystem.cpp

+14-18
Original file line numberDiff line numberDiff line change
@@ -2038,30 +2038,28 @@ class ClangShellCommand : public ExternalCommand {
20382038
ClangShellCommand* command)
20392039
: ti(ti), command(command) {}
20402040

2041-
virtual void error(const char* message, uint64_t position) override {
2041+
virtual void error(StringRef message, uint64_t position) override {
20422042
getBuildSystem(ti).getDelegate().commandHadError(command,
20432043
"error reading dependency file '" + command->depsPath +
2044-
"': " + std::string(message));
2044+
"': " + message.str());
20452045
++numErrors;
20462046
}
20472047

2048-
virtual void actOnRuleDependency(const char* dependency,
2049-
uint64_t length,
2050-
const StringRef unescapedWord) override {
2048+
virtual void actOnRuleDependency(StringRef dependency,
2049+
StringRef unescapedWord) override {
20512050
ti.discoveredDependency(BuildKey::makeNode(unescapedWord).toData());
20522051
getBuildSystem(ti).getDelegate().commandFoundDiscoveredDependency(command, unescapedWord,
20532052
DiscoveredDependencyKind::Input);
20542053
}
20552054

2056-
virtual void actOnRuleStart(const char* name, uint64_t length,
2057-
const StringRef unescapedWord) override {}
2055+
virtual void actOnRuleStart(StringRef name,
2056+
StringRef unescapedWord) override {}
20582057

20592058
virtual void actOnRuleEnd() override {}
20602059
};
20612060

20622061
DepsActions actions(ti, this);
2063-
core::MakefileDepsParser(input->getBufferStart(), input->getBufferSize(),
2064-
actions).parse();
2062+
core::MakefileDepsParser(input->getBuffer(), actions).parse();
20652063
return actions.numErrors == 0;
20662064
}
20672065

@@ -2604,16 +2602,15 @@ class SwiftCompilerShellCommand : public ExternalCommand {
26042602
StringRef depsPath, Command* command)
26052603
: ti(ti), depsPath(depsPath) {}
26062604

2607-
virtual void error(const char* message, uint64_t position) override {
2605+
virtual void error(StringRef message, uint64_t position) override {
26082606
getBuildSystem(ti).getDelegate().commandHadError(command,
26092607
"error reading dependency file '" + depsPath.str() +
2610-
"': " + std::string(message));
2608+
"': " + message.str());
26112609
++numErrors;
26122610
}
26132611

2614-
virtual void actOnRuleDependency(const char* dependency,
2615-
uint64_t length,
2616-
const StringRef unescapedWord) override {
2612+
virtual void actOnRuleDependency(StringRef dependency,
2613+
StringRef unescapedWord) override {
26172614
// Only process dependencies for the first rule (the output file), the
26182615
// rest are identical.
26192616
if (ruleNumber == 0) {
@@ -2623,17 +2620,16 @@ class SwiftCompilerShellCommand : public ExternalCommand {
26232620
}
26242621
}
26252622

2626-
virtual void actOnRuleStart(const char* name, uint64_t length,
2627-
const StringRef unescapedWord) override {}
2623+
virtual void actOnRuleStart(StringRef name,
2624+
StringRef unescapedWord) override {}
26282625

26292626
virtual void actOnRuleEnd() override {
26302627
++ruleNumber;
26312628
}
26322629
};
26332630

26342631
DepsActions actions(ti, depsPath, this);
2635-
core::MakefileDepsParser(input->getBufferStart(), input->getBufferSize(),
2636-
actions).parse();
2632+
core::MakefileDepsParser(input->getBuffer(), actions).parse();
26372633
return actions.numErrors == 0;
26382634
}
26392635

Diff for: lib/BuildSystem/ShellCommand.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,17 @@ bool ShellCommand::processMakefileDiscoveredDependencies(BuildSystem& system,
135135
ShellCommand* command, StringRef depsPath)
136136
: system(system), ti(ti), command(command), depsPath(depsPath) {}
137137

138-
virtual void error(const char* message, uint64_t position) override {
138+
virtual void error(StringRef message, uint64_t position) override {
139139
std::string msg;
140140
raw_string_ostream msgStream(msg);
141-
msgStream << "error reading dependency file '" << depsPath.str() << "': "
141+
msgStream << "error reading dependency file '" << depsPath << "': "
142142
<< message << " at position " << position;
143143
system.getDelegate().commandHadError(command, msgStream.str());
144144
++numErrors;
145145
}
146146

147-
virtual void actOnRuleDependency(const char* dependency,
148-
uint64_t length,
149-
const StringRef unescapedWord) override {
147+
virtual void actOnRuleDependency(StringRef dependency,
148+
StringRef unescapedWord) override {
150149
if (llvm::sys::path::is_absolute(unescapedWord)) {
151150
ti.discoveredDependency(BuildKey::makeNode(unescapedWord).toData());
152151
system.getDelegate().commandFoundDiscoveredDependency(command, unescapedWord, DiscoveredDependencyKind::Input);
@@ -166,15 +165,14 @@ bool ShellCommand::processMakefileDiscoveredDependencies(BuildSystem& system,
166165
system.getDelegate().commandFoundDiscoveredDependency(command, absPath, DiscoveredDependencyKind::Input);
167166
}
168167

169-
virtual void actOnRuleStart(const char* name, uint64_t length,
168+
virtual void actOnRuleStart(StringRef name,
170169
const StringRef unescapedWord) override {}
171170

172171
virtual void actOnRuleEnd() override {}
173172
};
174173

175174
DepsActions actions(system, ti, this, depsPath);
176-
core::MakefileDepsParser(input->getBufferStart(), input->getBufferSize(),
177-
actions).parse();
175+
core::MakefileDepsParser(input->getBuffer(), actions).parse();
178176
return actions.numErrors == 0;
179177
}
180178

0 commit comments

Comments
 (0)