.NET for Apple Platforms

repository·main·Indexed 25 days ago

https://github.com/dotnet/macios

SDKs for building native applications for iOS, Mac Catalyst, macOS, and tvOS using C# and the .NET Base Class Library. These SDKs are a core component of .NET MAUI but can be used for standalone native development. The repository includes the native runtime components for Xamarin.iOS and Xamarin.Mac, as well as various build scripts for managing dependencies via darc, generating framework constants, and configuring .NET workloads.

Tokens
76.6K
Snippets
173
Records
447
Agent score
81%

What's inside dotnet-macios

  1. Overview of the Native Runtime

    main
    The runtime directory contains the native runtime components for Xamarin.iOS and Xamarin.Mac. It provides the necessary glue between the native runtime and the managed runtime, including generated code for delegates and abstraction layers for the Mono runtime.
  2. Overview of generate-frameworks-constants script

    main
    The generate-frameworks-constants script is a build-time utility used to generate a C# file within the ObjCRuntime namespace. This generated file contains constant string definitions representing the filesystem paths to various system frameworks required by specific platforms (such as iOS, Mac Catalyst, macOS, or tvOS).
  3. Overview of the Assembly Preparer library

    main

    The Assembly Preparer is a library used during the build process to modify assemblies. Its primary purposes are to:

    • Collect build information: Gather data required for a successful build.
    • Transform code patterns: Modify code to ensure it is correctly recognized and handled by trimmers.
    • Optimize code: Transform recognizable patterns to improve performance.
    • Precompute data: Perform computations at build time to reduce app size and increase runtime speed.

    Currently, it includes the PreserveCodeBlockHandler, which injects code into user assemblies to prevent trimmers from incorrectly removing necessary code generated by the system.

  4. Overview of .NET for Apple platforms

    main

    This repository provides the SDKs for building native applications on Apple platforms using C# and .NET. Supported platforms include:

    • iOS
    • Mac Catalyst
    • macOS
    • tvOS

    These SDKs are part of .NET MAUI but can also be used independently for native development. Developers can use Visual Studio or Visual Studio Code as their primary IDEs.

  5. Understand the Managed Static Registrar design

    main

    The managed static registrar is a variation of the static registrar designed for compatibility with the NativeAOT compiler (avoiding unsupported features like metadata tokens). It moves more logic into managed code to improve performance and testability across Mono and CoreCLR runtimes.

    Key Mechanisms

    • Exported Methods: For every method exported to Objective-C, the registrar generates a managed method decorated with the [UnmanagedCallersOnly] attribute. This allows the method to have a native entry point that can be called directly from generated Objective-C code in AOT scenarios.
    • Type Mapping: The registrar creates numeric IDs for managed types and adds lookup tables in each assembly. This enables efficient lookups between Objective-C types and managed types using ObjCRuntime.IManagedRegistrar.LookupTypeId and ObjCRuntime.IManagedRegistrar.LookupType.
    • Method Mapping: In non-AOT scenarios, the registrar uses lookup tables to find function pointers for UnmanagedCallersOnly methods at runtime via ObjCRuntime.IManagedRegistrar.LookupUnmanagedFunction.

    Performance Benefits

    • macOS: Calling an exported managed method from Objective-C is 3-6x faster for simple method signatures.
    • Mac Catalyst: Calling an exported managed method from Objective-C is 30-50% faster for simple method signatures.
  6. Use Objective Sharpie to bootstrap Objective-C C# bindings

    main

    Objective Sharpie is a command-line tool designed to automate the initial creation of C# bindings for Objective-C frameworks. It parses framework header files using Clang to map the public API into a binding definition, ensuring high fidelity and thoroughness.

    Prerequisites: This tool is intended for experienced developers with advanced knowledge of Objective-C and C. You should have a strong understanding of how the native framework functions before attempting to bind it.

  7. Understand the generate-defines script output

    main

    The generate-defines script processes the list of supported frameworks for a specific platform. It produces a response file intended for the C# compiler. This file contains a -d:HAS_<framework> definition for every supported framework, allowing the compiler to conditionally compile code based on framework availability.

    -d:HAS_ACCELERATE
    -d:HAS_ACCESSIBILITY
    -d:HAS_ACCESSORYSETUPKIT
    -d:HAS_ACCOUNTS
    ...
    -d:HAS_WEBKIT
    -d:HAS_XKIT
  8. Understand the Azure DevOps Build System structure

    main

    The .NET for iOS/macOS build system uses Azure DevOps pipelines with a highly modular YAML structure. The system is divided into two primary entry points: CI Builds (for the main branch) and PR Builds (for pull requests).

    Key Differences between CI and PR Builds

    AspectCI BuildPR Build
    Entry Pointbuild-pipeline.ymlbuild-pull-request.yml
    PoolCIBuildPool (Trusted)PRBuildPool (Untrusted)
    Push NuGetsYesNo
    MaestroYesNo
    VS InsertionYes (on release branches)No
    SigningReal signingSkipped
    VariablesIncludes vsts-variables.yml and signing.ymlExcludes these variables
    LocalizationYes (main branch only)No