### Minimal iOS Application Example Source: https://github.com/bazelbuild/rules_apple/blob/main/README.md A basic example demonstrating how to define a Swift library and an iOS application using rules_apple and rules_swift. Ensure you have the necessary load statements for rules_apple and rules_swift. ```python load("@rules_apple//apple:ios.bzl", "ios_application") load("@rules_swift//swift:swift.bzl", "swift_library") swift_library( name = "MyLibrary", srcs = glob(["**/*.swift"]), ) ios_application( name = "App", bundle_id = "com.example.app", families = ["iphone"], minimum_os_version = "26.0", deps = [":MyLibrary"], ) ``` -------------------------------- ### Setup Provisioning Profile Repository Extension Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-apple.md Configure the provisioning_profile_repository_extension to use fallback profiles. This is typically done at the beginning of your BUILD file. ```python provisioning_profile_repository_extension = use_extension("@rules_apple//apple:apple.bzl", "provisioning_profile_repository_extension") provisioning_profile_repository_extension.setup(fallback_profiles) ``` -------------------------------- ### ios_application with ios_extension Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/frameworks.md An example demonstrating an ios_application with an ios_extension. Both statically link shared libraries, which can increase binary size. ```build ios_application( name = "MyiOSApp", deps = [":MyAppLib"], ) ios_extension( name = "MyiOSExtension", deps = [":MyExtensionLib"], ) objc_library( name = "MyAppLib", srcs = [...], deps = [":LargeSharedLib"], ) objc_library( name = "MyExtensionLib", srcs = [...], deps = [":LargeSharedLib"], ) objc_library( name = "LargeSharedLib", srcs = [...], ) ``` -------------------------------- ### Basic ios_application Target Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/frameworks.md A simple example of an ios_application target. This setup statically links shared libraries into the application and extension binaries. ```build ios_application( name = "MyiOSApp", deps = [":MyAppLib"], ) objc_library( name = "MyAppLib", srcs = [...], deps = [":LargeSharedLib"], ) objc_library( name = "LargeSharedLib", srcs = [...], ) ``` -------------------------------- ### Basic Application with Library Dependency Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/resources.md This setup demonstrates a simple application depending on a library that includes a resource. The resource is packaged directly into the application bundle. ```build ios_application( name = "MyApplication", deps = [":MyApplicationLibrary"], ) objc_library( name = "MyApplicationLibrary", srcs = [...], deps = [":MySharedLibrary"], ) objc_library( name = "MySharedLibrary", srcs = [...], data = ["MySharedResource.txt"], ) ``` -------------------------------- ### Typical tvOS Build Test Usage Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-tvos.md Example of how to configure and use the tvos_build_test rule with a specific name, minimum OS version, and target library. ```starlark tvos_build_test( name = "my_build_test", minimum_os_version = "12.0", targets = [ "//some/package:my_library", ], ) ``` -------------------------------- ### Setup provisioning profile repository with fallbacks Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-apple.md Configure an external repository for discovering local provisioning profiles using `provisioning_profile_repository`. This is useful for setting up fallback profiles that are used if a newer version of a desired profile is not found locally. ```bzl provisioning_profile_repository = use_extension("@rules_apple//apple:apple.bzl", "provisioning_profile_repository_extension") provisioning_profile_repository.setup( fallback_profiles = "//path/to/some:filegroup", # Profiles to use if one isn't found locally ) ``` -------------------------------- ### Create a header map Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-header_map.md Example of creating a header map for a library. This generates a binary headermap file that clang can use. ```bzl header_map( name = "MyLib.hmap", hdrs = ["headers/MyLib.h"], ) ``` -------------------------------- ### ios_application with ios_framework Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/frameworks.md An example showing how to use ios_framework to extract shared libraries from application and extension binaries, reducing binary size. The dependency graph remains unchanged. ```build ios_application( name = "MyiOSApp", frameworks = [":MyiOSFramework"], deps = [":MyAppLib"], ) ios_extension( name = "MyiOSExtension", frameworks = [":MyiOSFramework"], deps = [":MyExtensionLib"], ) ios_framework( name = "MyiOSFramework", deps = [":LargeSharedLib"], ) objc_library( name = "MyAppLib", srcs = [...], deps = [":LargeSharedLib"], ) objc_library( name = "MyExtensionLib", srcs = [...], deps = [":LargeSharedLib"], ) objc_library( name = "LargeSharedLib", srcs = [...], ) ``` -------------------------------- ### Example Output for apple_core_data_model Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-resources.md Illustrates the expected 'outs' attribute format for the apple_core_data_model rule when generating Swift files from a Core Data model. ```python outs = { "Taxonomy": [ "taxonomy+CoreDataModel.swift", "Animal+CoreDataProperties.swift", ], } ``` -------------------------------- ### Typical Usage of macos_build_test Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-macos.md Example of how to use the `macos_build_test` rule in a Bazel BUILD file. Specify the name, minimum OS version, and the targets to be built. ```starlark macos_build_test( name = "my_build_test", minimum_os_version = "10.14", targets = [ "//some/package:my_library", ], ) ``` -------------------------------- ### Use header map in objc_library Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-header_map.md Example of how to integrate a generated header map into an `objc_library` target. This allows for consistent header import paths. ```bzl objc_library( name = "MyLib", module_name = "MyLib", srcs = ["MyLib.c"], hdrs = ["headers/MyLib.h"], deps = [":MyLib.hmap"], copts = ["-I."], includes = ["MyLib.hmap"] ) ``` -------------------------------- ### Typical ios_build_test Usage Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-ios.md Example of how to use the ios_build_test rule to define a build test for an iOS library. ```starlark ios_build_test( name = "my_build_test", minimum_os_version = "12.0", targets = [ "//some/package:my_library", ], ) ``` -------------------------------- ### Example Usage of xctrunner Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-xctrunner.md Demonstrates how to use the `xctrunner` rule to package an iOS UI test target into a XCTRunner.app. This is useful for creating executable test bundles. ```starlark load("//apple:xctrunner.bzl", "xctrunner") ios_ui_test( name = "HelloWorldSwiftUITests", minimum_os_version = "15.0", runner = "@rules_apple//apple/testing/default_runner:ios_xctestrun_ordered_runner", test_host = ":HelloWorldSwift", deps = [":UITests"], ) xctrunner( name = "HelloWorldSwiftXCTRunner", tests = [":HelloWorldSwiftUITests"], testonly = True, ) ``` -------------------------------- ### Load new Apple rules in BUILD files Source: https://github.com/bazelbuild/rules_apple/wiki/Migrating-from-the-native-rules Explicitly load the new rules from the rules_apple repository in your BUILD files. This example shows loading rules for iOS and watchOS applications and extensions. ```python load("@build_bazel_rules_apple//apple:ios.bzl", "ios_application", "ios_extension") load("@build_bazel_rules_apple//apple:watchos.bzl", "watchos_application", "watchos_extension") ``` -------------------------------- ### Instantiate MacosXPCServiceBundleInfo Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/providers.md Use this provider to mark a target as a macOS XPC Service bundle. It serves as a marker for dependency requirements. ```python load("@rules_apple//apple:providers.bzl", "MacosXPCServiceBundleInfo") MacosXPCServiceBundleInfo(*kwargs) ``` -------------------------------- ### Instantiate TvosApplicationBundleInfo Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/providers.md Use this provider to mark a target as a tvOS application bundle. It serves as a marker for dependency requirements. ```python load("@rules_apple//apple:providers.bzl", "TvosApplicationBundleInfo") TvosApplicationBundleInfo(*kwargs) ``` -------------------------------- ### Create Workspace Directory and Navigate Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/tutorials/ios-app.md Creates a new directory for the Bazel workspace and changes the current directory to it. This is the first step in setting up a new Bazel project. ```bash mkdir rules-apple-example cd rules-apple-example ``` -------------------------------- ### Instantiate TvosExtensionBundleInfo Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/providers.md Use this provider to mark a target as a tvOS application extension bundle. It serves as a marker for dependency requirements. ```python load("@rules_apple//apple:providers.bzl", "TvosExtensionBundleInfo") TvosExtensionBundleInfo(*kwargs) ``` -------------------------------- ### Instantiate TvosFrameworkBundleInfo Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/providers.md Use this provider to mark a target as a tvOS dynamic framework bundle. It serves as a marker for dependency requirements. ```python load("@rules_apple//apple:providers.bzl", "TvosFrameworkBundleInfo") TvosFrameworkBundleInfo(*kwargs) ``` -------------------------------- ### Get Parent Directory of Resource Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-resources.md Calculates the package-relative path to the parent directory of a given resource. This is useful for organizing and referencing resource files within the runfiles. ```python load("@rules_apple//apple:resources.bzl", "resources_common") resources_common.runfiles_resources_parent_dir(*, resource) ``` -------------------------------- ### Get Bundle Relative Parent Directory Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-resources.md Calculates the bundle-relative path for a resource. It finds the first directory with a specific extension and returns its path within the bundle. ```Starlark load("@rules_apple//apple:resources.bzl", "resources_common") resources_common.bundle_relative_parent_dir(resource, extension) ``` -------------------------------- ### Basic local_provisioning_profile declaration Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-apple.md A minimal declaration for `local_provisioning_profile` using default settings. ```starlark load("@rules_apple//apple:apple.bzl", "local_provisioning_profile") local_provisioning_profile(name = "app_debug_profile", profile_name = "Development App", team_id = "abc123", ) ``` -------------------------------- ### Load and use apple_universal_binary rule Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-apple.md Demonstrates how to load and use the apple_universal_binary rule in a Bazel BUILD file. This rule is used to create multi-architecture binaries. ```bazel load("@rules_apple//apple:apple.bzl", "apple_universal_binary") apple_universal_binary( name = "my_universal_binary", binary = ":my_regular_binary", platform_type = "ios", minimum_os_version = "13.0", ) ``` -------------------------------- ### Application with Library and Framework Dependencies on Same Resource Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/resources.md Illustrates a scenario where both the application's direct library and a dynamic framework have dependencies on the same resource. The resource is packaged in both the application bundle and the framework bundle. ```build ios_application( name = "MyApplication", ..., frameworks = [":MyFramework"], deps = [":MyLibrary"], ) ios_framework( name = "MyFramework", ..., deps = [":MySharedLibrary"], ) objc_library( name = "MyApplicationLibrary", srcs = [...], deps = [":MySharedLibrary"], data = ["MySharedResource.txt"], ) objc_library( name = "MySharedLibrary", srcs = [...], data = ["MySharedResource.txt"], ) ``` -------------------------------- ### Get Populated Resource Fields Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-resources.md Returns a list of field names from a provider's resource buckets that are not empty. Use to identify which resource fields are actively being used. ```bzl load("@rules_apple//apple:resources.bzl", "resources_common") resources_common.populated_resource_fields(provider) ``` -------------------------------- ### Using entitlements_validation_mode in ios_application Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/types.md Demonstrates how to set the entitlements validation mode for an `ios_application` rule. This example uses the `error` mode, which will raise errors for any entitlement issues found during the build. ```python load( "@rules_apple//apple:common.bzl", "entitlements_validation_mode", ) load( "@rules_apple//apple:ios.bzl", "ios_application", ) ios_application( name = "MyApp", entitlements = "MyApp.entitlements", entitlements_validation = entitlements_validation_mode.error, # other attributes... ) ``` -------------------------------- ### Typical Usage of visionos_build_test Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-visionos.md Demonstrates a common way to use the visionos_build_test rule, specifying the name, minimum OS version, and the library targets to build. ```Starlark visionos_build_test( name = "my_build_test", minimum_os_version = "1.0", targets = [ "//some/package:my_library", ], ) ``` -------------------------------- ### Instantiate MacosStaticFrameworkBundleInfo Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/providers.md Use this provider to mark a target as an macOS static framework bundle. It serves as a marker for dependency requirements. ```python load("@rules_apple//apple:providers.bzl", "MacosStaticFrameworkBundleInfo") MacosStaticFrameworkBundleInfo() ``` -------------------------------- ### apple_order_file Rule Example Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-linker.md Use this rule to inject order files into Apple linker lines. It concatenates and deduplicates order files before supplying linker flags. The rule short-circuits in non-optimized compilations, as order file generation is primarily for release/deployment builds to enhance runtime locality. Set `stats = True` to log linker usage information. ```starlark load("@rules_apple//apple:linker.bzl", "apple_order_file") apple_order_file( name = "app_order_file", deps = [ "my_file.order", "my_second_order_file.order", ], ) ios_application( name = "app", deps = [":app_order_file"], ) ``` -------------------------------- ### Define Static Framework Import Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/frameworks.md Use `apple_static_framework_import` to wrap pre-built static frameworks. The `framework_imports` attribute takes a glob of the framework's files. ```bazel apple_static_framework_import( name = "MyPrebuiltStaticFramework", framework_imports = glob(["MyPrebuiltStaticFramework.framework/**"]), ) ``` -------------------------------- ### Initialize AppleBinaryInfo Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/providers.md Initializes the AppleBinaryInfo provider. This provider propagates general information about an Apple binary target, such as the binary file, Info.plist, and product type. ```python load("@rules_apple//apple:providers.bzl", "AppleBinaryInfo") AppleBinaryInfo(*kwargs) ``` -------------------------------- ### Create SwiftUI App Entry Point Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/tutorials/ios-app.md Defines the main entry point for a SwiftUI iOS application. This code creates a basic window group displaying 'Hello from Bazel!'. ```swift import SwiftUI @main struct BazelApp: App { var body: some Scene { WindowGroup { Text("Hello from Bazel!") } } } ``` -------------------------------- ### Typical Usage of watchos_build_test Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/rules-watchos.md Demonstrates how to use the watchos_build_test rule to set up a build test for a watchOS library. ```starlark watchos_build_test( name = "my_build_test", minimum_os_version = "6.0", targets = [ "//some/package:my_library", ], ) ``` -------------------------------- ### Initialize AppleBinaryInfoplistInfo Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/providers.md Initializes the AppleBinaryInfoplistInfo provider. This provider is used to pass information about the Info.plist that was linked into an Apple binary target. ```python load("@rules_apple//apple:providers.bzl", "AppleBinaryInfoplistInfo") AppleBinaryInfoplistInfo(infoplist) ``` -------------------------------- ### Instantiate MacosXcTestBundleInfo Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/providers.md Use this provider to mark a target as a macOS .xctest bundle. It serves as a marker for dependency requirements. ```python load("@rules_apple//apple:providers.bzl", "MacosXcTestBundleInfo") MacosXcTestBundleInfo(*kwargs) ``` -------------------------------- ### apple_provider.make_apple_test_runner_info Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/providers.md Creates a new instance of the AppleTestRunnerInfo provider. It accepts a set of keyword arguments that must match the fields of AppleTestRunnerInfo. ```APIDOC ## apple_provider.make_apple_test_runner_info ### Description Creates a new instance of the `AppleTestRunnerInfo` provider. ### Parameters #### Path Parameters - **kwargs** (dict) - Required - A set of keyword arguments expected to match the fields of `AppleTestRunnerInfo`. See the documentation for `AppleTestRunnerInfo` for what these must be. ``` -------------------------------- ### Generate Linkmap for Binary Size Analysis Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/common_info.md Add `--objc_generate_linkmap` to your `bazel build` command to generate linkmaps. This helps in analyzing the contribution of dependencies to the final binary size. ```shell bazel build --objc_generate_linkmap //your/target ``` -------------------------------- ### Build iOS App Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/tutorials/ios-app.md Command to build the iOS application and package it into an uncompressed .ipa file targeting the Simulator. ```shell bazel build //:iOSApp ``` -------------------------------- ### TvosStaticFrameworkBundleInfo Provider Source: https://github.com/bazelbuild/rules_apple/blob/main/doc/providers.md Use this provider to mark a target as a tvOS static framework bundle. It acts as a marker and does not contain fields. ```bzl load("@rules_apple//apple:providers.bzl", "TvosStaticFrameworkBundleInfo") TvosStaticFrameworkBundleInfo(*kwargs) ```