Compilation toolchain tools in AOSP

In AOSP (Android Open Source Project), the compilation toolchain is a set of tools used to build the Android operating system. These tools work together to convert source code into executable binaries, which are used to generate the various components and applications of the Android system. The following are the main compilation toolchain components in AOSP:

  1. Clang/LLVM:
    • Clang is an open source compiler front end for the C, C++, Objective-C, and Objective-C++ programming languages.
    • LLVM (Low Level Virtual Machine) is a compiler infrastructure that provides a modular, flexible and extensible compiler framework.
    • Clang/LLVM has gradually replaced GCC as the preferred compilation toolchain in AOSP.
    • Clang provides better error reporting, warnings, and static analysis of code, helping to improve code quality and readability.
    • Clang also supports more C++11 and C++14 features, and generally compiles faster than GCC.

GCC (GNU Compiler Collection): GCC is a widely used open source compiler collection that supports multiple programming languages, including C, C++, Java, etc.

  1. GNU Binutils :

    • GNU Binutils is a set of binary tools for working with executable files, object files, shared libraries, etc.
    • It includes linker (ld), assembler (as), and other utilities related to binary file manipulation.
    • Clang/LLVM is used in conjunction with GNU Binutils to link the compiled object files into the final executable.
  2. GNU Make / Ninja:

    • make is a build automation tool that judges which source files need to be recompiled according to the rules in the Makefile to achieve an efficient build process.
    • AOSP used to use GNU Make as the default build system, but has since switched to the faster, lightweight Ninja build system.
    • Ninja speeds up the build process by generating fast build scripts called .ninja files.
    • Using Ninja to build AOSP can significantly reduce build time, especially for large projects such as Android systems.
  3. Java Development Kit (JDK):

    • JDK is the Java Development Kit, which is used to compile and build Java code.
    • Part of the core components and applications of Android are written in Java, so JDK is a necessary component to build the Android system.
    • Before compiling AOSP, you need to install the appropriate version of JDK and configure environment variables.
  4. Other Build Tools:

    • AOSP also uses some other build tools to help with code generation and optimization, such as Python, Perl, etc.
    • Some scripts and tools are used to process resources, generate resource files and AndroidManifest.xml, etc.

The process of compiling AOSP requires configuring the correct environment, including setting the appropriate compiler path, build system, and other required dependencies. Google provides detailed documentation and guides to help developers set up and use the compilation toolchain in AOSP. Building the entire Android system may take a certain amount of time and system resources, but with proper tools and configurations, you can speed up the compilation process and improve development efficiency.

Compile toolchain

The following is a brief introduction to the common tools in the compilation toolchain in AOSP and the meaning of the parameters

  1. Clang/LLVM:

    • usage:clang [options] <input-file(s)>
    • Parameter meaning: The options of the Clang compiler are used to control the compilation process. Common options include optimization level, warning level, target architecture, etc.
    • Example: Compile a single C source file
      clang -O2 -o output_file input_file.c
      
  2. GNU Binutils :

    • Linker (ld) usage:ld [options] <input-file(s)>
    • Linker parameter meaning: ld is a tool for linking object files to generate executable files or shared libraries. Parameters are used to specify input files, output files, link addresses, library search paths, etc.
    • Example: linking multiple object files to produce an executable
      ld -o output_file input_file1.o input_file2.o
      
  3. GNU Make / Ninja:

    • make usage:make [options] [target]
    • Make parameter meaning: The make tool builds the target file according to the rules and dependencies in the Makefile. Options can be used to control parallel builds, specify Makefile paths, etc.
    • Example: compile AOSP system
      make -j8
      
    • Ninja usage:ninja [options] [targets...]
    • Ninja parameter meaning: The Ninja build system uses .ninja files to execute builds, and options are used to control parallel builds, display build rules, and more.
    • Example: compile AOSP system
      ninja -j8
      
  4. Java Development Kit (JDK):

    • usage:javac [options] <source-file(s)>
    • Parameter meaning: javac is a Java compiler used to compile Java source code into bytecode files. Options are used to set build version, build target folder, etc.
    • Example: Compile Java source files
      javac -d out_dir input_file.java
      

compile example

Complete example: Compile a simple C++ program, link and produce an executable.

Suppose we have the following two files:

main.cpp:

#include <iostream>

int main() {
    
    
    std::cout << "Hello, AOSP Build Tools!" << std::endl;
    return 0;
}

Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := hello
LOCAL_SRC_FILES := main.cpp

include $(BUILD_EXECUTABLE)

Now, we compile and link this simple program using the compilation toolchain in AOSP:

  1. Compile main.cpp with Clang:

    clang++ -c main.cpp -o main.o
    
  2. Use GNU Binutils' ld link to generate an executable:

    ld -o hello main.o
    
  3. With the Ninja build system, you can use the definitions in Android.mk to simplify the build process:

    make -j8 hello
    

After completing the above steps, you will generate an executable file named hello in the current directory. Executing it will output "Hello, AOSP Build Tools!".

Clang/LLVM

Clang/LLVM is an open source compiler front-end and compiler infrastructure for supporting multiple programming languages ​​such as C, C++, Objective-C and Objective-C++. Its syntax and usage are somewhat different from traditional compilers such as GCC. The following is a comprehensive analysis of the syntax of Clang/LLVM:

  1. Basic compilation command :

    The basic command of the Clang compiler is clangto compile C, C++, Objective-C and Objective-C++ code.

    clang [options] <input-file(s)>
    

    options: Compilation options, used to control the compilation process, such as optimization level, warning level, target architecture, etc.

    input-file(s): The source file to be compiled, which can be a single source file or multiple source files.

  2. Compile the C++ source file :

    When compiling C++ source files with Clang, or is usually used .cppas .cxxthe file extension.

    clang++ [options] <input-file(s)>
    

    clang++It is an alias of Clang for C++ code, which can directly compile C++ source files.

  3. Optimization level options :

    Clang supports different optimization levels, which can -Obe specified via options.

    -O0     # 不进行优化
    -O1     # 基本优化级别
    -O2     # 中等优化级别
    -O3     # 较高优化级别
    -Os     # 优化代码尺寸
    
  4. Generate object files :

    Use -cthe option to compile source code into object files (.o files) without linking, suitable for subsequent linking steps.

    clang -c source_file.c -o object_file.o
    
  5. Link and build executable :

    In addition to being used as the front end of the compiler, Clang can also generate executable files from object files through the linker.

    clang file1.o file2.o -o executable
    

    This will link file1.oand file2.ogenerate executablean executable named .

  6. Preprocessing options :

    Clang supports preprocessing options, and you can view the preprocessed code.

    clang -E source_file.c -o preprocessed_file.c
    

    This will preprocess source_file.cand save the result in preprocessed_file.c.

  7. Other common options :

    • -I<dir>: Add header file search path.
    • -L<dir>: Add library file search path.
    • -l<library>: Link the specified library file.
    • -Wall: Turn on all warnings.
    • -std=<standard>: Specifies the compilation standard, such as C++11, C++14, etc.

The above are some common syntax and options of Clang/LLVM. Through these options, you can better control and customize the behavior of the Clang compiler.

GNU Binutils

GNU Binutils is a set of open source binary tools for processing executable files, object files, shared libraries, etc. It includes multiple tools such as linker (ld), assembler (as), disassembler (objdump), symbol viewer (nm), etc. The following is a comprehensive analysis of the syntax of common tools in GNU Binutils:

  1. Linker (ld) :

    ld [options] <input-file(s)>

    options: Linker options to control the linking process, such as linking output files, linking scripts, library search paths, etc.

    input-file(s): The target file to be linked, which can be a single target file or multiple target files.

  2. Assembler (as) :

    as [options] <input-file> -o <output-file>

    options: Assembler options used to control the assembly process, such as target architecture, debugging information, etc.

    input-file: The assembly code file to be assembled.

    -o <output-file>: Specifies the name of the output file.

  3. Disassembler (objdump) :

    objdump [options] <input-file>

    options: Disassembler options, used to control the disassembly process, such as displaying machine code, symbol table, segment information, etc.

    input-file: The object file to be disassembled.

  4. Symbol Viewer (nm) :

    nm [options] <input-file>

    options: Symbol viewer options for controlling the symbol viewing process, such as displaying symbol types, sorting, filtering, etc.

    input-file: The target file for the symbol to be viewed.

  5. Other tools :

    • objcopy: Copy the target file, used to modify the format or content of the target file.
    • strip: Remove symbol table and debug information from object file, used to reduce file size.
    • readelf: Display the information of the ELF file, including the header, segment table, symbol table, etc.
    • addr2line: Convert addresses to source code line numbers, useful for debugging.
    • size: Display the size information of the target file.

GNU Make / Ninja

GNU Make and Ninja are two commonly used build systems for managing and automating the build process of projects. They perform build tasks by reading Makefiles (for GNU Make) or .ninja files (for Ninja). The following is a comprehensive analysis of the syntax of GNU Make and Ninja:

GNU Make:

  1. Basic build command :

    make [options] [target]

    options: Build options, used to control the build process, such as specifying Makefiles, parallel builds, etc.

    target: Build target, which is the name of the target defined in the Makefile.

  2. Makefile basic syntax :

    Makefile is a configuration file for GNU Make that defines build rules and dependencies.

    • Define variables:VARNAME = value
    • Comments: #Lines starting with
    • Define the rules:target: dependencies
      target: dependencies
          commands
      
    • Implicit rules: GNU Make provides some implicit rules, such as compilation rules for C/C++ files, without explicit definition.
    • PHONY target: defines a pseudo-target, which is used to specify some special operations or commands instead of file construction.
      .PHONY: clean
      clean:
          rm -f *.o myprogram
      
  3. Makefile variables and automatic variables :

    In Makefiles, you can use variables and automatic variables to simplify build rules and commands.

    • $@: represents the current target
    • $<: represents the first dependency
    • $^: represents all dependencies
  4. Build the specified target :

    If no target is specified, Make will execute the first target in the Makefile by default. Targets to build can be specified on the command line.

    make target_name
    

Ninja:

  1. Basic build command :

    ninja [options] [targets...]

    options: Build options, used to control the build process, such as displaying build rules, parallel builds, etc.

    targets...: The build target, which is the name of the target defined in the .ninja file.

  2. Ninja file basic syntax :

    .ninja files are configuration files for the Ninja build system, used to define build rules and dependencies.

    • Define variables:VARNAME = value
    • Define the rules:rule rule_name
      rule_name command
      
    • Build rules:build output: rule_name input
    • Pre-rules:depfile = dep_file
    • PHONY target: Use phonytags to define pseudo-targets
      phony target_name
      
  3. Ninja variables :

    In .ninja files, variables can be used to simplify build rules.

    • $out: represents the output file
    • $in: represents the input file
    • $depfile: Represents dependent files
  4. Build the specified target :

    If no target is specified, Ninja will default to the first target in the .ninja file. Targets to build can be specified on the command line.

    ninja target_name
    

The above is the basic syntax and usage of GNU Make and Ninja. They can greatly simplify the build process when building large projects, improving build efficiency and maintainability.

Java Development Kit (JDK)

The Java Development Kit (JDK) is the Java Development Kit that provides the tools and libraries needed to develop, compile, and run Java applications. JDK provides components such as compilers, debuggers, and Java API class libraries to enable developers to create Java applications. The following is a comprehensive analysis of the syntax of the JDK:

  1. Java Compiler (javac) :

    The Java compiler (javac) is used to compile Java source code into bytecode files (.class files).

    javac [options] <source-file(s)>
    

    options: Compilation options, used to control the compilation process, such as specifying the compilation version, generating the target folder, etc.

    source-file(s): The Java source file to be compiled, which can be a single source file or multiple source files.

  2. Java virtual machine (java) :

    The Java Virtual Machine (java) is used to run Java applications.

    java [options] <class-name>
    

    options: Java virtual machine options, used to control the behavior of the virtual machine, such as setting the heap size, specifying startup parameters, etc.

    class-name: The full class name of the Java class to run.

  3. Java Documentation Generation Tool (javadoc) :

    The Java Documentation Generation Tool (javadoc) is used to generate API documentation from Java source code.

    javadoc [options] <source-file(s)>
    

    options: The javadoc option, used to control the document generation process, such as specifying the output directory, adding author information, etc.

    source-file(s): The Java source file to generate documentation for.

  4. Java debugger (jdb) :

    The Java Debugger (jdb) is used to debug Java programs, allowing developers to inspect and modify program state at runtime.

    jdb [options] <class-name>
    

    options: jdb options, used to control the debugging process, such as setting breakpoints, monitoring variables, etc.

    class-name: The full class name of the Java class to debug.

  5. Other JDK tools :

    • javap: Disassembly tool for viewing bytecode instructions of class files.
    • jarsigner: Used to sign and verify signatures for JAR files.
    • keytool: Used to generate and manage keystores and certificates.
    • javah: Used to generate the Java Native Interface (JNI) header files.

The above is the basic syntax and usage of common tools in JDK. JDK also provides other features and tools such as performance analysis tools (jvisualvm), JavaFX tools, JShell (Java 9+), etc.

Guess you like

Origin blog.csdn.net/weixin_44008788/article/details/132029374