Related articles
IOS14 App Clip Trilogy
- Learn how to create a good App Clip in iOS14
- Create a little Apple App, App Clip, what you should know
- How to make your App Clip have a good user experience
preface
HelloWord link: original text
Native applications run more efficiently than converted applications because the compiler can optimize the code for the target architecture. If an application only supports x86_64 architecture, it must run under Rosetta transformations on Apple chips. The universal binary itself can run on Apple chips and Intel-based Macs because it contains executable code for both architectures.
The following list contains the most common executable types that can be converted to common binaries. Ps: This list is not exhaustive, but it can be used as an entry point for evaluating projects.
- Apps
- App extensions
- Plug-ins
- Custom frameworks
- Static libraries
- Dynamic libraries
- Build tools
- Command-line tools
- Daemons and launch agents
- DriverKit extensions
- Kernel extensions
Download and install the latest version of Xcode12
Earlier versions of Xcode did not include the support needed to build and test generic versions of macOS code.
Follow the list of new Xcode engineering architectures
Xcode 12 and later automatically adds the ARM64 architecture to the list of standard architectures for all macOS binaries, including applications and libraries. During debugging and testing, Xcode builds only for the current system architecture by default, but it automatically builds generic binaries for the Release version of the code.
If you customize the Architectures build Settings in the Xcode project, delete your customization and use Standard Architectures Settings instead.
Update the list of custom Makefile structures
If you build your project using custom scripts or makefiles, add the ARM64 schema to the appropriate environment variables. Xcode uses the ARCHS environment variable to define the current build architecture. Other build systems may use different environment variables for similar purposes. After adding the variables to the appropriate environment variables, compile the code and verify that the compiler has created an ARM64 version of the code. To create a generic binary for your project, use the LIPo tool to merge the generated executables into a single executable binary.
For makefiles created outside of Xcode, use the -target option to pass the appropriate schema value to the compiler. The following example shows a makefile compiling a source file twice at a time, once for each schema. You then create a generic binary by merging the generated executable with the LIPo tool.
C $(CC) main.c -o x86_app-target x86_64-apple-macos10.12arm_app: main.c $(CC) main.c -o arm_app -target arm64-apple-macos11 universal_app: x86_app arm_app lipo -create -output universal_app x86_app arm_appCopy the code
Use macros to encapsulate platform-specific code
When you write code for a particular platform or processor type, isolate the code with appropriate conditional compilation statements. Based on the C code, the system defines a set of macros for you in the/usr/include/TargetConditionals. H. The Swift language also supports conditional compilation using conditional compilation blocks. If code is shared across multiple platforms, compiler-specific macros, such as ARM64 or AARCH64, can also be used in conditional compiled statements.
To distinguish code for a particular type of processor, add conditional compilation statements for the appropriate schema. General-purpose macOS applications support ARM64 and X86_64 architectures. The following example shows how to write conditional code for these architectures:
#if arch(arm64)
// Code meant for the arm64 architecture here.
#elseif arch(x86_64)
// Code meant for the x86_64 architecture here.
#endif
Copy the code
If code is shared between iOS and macOS applications, don’t assume that code for the ARM64 architecture will only run on iOS devices. It can also run on macOS applications based on Apple chips. Use the conditional compilation statement shown in the following example.
#if os(macOS)
// Put CPU-independent macOS code here.
#if arch(arm64)
// Put 64-bit arm64 Mac code here.
#elseif arch(x86_64)
// Put 64-bit x86_64 Mac code here.
#endif
#elseif targetEnvironment(macCatalyst)
// Put Mac Catalyst-specific code here.
#elseif os(iOS)
// Put iOS-specific code here.
#endif
Copy the code
Complete list about conditional compilation of macros, please see the/usr/include/TargetConditionals. H header file.
Compile your Target
When you compile code under the tunable version, by default Xcode will only compile for the current schema. You can create generic binaries with debug symbols on any Mac by changing the Build Active Architecture Only option for your project. Although you can create this binary on an Intel-based Mac, it cannot run or debug on the ARM64 architecture, only macs with Apple chips.
Determine if your binaries are generic
To the user, a generic binary looks no different than a binary built for a single architecture. When you build a generic binary, Xcode compiles your source files twice, once for each schema, and after linking the binaries for each schema, Xcode uses lipo tools to combine the binaries for that particular schema into a single executable. If you compile the source files yourself, you must call LIPo in your build script to merge the architecture-specific binaries into a single generic binary.
The following example shows how to view the architecture of a Mail application in macOS using LIPo:
% lipo -archs /System/Applications/Mail.app/Contents/MacOS/Mail
x86_64 arm64
Copy the code
Specifies the startup behavior of the application
For general-purpose binaries, the system tends to execute the architecture native to the current platform, and on Intel-based Macs, the system always executes the X86_64 architecture. On Apple chips, systems tend to execute on the ARM64 architecture. The user can force the system to run the application under the Rosetta transformation by enabling the option in the Finder’s Application Show Profile window.
If you don’t want the user to run your application under the Rosetta conversion, please add LSRequiresNativeExecution key to the application of the Info. In the file. When it is set to YES, the system prevents your application from running after the conversion. In addition, the Rosetta conversion option will be removed from the Display Overview window of your application. Do not include this key-value pair until you are sure that your application will work on Apple chips and Intel-based Macs.
If you want to set the priority of the architecture without preventing users from running your application during the transformation, add the LSArchitecturePriority key to your application’s info.plist file. The values of this key are an ordered array of strings that define the priority order for selecting schemas.
As an ios developer, when I encounter problems, it is particularly important to have a learning atmosphere and a communication circle, which is of great help to myself. This is my ios communication group: 711315161, sharing BAT, Ali interview questions, interview experience, discussing technology, and learning and growth together! Hope to help developers avoid detours.