Skip to content

Commit

Permalink
Update swift_library logic for module name generation
Browse files Browse the repository at this point in the history
* Modules are now named after its full target path by default.
* module_name attribute can be used to override this logic.

--
MOS_MIGRATED_REVID=123422704
  • Loading branch information
Dmitry Shevchenko authored and dslomov committed May 30, 2016
1 parent 7d0f7b6 commit befa57c
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
1 change: 1 addition & 0 deletions examples/swift/BarLib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion examples/swift/foo.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import class BarLib.Multiplier
import class examples_BarLib.Multiplier

public class Foo {
public init() {}
Expand Down
41 changes: 39 additions & 2 deletions src/test/shell/bazel/bazel_apple_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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"
26 changes: 16 additions & 10 deletions tools/build_defs/apple/swift.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)])
Expand Down Expand Up @@ -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]

Expand All @@ -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,
Expand All @@ -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.
Expand All @@ -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".
"""

0 comments on commit befa57c

Please sign in to comment.