Skip to content

Commit ab2fa5a

Browse files
authored
Merge pull request swiftlang#32 from aciidb0mb3r/accept-vector-args-clang-tool
[BuildSystem] Accept array of arguments in clang tool
2 parents 7aadcf9 + dbbfce1 commit ab2fa5a

File tree

2 files changed

+47
-10
lines changed

2 files changed

+47
-10
lines changed

lib/BuildSystem/BuildSystem.cpp

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,15 +1200,18 @@ class ShellTool : public Tool {
12001200

12011201
class ClangShellCommand : public ExternalCommand {
12021202
/// The compiler command to invoke.
1203-
std::string args;
1203+
std::vector<std::string> args;
12041204

12051205
/// The path to the dependency output file, if used.
12061206
std::string depsPath;
12071207

12081208
virtual uint64_t getSignature() override {
1209-
uint64_t result = ExternalCommand::getSignature();
1210-
result ^= basic::hashString(args);
1211-
return result;
1209+
using llvm::hash_combine;
1210+
llvm::hash_code code = ExternalCommand::getSignature();
1211+
for (const auto& arg: args) {
1212+
code = hash_combine(code, arg);
1213+
}
1214+
return size_t(code);
12121215
}
12131216

12141217
bool processDiscoveredDependencies(BuildSystemCommandInterface& bsci,
@@ -1267,13 +1270,24 @@ class ClangShellCommand : public ExternalCommand {
12671270
}
12681271

12691272
virtual void getVerboseDescription(SmallVectorImpl<char> &result) override {
1270-
llvm::raw_svector_ostream(result) << args;
1273+
llvm::raw_svector_ostream os(result);
1274+
bool first = true;
1275+
for (const auto& arg: args) {
1276+
if (!first) os << " ";
1277+
first = false;
1278+
basic::appendShellEscapedString(os, arg);
1279+
}
12711280
}
12721281

12731282
virtual bool configureAttribute(const ConfigureContext& ctx, StringRef name,
12741283
StringRef value) override {
12751284
if (name == "args") {
1276-
args = value;
1285+
// When provided as a scalar string, we default to executing using the
1286+
// shell.
1287+
args.clear();
1288+
args.push_back("/bin/sh");
1289+
args.push_back("-c");
1290+
args.push_back(value);
12771291
} else if (name == "deps") {
12781292
depsPath = value;
12791293
} else {
@@ -1284,7 +1298,13 @@ class ClangShellCommand : public ExternalCommand {
12841298
}
12851299
virtual bool configureAttribute(const ConfigureContext& ctx, StringRef name,
12861300
ArrayRef<StringRef> values) override {
1287-
return ExternalCommand::configureAttribute(ctx, name, values);
1301+
if (name == "args") {
1302+
args = std::vector<std::string>(values.begin(), values.end());
1303+
} else {
1304+
return ExternalCommand::configureAttribute(ctx, name, values);
1305+
}
1306+
1307+
return true;
12881308
}
12891309
virtual bool configureAttribute(
12901310
const ConfigureContext& ctx, StringRef name,
@@ -1295,8 +1315,13 @@ class ClangShellCommand : public ExternalCommand {
12951315
virtual bool executeExternalCommand(BuildSystemCommandInterface& bsci,
12961316
Task* task,
12971317
QueueJobContext* context) override {
1318+
std::vector<StringRef> commandLine;
1319+
for (const auto& arg: args) {
1320+
commandLine.push_back(arg);
1321+
}
1322+
12981323
// Execute the command.
1299-
if (!bsci.getExecutionQueue().executeShellCommand(context, args)) {
1324+
if (!bsci.getExecutionQueue().executeProcess(context, commandLine)) {
13001325
// If the command failed, there is no need to gather dependencies.
13011326
return false;
13021327
}

tests/BuildSystem/Parser/basic.llbuild

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ nodes:
6565
# CHECK: command('link-hello')
6666
# CHECK: -- 'tool': 'link'
6767
# CHECK: -- 'description': 'LINK'
68-
# CHECK: -- 'inputs': ['hello.o']
68+
# CHECK: -- 'inputs': ['hello.o', 'bye.o']
6969
# CHECK: -- 'outputs': ['hello']
7070
# CHECK: -- -- loaded command('link-hello')
7171
# CHECK: command('cc-hello.o')
@@ -76,11 +76,18 @@ nodes:
7676
# CHECK: -- 'extra-map': {
7777
# CHECK: -- 'key': 'value'
7878
# CHECK: -- }
79+
# CHECK: tool('clang')
80+
# CHECK: command('bye.o')
81+
# CHECK: -- 'tool': 'clang')
82+
# CHECK: -- 'inputs': ['bye.c']
83+
# CHECK: -- 'outputs': ['bye.o']
84+
# CHECK: -- 'args': ['-O0', '-g']
85+
# CHECK: -- -- loaded command('bye.o')
7986
commands:
8087
link-hello:
8188
tool: link
8289
description: LINK
83-
inputs: ["hello.o"]
90+
inputs: ["hello.o", "bye.o"]
8491
outputs: ["hello"]
8592
cc-hello.o:
8693
tool: cc
@@ -89,3 +96,8 @@ commands:
8996
args: ["-O0"]
9097
extra-map:
9198
key: value
99+
bye.o:
100+
tool: clang
101+
inputs: ["bye.c"]
102+
outputs: ["bye.o"]
103+
args: ["-O0", "-g"]

0 commit comments

Comments
 (0)