Sima Niu asked a gentleman. The Master said, “The superior man is free from worry and fear.” He said, “Is it true that he is a gentleman without fear or sorrow?” The Master said, “Introspection without guilt, what worries you and what fears you?” The Analects of Confucius: Yan Yuan

A hundred blog series. This is:

  • [v61. Xx HongMeng kernel source code analysis (ninja ninja) | all ninja can you unhappy

Compilation and construction related articles are:

  • V50. Xx HongMeng kernel source code analysis (compile environment) | compiler HongMeng drop pit prevention guide
  • V57. Xx HongMeng kernel source code analysis (compilation) | simple case at compilation process
  • Script v58. Xx HongMeng kernel source code analysis (environment) | compiler HongMeng so simple
  • V59. Xx HongMeng kernel source code analysis, build tools | suitable melon touch cane debugging HongMeng build process
  • Its v60. Xx HongMeng kernel source code analysis (designed.the gn) | designed.the gn syntax and use in HongMeng
  • V61. Xx HongMeng kernel source code analysis (ninja ninja) | ninja can upset

Ninja | ninja.

Ninja is an open source custom build system from a Google engineer named Evan Martin. It was originally developed for Chrome. Martin named it Ninja because it was strikes quickly. This is the characteristic of ninjas, unfortunately Martin does not understand Chinese culture, otherwise it would be more appropriate to call Xiao Li throwing knives. How many pieces are there? In Martin’s own words, When a file is modified, Ninja can go from discovery to compilation 10 times faster than Make. Whether there is 10 times is not the focus of this discussion, they made it, even if the cowhide should be blown. This article is to take a closer look at how Hong Meng uses Ninja.

Ninja is a speed-focused build system, and instead of making, they rely on file timestamps to detect rewrites.

  • It is designed for higher-level build systems to generate their input files, and they don’t want you to edit them manually.ninjaFiles can be generated.ninjaThe tool hasgn.cmake.premakeYou can even write one yourselfninjaBuild tools.
  • ninjaIt is very efficient and can be understood as assembly language in building systems.
  • ninjaThe file has no branch, circular flow control, is specified a bunch of rules of the file, so it is more thanMakefileSimple a lot
  • So far knownGoogleChrome.AndroidA part of,LLVM.V8, Ark compiler, Hongmeng and other large systems are usedninjaBuild.

The basic concept

An edge is a build statement that specifies a target (output), a rule, and an input. It is an edge in the topology of the compilation process. Target The target that needs to be generated by the compilation process, as specified by the build statement. Output The first half of the build statement, which is another name for target. Input The second half of an input build statement, used to produce an output file or target, otherwise known as a dependency. Rule A rule determines how to produce output from input by specifying command and some built-in variables. Pool A group of rules or edges in a pool. By specifying their depth, you can control the maximum parallelism. Scope Specifies the scope of a variable, from the block level of rule and build statements to the file level. Rule also has scope. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the keyword build define an edge. Rule Defines a rule. Pool Defines a pool. Default Specifies one or more default targets. Include Adds a Ninja file to the current scope. Subninja Adds a Ninja file with a different scope than the current file. Phony a built-in special rule that specifies non-file targets.Copy the code

Simple ninja

First of all, Ninja is supposed to be simple and stiff. Anything that can be generated by a tool must be a repetition of some simple, many simple pieces that effectively add up to a set of rules to solve a complex problem, so think about it for a moment. Ninja is so simple that it doesn’t have much grammar, just a few concepts and rules. As a result, the Ninja reference manual is much simpler than the GN reference manual.

Here’s an example:

Rule cc command = GCC $cflags -c $in -o $out build foo.o: cc foo.c build special. Cc special. C cflags = -wall # local variable, scope is only valid on compilation of specialCopy the code

Interpretation of the

  • cflags: Defines a user variable that is used to pass rules.
  • rule: Define a name calledccThe rules.
    • command: Generates a bash command that accepts three external arguments
  • The first onebuildThat will befoo.cwithccRules compiled intofoo.o
    • Final compilation options:gcc -Wall -Werror -c foo.c -o foo.o
  • The secondbuildThat will bespecial.cwithccRules compiled intospecial.o
    • Final compilation options:gcc -Wall -c foo.c -o foo.o
  • in.outisninjaTwo built-in variables of.

Phony rules

As with calling Vladimir Vladimirovich Putin, some file paths can be quite long, but Ninja offers the ability to alias files just for convenience.

build ability: phony ./libability.so build ability_notes: phony obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/ability_notes.stamp build ability_test: phony obj/foundation/aafwk/aafwk_lite/services/abilitymgr_lite/unittest/ability_test.stamp build ability_test_pageAbilityTest_group_lv0: phony obj/foundation/aafwk/aafwk_lite/services/abilitymgr_lite/unittest/test_lv0/page_ability_test/ability_test_pageAbilityTes t_group_lv0.stampCopy the code

With that in mind, it should be easy to read the Ninja part of Hong Meng.

HongMeng | ninja

In [its v60. Xx HongMeng kernel source code analysis (designed.the gn) | designed.the gn syntax and in the use of HongMeng] at the end of the article is that through designed.the gn gen generates the following files and directories

Turing @ ubuntu: / home/openharmony/code - v1.1.1 - LTS/out/hispark_aries ipcamera_hispark_aries $ls args. Designed.the gn build. The ninja build.ninja.d NOTICE_FILE obj test_info toolchain.ninjaCopy the code
  • args.gn: Some parameters
  • build.ninja : ninjaThe master file
  • build.ninja.d: Records all generated.ninjaA build. gn file generates a. Ninja file
  • Obj: output location of component module construction/compilation files.
  • Toolchain: Place ninja rules that will be added to build by subninja. Ninja

build.ninja

Build. Ninja

Ninja_required_version = 1.7.2 rule gn Command =.. /.. /.. /.. /tools/gn --root=.. /.. /.. -q --dotfile=.. /.. /.. /build/lite/.gn --script-executable=python3 gen . description = Regenerating ninja files build build.ninja: gn generator = 1 depfile = build.ninja.d subninja toolchain.ninja build ability: phony ./libability.so build ability_notes: phony obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/ability_notes.stamp build ability_test: phony obj/foundation/aafwk/aafwk_lite/services/abilitymgr_lite/unittest/ability_test.stamp build ability_test_pageAbilityTest_group_lv0: phony obj/foundation/aafwk/aafwk_lite/services/abilitymgr_lite/unittest/test_lv0/page_ability_test/ability_test_pageAbilityTes T_group_lv0. Stamp # build all: phony $ ./libcameraApp.so $ obj/applications/sample/camera/cameraApp/cameraApp_hap.stamp $ ./libgallery.so $ ... default allCopy the code

Interpretation of the

  • The first part is to define agnRules, for what? Let me regenerate it*ninjafile
  • subninjaThe equivalent of#includefile
  • default allTo specify one or more default targets

Toolchain | defined rules

Toolchain. ninja defines rules for compiling C, C ++, assembler, linking, static/dynamic linking libraries, timestamps, copying, and more. As follows:

rule cxx command = /root/llvm/bin/clang++ ${defines} ${include_dirs} ${cflags_cc} -c ${in} -o ${out} description = clang++ ${out} depfile = ${out}.d deps = gcc rule alink command = /root/llvm/bin/llvm-ar -cr ${out} @"${out}.rsp" description = AR ${out} rspfile = ${out}.rsp rspfile_content = ${in} rule link command = /root/llvm/bin/clang ${ldflags}  ${in} ${libs} -o ${output_dir}/bin/${target_output_name}${output_extension} description = LLVM LINK ${output_dir}/bin/${target_output_name}${output_extension} rspfile = ${output_dir}/bin/${target_output_name}${output_extension}.rsp rspfile_content = ${in} rule solink command = /root/llvm/bin/clang -shared ${ldflags} ${in} ${libs} -o ${output_dir}/${target_output_name}${output_extension} description = SOLINK ${output_dir}/${target_output_name}${output_extension} rspfile = ${output_dir}/${target_output_name}${output_extension}.rsp rspfile_content = ${in} rule stamp command = /usr/bin/touch ${out} description = STAMP ${out} rule asm command = /root/llvm/bin/clang ${include_dirs} ${asmflags} -c ${in} -o ${out}  description = ASM ${out} depfile = ${out}.d deps = gcc rule cc command = /root/llvm/bin/clang ${defines} ${include_dirs} ${cflags} ${cflags_c} -c ${in} -o ${out} description = clang ${out} rule copy command = cp -afd ${in} ${out} description = COPY ${in} ${out}Copy the code
  • Notice the description field in these rules, which is then typed to the console. Each output is a build, as shown in the figure, which tells you what rules were used to build.

Components to compile

This article uses the ability component as an example to illustrate ninja’s ability component compilation. Each component has its own ninja, which describes the compilation details of the component. The entire hongmeng system was built and compiled by a number of ninja clones.

├ ─ ─ foundation │ ├ ─ ─ aafwk │ │ └ ─ ─ aafwk_lite │ │ ├ ─ ─ frameworks │ │ │ ├ ─ ─ ability_lite │ │ │ │ └ ─ ─ ability. The ninjaCopy the code

Ability. Ninja

defines = -DOHOS_APPEXECFWK_BMS_BUNDLEMANAGER \
          -D_XOPEN_SOURCE=700 -DOHOS_DEBUG \
          -D_FORTIFY_SOURCE=2 \
          -D__LITEOS__ -D__LITEOS_A__
include_dirs = -I../../../foundation/aafwk/aafwk_lite/frameworks/abilitymgr_lite/include \
               -I../../../foundation/aafwk/aafwk_lite/frameworks/want_lite/include \
               -I../../../foundation/aafwk/aafwk_lite/interfaces/innerkits/abilitymgr_lite \
               -I../../../foundation/aafwk/aafwk_lite/interfaces/kits/want_lite \
               -I../../../foundation/aafwk/aafwk_lite/interfaces/kits/ability_lite \
               -I../../../foundation/appexecfwk/appexecfwk_lite/utils/bundle_lite \
               -I../../../foundation/appexecfwk/appexecfwk_lite/interfaces/kits/bundle_lite \
               -I../../../foundation/appexecfwk/appexecfwk_lite/frameworks/bundle_lite/include \
               -I../../../foundation/graphic/ui/frameworks -I../../../foundation/graphic/surface/interfaces/kits \
               -I../../../foundation/distributedschedule/samgr_lite/interfaces/kits/registry \
               -I../../../foundation/distributedschedule/samgr_lite/interfaces/kits/samgr \
               -I../../../foundation/communication/ipc_lite/frameworks/liteipc/include \
               -I../../../kernel/liteos_a/kernel/include \
               -I../../../kernel/liteos_a/kernel/common \
               -I../../../third_party/bounds_checking_function/include \
               -I../../../third_party/freetype/include \
               -I../../../utils/native/lite/kv_store/innerkits \
               -I../../../utils/native/lite/include \
               -I../../../foundation/aafwk/aafwk_lite/frameworks/ability_lite/include \
               -I../../../foundation/aafwk/aafwk_lite/frameworks/ability_lite \
               -I/root/llvm/include/c++/v1 \
               -I../../../prebuilts/lite/sysroot/usr/include/arm-liteos \
               -I../../../base/hiviewdfx/hilog_lite/interfaces/native/innerkits/hilog \
               -I../../../base/hiviewdfx/hilog_lite/interfaces/native/innerkits \
               -I../../../third_party/bounds_checking_function/include \
               -I../../../third_party/bounds_checking_function/include \
               -I../../../foundation/communication/ipc_lite/interfaces/kits \
               -I../../../utils/native/lite/include
cflags = -Wall -Wno-format -Wno-format-extra-args -fPIC \
          --target=arm-liteos \
          --sysroot=/home/openharmony/prebuilts/lite/sysroot \
          -Oz -flto -mfloat-abi=softfp -mcpu=cortex-a7 -nostdlib -fno-common -fno-builtin -fno-strict-aliasing -Wall -fsigned-char -mno-unaligned-access -fno-omit-frame-pointer -fstack-protector-all -fPIC
cflags_cc = -Wall -Wno-format -Wno-format-extra-args -fPIC \
            --target=arm-liteos \
            --sysroot=/home/openharmony/prebuilts/lite/sysroot \
            -Oz -flto -mfloat-abi=softfp -mcpu=cortex-a7 -nostdlib -fno-common -fno-builtin -fno-strict-aliasing -Wall -mno-unaligned-access -fno-omit-frame-pointer -fstack-protector-all -fexceptions -std=c++11 -fPIC
target_output_name = libability

build obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability.o: cxx ../../../foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/ability.cpp
build obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_context.o: cxx ../../../foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/ability_context.cpp
build obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_env.o: cxx ../../../foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/ability_env.cpp
build obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_env_impl.o: cxx ../../../foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/ability_env_impl.cpp
build obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_event_handler.o: cxx ../../../foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/ability_event_handler.cpp
build obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_loader.o: cxx ../../../foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/ability_loader.cpp
build obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_main.o: cxx ../../../foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/ability_main.cpp
build obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_scheduler.o: cxx ../../../foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/ability_scheduler.cpp
build obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_thread.o: cxx ../../../foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/ability_thread.cpp

build ./libability.so: solink \
  obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability.o \
  obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_context.o \
  obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_env.o \
  obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_env_impl.o \
  obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_event_handler.o \
  obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_loader.o \
  obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_main.o \
  obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_scheduler.o \
  obj/foundation/aafwk/aafwk_lite/frameworks/ability_lite/src/libability.ability_thread.o \
  ./libabilitymanager.so ./libbundle.so ./libhilog_shared.so ./libliteipc_adapter.so \
  ./libsec_shared.so ./libutils_kv_store.so || obj/utils/native/lite/kv_store/kv_store.stamp
  ldflags = -lstdc++ \
  --target=arm-liteos \
  --sysroot=/home/openharmony/prebuilts/lite/sysroot \
  -L/root/llvm/lib/arm-liteos/c++ \
  -L/home/openharmony/prebuilts/lite/sysroot/usr/lib/arm-liteos \
  -L/root/llvm/lib/clang/9.0.0/lib/arm-liteos \
  -lclang_rt.builtins -lc -lc++ -lc++abi \
  --sysroot=/home/openharmony/prebuilts/lite/sysroot \
  -mcpu=cortex-a7 -lc \
  -L/home/openharmony/out/hispark_aries/ipcamera_hispark_aries \
  -Wl,-rpath-link=/home/openharmony/out/hispark_aries/ipcamera_hispark_aries -Wl,-z,now -Wl,-z,relro -Wl,-z,noexecstack
  libs =
  frameworks =
  output_extension = .so
  output_dir = .
Copy the code

Interpretation of the

  • defines.include_dirs.cflags_ccAre all user-defined variables in order to giverule cxxPrepare the parameters, yes.cppThe compiler uses this rule
    rule cxx
      command = /root/llvm/bin/clang++ ${defines} ${include_dirs} ${cflags_cc} -c ${in} -o ${out}
      description = clang++ ${out}
      depfile = ${out}.d
      deps = gcc
    Copy the code
  • in.outAre two built-in variables that do not need to be definedbuildProvide, which compiles them individually.oFile.
  • Used in the current directory at lastsolinkRule, generate a dynamic link librarylibability.so.
    rule solink
      command = /root/llvm/bin/clang -shared ${ldflags} ${in} ${libs} -o ${output_dir}/${target_output_name}${output_extension}
      description = SOLINK ${output_dir}/${target_output_name}${output_extension}
      rspfile = ${output_dir}/${target_output_name}${output_extension}.rsp
      rspfile_content = ${in}
    Copy the code

Ability | the resulting file

Turing @ ubuntu: / home/openharmony/code - v1.1.1 - LTS/out/hispark_aries/ipcamera_hispark_aries/obj/foundation/aafwk/aafwk_lite └── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ── ─ O Heavy Exercises ── Heavy Exercises. Heavy Exercises. Heavy Exercises ── Heavy exercises. Heavy Exercises O School exercises ── School exercises. School exercises ── School exercises. School exercises ── School exercises Libability. Ability. O ├ ─ ─ libability. Ability_scheduler. O └ ─ ─ libability. Ability_thread. O 1 directory, 12 files Turing @ ubuntu: / home/openharmony/code - v1.1.1 - LTS/out/hispark_aries/ipcamera_hispark_aries/obj/foundation/aafwk/aafwk_lite /frameworks/ability_lite$ stat ability_notes.stamp File: ability_notes.stamp Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 805h/2053d Inode: 1217028 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/ turing) Gid: (0/ root) Access: 2021-07-21 00:38:52. 237373740-0700 Modify: 2021-07-21 00:34:30.20731256-0700 Change: The 2021-07-21 00:34:30. 207312566-0700Copy the code

Intensive reading of the kernel source code

Four code stores synchronous annotation kernel source code, >> view the Gitee repository

Analysis of 100 blogs. Dig deep into the core

Add comments to hongmeng kernel source code process, sort out the following article. Content based on the source code, often in life scene analogy as much as possible into the kernel knowledge of a scene, with a pictorial sense, easy to understand memory. It’s important to speak in a way that others can understand! The 100 blogs are by no means a bunch of ridiculously difficult concepts being put forward by Baidu. That’s not interesting. More hope to make the kernel become lifelike, feel more intimate. It’s hard, it’s hard, but there’s no turning back. 😛 and code bugs need to be constantly debug, there will be many mistakes and omissions in the article and annotation content, please forgive, but will be repeatedly amended, continuous update. Xx represents the number of modifications, refined, concise and comprehensive, and strive to create high-quality content.

Compile build The fundamental tools Loading operation Process management
Compile environment

The build process

Environment script

Build tools

Designed.the gn application

Ninja ninja

Two-way linked list

Bitmap management

In the stack way

The timer

Atomic operation

Time management

The ELF format

The ELF parsing

Static link

relocation

Process image

Process management

Process concept

Fork

Special process

Process recycling

Signal production

Signal consumption

Shell editor

Shell parsing

Process of communication Memory management Ins and outs Task management
spinlocks

The mutex

Process of communication

A semaphore

Incident control

The message queue

Memory allocation

Memory management

Memory assembly

The memory mapping

Rules of memory

Physical memory

Total directory

Scheduling the story

Main memory slave

The source code comments

Source structure

Static site

The clock task

Task scheduling

Task management

The scheduling queue

Scheduling mechanism

Thread concept

Concurrent parallel

The system calls

Task switching

The file system Hardware architecture
File concept

The file system

The index node

Mount the directory

Root file system

Character device

VFS

File handle

Pipeline file

Compilation basis

Assembly and the cords

Working mode

register

Anomaly over

Assembly summary

Interrupt switch

Interrupt concept

Interrupt management

HongMeng station | into a little bit every day, the original is not easy, welcome to reprint, please indicate the source.