-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add feature to propagate the generated module map
Allows `objc_library` to modularly import `swift_library` targets without having to generate a similar module map.
- Loading branch information
1 parent
ea2d4bd
commit 5dba983
Showing
9 changed files
with
185 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# This example tests that Swift code can be called from Objective-C code and | ||
# vice versa when it is linked into the existing native linking rules for Apple | ||
# platforms. | ||
|
||
load( | ||
"//swift:swift.bzl", | ||
"swift_binary", | ||
"swift_library", | ||
) | ||
|
||
licenses(["notice"]) | ||
|
||
objc_library( | ||
name = "PrintStream", | ||
srcs = ["OIPrintStream.m"], | ||
hdrs = ["OIPrintStream.h"], | ||
target_compatible_with = ["@platforms//os:macos"], | ||
) | ||
|
||
swift_library( | ||
name = "Printer", | ||
srcs = ["Printer.swift"], | ||
# features = ["swift.propagate_generated_module_map"], | ||
generated_header_name = "generated_header/Printer-Swift.h", | ||
generates_header = True, | ||
deps = [":PrintStream"], | ||
) | ||
|
||
objc_library( | ||
name = "main", | ||
srcs = ["main.m"], | ||
enable_modules = True, | ||
target_compatible_with = ["@platforms//os:macos"], | ||
deps = [":Printer"], | ||
) | ||
|
||
swift_binary( | ||
name = "objc_interop_modulemap", | ||
deps = [":main"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2018 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
/** A very contrived interface for writing strings to a file handle. */ | ||
@interface OIPrintStream : NSObject | ||
|
||
- (nonnull instancetype)initWithFileHandle:(nonnull NSFileHandle *)fileHandle; | ||
|
||
- (void)printString:(nonnull NSString *)message; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2018 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#import "examples/apple/objc_interop_modulemap/OIPrintStream.h" | ||
|
||
@implementation OIPrintStream { | ||
NSFileHandle *_fileHandle; | ||
} | ||
|
||
- (instancetype)initWithFileHandle:(nonnull NSFileHandle *)fileHandle { | ||
if (self = [super init]) { | ||
_fileHandle = fileHandle; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)printString:(nonnull NSString *)message { | ||
NSData *data = [message dataUsingEncoding:NSUTF8StringEncoding]; | ||
[_fileHandle writeData:data]; | ||
NSData *newline = [NSData dataWithBytes:"\n" length:1]; | ||
[_fileHandle writeData:newline]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2018 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import Foundation | ||
import examples_apple_objc_interop_modulemap_PrintStream | ||
|
||
@objc(OIPrinter) | ||
public class Printer: NSObject { | ||
|
||
private let stream: OIPrintStream | ||
private let prefix: String | ||
|
||
@objc public init(prefix: NSString) { | ||
self.stream = OIPrintStream(fileHandle: .standardOutput) | ||
self.prefix = prefix as String | ||
} | ||
|
||
@objc public func print(_ message: NSString) { | ||
stream.print("\(prefix)\(message)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2018 The Bazel Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
@import examples_apple_objc_interop_modulemap_Printer; | ||
|
||
int main(int argc, char **argv) { | ||
@autoreleasepool { | ||
OIPrinter *printer = [[OIPrinter alloc] initWithPrefix:@"*** "]; | ||
[printer print:@"Hello world"]; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters