Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move symbol stripping test to starlark #246

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions test/binary_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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],
)
25 changes: 23 additions & 2 deletions test/rules/apple_verification_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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 []),
Expand Down Expand Up @@ -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(
Expand Down
43 changes: 0 additions & 43 deletions test/shell/objc_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
17 changes: 12 additions & 5 deletions test/test_data/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions test/test_data/objc_lib_with_unused_symbol.m
Original file line number Diff line number Diff line change
@@ -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);
}
9 changes: 9 additions & 0 deletions test/verify_stripped_symbols.sh
Original file line number Diff line number Diff line change
@@ -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)
9 changes: 9 additions & 0 deletions test/verify_unused_symbol_exists.sh
Original file line number Diff line number Diff line change
@@ -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)