aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--examples/swift/BarLib/BUILD1
-rw-r--r--examples/swift/foo.swift2
-rwxr-xr-xsrc/test/shell/bazel/bazel_apple_test.sh41
-rw-r--r--tools/build_defs/apple/swift.bzl26
4 files changed, 57 insertions, 13 deletions
diff --git a/examples/swift/BarLib/BUILD b/examples/swift/BarLib/BUILD
index d575a879f1..cfd566979f 100644
--- a/examples/swift/BarLib/BUILD
+++ b/examples/swift/BarLib/BUILD
@@ -7,6 +7,7 @@ load("//tools/build_defs/apple:swift.bzl", "swift_library")
swift_library(
name = "BarLib",
srcs = ["mul.swift"],
+ module_name = "examples_BarLib",
)
filegroup(
diff --git a/examples/swift/foo.swift b/examples/swift/foo.swift
index 067c6730cd..1dd35c4407 100644
--- a/examples/swift/foo.swift
+++ b/examples/swift/foo.swift
@@ -1,4 +1,4 @@
-import class BarLib.Multiplier
+import class examples_BarLib.Multiplier
public class Foo {
public init() {}
diff --git a/src/test/shell/bazel/bazel_apple_test.sh b/src/test/shell/bazel/bazel_apple_test.sh
index db4b256137..3cb2dbeef7 100755
--- a/src/test/shell/bazel/bazel_apple_test.sh
+++ b/src/test/shell/bazel/bazel_apple_test.sh
@@ -100,9 +100,9 @@ EOF
function test_swift_library() {
local swift_lib_pkg=examples/swift
- assert_build_output ./bazel-genfiles/${swift_lib_pkg}/swift_lib.a \
+ assert_build_output ./bazel-genfiles/${swift_lib_pkg}/examples_swift_swift_lib.a \
${swift_lib_pkg}:swift_lib --ios_sdk_version=$IOS_SDK_VERSION
- assert_build_output ./bazel-genfiles/${swift_lib_pkg}/swift_lib.swiftmodule \
+ assert_build_output ./bazel-genfiles/${swift_lib_pkg}/examples_swift_swift_lib.swiftmodule \
${swift_lib_pkg}:swift_lib --ios_sdk_version=$IOS_SDK_VERSION
}
@@ -251,4 +251,41 @@ EOF
//ios:swift_lib >$TEST_log 2>&1 || fail "should build"
}
+function test_swift_imports_swift() {
+ rm -rf ios
+ mkdir -p ios
+
+ cat >ios/main.swift <<EOF
+import Foundation
+import ios_util
+
+public class SwiftClass {
+ public func bar() -> String {
+ return Utility().foo()
+ }
+}
+EOF
+
+ cat >ios/Utility.swift <<EOF
+public class Utility {
+ public init() {}
+ public func foo() -> String { return "foo" }
+}
+EOF
+
+ cat >ios/BUILD <<EOF
+load("//tools/build_defs/apple:swift.bzl", "swift_library")
+
+swift_library(name = "swift_lib",
+ srcs = ["main.swift"],
+ deps = [":util"])
+
+swift_library(name = "util",
+ srcs = ['Utility.swift'])
+EOF
+
+ bazel build --verbose_failures --ios_sdk_version=$IOS_SDK_VERSION \
+ //ios:swift_lib >$TEST_log 2>&1 || fail "should build"
+}
+
run_suite "apple_tests"
diff --git a/tools/build_defs/apple/swift.bzl b/tools/build_defs/apple/swift.bzl
index f75557534c..e08d4306c4 100644
--- a/tools/build_defs/apple/swift.bzl
+++ b/tools/build_defs/apple/swift.bzl
@@ -49,6 +49,10 @@ def _swift_target(cpu, sdk_version):
"""Returns a target triplet for Swift compiler."""
return "%s-apple-ios%s" % (cpu, sdk_version)
+def _module_name(ctx):
+ """Returns a module name for the given rule context."""
+ return ctx.label.package.lstrip("//").replace("/", "_") + "_" + ctx.label.name
+
def _swift_library_impl(ctx):
"""Implementation for swift_library Skylark rule."""
cpu = ctx.fragments.apple.ios_cpu()
@@ -57,6 +61,8 @@ def _swift_library_impl(ctx):
target = _swift_target(cpu, sdk_version)
apple_toolchain = apple_common.apple_toolchain()
+ module_name = ctx.attr.module_name or _module_name(ctx)
+
# A list of paths to pass with -F flag.
frameworks = set([
apple_toolchain.platform_developer_framework_dir(ctx.fragments.apple)])
@@ -93,9 +99,9 @@ def _swift_library_impl(ctx):
# TODO(b/28005753): Currently this is not really a library, but an object
# file, does not matter to the linker, but should be replaced with proper ar
# call.
- output_lib = ctx.outputs.swift_lib
- output_module = ctx.outputs.swift_module
- output_header = ctx.outputs.swift_header
+ output_lib = ctx.new_file(module_name + ".a")
+ output_module = ctx.new_file(module_name + ".swiftmodule")
+ output_header = ctx.new_file(ctx.label.name + "-Swift.h")
srcs_args = [f.path for f in ctx.files.srcs]
@@ -116,7 +122,7 @@ def _swift_library_impl(ctx):
"-frontend",
"-emit-object",
"-emit-module-path", output_module.path,
- "-module-name", ctx.label.name,
+ "-module-name", module_name,
"-emit-objc-header-path", output_header.path,
"-parse-as-library",
"-target", target,
@@ -143,23 +149,20 @@ def _swift_library_impl(ctx):
swift=struct(
transitive_libs=[output_lib] + dep_libs,
transitive_modules=[output_module] + dep_modules),
- objc=objc_provider)
+ objc=objc_provider,
+ files=set([output_lib, output_module, output_header]))
swift_library = rule(
_swift_library_impl,
attrs = {
"srcs": attr.label_list(allow_files = FileType([".swift"])),
"deps": attr.label_list(providers=[["swift"], ["objc"]]),
+ "module_name": attr.string(mandatory=False),
"_xcrunwrapper": attr.label(
executable=True,
default=Label(XCRUNWRAPPER_LABEL))},
fragments = ["apple"],
output_to_genfiles=True,
- outputs = {
- "swift_lib": "%{name}.a",
- "swift_module": "%{name}.swiftmodule",
- "swift_header": "%{name}-Swift.h",
- },
)
"""
Builds a Swift module.
@@ -170,4 +173,7 @@ Dependant targets can import this module as "import RuleName".
Args:
srcs: Swift sources that comprise this module.
deps: Other Swift modules.
+ module_name: Optional. Sets the Swift module name for this target. By default
+ the module name is the target path with all special symbols
+ replaced by "_", e.g. //foo:bar can be imported as "foo_bar".
"""