Skip to content

Commit d3ebc28

Browse files
authored
Merge pull request #12507 from modocache/sr-2660-driver-swiftmodule-linker-inputs
[SR-2660][Driver] Handle .swiftmodule inputs
2 parents 947250f + b81ad22 commit d3ebc28

File tree

5 files changed

+72
-29
lines changed

5 files changed

+72
-29
lines changed

lib/Driver/Driver.cpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1414,14 +1414,21 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
14141414
}
14151415
case types::TY_SwiftModuleFile:
14161416
case types::TY_SwiftModuleDocFile:
1417-
// Module inputs are okay if generating a module.
14181417
if (OI.ShouldGenerateModule) {
1418+
// When generating a .swiftmodule, treat .swiftmodule files as
1419+
// inputs to a MergeModule action.
14191420
AllModuleInputs.push_back(Current);
14201421
break;
1422+
} else if (OI.shouldLink()) {
1423+
// Otherwise, if linking, pass .swiftmodule files as inputs to the
1424+
// linker, so that their debug info is available.
1425+
AllLinkerInputs.push_back(Current);
1426+
break;
1427+
} else {
1428+
Diags.diagnose(SourceLoc(), diag::error_unexpected_input_file,
1429+
InputArg->getValue());
1430+
continue;
14211431
}
1422-
Diags.diagnose(SourceLoc(), diag::error_unexpected_input_file,
1423-
InputArg->getValue());
1424-
continue;
14251432
case types::TY_AutolinkFile:
14261433
case types::TY_Object:
14271434
// Object inputs are only okay if linking.
@@ -1564,13 +1571,18 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
15641571
auto *LinkAction = C.createAction<LinkJobAction>(AllLinkerInputs,
15651572
OI.LinkAction);
15661573

1567-
if (TC.getTriple().getObjectFormat() == llvm::Triple::ELF ||
1568-
TC.getTriple().isOSCygMing()) {
1569-
// On ELF platforms there's no built in autolinking mechanism, so we
1570-
// pull the info we need from the .o files directly and pass them as an
1571-
// argument input file to the linker.
1574+
// On ELF platforms there's no built in autolinking mechanism, so we
1575+
// pull the info we need from the .o files directly and pass them as an
1576+
// argument input file to the linker.
1577+
SmallVector<const Action *, 2> AutolinkExtractInputs;
1578+
for (const Action *A : AllLinkerInputs)
1579+
if (A->getType() == types::TY_Object)
1580+
AutolinkExtractInputs.push_back(A);
1581+
if (!AutolinkExtractInputs.empty() &&
1582+
(TC.getTriple().getObjectFormat() == llvm::Triple::ELF ||
1583+
TC.getTriple().isOSCygMing())) {
15721584
auto *AutolinkExtractAction =
1573-
C.createAction<AutolinkExtractJobAction>(AllLinkerInputs);
1585+
C.createAction<AutolinkExtractJobAction>(AutolinkExtractInputs);
15741586
// Takes the same inputs as the linker...
15751587
// ...and gives its output to the linker.
15761588
LinkAction->addInput(AutolinkExtractAction);

lib/Driver/ToolChains.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,41 @@ static const size_t TOO_MANY_FILES = 128;
4242

4343
static void addInputsOfType(ArgStringList &Arguments,
4444
ArrayRef<const Action *> Inputs,
45-
types::ID InputType) {
45+
types::ID InputType,
46+
const char *PrefixArgument = nullptr) {
4647
for (auto &Input : Inputs) {
4748
if (Input->getType() != InputType)
4849
continue;
50+
if (PrefixArgument)
51+
Arguments.push_back(PrefixArgument);
4952
Arguments.push_back(cast<InputAction>(Input)->getInputArg().getValue());
5053
}
5154
}
5255

5356
static void addInputsOfType(ArgStringList &Arguments,
5457
ArrayRef<const Job *> Jobs,
55-
types::ID InputType) {
58+
types::ID InputType,
59+
const char *PrefixArgument = nullptr) {
5660
for (const Job *Cmd : Jobs) {
5761
auto &output = Cmd->getOutput().getAnyOutputForType(InputType);
58-
if (!output.empty())
62+
if (!output.empty()) {
63+
if (PrefixArgument)
64+
Arguments.push_back(PrefixArgument);
5965
Arguments.push_back(output.c_str());
66+
}
6067
}
6168
}
6269

6370
static void addPrimaryInputsOfType(ArgStringList &Arguments,
6471
ArrayRef<const Job *> Jobs,
65-
types::ID InputType) {
72+
types::ID InputType,
73+
const char *PrefixArgument = nullptr) {
6674
for (const Job *Cmd : Jobs) {
6775
auto &outputInfo = Cmd->getOutput();
6876
if (outputInfo.getPrimaryOutputType() == InputType) {
6977
for (const std::string &Output : outputInfo.getPrimaryOutputFilenames()) {
78+
if (PrefixArgument)
79+
Arguments.push_back(PrefixArgument);
7080
Arguments.push_back(Output.c_str());
7181
}
7282
}
@@ -1228,20 +1238,17 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,
12281238

12291239
addInputsOfType(Arguments, context.InputActions, types::TY_Object);
12301240

1231-
if (context.OI.DebugInfoKind > IRGenDebugInfoKind::LineTables) {
1232-
size_t argCount = Arguments.size();
1233-
if (context.OI.CompilerMode == OutputInfo::Mode::SingleCompile)
1234-
addInputsOfType(Arguments, context.Inputs, types::TY_SwiftModuleFile);
1235-
else
1236-
addPrimaryInputsOfType(Arguments, context.Inputs,
1237-
types::TY_SwiftModuleFile);
1238-
1239-
if (Arguments.size() > argCount) {
1240-
assert(argCount + 1 == Arguments.size() &&
1241-
"multiple swiftmodules found for -g");
1242-
Arguments.insert(Arguments.end() - 1, "-add_ast_path");
1243-
}
1244-
}
1241+
if (context.OI.CompilerMode == OutputInfo::Mode::SingleCompile)
1242+
addInputsOfType(Arguments, context.Inputs, types::TY_SwiftModuleFile,
1243+
"-add_ast_path");
1244+
else
1245+
addPrimaryInputsOfType(Arguments, context.Inputs,
1246+
types::TY_SwiftModuleFile, "-add_ast_path");
1247+
1248+
// Add all .swiftmodule file inputs as arguments, preceded by the
1249+
// "-add_ast_path" linker option.
1250+
addInputsOfType(Arguments, context.InputActions, types::TY_SwiftModuleFile,
1251+
"-add_ast_path");
12451252

12461253
switch (job.getKind()) {
12471254
case LinkKind::None:

test/Driver/actions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@
118118
// DEBUG-LINK-ONLY: 5: link, {0, 1, 4}, image
119119
// DEBUG-LINK-ONLY: 6: generate-dSYM, {5}, dSYM
120120

121+
// RUN: touch %t/c.swift
122+
// RUN: %swiftc_driver -driver-print-actions %t/c.swift %t/a.o %t/b.o %t/a.swiftmodule %t/b.swiftmodule -o main 2>&1 | %FileCheck %s -check-prefix=LINK-SWIFTMODULES
123+
// LINK-SWIFTMODULES: 0: input, "{{.*}}/c.swift", swift
124+
// LINK-SWIFTMODULES: 1: compile, {0}, object
125+
// LINK-SWIFTMODULES: 2: input, "{{.*}}/a.o", object
126+
// LINK-SWIFTMODULES: 3: input, "{{.*}}/b.o", object
127+
// LINK-SWIFTMODULES: 4: input, "{{.*}}/a.swiftmodule", swiftmodule
128+
// LINK-SWIFTMODULES: 5: input, "{{.*}}/b.swiftmodule", swiftmodule
129+
// LINK-SWIFTMODULES: 6: link, {1, 2, 3, 4, 5}, image
130+
121131
// RUN: touch %t/a.o %t/b.o
122132
// RUN: %swiftc_driver -driver-print-actions %t/a.o %s -o main 2>&1 | %FileCheck %s -check-prefix=COMPILE-PLUS-OBJECT
123133
// COMPILE-PLUS-OBJECT: 0: input, "{{.*}}/a.o", object

test/Driver/linker.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242

4343
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 -g %s | %FileCheck -check-prefix DEBUG %s
4444

45+
// RUN: touch %t/a.o
46+
// RUN: touch %t/a.swiftmodule
47+
// RUN: touch %t/b.o
48+
// RUN: touch %t/b.swiftmodule
49+
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 %s %t/a.o %t/a.swiftmodule %t/b.o %t/b.swiftmodule -o linker | %FileCheck -check-prefix LINK-SWIFTMODULES %s
50+
4551
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.10 %s > %t.simple-macosx10.10.txt
4652
// RUN: %FileCheck %s < %t.simple-macosx10.10.txt
4753
// RUN: %FileCheck -check-prefix SIMPLE %s < %t.simple-macosx10.10.txt
@@ -265,6 +271,11 @@
265271
// DEBUG: linker
266272
// DEBUG: -o linker.dSYM
267273

274+
// LINK-SWIFTMODULES: bin/swift
275+
// LINK-SWIFTMODULES-NEXT: bin/ld{{"? }}
276+
// LINK-SWIFTMODULES-SAME: -add_ast_path {{.*}}/a.swiftmodule
277+
// LINK-SWIFTMODULES-SAME: -add_ast_path {{.*}}/b.swiftmodule
278+
// LINK-SWIFTMODULES-SAME: -o linker
268279

269280
// COMPILE_AND_LINK: bin/swift
270281
// COMPILE_AND_LINK-NOT: /a.o

test/Driver/unknown-inputs.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// COMPILE: 1: compile, {0}, object
1212

1313
// RUN: %swiftc_driver -driver-print-actions %t/empty 2>&1 | %FileCheck -check-prefix=LINK-%target-object-format %s
14-
// RUN: not %swiftc_driver -driver-print-actions %t/empty.swiftmodule 2>&1 | %FileCheck -check-prefix=ERROR %s
14+
// RUN: %swiftc_driver -driver-print-actions %t/empty.swiftmodule 2>&1 | %FileCheck -check-prefix=LINK-SWIFTMODULES %s
1515
// RUN: %swiftc_driver -driver-print-actions %t/empty.o 2>&1 | %FileCheck -check-prefix=LINK-%target-object-format %s
1616
// RUN: not %swiftc_driver -driver-print-actions %t/empty.h 2>&1 | %FileCheck -check-prefix=ERROR %s
1717
// RUN: %swiftc_driver -driver-print-actions %t/empty.swift 2>&1 | %FileCheck -check-prefix=COMPILE %s
@@ -23,6 +23,9 @@
2323
// LINK-elf: 1: swift-autolink-extract, {0}, autolink
2424
// LINK-elf: 2: link, {0, 1}, image
2525

26+
// LINK-SWIFTMODULES: 0: input, "{{.*}}.swiftmodule", swiftmodule
27+
// LINK-SWIFTMODULES: 1: link, {0}, image
28+
2629
// RUN: not %swiftc_driver -driver-print-actions -emit-module %t/empty 2>&1 | %FileCheck -check-prefix=ERROR %s
2730
// RUN: %swiftc_driver -driver-print-actions -emit-module %t/empty.swiftmodule 2>&1 | %FileCheck -check-prefix=MODULE %s
2831
// RUN: not %swiftc_driver -driver-print-actions -emit-module %t/empty.o 2>&1 | %FileCheck -check-prefix=ERROR %s

0 commit comments

Comments
 (0)