IOS compilation process, ios Compilation
Compilation process
The basic compilation process is divided into four steps:
Then, by parsing the xcode compilation log, we can find that xcode is compiled separately based on the target. The specific compilation process of each target can also be obtained by expanding the log. The basic format is to briefly describe what to do, and then indented several lines to describe the specific operation. For example:
(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
After processing the pch header file, switch to the pch directory, set the environment variables, start clang, and perform a series of configurations. After that, a specific.oFiles are used as outputs (generally there are multiple files, each of which has one for different platform architectures, but these files are usually aggregated into a common library .). At the same time, note that different targets are compiled in order, depending on the dependencies between targets.
During xcode compilation, most of the commands can be self-interpreted. However, some commands cannot be seen directly:
ld: Used to generate executable files.
libtool: A tool that generates lib.
(This part will be explained in the compilation process of subsequent articles)
The next step is to control the compilation process. In xcode, you can use Build phases,Build settingsAnd Build rules.
Build phasesIt is mainly used to control the entire process from the source file to the executable file, so it should be said that it is oriented to the source file, including the files to be compiled, and some custom scripts to be executed during the compilation process.
Build rulesIt is mainly used to control how to compile a certain type of source files. If you compile a specific type of source file, you should edit it here. At the same time, a large number of environment variables in xcode will be used here. The complete official documentation is here: Build Settings Reference
Build settingsThis is to set the details of the compilation. In this window, you can see a large number of setting options, from compilation to packaging to code signature. Pay attention to the section classification of settings, at the same time, the inspector on the right can be used to better understand the meaning of the options.
Finally, let's talk about our engineering documents..pbxprojAll of the preceding options are saved in this file. Of course, it also includes the target information and the information of all project files. This file is a text file and can be opened in a text editor. The content is basically readable. The basic idea is object-oriented. Everything has attributes. If the attribute is another object, the value is a "Reference" of that object, which is represented by a string of numbers (unique. Each object has such a reference.
Compiler
First, what is the compiler doing? The compiler is used to convert source code files to lower-level languages (and Static Analysis of statements ), the clang compiler used by xcode is used to convert the source code into a lower-level llvm ir (Intermedia Representation), which is unrelated to the operating system, then LLVM uses this intermediate language to output the next binary file. Thanks to LLVM's three-tier architecture, LLVM can have multiple inputs and outputs (the first-tier architecture of LLVM is used to process inputs, the second is used to optimize IR, and the third is used for output)I encountered a problem here. I don't know what the relationship between clang and LLVM is. I guess I can understand how the compiler works.
Generally, a compiler can compile multiple languages to generate code on multiple platforms. Therefore, the front-end and back-end are divided. Sometimes there are middle-end arguments.
The frontend is language-related and the output is an abstract syntax tree;
The backend is machine-related and the output is machine code. Some optimizations are machine-independent and may be listed as the middle end.
Take gcc as an example. The intermediate language generated by the front-end is GENERIC, then converted to gimple for machine-independent optimization, and finally to RTL for machine-related optimization and machine code generation.
These three parts can be called front-end, middle-end, and backend respectively. However, the gimple phase is available only after gcc 4, and the gcc 3.x version is optimized on RTL.
In addition, in actual implementation, machine-related optimization may also be implemented in the gimple phase (in turn, RTL also has machine-independent optimization). The Division is not so clear.
That is to say, the previous section Completed Syntactic Analysis and other related work, and will not optimize the Machine Platform accordingly. The backend is part of the real robot code. Clang is only the front-end part of a compiler. The term LLVM cannot be generalized. The specific difference is described in this blog.
If you are interested in the compiler itself, you can look at the compilation principles (one of the three major Romances of programmers), and then you can understand how a compiler should be written.
Here is
Statement: This article aims to better read and remember the original article.
Other related documents:
1. Write a compiler from scratch
2. For more information about open classes and zhihu columns of Stanford University, see
3. Use Xcode to view preprocessing files