From 68a80e1ea5553ef4afea6a66e6c22ba413e73ccf Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Tue, 11 Jul 2023 12:05:33 -0700 Subject: [PATCH] Add initial visionOS support Depends on https://github.com/bazelbuild/bazel/pull/18905 and https://github.com/bazelbuild/platforms/pull/71 --- README.md | 2 +- configs/platforms.bzl | 15 +++++++++++++++ crosstool/cc_toolchain_config.bzl | 18 ++++++++++++++++-- lib/BUILD | 1 + test/binary_tests.bzl | 30 ++++++++++++++++++++++++++++++ test/shell/verify_binary.sh | 4 ++++ test/test_data/BUILD | 27 +++++++++++++++++++++++++++ test/test_data/lib.cc | 3 +++ test/test_data/lib.m | 3 +++ test/test_data/main.m | 3 +++ 10 files changed, 103 insertions(+), 3 deletions(-) create mode 100644 test/test_data/lib.cc create mode 100644 test/test_data/lib.m create mode 100644 test/test_data/main.m diff --git a/README.md b/README.md index 27a6818..7689cf4 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Apple related [platforms](platforms/BUILD) and [constraints](constraints/BUILD) definitions, and small helper functions for rules authors targeting Apple platforms. -If you want to build iOS, tvOS, watchOS, or macOS apps, use +If you want to build iOS, tvOS, visionOS, watchOS, or macOS apps, use [`rules_apple`][rules_apple]. If you want to build Swift use diff --git a/configs/platforms.bzl b/configs/platforms.bzl index 262f751..fba1f87 100644 --- a/configs/platforms.bzl +++ b/configs/platforms.bzl @@ -61,6 +61,21 @@ APPLE_PLATFORMS_CONSTRAINTS = { "@platforms//cpu:arm64", "@build_bazel_apple_support//constraints:simulator", ], + "visionos_arm64": [ + "@platforms//os:visionos", + "@platforms//cpu:arm64", + "@build_bazel_apple_support//constraints:device", + ], + "visionos_sim_arm64": [ + "@platforms//os:visionos", + "@platforms//cpu:arm64", + "@build_bazel_apple_support//constraints:simulator", + ], + "visionos_x86_64": [ + "@platforms//os:visionos", + "@platforms//cpu:x86_64", + "@build_bazel_apple_support//constraints:simulator", + ], "watchos_arm64": [ "@platforms//os:watchos", "@platforms//cpu:arm64", diff --git a/crosstool/cc_toolchain_config.bzl b/crosstool/cc_toolchain_config.bzl index d01c81d..ae635b9 100644 --- a/crosstool/cc_toolchain_config.bzl +++ b/crosstool/cc_toolchain_config.bzl @@ -55,6 +55,8 @@ def _impl(ctx): target_system_name = "arm64-apple-ios{}".format(target_os_version) elif (ctx.attr.cpu == "tvos_arm64"): target_system_name = "arm64-apple-tvos{}".format(target_os_version) + elif (ctx.attr.cpu == "visionos_arm64"): + target_system_name = "arm64-apple-xros{}".format(target_os_version) elif (ctx.attr.cpu == "watchos_arm64_32"): target_system_name = "arm64_32-apple-watchos{}".format(target_os_version) elif (ctx.attr.cpu == "ios_arm64e"): @@ -73,6 +75,8 @@ def _impl(ctx): target_system_name = "arm64-apple-ios{}-simulator".format(target_os_version) elif (ctx.attr.cpu == "tvos_sim_arm64"): target_system_name = "arm64-apple-tvos{}-simulator".format(target_os_version) + elif (ctx.attr.cpu == "visionos_sim_arm64"): + target_system_name = "arm64-apple-xros{}-simulator".format(target_os_version) elif (ctx.attr.cpu == "watchos_arm64"): target_system_name = "arm64-apple-watchos{}-simulator".format(target_os_version) elif (ctx.attr.cpu == "darwin_x86_64"): @@ -83,6 +87,8 @@ def _impl(ctx): target_system_name = "arm64e-apple-macosx{}".format(target_os_version) elif (ctx.attr.cpu == "tvos_x86_64"): target_system_name = "x86_64-apple-tvos{}-simulator".format(target_os_version) + elif (ctx.attr.cpu == "visionos_x86_64"): + target_system_name = "x86_64-apple-xros{}-simulator".format(target_os_version) elif (ctx.attr.cpu == "watchos_x86_64"): target_system_name = "x86_64-apple-watchos{}-simulator".format(target_os_version) else: @@ -101,7 +107,7 @@ def _impl(ctx): abi_version = "local" arch = ctx.attr.cpu.split("_", 1)[-1] - if ctx.attr.cpu in ["ios_sim_arm64", "tvos_sim_arm64", "watchos_arm64"]: + if ctx.attr.cpu in ["ios_sim_arm64", "tvos_sim_arm64", "visionos_sim_arm64", "watchos_arm64"]: arch = "arm64" all_link_actions = [ @@ -675,8 +681,11 @@ def _impl(ctx): ctx.attr.cpu == "ios_arm64e" or ctx.attr.cpu == "ios_armv7" or ctx.attr.cpu == "ios_i386" or - ctx.attr.cpu == "ios_x86_64" or ctx.attr.cpu == "ios_sim_arm64" or + ctx.attr.cpu == "ios_x86_64" or + ctx.attr.cpu == "visionos_arm64" or + ctx.attr.cpu == "visionos_sim_arm64" or + ctx.attr.cpu == "visionos_x86_64" or ctx.attr.cpu == "watchos_arm64_32" or ctx.attr.cpu == "watchos_armv7k" or ctx.attr.cpu == "watchos_i386" or @@ -864,6 +873,8 @@ def _impl(ctx): ctx.attr.cpu == "ios_sim_arm64" or ctx.attr.cpu == "tvos_x86_64" or ctx.attr.cpu == "tvos_sim_arm64" or + ctx.attr.cpu == "visionos_sim_arm64" or + ctx.attr.cpu == "visionos_x86_64" or ctx.attr.cpu == "watchos_i386" or ctx.attr.cpu == "watchos_x86_64" or ctx.attr.cpu == "watchos_arm64"): @@ -1413,6 +1424,9 @@ def _impl(ctx): ctx.attr.cpu == "tvos_arm64" or ctx.attr.cpu == "tvos_x86_64" or ctx.attr.cpu == "tvos_sim_arm64" or + ctx.attr.cpu == "visionos_arm64" or + ctx.attr.cpu == "visionos_x86_64" or + ctx.attr.cpu == "visionos_sim_arm64" or ctx.attr.cpu == "watchos_arm64_32" or ctx.attr.cpu == "watchos_armv7k" or ctx.attr.cpu == "watchos_i386" or diff --git a/lib/BUILD b/lib/BUILD index 1297493..3a8767a 100644 --- a/lib/BUILD +++ b/lib/BUILD @@ -15,6 +15,7 @@ objc_library( "@platforms//os:ios": [], "@platforms//os:macos": [], "@platforms//os:tvos": [], + "@platforms//os:visionos": [], "@platforms//os:watchos": [], "//conditions:default": ["@platforms//:incompatible"], }), diff --git a/test/binary_tests.bzl b/test/binary_tests.bzl index d32e46e..936f53c 100644 --- a/test/binary_tests.bzl +++ b/test/binary_tests.bzl @@ -15,3 +15,33 @@ def binary_test_suite(name): verifier_script = "//test/shell:verify_binary.sh", target_under_test = "//test/test_data:apple_binary", ) + + apple_verification_test( + name = "{}_visionos_device_test".format(name), + tags = [name], + build_type = "device", + cpus = {"visionos_cpus": "arm64"}, + expected_platform_type = "visionos", + verifier_script = "//test/shell:verify_binary.sh", + target_under_test = "//test/test_data:visionos_binary", + ) + + apple_verification_test( + name = "{}_visionos_arm64_simulator_test".format(name), + tags = [name], + build_type = "simulator", + cpus = {"visionos_cpus": "sim_arm64"}, + expected_platform_type = "visionos", + verifier_script = "//test/shell:verify_binary.sh", + target_under_test = "//test/test_data:visionos_binary", + ) + + apple_verification_test( + name = "{}_visionos_x86_64_simulator_test".format(name), + tags = [name], + build_type = "simulator", + cpus = {"visionos_cpus": "x86_64"}, + expected_platform_type = "visionos", + verifier_script = "//test/shell:verify_binary.sh", + target_under_test = "//test/test_data:visionos_binary", + ) diff --git a/test/shell/verify_binary.sh b/test/shell/verify_binary.sh index 44943ad..1da7040 100755 --- a/test/shell/verify_binary.sh +++ b/test/shell/verify_binary.sh @@ -8,6 +8,10 @@ if [[ "$PLATFORM_TYPE" == "ios" && "$BUILD_TYPE" == "device" ]]; then expected_platform="IPHONEOS" elif [[ "$PLATFORM_TYPE" == "ios" && "$BUILD_TYPE" == "simulator" ]]; then expected_platform="IPHONESIMULATOR" +elif [[ "$PLATFORM_TYPE" == "visionos" && "$BUILD_TYPE" == "device" ]]; then + expected_platform="XROS" +elif [[ "$PLATFORM_TYPE" == "visionos" && "$BUILD_TYPE" == "simulator" ]]; then + expected_platform="XROSSIMULATOR" fi otool_output=$(otool -lv "$binary") diff --git a/test/test_data/BUILD b/test/test_data/BUILD index ad59a4f..d6952bf 100644 --- a/test/test_data/BUILD +++ b/test/test_data/BUILD @@ -38,3 +38,30 @@ starlark_apple_binary( tags = TARGETS_UNDER_TEST_TAGS, deps = [":cc_main"], ) + +cc_library( + name = "cc_lib", + srcs = ["lib.cc"], + tags = TARGETS_UNDER_TEST_TAGS, +) + +objc_library( + name = "objc_lib", + srcs = ["lib.m"], + tags = TARGETS_UNDER_TEST_TAGS, + deps = ["cc_lib"], +) + +objc_library( + name = "objc_main", + srcs = ["main.m"], + tags = TARGETS_UNDER_TEST_TAGS, + deps = ["objc_lib"], +) + +starlark_apple_binary( + name = "visionos_binary", + platform_type = "visionos", + tags = TARGETS_UNDER_TEST_TAGS, + deps = [":objc_main"], +) diff --git a/test/test_data/lib.cc b/test/test_data/lib.cc new file mode 100644 index 0000000..55c772a --- /dev/null +++ b/test/test_data/lib.cc @@ -0,0 +1,3 @@ +int cc_function() { + return 2; +} diff --git a/test/test_data/lib.m b/test/test_data/lib.m new file mode 100644 index 0000000..2f11078 --- /dev/null +++ b/test/test_data/lib.m @@ -0,0 +1,3 @@ +int objc_function() { + return 1; +} diff --git a/test/test_data/main.m b/test/test_data/main.m new file mode 100644 index 0000000..9238e52 --- /dev/null +++ b/test/test_data/main.m @@ -0,0 +1,3 @@ +int main() { + return 42; +}