Skip to content

[SR-2660][Driver] Handle .swiftmodule inputs #12507

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1414,14 +1414,21 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
}
case types::TY_SwiftModuleFile:
case types::TY_SwiftModuleDocFile:
// Module inputs are okay if generating a module.
if (OI.ShouldGenerateModule) {
// When generating a .swiftmodule, treat .swiftmodule files as
// inputs to a MergeModule action.
AllModuleInputs.push_back(Current);
break;
} else if (OI.shouldLink()) {
// Otherwise, if linking, pass .swiftmodule files as inputs to the
// linker, so that their debug info is available.
AllLinkerInputs.push_back(Current);
break;
} else {
Diags.diagnose(SourceLoc(), diag::error_unexpected_input_file,
InputArg->getValue());
continue;
}
Diags.diagnose(SourceLoc(), diag::error_unexpected_input_file,
InputArg->getValue());
continue;
case types::TY_AutolinkFile:
case types::TY_Object:
// Object inputs are only okay if linking.
Expand Down Expand Up @@ -1564,13 +1571,18 @@ void Driver::buildActions(SmallVectorImpl<const Action *> &TopLevelActions,
auto *LinkAction = C.createAction<LinkJobAction>(AllLinkerInputs,
OI.LinkAction);

if (TC.getTriple().getObjectFormat() == llvm::Triple::ELF ||
TC.getTriple().isOSCygMing()) {
// On ELF platforms there's no built in autolinking mechanism, so we
// pull the info we need from the .o files directly and pass them as an
// argument input file to the linker.
// On ELF platforms there's no built in autolinking mechanism, so we
// pull the info we need from the .o files directly and pass them as an
// argument input file to the linker.
SmallVector<const Action *, 2> AutolinkExtractInputs;
for (const Action *A : AllLinkerInputs)
if (A->getType() == types::TY_Object)
AutolinkExtractInputs.push_back(A);
if (!AutolinkExtractInputs.empty() &&
(TC.getTriple().getObjectFormat() == llvm::Triple::ELF ||
TC.getTriple().isOSCygMing())) {
auto *AutolinkExtractAction =
C.createAction<AutolinkExtractJobAction>(AllLinkerInputs);
C.createAction<AutolinkExtractJobAction>(AutolinkExtractInputs);
// Takes the same inputs as the linker...
// ...and gives its output to the linker.
LinkAction->addInput(AutolinkExtractAction);
Expand Down
43 changes: 25 additions & 18 deletions lib/Driver/ToolChains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,41 @@ static const size_t TOO_MANY_FILES = 128;

static void addInputsOfType(ArgStringList &Arguments,
ArrayRef<const Action *> Inputs,
types::ID InputType) {
types::ID InputType,
const char *PrefixArgument = nullptr) {
for (auto &Input : Inputs) {
if (Input->getType() != InputType)
continue;
if (PrefixArgument)
Arguments.push_back(PrefixArgument);
Arguments.push_back(cast<InputAction>(Input)->getInputArg().getValue());
}
}

static void addInputsOfType(ArgStringList &Arguments,
ArrayRef<const Job *> Jobs,
types::ID InputType) {
types::ID InputType,
const char *PrefixArgument = nullptr) {
for (const Job *Cmd : Jobs) {
auto &output = Cmd->getOutput().getAnyOutputForType(InputType);
if (!output.empty())
if (!output.empty()) {
if (PrefixArgument)
Arguments.push_back(PrefixArgument);
Arguments.push_back(output.c_str());
}
}
}

static void addPrimaryInputsOfType(ArgStringList &Arguments,
ArrayRef<const Job *> Jobs,
types::ID InputType) {
types::ID InputType,
const char *PrefixArgument = nullptr) {
for (const Job *Cmd : Jobs) {
auto &outputInfo = Cmd->getOutput();
if (outputInfo.getPrimaryOutputType() == InputType) {
for (const std::string &Output : outputInfo.getPrimaryOutputFilenames()) {
if (PrefixArgument)
Arguments.push_back(PrefixArgument);
Arguments.push_back(Output.c_str());
}
}
Expand Down Expand Up @@ -1228,20 +1238,17 @@ toolchains::Darwin::constructInvocation(const LinkJobAction &job,

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

if (context.OI.DebugInfoKind > IRGenDebugInfoKind::LineTables) {
size_t argCount = Arguments.size();
if (context.OI.CompilerMode == OutputInfo::Mode::SingleCompile)
addInputsOfType(Arguments, context.Inputs, types::TY_SwiftModuleFile);
else
addPrimaryInputsOfType(Arguments, context.Inputs,
types::TY_SwiftModuleFile);

if (Arguments.size() > argCount) {
assert(argCount + 1 == Arguments.size() &&
"multiple swiftmodules found for -g");
Arguments.insert(Arguments.end() - 1, "-add_ast_path");
}
}
if (context.OI.CompilerMode == OutputInfo::Mode::SingleCompile)
addInputsOfType(Arguments, context.Inputs, types::TY_SwiftModuleFile,
"-add_ast_path");
else
addPrimaryInputsOfType(Arguments, context.Inputs,
types::TY_SwiftModuleFile, "-add_ast_path");

// Add all .swiftmodule file inputs as arguments, preceded by the
// "-add_ast_path" linker option.
addInputsOfType(Arguments, context.InputActions, types::TY_SwiftModuleFile,
"-add_ast_path");

switch (job.getKind()) {
case LinkKind::None:
Expand Down
10 changes: 10 additions & 0 deletions test/Driver/actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,16 @@
// DEBUG-LINK-ONLY: 5: link, {0, 1, 4}, image
// DEBUG-LINK-ONLY: 6: generate-dSYM, {5}, dSYM

// RUN: touch %t/c.swift
// 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
// LINK-SWIFTMODULES: 0: input, "{{.*}}/c.swift", swift
// LINK-SWIFTMODULES: 1: compile, {0}, object
// LINK-SWIFTMODULES: 2: input, "{{.*}}/a.o", object
// LINK-SWIFTMODULES: 3: input, "{{.*}}/b.o", object
// LINK-SWIFTMODULES: 4: input, "{{.*}}/a.swiftmodule", swiftmodule
// LINK-SWIFTMODULES: 5: input, "{{.*}}/b.swiftmodule", swiftmodule
// LINK-SWIFTMODULES: 6: link, {1, 2, 3, 4, 5}, image

// RUN: touch %t/a.o %t/b.o
// RUN: %swiftc_driver -driver-print-actions %t/a.o %s -o main 2>&1 | %FileCheck %s -check-prefix=COMPILE-PLUS-OBJECT
// COMPILE-PLUS-OBJECT: 0: input, "{{.*}}/a.o", object
Expand Down
11 changes: 11 additions & 0 deletions test/Driver/linker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@

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

// RUN: touch %t/a.o
// RUN: touch %t/a.swiftmodule
// RUN: touch %t/b.o
// RUN: touch %t/b.swiftmodule
// 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

// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.10 %s > %t.simple-macosx10.10.txt
// RUN: %FileCheck %s < %t.simple-macosx10.10.txt
// RUN: %FileCheck -check-prefix SIMPLE %s < %t.simple-macosx10.10.txt
Expand Down Expand Up @@ -265,6 +271,11 @@
// DEBUG: linker
// DEBUG: -o linker.dSYM

// LINK-SWIFTMODULES: bin/swift
// LINK-SWIFTMODULES-NEXT: bin/ld{{"? }}
// LINK-SWIFTMODULES-SAME: -add_ast_path {{.*}}/a.swiftmodule
// LINK-SWIFTMODULES-SAME: -add_ast_path {{.*}}/b.swiftmodule
// LINK-SWIFTMODULES-SAME: -o linker

// COMPILE_AND_LINK: bin/swift
// COMPILE_AND_LINK-NOT: /a.o
Expand Down
5 changes: 4 additions & 1 deletion test/Driver/unknown-inputs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// COMPILE: 1: compile, {0}, object

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

// LINK-SWIFTMODULES: 0: input, "{{.*}}.swiftmodule", swiftmodule
// LINK-SWIFTMODULES: 1: link, {0}, image

// RUN: not %swiftc_driver -driver-print-actions -emit-module %t/empty 2>&1 | %FileCheck -check-prefix=ERROR %s
// RUN: %swiftc_driver -driver-print-actions -emit-module %t/empty.swiftmodule 2>&1 | %FileCheck -check-prefix=MODULE %s
// RUN: not %swiftc_driver -driver-print-actions -emit-module %t/empty.o 2>&1 | %FileCheck -check-prefix=ERROR %s
Expand Down