1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
#!/bin/bash
#
# 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.
#
# Tests the examples provided in Bazel
#
# Load test environment
source $(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/test-setup.sh \
|| { echo "test-setup.sh not found!" >&2; exit 1; }
if [ "${PLATFORM}" != "darwin" ]; then
echo "This test suite requires running on OS X" >&2
exit 0
fi
function set_up() {
copy_examples
setup_objc_test_support
# Allow access to //external:xcrunwrapper.
rm WORKSPACE
ln -sv ${workspace_file} WORKSPACE
}
function make_app() {
rm -rf ios
mkdir -p ios
touch ios/dummy.swift
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",
# TODO(b/28723643): This dummy is only here to trigger the
# USES_SWIFT flag on ObjcProvider and should not be necessary.
srcs = ['dummy.swift'],
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}/examples_swift_swift_lib.a \
${swift_lib_pkg}:swift_lib --ios_sdk_version=$IOS_SDK_VERSION
assert_build_output ./bazel-genfiles/${swift_lib_pkg}/examples_swift_swift_lib.swiftmodule \
${swift_lib_pkg}:swift_lib --ios_sdk_version=$IOS_SDK_VERSION
}
function test_build_app() {
make_app
bazel build --verbose_failures --ios_sdk_version=$IOS_SDK_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
touch ios/dummy.swift
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",
# TODO(b/28723643): This dummy is only here to trigger the
# USES_SWIFT flag on ObjcProvider and should not be necessary.
srcs = ['app.m', 'dummy.swift'],
deps = [":SwiftMain"])
EOF
bazel build --verbose_failures --ios_sdk_version=$IOS_SDK_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>
@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'])
EOF
bazel build --verbose_failures --ios_sdk_version=$IOS_SDK_VERSION \
--experimental_objc_enable_module_maps \
//ios:swift_lib >$TEST_log 2>&1 || fail "should build"
}
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
touch ios/dummy.swift
cat >ios/main.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 = ["dummy.swift"],
deps = [":swift_lib"])
swift_library(name = "swift_lib",
srcs = ["main.swift"],
deps = [":dylib"])
objc_framework(name = "dylib",
framework_imports = glob(["BlazeFramework.framework/**"]),
is_dynamic = 1)
EOF
bazel build --verbose_failures --ios_sdk_version=$IOS_SDK_VERSION \
--ios_minimum_os=8.0 \
//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"
|