diff options
author | thomasvl <thomasvl@google.com> | 2017-09-08 21:44:06 +0200 |
---|---|---|
committer | Philipp Wollermann <philwo@google.com> | 2017-09-11 13:07:19 +0200 |
commit | e9c788a9711e8f780de7eb5bd3e1bed7994eb88c (patch) | |
tree | 7315d141dd6149e53f53f07701ffed04838ddbb9 | |
parent | dd8bfab0bc3d2e663c38ec3df4b078d294078449 (diff) |
Remove swift_library and tests, lives in the apple rules repo now.
RELNOTES: The swift_library rule that is distributed with Bazel has been deleted. Users who wish to compile Swift should use the rules in https://github.com/bazelbuild/rules_apple instead.
PiperOrigin-RevId: 168032330
-rw-r--r-- | examples/BUILD | 1 | ||||
-rw-r--r-- | examples/swift/BUILD | 19 | ||||
-rw-r--r-- | examples/swift/BarLib/BUILD | 16 | ||||
-rw-r--r-- | examples/swift/BarLib/mul.swift | 8 | ||||
-rw-r--r-- | examples/swift/constants.swift | 4 | ||||
-rw-r--r-- | examples/swift/foo.swift | 9 | ||||
-rwxr-xr-x | src/test/shell/bazel/apple/bazel_apple_test.sh | 791 | ||||
-rw-r--r-- | tools/build_defs/apple/swift.bzl | 501 |
8 files changed, 0 insertions, 1349 deletions
diff --git a/examples/BUILD b/examples/BUILD index bf4452b175..fe3a67e528 100644 --- a/examples/BUILD +++ b/examples/BUILD @@ -13,7 +13,6 @@ filegroup( "//examples/py:srcs", "//examples/py_native:srcs", "//examples/shell:srcs", - "//examples/swift:srcs", "//examples/windows/dll:srcs", ], ) diff --git a/examples/swift/BUILD b/examples/swift/BUILD deleted file mode 100644 index e2cbb195e2..0000000000 --- a/examples/swift/BUILD +++ /dev/null @@ -1,19 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -licenses(["notice"]) # Apache 2.0 - -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library( - name = "swift_lib", - srcs = glob(["*.swift"]), - deps = ["//examples/swift/BarLib"], -) - -filegroup( - name = "srcs", - srcs = glob(["**"]) + [ - "//examples/swift/BarLib:srcs", - ], - visibility = ["//examples:__pkg__"], -) diff --git a/examples/swift/BarLib/BUILD b/examples/swift/BarLib/BUILD deleted file mode 100644 index cfd566979f..0000000000 --- a/examples/swift/BarLib/BUILD +++ /dev/null @@ -1,16 +0,0 @@ -package(default_visibility = ["//visibility:public"]) - -licenses(["notice"]) # Apache 2.0 - -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library( - name = "BarLib", - srcs = ["mul.swift"], - module_name = "examples_BarLib", -) - -filegroup( - name = "srcs", - srcs = glob(["**"]), -) diff --git a/examples/swift/BarLib/mul.swift b/examples/swift/BarLib/mul.swift deleted file mode 100644 index 898c0b6877..0000000000 --- a/examples/swift/BarLib/mul.swift +++ /dev/null @@ -1,8 +0,0 @@ -/// Class used to multiply stuff. -public class Multiplier { - public init() {} - - public func multiply(a first: Int, b second: Int) -> Int { - return first * second - } -} diff --git a/examples/swift/constants.swift b/examples/swift/constants.swift deleted file mode 100644 index 6c16d34a82..0000000000 --- a/examples/swift/constants.swift +++ /dev/null @@ -1,4 +0,0 @@ -class Constants { - static var x = 2 - static var y = 3 -}
\ No newline at end of file diff --git a/examples/swift/foo.swift b/examples/swift/foo.swift deleted file mode 100644 index 4f80dad878..0000000000 --- a/examples/swift/foo.swift +++ /dev/null @@ -1,9 +0,0 @@ -import class examples_BarLib.Multiplier - -public class Foo { - public init() {} - - public func multiply() -> Int { - return Multiplier().multiply(a: Constants.x, b: Constants.y) - } -} diff --git a/src/test/shell/bazel/apple/bazel_apple_test.sh b/src/test/shell/bazel/apple/bazel_apple_test.sh index dfed68fc27..7df8991c24 100755 --- a/src/test/shell/bazel/apple/bazel_apple_test.sh +++ b/src/test/shell/bazel/apple/bazel_apple_test.sh @@ -43,393 +43,6 @@ function set_up() { use_bazel_workspace_file } -function make_app() { - rm -rf ios - mkdir -p ios - - cat >ios/app.swift <<EOF -import UIKit - -@UIApplicationMain -class AppDelegate: UIResponder, UIApplicationDelegate { - var window: UIWindow? - func application( - application: UIApplication, - didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) - -> Bool { - NSLog("Hello, world") - return true - } -} -EOF - - cat >ios/App-Info.plist <<EOF -<plist version="1.0"> -<dict> - <key>CFBundleExecutable</key> - <string>app</string> - <key>CFBundleName</key> - <string>app</string> - <key>CFBundleDisplayName</key> - <string>app</string> - <key>CFBundlePackageType</key> - <string>APPL</string> - <key>CFBundleIdentifier</key> - <string>com.google.app</string> - <key>CFBundleSignature</key> - <string>????</string> - <key>CFBundleVersion</key> - <string>1.0</string> - <key>LSRequiresIPhoneOS</key> - <true/> -</dict> -</plist> -EOF - - cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "SwiftMain", - srcs = ["app.swift"]) - -objc_binary(name = "bin", - srcs = ["//tools/objc:dummy.c"], - deps = [":SwiftMain"]) - -ios_application(name = "app", - binary = ':bin', - infoplist = 'App-Info.plist') -EOF -} - -function test_swift_library() { - local swift_lib_pkg=examples/swift - assert_build_output ./bazel-genfiles/${swift_lib_pkg}/swift_lib/_objs/examples_swift_swift_lib.a \ - ${swift_lib_pkg}:swift_lib --xcode_version=$XCODE_VERSION - assert_build_output ./bazel-genfiles/${swift_lib_pkg}/swift_lib/_objs/examples_swift_swift_lib.swiftmodule \ - ${swift_lib_pkg}:swift_lib --xcode_version=$XCODE_VERSION -} - -function test_build_app() { - make_app - - bazel build --verbose_failures --xcode_version=$XCODE_VERSION \ - //ios:app >$TEST_log 2>&1 || fail "should pass" - ls bazel-bin/ios/app.ipa || fail "should generate app.ipa" -} - -function test_objc_depends_on_swift() { - rm -rf ios - mkdir -p ios - - cat >ios/main.swift <<EOF -import Foundation - -@objc public class Foo: NSObject { - public func bar() -> Int { return 42; } -} -EOF - - cat >ios/app.m <<EOF -#import <UIKit/UIKit.h> -#import "ios/SwiftMain-Swift.h" - -int main(int argc, char *argv[]) { - @autoreleasepool { - NSLog(@"%d", [[[Foo alloc] init] bar]); - return UIApplicationMain(argc, argv, nil, nil); - } -} -EOF - - cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "SwiftMain", - srcs = ["main.swift"]) - -objc_binary(name = "bin", - srcs = ['app.m',], - deps = [":SwiftMain"]) -EOF - - bazel build --verbose_failures --xcode_version=$XCODE_VERSION \ - //ios:bin >$TEST_log 2>&1 || fail "should build" -} - -function test_swift_imports_objc() { - rm -rf ios - mkdir -p ios - - cat >ios/main.swift <<EOF -import Foundation -import ios_ObjcLib - -public class SwiftClass { - public func bar() -> String { - return ObjcClass().foo() - } -} -EOF - - cat >ios/ObjcClass.h <<EOF -#import <Foundation/Foundation.h> - -#if !DEFINE_FOO -#error "Define is not passed in" -#endif - -#if !COPTS_FOO -#error "Copt is not passed in -#endif - -@interface ObjcClass : NSObject -- (NSString *)foo; -@end -EOF - - cat >ios/ObjcClass.m <<EOF -#import "ObjcClass.h" -@implementation ObjcClass -- (NSString *)foo { return @"Hello ObjcClass"; } -@end -EOF - - cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "swift_lib", - srcs = ["main.swift"], - deps = [":ObjcLib"]) - -objc_library(name = "ObjcLib", - hdrs = ['ObjcClass.h'], - srcs = ['ObjcClass.m'], - defines = ["DEFINE_FOO=1"]) -EOF - - bazel build --verbose_failures --objccopt=-DCOPTS_FOO=1 -s \ - --xcode_version=$XCODE_VERSION \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should build" - expect_log "-module-cache-path bazel-out/darwin_x86_64-fastbuild/.*/_objc_module_cache" -} - -function test_swift_import_objc_framework() { - rm -rf ios - mkdir -p ios - - # Copy the prebuilt framework into app's directory. - cp -RL "${BAZEL_RUNFILES}/tools/build_defs/apple/test/testdata/BlazeFramework.framework" ios - - cat >ios/app.swift <<EOF -import UIKit - -import BlazeFramework - -@UIApplicationMain -class AppDelegate: UIResponder, UIApplicationDelegate { - var window: UIWindow? - func application( - application: UIApplication, - didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) - -> Bool { - NSLog("\(Multiplier().foo())") - return true - } -} -EOF - - cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -objc_binary(name = "bin", - srcs = ["//tools/objc:dummy.c"], - deps = [":swift_lib"]) - -swift_library(name = "swift_lib", - srcs = ["app.swift"], - deps = [":dylib"]) - -objc_framework(name = "dylib", - framework_imports = glob(["BlazeFramework.framework/**"]), - is_dynamic = 1) -EOF - - bazel build --verbose_failures --ios_minimum_os=8.0 \ - --xcode_version=$XCODE_VERSION \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should build" -} - -function test_fat_apple_binary() { - rm -rf package - mkdir -p package - cat > package/BUILD <<EOF -objc_library( - name = "lib_a", - srcs = ["a.m"], -) -objc_library( - name = "lib_b", - srcs = ["b.m"], -) -apple_binary( - name = "main_binary", - deps = [":lib_a", ":lib_b"], - srcs = ["main.m"], - platform_type = "ios", -) -genrule( - name = "lipo_run", - srcs = [":main_binary_lipobin"], - outs = ["lipo_out"], - cmd = - "set -e && " + - "lipo -info \$(location :main_binary_lipobin) > \$(@)", - tags = ["requires-darwin"], -) -EOF - touch package/a.m - touch package/b.m - cat > package/main.m <<EOF -int main() { - return 0; -} -EOF - - bazel build --verbose_failures //package:lipo_out \ - --ios_multi_cpus=i386,x86_64 \ - --xcode_version=$XCODE_VERSION \ - || fail "should build apple_binary and obtain info via lipo" - - cat bazel-genfiles/package/lipo_out | grep "i386 x86_64" \ - || fail "expected output binary to contain 2 architectures" -} - -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 --xcode_version=$XCODE_VERSION \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should build" -} - -function test_swift_tests() { - make_app - - cat >ios/internal.swift <<EOF -internal class InternalClass { - func foo() -> String { return "bar" } -} -EOF - - cat >ios/tests.swift <<EOF - import XCTest - @testable import ios_SwiftMain - -class FooTest: XCTestCase { - func testFoo() { XCTAssertEqual(2, 3) } - func testInternalClass() { XCTAssertEqual(InternalClass().foo(), "bar") } -} -EOF - - cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "SwiftMain", - srcs = ["app.swift", "internal.swift"]) - -objc_binary(name = "bin", - srcs = ["//tools/objc:dummy.c"], - deps = [":SwiftMain"]) - -ios_application(name = "app", - binary = ':bin', - infoplist = 'App-Info.plist') - -swift_library(name = "SwiftTest", - srcs = ["tests.swift"], - deps = [":SwiftMain"]) - -ios_test(name = "app_test", - srcs = ["//tools/objc:dummy.c"], - deps = [":SwiftTest"], - xctest_app = "app") -EOF - - bazel build --verbose_failures --xcode_version=$XCODE_VERSION \ - --ios_minimum_os=8.0 \ - //ios:app_test >$TEST_log 2>&1 || fail "should build" - - otool -lv bazel-out/ios_x86_64-fastbuild/bin/ios/app_test_bin \ - | grep @executable_path/Frameworks -sq \ - || fail "expected test binary to contain @executable_path in LC_RPATH" - - otool -lv bazel-out/ios_x86_64-fastbuild/bin/ios/app_test_bin \ - | grep @loader_path/Frameworks -sq \ - || fail "expected test binary to contain @loader_path in LC_RPATH" - -} - -function test_swift_compilation_mode_flags() { - rm -rf ios - mkdir -p ios - - cat >ios/debug.swift <<EOF -// A trick to break compilation when DEBUG is not set. -func foo() { - #if DEBUG - var x: Int - #endif - x = 3 -} -EOF - - cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "swift_lib", - srcs = ["debug.swift"]) -EOF - - ! bazel build --verbose_failures -c opt \ - --xcode_version=$XCODE_VERSION \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should not build" - expect_log "error: use of unresolved identifier 'x'" - - bazel build --verbose_failures -c dbg \ - --xcode_version=$XCODE_VERSION \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should build" -} - function test_fat_binary_no_srcs() { mkdir -p package cat > package/BUILD <<EOF @@ -471,69 +84,6 @@ EOF || fail "expected output binary to contain 2 architectures" } -function test_swift_defines() { - rm -rf ios - mkdir -p ios - touch ios/dummy.swift - - 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 - - #if !DEP_FLAG - let x: String = 2 // Invalid statement, should throw compiler error when DEP_FLAG is not set - #endif - } -} -EOF - - cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "dep_lib", - srcs = ["dummy.swift"], - defines = ["DEP_FLAG"]) - -swift_library(name = "swift_lib", - srcs = ["main.swift"], - defines = ["FLAG"], - deps = [":dep_lib"]) -EOF - - bazel build --verbose_failures --xcode_version=$XCODE_VERSION \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should build" -} - -function test_apple_watch_with_swift() { - make_app - - cat >ios/watchapp.swift <<EOF - import WatchKit - class ExtensionDelegate: NSObject, WKExtensionDelegate { - func applicationDidFinishLaunching() {} - } -EOF - - cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "WatchModule", - srcs = ["watchapp.swift"]) - -apple_binary(name = "bin", - deps = [":WatchModule"], - platform_type = "watchos") -EOF - - bazel build --verbose_failures --xcode_version=$XCODE_VERSION \ - //ios:bin >$TEST_log 2>&1 || fail "should build" -} - function test_host_xcodes() { XCODE_VERSION=$(env -i xcodebuild -version | grep "Xcode" \ | sed -E "s/Xcode (([0-9]|.)+).*/\1/") @@ -566,347 +116,6 @@ function test_host_xcodes() { assert_equals $DEFAULT_LABEL $(cat xcode_version_target) } -function test_no_object_file_collisions() { - rm -rf ios - mkdir -p ios - - touch ios/foo.swift - - cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "Foo", - srcs = ["foo.swift"]) -swift_library(name = "Bar", - srcs = ["foo.swift"]) -EOF - - bazel build --verbose_failures --xcode_version=$XCODE_VERSION \ - //ios:{Foo,Bar} >$TEST_log 2>&1 || fail "should build" -} - -function test_minimum_os() { - rm -rf ios - mkdir -p ios - - touch ios/foo.swift - - cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "foo", - srcs = ["foo.swift"]) -EOF - - bazel build --verbose_failures -s --announce_rc \ - --xcode_version=$XCODE_VERSION --ios_minimum_os=9.0 \ - //ios:foo >$TEST_log 2>&1 || fail "should build" - - # Get the min OS version encoded as "version" argument of - # LC_VERSION_MIN_IPHONEOS load command in Mach-O - MIN_OS=$(otool -l bazel-genfiles/ios/foo/_objs/ios_foo.a | \ - grep -A 3 LC_VERSION_MIN_IPHONEOS | grep version | cut -d " " -f4) - assert_equals $MIN_OS "9.0" -} - -function test_swift_copts() { - 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 - - #if !CMD_FLAG - let y: String = 1 // Invalid statement, should throw compiler error when CMD_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"], - copts = ["-DFLAG"]) -EOF - - bazel build --verbose_failures --xcode_version=$XCODE_VERSION \ - --swiftcopt=-DCMD_FLAG \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should build" -} - -function test_swift_bitcode() { - rm -rf ios - mkdir -p ios - -cat >ios/main.swift <<EOF -func f() {} -EOF - -cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "swift_lib", - srcs = ["main.swift"]) -EOF - - ARCHIVE=bazel-genfiles/ios/swift_lib/_objs/ios_swift_lib.a - - # No bitcode - bazel build --verbose_failures --xcode_version=$XCODE_VERSION --ios_multi_cpus=arm64 \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should build" - ! otool -l $ARCHIVE | grep __bitcode -sq \ - || fail "expected a.o to not contain bitcode" - - # Bitcode marker - bazel build --verbose_failures \ - --xcode_version=$XCODE_VERSION --apple_bitcode=embedded_markers --ios_multi_cpus=arm64 \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should build" - # Bitcode marker has a length of 1. - assert_equals $(size -m $ARCHIVE | grep __bitcode | cut -d: -f2 | tr -d ' ') "1" - - # Full bitcode - bazel build --verbose_failures \ - --xcode_version=$XCODE_VERSION --apple_bitcode=embedded --ios_multi_cpus=arm64 \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should build" - otool -l $ARCHIVE | grep __bitcode -sq \ - || fail "expected a.o to contain bitcode" - - # Bitcode disabled because of simulator architecture - bazel build --verbose_failures \ - --xcode_version=$XCODE_VERSION --apple_bitcode=embedded --ios_multi_cpus=x86_64 \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should build" - ! otool -l $ARCHIVE | grep __bitcode -sq \ - || fail "expected a.o to not contain bitcode" -} - -function test_swift_name_validation() { - rm -rf ios - mkdir -p ios - - touch ios/main.swift - touch ios/main.m - -cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "swift-lib", - srcs = ["main.swift"]) -EOF - - ! bazel build --verbose_failures \ - --xcode_version=$XCODE_VERSION \ - //ios:swift-lib >$TEST_log 2>&1 || fail "should fail" - expect_log "Error in target '//ios:swift-lib'" - - cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -objc_library(name = "bad-dep", srcs = ["main.m"]) - -swift_library(name = "swift_lib", - srcs = ["main.swift"], deps=[":bad-dep"]) -EOF - - ! bazel build --verbose_failures \ - --xcode_version=$XCODE_VERSION \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should fail" - expect_log "Error in target '//ios:bad-dep'" -} - -function test_swift_ast_is_recorded() { - rm -rf ios - mkdir -p ios - - touch ios/main.swift - cat >ios/dep.swift <<EOF -import UIKit -// Add dummy code so that Swift symbols are exported into final binary, which -// will cause runtime libraries to be packaged into the IPA -class X: UIViewController {} -EOF - - cat >ios/main.m <<EOF -#import <UIKit/UIKit.h> - -int main(int argc, char *argv[]) { - @autoreleasepool { - return UIApplicationMain(argc, argv, nil, nil); - } -} -EOF - - cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "dep", - srcs = ["dep.swift"]) - -swift_library(name = "swift_lib", - srcs = ["main.swift"], - deps = [":dep"]) -objc_binary(name = "bin", - srcs = ["main.m"], - deps = [":swift_lib"]) -EOF - - bazel build --verbose_failures --xcode_version=$XCODE_VERSION -s \ - //ios:bin >$TEST_log 2>&1 || fail "should build" - expect_log "-Xlinker -add_ast_path -Xlinker bazel-out/ios_x86_64-fastbuild/.*/ios/dep/_objs/ios_dep.swiftmodule" - expect_log "-Xlinker -add_ast_path -Xlinker bazel-out/ios_x86_64-fastbuild/.*/ios/swift_lib/_objs/ios_swift_lib.swiftmodule" -} - -function test_swiftc_script_mode() { - rm -rf ios - mkdir -p ios - touch ios/foo.swift - - cat >ios/top.swift <<EOF -print() // Top level expression outside of main.swift, should fail. -EOF - - cat >ios/main.swift <<EOF -import UIKit - -class AppDelegate: UIResponder, UIApplicationDelegate {} - -#if swift(>=3) -UIApplicationMain( - CommandLine.argc, - UnsafeMutableRawPointer(CommandLine.unsafeArgv) - .bindMemory( - to: UnsafeMutablePointer<Int8>.self, - capacity: Int(CommandLine.argc)), - nil, - NSStringFromClass(AppDelegate.self) -) -#else -UIApplicationMain( - Process.argc, UnsafeMutablePointer<UnsafeMutablePointer<CChar>>(Process.unsafeArgv), - nil, NSStringFromClass(AppDelegate) -) -#endif -EOF - -cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -swift_library(name = "main_should_compile_as_script", - srcs = ["main.swift", "foo.swift"]) -swift_library(name = "top_should_not_compile_as_script", - srcs = ["top.swift"]) -swift_library(name = "single_source_should_compile_as_library", - srcs = ["foo.swift"]) -EOF - - bazel build --verbose_failures --xcode_version=$XCODE_VERSION \ - //ios:single_source_should_compile_as_library \ - //ios:main_should_compile_as_script >$TEST_log 2>&1 || fail "should build" - - ! bazel build --verbose_failures --xcode_version=$XCODE_VERSION \ - //ios:top_should_not_compile_as_script >$TEST_log 2>&1 || fail "should not build" - expect_log "ios/top.swift:1:1: error: expressions are not allowed at the top level" -} - -# Test that it's possible to import Clang module of a target that contains private headers. -function test_import_module_with_private_hdrs() { - rm -rf ios - mkdir -p ios - touch ios/Foo.h ios/Foo_Private.h - -cat >ios/main.swift <<EOF -import ios_lib -EOF - -cat >ios/BUILD <<EOF -load("//tools/build_defs/apple:swift.bzl", "swift_library") - -objc_library(name = "lib", - srcs = ["Foo_Private.h"], - hdrs = ["Foo.h"]) - -swift_library(name = "swiftmodule", - srcs = ["main.swift"], - deps = [":lib"]) -EOF - bazel build --verbose_failures --xcode_version=$XCODE_VERSION \ - //ios:swiftmodule >$TEST_log 2>&1 || fail "should build" -} - -function test_swift_whole_module_optimization() { - 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"], - copts = ["-wmo"]) - -swift_library(name = "util", - srcs = ['Utility.swift'], - copts = ["-whole-module-optimization"]) -EOF - - bazel build --verbose_failures --xcode_version=$XCODE_VERSION \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should build" -} - -function test_swift_dsym() { - rm -rf ios - mkdir -p ios - - cat >ios/main.swift <<EOF -import Foundation - -public class SwiftClass { - public func bar() -> 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"]) -EOF - - bazel build -c opt --apple_generate_dsym \ - --verbose_failures --xcode_version=$XCODE_VERSION \ - //ios:swift_lib >$TEST_log 2>&1 || fail "should build" - - # Verify that debug info is present. - dwarfdump -R bazel-genfiles/ios/swift_lib/_objs/ios_swift_lib.a \ - | grep -sq "__DWARF" \ - || fail "should contain DWARF data" -} - function test_apple_binary_crosstool_ios() { rm -rf package mkdir -p package diff --git a/tools/build_defs/apple/swift.bzl b/tools/build_defs/apple/swift.bzl deleted file mode 100644 index a8f1a16908..0000000000 --- a/tools/build_defs/apple/swift.bzl +++ /dev/null @@ -1,501 +0,0 @@ -# Copyright 2016 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. - -"""Skylark rules for Swift. - -NOTE: This file is deprecated and will be removed soon. If you depend on it, -please start using the version from the rules_apple repository -(https://github.com/bazelbuild/rules_apple) instead. -""" - -load("shared", - "xcrun_action", - "XCRUNWRAPPER_LABEL", - "module_cache_path", - "label_scoped_path") - -def _parent_dirs(dirs): - """Returns a depset of parent directories for each directory in dirs.""" - return depset([f.rpartition("/")[0] for f in dirs]) - - -def _framework_names(dirs): - """Returns the framework name for each directory in dir.""" - return depset([f.rpartition("/")[2].partition(".")[0] for f in dirs]) - - -def _intersperse(separator, iterable): - """Inserts separator before each item in iterable.""" - result = [] - for x in iterable: - result.append(separator) - result.append(x) - - return result - - -def _swift_target(cpu, platform, sdk_version): - """Returns a target triplet for Swift compiler.""" - platform_string = str(platform.platform_type) - if platform_string not in ["ios", "watchos", "tvos"]: - fail("Platform '%s' is not supported" % platform_string) - - return "%s-apple-%s%s" % (cpu, platform_string, sdk_version) - - -def _swift_compilation_mode_flags(ctx): - """Returns additional swiftc flags for the current compilation mode.""" - mode = ctx.var["COMPILATION_MODE"] - - flags = [] - if mode == "dbg" or mode == "fastbuild": - # TODO(dmishe): Find a way to test -serialize-debugging-options - flags += [ - "-Onone", "-DDEBUG", "-enable-testing", "-Xfrontend", - "-serialize-debugging-options" - ] - elif mode == "opt": - flags += ["-O", "-DNDEBUG"] - - if mode == "dbg" or ctx.fragments.objc.generate_dsym: - flags.append("-g") - - return flags - - -def _clang_compilation_mode_flags(ctx): - """Returns additional clang flags for the current compilation mode.""" - - # In general, every compilation mode flag from native objc_ rules should be - # passed, but -g seems to break Clang module compilation. Since this flag does - # not make much sense for module compilation and only touches headers, - # it's ok to omit. - native_clang_flags = ctx.fragments.objc.copts_for_current_compilation_mode - - return [x for x in native_clang_flags if x != "-g"] - - -def _swift_bitcode_flags(ctx): - """Returns bitcode flags based on selected mode.""" - mode = str(ctx.fragments.apple.bitcode_mode) - if mode == "embedded": - return ["-embed-bitcode"] - elif mode == "embedded_markers": - return ["-embed-bitcode-marker"] - - return [] - - -def swift_module_name(label): - """Returns a module name for the given label.""" - return label.package.lstrip("//").replace("/", "_") + "_" + label.name - - -def _swift_lib_dir(ctx): - """Returns the location of swift runtime directory to link against.""" - platform_str = ctx.fragments.apple.single_arch_platform.name_in_plist.lower() - - toolchain_name = "XcodeDefault" - if hasattr(ctx.fragments.apple, "xcode_toolchain"): - toolchain = ctx.fragments.apple.xcode_toolchain - - # We cannot use non Xcode-packaged toolchains, and the only one non-default - # toolchain known to exist (as of Xcode 8.1) is this one. - if toolchain == "com.apple.dt.toolchain.Swift_2_3": - toolchain_name = "Swift_2.3" - - return "{0}/Toolchains/{1}.xctoolchain/usr/lib/swift/{2}".format( - apple_common.apple_toolchain().developer_dir(), toolchain_name, platform_str) - - -def _swift_linkopts(ctx): - """Returns additional linker arguments for the given rule context.""" - return depset(["-L" + _swift_lib_dir(ctx)]) - - -def _swift_xcrun_args(ctx): - """Returns additional arguments that should be passed to xcrun.""" - if ctx.fragments.apple.xcode_toolchain: - return ["--toolchain", ctx.fragments.apple.xcode_toolchain] - - return [] - - -def _swift_parsing_flags(ctx): - """Returns additional parsing flags for swiftc.""" - srcs = ctx.files.srcs - - # swiftc has two different parsing modes: script and library. - # The difference is that in script mode top-level expressions are allowed. - # This mode is triggered when the file compiled is called main.swift. - # Additionally, script mode is used when there's just one file in the - # compilation. we would like to avoid that and therefore force library mode - # when there's only one source and it's not called main. - if len(srcs) == 1 and srcs[0].basename != "main.swift": - return ["-parse-as-library"] - return [] - - -def _is_valid_swift_module_name(string): - """Returns True if the string is a valid Swift module name.""" - if not string: - return False - - for char in string: - # Check that the character is in [a-zA-Z0-9_] - if not (char.isalnum() or char == "_"): - return False - - return True - - -def _validate_rule_and_deps(ctx): - """Validates the target and its dependencies.""" - - name_error_str = ("Error in target '%s', Swift target and its dependencies' "+ - "names can only contain characters in [a-zA-Z0-9_].") - - # Validate the name of the target - if not _is_valid_swift_module_name(ctx.label.name): - fail(name_error_str % ctx.label) - - # Validate names of the dependencies - for dep in ctx.attr.deps: - if not _is_valid_swift_module_name(dep.label.name): - fail(name_error_str % dep.label) - - -def swiftc_inputs(ctx): - """Determine the list of inputs required for the compile action. - - Args: - ctx: rule context. - - Returns: - A list of files needed by swiftc. - """ - swift_providers = [x.swift for x in ctx.attr.deps if hasattr(x, "swift")] - objc_providers = [x.objc for x in ctx.attr.deps if hasattr(x, "objc")] - - dep_modules = [] - for swift in swift_providers: - dep_modules += swift.transitive_modules - - objc_files = depset() - for objc in objc_providers: - objc_files += objc.header - objc_files += objc.module_map - objc_files += depset(objc.static_framework_file) - objc_files += depset(objc.dynamic_framework_file) - - return ctx.files.srcs + dep_modules + list(objc_files) - - -def swiftc_args(ctx): - """Returns an almost compelete array of arguments to be passed to swiftc. - - This macro is intended to be used by the swift_library rule implementation - below but it also may be used by other rules outside this file. It has no - side effects and does not modify ctx. It expects ctx to contain the same - fragments and attributes as swift_library (you're encouraged to depend on - SWIFT_LIBRARY_ATTRS in your rule definition). - - Args: - ctx: rule context - - Returns: - A list of command line arguments for swiftc. The returned arguments - include everything except the arguments generation of which would require - adding new files or actions. - """ - - apple_fragment = ctx.fragments.apple - - cpu = apple_fragment.single_arch_cpu - platform = apple_fragment.single_arch_platform - - target_os = apple_fragment.minimum_os_for_platform_type( - platform.platform_type) - target = _swift_target(cpu, platform, target_os) - apple_toolchain = apple_common.apple_toolchain() - - module_name = ctx.attr.module_name or swift_module_name(ctx.label) - - # A list of paths to pass with -F flag. - framework_dirs = depset([ - apple_toolchain.platform_developer_framework_dir(apple_fragment)]) - - # Collect transitive dependecies. - dep_modules = [] - swiftc_defines = ctx.attr.defines - - swift_providers = [x.swift for x in ctx.attr.deps if hasattr(x, "swift")] - objc_providers = [x.objc for x in ctx.attr.deps if hasattr(x, "objc")] - - for swift in swift_providers: - dep_modules += swift.transitive_modules - swiftc_defines += swift.transitive_defines - - objc_includes = depset() # Everything that needs to be included with -I - objc_module_maps = depset() # Module maps for dependent targets - objc_defines = depset() - static_frameworks = depset() - for objc in objc_providers: - objc_includes += objc.include - objc_module_maps += objc.module_map - - static_frameworks += _framework_names(objc.framework_dir) - framework_dirs += _parent_dirs(objc.framework_dir) - framework_dirs += _parent_dirs(objc.dynamic_framework_dir) - - # objc_library#copts is not propagated to its dependencies and so it is not - # collected here. In theory this may lead to un-importable targets (since - # their module cannot be compiled by clang), but did not occur in practice. - objc_defines += objc.define - - srcs_args = [f.path for f in ctx.files.srcs] - - # Include each swift module's parent directory for imports to work. - include_dirs = depset([x.dirname for x in dep_modules]) - - # Include the genfiles root so full-path imports can work for generated protos. - include_dirs += depset([ctx.genfiles_dir.path]) - - 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 swiftc_defines] - - # Disable the LC_LINKER_OPTION load commands for static frameworks automatic - # linking. This is needed to correctly deduplicate static frameworks from also - # being linked into test binaries where it is also linked into the app binary. - autolink_args =_intersperse( - "-Xfrontend", - _intersperse("-disable-autolink-framework", static_frameworks)) - - clang_args = _intersperse( - "-Xcc", - - # Add the current directory to clang's search path. - # This instance of clang is spawned by swiftc to compile module maps and - # is not passed the current directory as a search path by default. - ["-iquote", "."] - - # Pass DEFINE or copt values from objc configuration and rules to clang - + ["-D" + x for x in objc_defines] + ctx.fragments.objc.copts - + _clang_compilation_mode_flags(ctx) - - # Load module maps explicitly instead of letting Clang discover them on - # search paths. This is needed to avoid a case where Clang may load the - # same header both in modular and non-modular contexts, leading to - # duplicate definitions in the same file. - # https://llvm.org/bugs/show_bug.cgi?id=19501 - + ["-fmodule-map-file=%s" % x.path for x in objc_module_maps]) - - args = [ - "-emit-object", - "-module-name", - module_name, - "-target", - target, - "-sdk", - apple_toolchain.sdk_dir(), - "-module-cache-path", - module_cache_path(ctx), - ] - - if ctx.configuration.coverage_enabled: - args.extend(["-profile-generate", "-profile-coverage-mapping"]) - - args.extend(_swift_compilation_mode_flags(ctx)) - args.extend(_swift_bitcode_flags(ctx)) - args.extend(_swift_parsing_flags(ctx)) - args.extend(srcs_args) - args.extend(include_args) - args.extend(framework_args) - args.extend(clang_args) - args.extend(define_args) - args.extend(autolink_args) - args.extend(ctx.fragments.swift.copts()) - args.extend(ctx.attr.copts) - - return args - - -def _swift_library_impl(ctx): - """Implementation for swift_library Skylark rule.""" - print("This file is deprecated and will be removed soon. Please start " + - "using the version from the rules_apple repository " + - "(https://github.com/bazelbuild/rules_apple) instead.") - - _validate_rule_and_deps(ctx) - - # Collect transitive dependecies. - dep_modules = [] - dep_libs = [] - swiftc_defines = ctx.attr.defines - - swift_providers = [x.swift for x in ctx.attr.deps if hasattr(x, "swift")] - objc_providers = [x.objc for x in ctx.attr.deps if hasattr(x, "objc")] - - for swift in swift_providers: - dep_libs += swift.transitive_libs - dep_modules += swift.transitive_modules - swiftc_defines += swift.transitive_defines - - # A unique path for rule's outputs. - objs_outputs_path = label_scoped_path(ctx, "_objs/") - - module_name = ctx.attr.module_name or swift_module_name(ctx.label) - output_lib = ctx.new_file(objs_outputs_path + module_name + ".a") - output_module = ctx.new_file(objs_outputs_path + module_name + ".swiftmodule") - - # These filenames are guaranteed to be unique, no need to scope. - output_header = ctx.new_file(ctx.label.name + "-Swift.h") - swiftc_output_map_file = ctx.new_file(ctx.label.name + ".output_file_map.json") - - swiftc_output_map = struct() # Maps output types to paths. - output_objs = [] # Object file outputs, used in archive action. - swiftc_outputs = [] # Other swiftc outputs that aren't processed further. - - # Check if the user enabled Whole Module Optimization (WMO) - # This is highly experimental and tracked in b/29465250 - has_wmo = ("-wmo" in ctx.attr.copts) or ("-whole-module-optimization" in ctx.attr.copts) - - for source in ctx.files.srcs: - basename = source.basename - output_map_entry = {} - - # Output an object file - obj = ctx.new_file(objs_outputs_path + basename + ".o") - output_objs.append(obj) - output_map_entry["object"] = obj.path - - # Output a partial module file, unless WMO is enabled in which case only - # the final, complete module will be generated. - if not has_wmo: - partial_module = ctx.new_file(objs_outputs_path + basename + - ".partial_swiftmodule") - swiftc_outputs.append(partial_module) - output_map_entry["swiftmodule"] = partial_module.path - - swiftc_output_map += struct(**{source.path: struct(**output_map_entry)}) - - # Write down the intermediate outputs map for this compilation, to be used - # with -output-file-map flag. - # It's a JSON file that maps each source input (.swift) to its outputs - # (.o, .bc, .d, ...) - # Example: - # {'foo.swift': - # {'object': 'foo.o', 'bitcode': 'foo.bc', 'dependencies': 'foo.d'}} - # There's currently no documentation on this option, however all of the keys - # are listed here https://github.com/apple/swift/blob/swift-2.2.1-RELEASE/include/swift/Driver/Types.def - ctx.file_action(output=swiftc_output_map_file, content=swiftc_output_map.to_json()) - - args = _swift_xcrun_args(ctx) + ["swiftc"] + swiftc_args(ctx) - args += [ - "-I" + output_module.dirname, - "-emit-module-path", - output_module.path, - "-emit-objc-header-path", - output_header.path, - "-output-file-map", - swiftc_output_map_file.path, - ] - - if has_wmo: - # WMO has two modes: threaded and not. We want the threaded mode because it - # will use the output map we generate. This leads to a better debug - # experience in lldb and Xcode. - # TODO(b/32571265): 12 has been chosen as the best option for a Mac Pro, - # we should get an interface in Bazel to get core count. - args.extend(["-num-threads", "12"]) - - xcrun_action( - ctx, - inputs=swiftc_inputs(ctx) + [swiftc_output_map_file], - outputs=[output_module, output_header] + output_objs + swiftc_outputs, - mnemonic="SwiftCompile", - arguments=args, - use_default_shell_env=False, - progress_message=("Compiling Swift module %s (%d files)" % - (ctx.label.name, len(ctx.files.srcs)))) - - xcrun_action(ctx, - inputs=output_objs, - outputs=(output_lib,), - mnemonic="SwiftArchive", - arguments=[ - "libtool", "-static", "-o", output_lib.path - ] + [x.path for x in output_objs], - progress_message=("Archiving Swift objects %s" % ctx.label.name)) - - # This tells the linker to write a reference to .swiftmodule as an AST symbol - # in the final binary. - # With dSYM enabled, this results in a __DWARF,__swift_ast section added to - # the dSYM binary, from where LLDB is able deserialize module information. - # Without dSYM, LLDB will follow the AST references, however there is a bug - # where it follows only the first one https://bugs.swift.org/browse/SR-2637 - # This means that dSYM is required for debugging until that is resolved. - extra_linker_args = ["-Xlinker -add_ast_path -Xlinker " + output_module.path] - - objc_provider = apple_common.new_objc_provider( - library=depset([output_lib] + dep_libs), - header=depset([output_header]), - providers=objc_providers, - linkopt=_swift_linkopts(ctx) + extra_linker_args, - link_inputs=depset([output_module]), - uses_swift=True,) - - return struct( - swift=struct( - transitive_libs=[output_lib] + dep_libs, - transitive_modules=[output_module] + dep_modules, - transitive_defines=swiftc_defines), - objc=objc_provider, - files=depset([output_lib, output_module, output_header])) - -SWIFT_LIBRARY_ATTRS = { - "srcs": attr.label_list(allow_files = [".swift"], allow_empty=False), - "deps": attr.label_list(providers=[["swift"], ["objc"]]), - "module_name": attr.string(mandatory=False), - "defines": attr.string_list(mandatory=False, allow_empty=True), - "copts": attr.string_list(mandatory=False, allow_empty=True), - "_xcrunwrapper": attr.label( - executable=True, - cfg="host", - default=Label(XCRUNWRAPPER_LABEL)) -} - -swift_library = rule( - _swift_library_impl, - attrs = SWIFT_LIBRARY_ATTRS, - fragments = ["apple", "objc", "swift"], - output_to_genfiles=True, -) -""" -Builds a Swift module. - -A module is a pair of static library (.a) + module header (.swiftmodule). -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". - copts: A list of flags passed to swiftc command line. - defines: Each VALUE in this attribute is passed as -DVALUE to the compiler for - this and dependent targets. -""" |