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,15 +117,32 @@ 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 = """ 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( 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 < -/* 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 <"$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 + +// 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)