summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKeith Smiley <keithbsmiley@gmail.com>2023-08-21 14:47:16 -0700
committerGitHub <noreply@github.com>2023-08-21 16:47:16 -0500
commitf72056a932df112f2b262f8a2bd326cc340f2499 (patch)
tree8d0a57698ac526502bb3586df6d6ebcc66ff8693
parent48a8ff8fea94a5057409bdf5be537945c241f361 (diff)
downloadbazelbuild-apple_support-f72056a932df112f2b262f8a2bd326cc340f2499.tar.gz
Move symbol stripping test to starlark (#246)
This also verifies not passing the required flags keeps the symbols just in case we change that default accidentally
-rw-r--r--test/binary_tests.bzl28
-rw-r--r--test/rules/apple_verification_test.bzl25
-rwxr-xr-xtest/shell/objc_test.sh43
-rw-r--r--test/test_data/BUILD17
-rw-r--r--test/test_data/objc_lib_with_unused_symbol.m11
-rwxr-xr-xtest/verify_stripped_symbols.sh9
-rwxr-xr-xtest/verify_unused_symbol_exists.sh9
7 files changed, 92 insertions, 50 deletions
diff --git a/test/binary_tests.bzl b/test/binary_tests.bzl
index 87b6f79..dd358d7 100644
--- a/test/binary_tests.bzl
+++ b/test/binary_tests.bzl
@@ -6,6 +6,12 @@ load(
)
def binary_test_suite(name):
+ """Test various aspects of binary generation
+
+ Args:
+ name: The prefix of each test name
+ """
+
apple_verification_test(
name = "{}_macos_binary_test".format(name),
tags = [name],
@@ -45,3 +51,25 @@ def binary_test_suite(name):
verifier_script = "//test/shell:verify_binary.sh",
target_under_test = "//test/test_data:visionos_binary",
)
+
+ apple_verification_test(
+ name = "{}_unused_symbol_is_kept_by_default".format(name),
+ build_type = "simulator",
+ cpus = {"ios_multi_cpus": "x86_64"},
+ compilation_mode = "fastbuild",
+ objc_enable_binary_stripping = False,
+ verifier_script = "//test:verify_unused_symbol_exists.sh",
+ target_under_test = "//test/test_data:ios_app_with_unused_symbol",
+ tags = [name],
+ )
+
+ apple_verification_test(
+ name = "{}_unused_symbol_is_stripped".format(name),
+ build_type = "simulator",
+ cpus = {"ios_multi_cpus": "x86_64"},
+ compilation_mode = "opt",
+ objc_enable_binary_stripping = True,
+ verifier_script = "//test:verify_stripped_symbols.sh",
+ target_under_test = "//test/test_data:ios_app_with_unused_symbol",
+ tags = [name],
+ )
diff --git a/test/rules/apple_verification_test.bzl b/test/rules/apple_verification_test.bzl
index b34456b..0108f44 100644
--- a/test/rules/apple_verification_test.bzl
+++ b/test/rules/apple_verification_test.bzl
@@ -20,9 +20,11 @@ _supports_visionos = hasattr(apple_common.platform_type, "visionos")
def _transition_impl(_, attr):
output_dictionary = {
+ "//command_line_option:compilation_mode": attr.compilation_mode,
"//command_line_option:cpu": "darwin_x86_64",
"//command_line_option:ios_signing_cert_name": "-",
"//command_line_option:macos_cpus": "x86_64",
+ "//command_line_option:objc_enable_binary_stripping": attr.objc_enable_binary_stripping,
}
if attr.build_type == "simulator":
output_dictionary.update({
@@ -54,10 +56,12 @@ _transition = transition(
implementation = _transition_impl,
inputs = [],
outputs = [
+ "//command_line_option:compilation_mode",
"//command_line_option:cpu",
"//command_line_option:ios_multi_cpus",
"//command_line_option:ios_signing_cert_name",
"//command_line_option:macos_cpus",
+ "//command_line_option:objc_enable_binary_stripping",
"//command_line_option:tvos_cpus",
"//command_line_option:watchos_cpus",
] + (["//command_line_option:visionos_cpus"] if _supports_visionos else []),
@@ -113,10 +117,13 @@ apple_verification_test = rule(
Type of build for the target under test. Possible values are `simulator` or `device`.
""",
),
- "expected_platform_type": attr.string(
+ "compilation_mode": attr.string(
+ values = ["fastbuild", "opt", "dbg"],
doc = """
-The apple_platform_type the binary should have been built for.
+Possible values are `fastbuild`, `dbg` or `opt`. Defaults to `fastbuild`.
+https://docs.bazel.build/versions/master/user-manual.html#flag--compilation_mode
""",
+ default = "fastbuild",
),
"cpus": attr.string_dict(
doc = """
@@ -124,6 +131,20 @@ Dictionary of command line options cpu flags and the list of
cpu's to use for test under target (e.g. {'ios_multi_cpus': ['arm64', 'x86_64']})
""",
),
+ "expected_platform_type": attr.string(
+ default = "",
+ doc = """
+The apple_platform_type the binary should have been built for.
+""",
+ ),
+ "objc_enable_binary_stripping": attr.bool(
+ default = False,
+ doc = """
+Whether to perform symbol and dead-code strippings on linked binaries. Binary
+strippings will be performed if both this flag and --compilation_mode=opt are
+specified.
+""",
+ ),
"target_under_test": attr.label(
mandatory = True,
doc = "The binary being verified.",
diff --git a/test/shell/objc_test.sh b/test/shell/objc_test.sh
index ba95dec..3f00f71 100755
--- a/test/shell/objc_test.sh
+++ b/test/shell/objc_test.sh
@@ -95,47 +95,4 @@ EOF
fail "Timestamp of contents of archive file should be zero"
}
-function test_strip_symbols() {
- setup_objc_test_support
-
- rm -rf ios
- mkdir -p ios
-
- cat >ios/main.m <<EOF
-#import <UIKit/UIKit.h>
-/* function declaration */
-int addOne(int num);
-int addOne(int num) {
- return num + 1;
-}
- int main(int argc, char *argv[]) {
- NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
- int retVal = UIApplicationMain(argc, argv, nil, nil);
- [pool release];
- return retVal;
-}
-EOF
-
- cat >ios/BUILD <<EOF
-load("@build_bazel_apple_support//test:starlark_apple_binary.bzl", "starlark_apple_binary")
-starlark_apple_binary(name = 'app',
- deps = [':main'],
- minimum_os_version = '13.0',
- platform_type = 'ios')
-objc_library(name = 'main',
- non_arc_srcs = ['main.m'])
-EOF
-
- bazel build --verbose_failures \
- --noincompatible_enable_cc_toolchain_resolution \
- --apple_platform_type=ios \
- --objc_enable_binary_stripping=true \
- --compilation_mode=opt \
- //ios:app >"$TEST_log" 2>&1 || fail "should pass"
- ls bazel-out/*/bin/ios/app_lipobin \
- || fail "should generate lipobin (stripped binary)"
- ! nm bazel-out/*/bin/ios/app_lipobin | grep addOne \
- || fail "should fail to find symbol addOne"
-}
-
run_suite "objc/ios test suite"
diff --git a/test/test_data/BUILD b/test/test_data/BUILD
index 8221a08..81f8e23 100644
--- a/test/test_data/BUILD
+++ b/test/test_data/BUILD
@@ -60,11 +60,18 @@ objc_library(
deps = ["objc_lib"],
)
-config_setting(
- name = "compiler_gcc",
- flag_values = {
- "@bazel_tools//tools/cpp:compiler": "gcc",
- },
+objc_library(
+ name = "objc_lib_with_unused_symbol",
+ srcs = ["objc_lib_with_unused_symbol.m"],
+ tags = TARGETS_UNDER_TEST_TAGS,
+)
+
+starlark_apple_binary(
+ name = "ios_app_with_unused_symbol",
+ minimum_os_version = "13.0",
+ platform_type = "ios",
+ tags = TARGETS_UNDER_TEST_TAGS,
+ deps = [":objc_lib_with_unused_symbol"],
)
config_setting(
diff --git a/test/test_data/objc_lib_with_unused_symbol.m b/test/test_data/objc_lib_with_unused_symbol.m
new file mode 100644
index 0000000..c91cad8
--- /dev/null
+++ b/test/test_data/objc_lib_with_unused_symbol.m
@@ -0,0 +1,11 @@
+#import <UIKit/UIKit.h>
+
+// This is untentionally unused
+int addOne(int num);
+int addOne(int num) {
+ return num + 1;
+}
+
+int main(int argc, char *argv[]) {
+ return UIApplicationMain(argc, argv, nil, nil);
+}
diff --git a/test/verify_stripped_symbols.sh b/test/verify_stripped_symbols.sh
new file mode 100755
index 0000000..2049535
--- /dev/null
+++ b/test/verify_stripped_symbols.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+set -euo pipefail
+set -x
+
+readonly binary="%{binary}s"
+
+! nm "$binary" | grep addOne \
+ || (echo "should fail to find symbol addOne" >&2 && exit 1)
diff --git a/test/verify_unused_symbol_exists.sh b/test/verify_unused_symbol_exists.sh
new file mode 100755
index 0000000..92928d2
--- /dev/null
+++ b/test/verify_unused_symbol_exists.sh
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+set -euo pipefail
+set -x
+
+readonly binary="%{binary}s"
+
+nm "$binary" | grep addOne \
+ || (echo "should find symbol addOne" >&2 && exit 1)