Skip to content

Commit 845e8be

Browse files
committed
[Driver] Add -experimental-emit-interface for textual interfaces
This will eventually become plain old -emit-interface, but we don't want people to be using it just yet. This is just for testing.
1 parent 4d9b85b commit 845e8be

File tree

7 files changed

+104
-21
lines changed

7 files changed

+104
-21
lines changed

include/swift/Driver/Driver.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ class Driver {
341341
StringRef workingDirectory,
342342
CommandOutput *Output) const;
343343

344+
void chooseTextualInterfacePath(Compilation &C, const JobAction *JA,
345+
StringRef workingDirectory,
346+
llvm::SmallString<128> &buffer,
347+
CommandOutput *output) const;
348+
344349
void chooseRemappingOutputPath(Compilation &C, const TypeToPathMap *OutputMap,
345350
CommandOutput *Output) const;
346351

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,10 @@ def emit_module_path_EQ : Joined<["-"], "emit-module-path=">,
296296
ArgumentIsPath]>,
297297
Alias<emit_module_path>;
298298

299+
def experimental_emit_interface : Flag<["-"], "experimental-emit-interface">,
300+
Flags<[NoInteractiveOption, HelpHidden]>,
301+
HelpText<"Test out the textual interfaces feature">;
302+
299303
def emit_objc_header : Flag<["-"], "emit-objc-header">,
300304
Flags<[FrontendOption, NoInteractiveOption, DoesNotAffectIncrementalBuild]>,
301305
HelpText<"Emit an Objective-C header file">;

lib/Driver/Driver.cpp

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,14 +1481,21 @@ void Driver::buildOutputInfo(const ToolChain &TC, const DerivedArgList &Args,
14811481
// top-level output.
14821482
OI.ShouldGenerateModule = true;
14831483
OI.ShouldTreatModuleAsTopLevelOutput = true;
1484-
} else if ((OI.DebugInfoLevel > IRGenDebugInfoLevel::LineTables &&
1485-
OI.shouldLink()) ||
1486-
Args.hasArg(options::OPT_emit_objc_header,
1487-
options::OPT_emit_objc_header_path)) {
1484+
} else if (OI.DebugInfoLevel > IRGenDebugInfoLevel::LineTables &&
1485+
OI.shouldLink()) {
14881486
// An option has been passed which requires a module, but the user hasn't
14891487
// requested one. Generate a module, but treat it as an intermediate output.
14901488
OI.ShouldGenerateModule = true;
14911489
OI.ShouldTreatModuleAsTopLevelOutput = false;
1490+
} else if (Args.hasArg(options::OPT_emit_objc_header,
1491+
options::OPT_emit_objc_header_path,
1492+
options::OPT_experimental_emit_interface) &&
1493+
OI.CompilerMode != OutputInfo::Mode::SingleCompile) {
1494+
// An option has been passed which requires whole-module knowledge, but we
1495+
// don't have that. Generate a module, but treat it as an intermediate
1496+
// output.
1497+
OI.ShouldGenerateModule = true;
1498+
OI.ShouldTreatModuleAsTopLevelOutput = false;
14921499
} else {
14931500
// No options require a module, so don't generate one.
14941501
OI.ShouldGenerateModule = false;
@@ -2461,6 +2468,9 @@ Job *Driver::buildJobsForAction(Compilation &C, const JobAction *JA,
24612468
chooseSwiftModuleDocOutputPath(C, OutputMap, workingDirectory,
24622469
Output.get());
24632470

2471+
if (C.getArgs().hasArg(options::OPT_experimental_emit_interface))
2472+
chooseTextualInterfacePath(C, JA, workingDirectory, Buf, Output.get());
2473+
24642474
if (C.getArgs().hasArg(options::OPT_update_code) && isa<CompileJobAction>(JA))
24652475
chooseRemappingOutputPath(C, OutputMap, Output.get());
24662476

@@ -2757,6 +2767,33 @@ void Driver::chooseRemappingOutputPath(Compilation &C,
27572767
}
27582768
}
27592769

2770+
void Driver::chooseTextualInterfacePath(Compilation &C, const JobAction *JA,
2771+
StringRef workingDirectory,
2772+
llvm::SmallString<128> &buffer,
2773+
CommandOutput *output) const {
2774+
switch (C.getOutputInfo().CompilerMode) {
2775+
case OutputInfo::Mode::StandardCompile:
2776+
case OutputInfo::Mode::BatchModeCompile:
2777+
if (!isa<MergeModuleJobAction>(JA))
2778+
return;
2779+
break;
2780+
case OutputInfo::Mode::SingleCompile:
2781+
if (!isa<CompileJobAction>(JA))
2782+
return;
2783+
break;
2784+
case OutputInfo::Mode::Immediate:
2785+
case OutputInfo::Mode::REPL:
2786+
llvm_unreachable("these modes aren't usable with 'swiftc'");
2787+
}
2788+
2789+
StringRef outputPath = *getOutputFilenameFromPathArgOrAsTopLevel(
2790+
C.getOutputInfo(), C.getArgs(), llvm::opt::OptSpecifier(),
2791+
file_types::TY_SwiftModuleInterfaceFile,
2792+
/*TreatAsTopLevelOutput*/true, workingDirectory, buffer);
2793+
output->setAdditionalOutputForType(file_types::TY_SwiftModuleInterfaceFile,
2794+
outputPath);
2795+
}
2796+
27602797
void Driver::chooseSerializedDiagnosticsPath(Compilation &C,
27612798
const JobAction *JA,
27622799
const TypeToPathMap *OutputMap,

lib/Driver/ToolChains.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,10 @@ void ToolChain::JobContext::addFrontendSupplementaryOutputArguments(
543543
addOutputsOfType(arguments, Output, Args, file_types::TY_SwiftModuleDocFile,
544544
"-emit-module-doc-path");
545545

546+
addOutputsOfType(arguments, Output, Args,
547+
file_types::ID::TY_SwiftModuleInterfaceFile,
548+
"-emit-interface-path");
549+
546550
addOutputsOfType(arguments, Output, Args,
547551
file_types::TY_SerializedDiagnostics,
548552
"-serialize-diagnostics-path");
@@ -792,6 +796,9 @@ ToolChain::constructInvocation(const MergeModuleJobAction &job,
792796
Arguments);
793797
addOutputsOfType(Arguments, context.Output, context.Args,
794798
file_types::TY_SwiftModuleDocFile, "-emit-module-doc-path");
799+
addOutputsOfType(Arguments, context.Output, context.Args,
800+
file_types::ID::TY_SwiftModuleInterfaceFile,
801+
"-emit-interface-path");
795802
addOutputsOfType(Arguments, context.Output, context.Args,
796803
file_types::TY_SerializedDiagnostics,
797804
"-serialize-diagnostics-path");

test/Driver/emit-interface.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 %s -experimental-emit-interface -o %t/foo 2>&1 | %FileCheck %s
2+
3+
// CHECK: swift -frontend
4+
// CHECK-SAME: emit-interface.swift
5+
// CHECK: swift -frontend -merge-modules
6+
// CHECK-SAME: -emit-interface-path {{.+}}/foo.swiftinterface
7+
// CHECK: bin/ld
8+
9+
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 %s -experimental-emit-interface -o %t/foo -force-single-frontend-invocation 2>&1 | %FileCheck -check-prefix=CHECK-WHOLE-MODULE %s
10+
11+
// CHECK-WHOLE-MODULE: swift -frontend
12+
// CHECK-WHOLE-MODULE-SAME: emit-interface.swift
13+
// CHECK-WHOLE-MODULE-SAME: -emit-interface-path {{.+}}/foo.swiftinterface
14+
// CHECK-WHOLE-MODULE-NOT: -merge-modules
15+
// CHECK-WHOLE-MODULE: bin/ld
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// FIXME: BEGIN -enable-source-import hackaround
2+
// RUN: %empty-directory(%t)
3+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t %clang-importer-sdk-path/swift-modules/Foundation.swift
4+
// FIXME: END -enable-source-import hackaround
5+
6+
// RUN: %target-swiftc_driver %clang-importer-sdk-nosource -emit-module -o %t.1.swiftmodule -emit-objc-header -emit-objc-header-path %t.1.h -module-name ThisModule %s %S/Inputs/main.swift %S/Inputs/lib.swift -I %t
7+
// RUN: %target-swiftc_driver %clang-importer-sdk-nosource -emit-module -o %t.2.swiftmodule -emit-objc-header -emit-objc-header-path %t.2.h -module-name ThisModule %s %S/Inputs/main.swift %S/Inputs/lib.swift -force-single-frontend-invocation -I %t
8+
// RUN: diff %t.1.h %t.2.h
9+
10+
// REQUIRES: objc_interop
11+
12+
import Foundation
13+
14+
public class A: NSObject {
15+
func foo() {}
16+
@objc func bar(x: Int, baz y: Int) -> Int { return 1 }
17+
}
18+
public class B: A {
19+
func doSomething() {}
20+
}

test/Driver/emit-objc-header.swift

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
// FIXME: BEGIN -enable-source-import hackaround
2-
// RUN: %empty-directory(%t)
3-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -emit-module -o %t %clang-importer-sdk-path/swift-modules/Foundation.swift
4-
// FIXME: END -enable-source-import hackaround
1+
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 %s -emit-objc-header-path %t/foo.h 2>&1 | %FileCheck %s
52

6-
// RUN: %target-swiftc_driver %clang-importer-sdk-nosource -emit-module -o %t.1.swiftmodule -emit-objc-header -emit-objc-header-path %t.1.h -module-name ThisModule %s %S/Inputs/main.swift %S/Inputs/lib.swift -I %t
7-
// RUN: %target-swiftc_driver %clang-importer-sdk-nosource -emit-module -o %t.2.swiftmodule -emit-objc-header -emit-objc-header-path %t.2.h -module-name ThisModule %s %S/Inputs/main.swift %S/Inputs/lib.swift -force-single-frontend-invocation -I %t
8-
// RUN: diff %t.1.h %t.2.h
3+
// CHECK: swift -frontend
4+
// CHECK-SAME: emit-objc-header.swift
5+
// CHECK: swift -frontend -merge-modules
6+
// CHECK-SAME: -emit-objc-header-path {{.+}}/foo.h
7+
// CHECK: bin/ld
98

10-
// REQUIRES: objc_interop
9+
// RUN: %swiftc_driver -driver-print-jobs -target x86_64-apple-macosx10.9 %s -emit-objc-header-path %t/foo.h -force-single-frontend-invocation 2>&1 | %FileCheck -check-prefix=CHECK-WHOLE-MODULE %s
1110

12-
import Foundation
13-
14-
public class A: NSObject {
15-
func foo() {}
16-
@objc func bar(x: Int, baz y: Int) -> Int { return 1 }
17-
}
18-
public class B: A {
19-
func doSomething() {}
20-
}
11+
// CHECK-WHOLE-MODULE: swift -frontend
12+
// CHECK-WHOLE-MODULE-SAME: emit-objc-header.swift
13+
// CHECK-WHOLE-MODULE-SAME: -emit-objc-header-path {{.+}}/foo.h
14+
// CHECK-WHOLE-MODULE-NOT: -merge-modules
15+
// CHECK-WHOLE-MODULE: bin/ld

0 commit comments

Comments
 (0)