Build Process (objc), build process objc
Read this article and take notes.
We 've been spoiled in recent days-we just need to click a button in Xcode (this button looks a bit like playing some music). After a few seconds, our program will run, unless there are some errors, this is amazing.
In this article, we will explain the Build process from a more advanced perspective, and explore the relationship between the project setting information exposed in the Xcode interface and the Build process. I will introduce some other articles in this article to further explore the actual implementation of each step in the Build process.
Decrypt Build logs
To understand the internal working principles of Xcode build, we first aim at the complete log file. Open Log Navigator and select a Build from the list. Xcode will display the log file perfectly.
By default, a large amount of information is hidden in the Xcode interface. By selecting a task and clicking the expand button on the right, you can see the details of each task. Another option is to select one or more tasks in the list and then select Cmd-C, which copies all plain text information to the clipboard. Finally, we can select "Copy transcript for shown results" in the Editor menu to Copy all the log information to the clipboard.
In the example given in this article, there are nearly 10,000 lines of log information (in fact, most of the log information is generated when OpenSSL is compiled, not the code we wrote ). Let's get started!
Observe the output log information. First, we will find that the log information is divided into several different sections, which correspond to the targets in our project:
Build target Pods-SSZipArchive...Build target Makefile-openssl...Build target Pods-AFNetworking...Build target crypto...Build target Pods...Build target ssl...Build target objcio
The project involved in this article has several dependencies: AFNetworking and SSZipArchive are included in Pods, while OpenSSL is included in the project as a Child Project.
For each target in the project, Xcode performs a series of operations to convert the source code to machine-readable binary files based on the selected platform. Next we will take a closer look at the first target: SSZipArchive.
In the log information output for this target, we can see the details of each task being executed. For example, the first task is to process a pre-compiled header file (to enhance log information readability, I omitted many details ):
(1) ProcessPCH /.../Pods-SSZipArchive-prefix.pch.pch Pods-SSZipArchive-prefix.pch normal armv7 objective-c com.apple.compilers.llvm.clang.1_0.compiler (2) cd /.../Dev/objcio/Pods setenv LANG en_US.US-ASCII setenv PATH "..." (3) /.../Xcode.app/.../clang (4) -x objective-c-header (5) -arch armv7 ... configuration and warning flags ... (6) -DDEBUG=1 -DCOCOAPODS=1 ... include paths and more ... (7) -c (8) /.../Pods-SSZipArchive-prefix.pch (9) -o /.../Pods-SSZipArchive-prefix.pch.pch
During the build process, each task will see the log information similar to the above, so we can learn more through the above log information.
- Similar to each log information block above, a log is used to describe related tasks as the starting point.
- Then, three lines of log information with indentation are output, listing the statements executed by the task. Here, the working directory has changed and the LANG and PATH environment variables are set.
- This is a miracle. To process
.pch
File, clang is called, and many options are attached. The complete call process and all parameters are shown in the output log. Let's take a look at several of the parameters...
-x
The identifier is used to specify the language in use.objective-c-header
.
- Specify the target architecture
armv7
.
- Suggestion
#defines
Has been added.
-c
The identifier is used to tell clang what to do.-c
Run the pre-processor, lexical analyzer, type check, LLVM generation and optimization, and target to specify the generation stage of assembly code. Finally, run the assembler to generate.o
.
- Input File.
- Output file.
Although there is a large amount of log information, I will not detail each task. Our focus is to give you a comprehensive understanding of which tools will be called and what parameters will be used in the build process.
For this target, although there is only one.pch
File, but in factobjective-c-header
File processing involves two tasks. By observing the specific output log information, we can know the details:
ProcessPCH /.../Pods-SSZipArchive-prefix.pch.pch Pods-SSZipArchive-prefix.pch normal armv7 objective-c ...ProcessPCH /.../Pods-SSZipArchive-prefix.pch.pch Pods-SSZipArchive-prefix.pch normal armv7s objective-c ...
From the above log information, we can see that target has built the armv7 and armv7s for the two architectures. Therefore, clang processes the files twice, each of which targets one architecture.
After processing the pre-compiled header file, you can see that there are several other task types for SSZipArchive target.
CompileC ...Libtool ...CreateUniversalBinary ...
As the name implies:CompileC
Used for compilation.m
And.c
File,Libtool
Used to build a library from the target file, andCreateUniversalBinary
The two.a
Files (each file corresponds to a schema) are merged into a common binary file, which can be run on both armv7 and armv7s.
Then, some other dependencies in the project will also take such similar steps. After AFNetworking is compiled, it is linked to SSZipArchive as the pod library. After OpenSSL is compiled, it processes crypto and ssl target.
After all the dependencies are built, it is our program's target. When building this target, the output log information will contain some very valuable content that has not been shown before:
PhaseScriptExecution ...DataModelVersionCompile ...Ld ...GenerateDSYMFile ...CopyStringsFile ...CpResource ...CopyPNGFile ...CompileAssetCatalog ...ProcessInfoPlistFile ...ProcessProductPackaging /.../some-hash.mobileprovision ...ProcessProductPackaging objcio/objcio.entitlements ...CodeSign ...
In the preceding task list, the unique task that cannot be distinguished by name may beLd
,Ld
Is the name of a linker toollibtool
Very similar. Actually,libtool
It is also a simple callld
Andlipo
. 'Ld 'is used to build executable files, whilelibtool
Is used to build library files. Read two articles, Daniel and Chris.