aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Dmitry Shevchenko <dmishe@google.com>2016-07-29 20:14:21 +0000
committerGravatar Yun Peng <pcloudy@google.com>2016-08-01 08:07:37 +0000
commitdf145011b10e50b1553231795adc943a6f6c871e (patch)
tree8625e29a54c513a10fec8cfb80082349cb43a6be
parentbf01d8b17b5986eddad59107a23177ec0062021f (diff)
Add a way to set build configuration values (defines) on swift_library targets.
-- MOS_MIGRATED_REVID=128838998
-rwxr-xr-xsrc/test/shell/bazel/bazel_apple_test.sh29
-rw-r--r--tools/build_defs/apple/swift.bzl18
2 files changed, 45 insertions, 2 deletions
diff --git a/src/test/shell/bazel/bazel_apple_test.sh b/src/test/shell/bazel/bazel_apple_test.sh
index bd78a84e9a..5f56934bfc 100755
--- a/src/test/shell/bazel/bazel_apple_test.sh
+++ b/src/test/shell/bazel/bazel_apple_test.sh
@@ -488,4 +488,33 @@ EOF
|| fail "expected output binary to contain 2 architectures"
}
+function test_swift_defines() {
+ rm -rf ios
+ mkdir -p ios
+
+ cat >ios/main.swift <<EOF
+import Foundation
+
+public class SwiftClass {
+ public func bar() {
+ #if !FLAG
+ let x: String = 1 // Invalid statement, should throw compiler error when FLAG is not set
+ #endif
+ }
+}
+EOF
+
+ cat >ios/BUILD <<EOF
+load("//tools/build_defs/apple:swift.bzl", "swift_library")
+
+swift_library(name = "swift_lib",
+ srcs = ["main.swift"],
+ defines = ["FLAG"])
+EOF
+
+ bazel build --verbose_failures --ios_sdk_version=$IOS_SDK_VERSION \
+ --xcode_version=$XCODE_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 6d4220e311..fe0ca853f1 100644
--- a/tools/build_defs/apple/swift.bzl
+++ b/tools/build_defs/apple/swift.bzl
@@ -136,6 +136,7 @@ def _swift_library_impl(ctx):
include_args = ["-I%s" % d for d in include_dirs + objc_includes]
framework_args = ["-F%s" % x for x in framework_dirs]
+ define_args = ["-D%s" % x for x in ctx.attr.defines]
clang_args = _intersperse(
"-Xcc",
@@ -180,6 +181,7 @@ def _swift_library_impl(ctx):
args.extend(include_args)
args.extend(framework_args)
args.extend(clang_args)
+ args.extend(define_args)
xcrun_action(ctx,
inputs=ctx.files.srcs + dep_modules + list(objc_files) +
@@ -219,6 +221,7 @@ swift_library = rule(
"srcs": attr.label_list(allow_files = [".swift"]),
"deps": attr.label_list(providers=[["swift"], ["objc"]]),
"module_name": attr.string(mandatory=False),
+ "defines": attr.string_list(mandatory=False, allow_empty=True),
"_xcrunwrapper": attr.label(
executable=True,
default=Label(XCRUNWRAPPER_LABEL))},
@@ -235,6 +238,17 @@ 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".
+ the module name is the target path with all special symbols replaced
+ by "_", e.g. //foo:bar can be imported as "foo_bar".
+ defines: A list of values for build configuration options (-D). These values
+ can be then used for conditional compilation blocks in code. For example:
+
+ BUILD:
+ swift_library(
+ defines = ["VALUE"]
+ )
+
+ Code:
+ #if VALUE
+ #endif
"""