当前位置:   article > 正文

Android 10 根文件系统和编译系统(二十一):Android root根目录生成过程分析_旗浩 rom

旗浩 rom

   配套系列教学视频链接:

      安卓系列教程之ROM系统开发-百问100ask


说明

系统:AOSP Android10.0

设备:RK3399 开发板 

前言

    在Android源码编译完成的out目录下, 有out/target/product/产品名/root/, 该目录是作为Android系统运行启动是的根目录, 包含了整个Android根文件系统的目录架构以及system, vendor等分区的挂载点,本章节重点讲解Android的根目录下的子目录是如何在编译的时候创建出来的,并可以将之前学习的知识点综合应用到这个章节来。起到温故知新的作用。


一, Android根目录的来源

    在Android源码编译完成的out目录下, 有out/target/product/产品名/root/, 该目录是作为Android系统运行启动是的根目录, 包含了整个Android根文件系统的目录架构以及system, vendor等分区的挂载点,而out/target/product/产品名/root/目录中的大部分文件都是由源码中的system/core/rootdir/生成的。

 最终生成的root目录如下,以rk3399 Android10举例

ls out/target/product/rk3399_roc_pc_plus/root/

二,rootdir目录Android.mk分析

LOCAL_PATH:= $(call my-dir)

#######################################

# init.rc

include $(CLEAR_VARS)

LOCAL_MODULE := init.rc

LOCAL_SRC_FILES := $(LOCAL_MODULE)

LOCAL_MODULE_CLASS := ETC

LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)

LOCAL_REQUIRED_MODULES := fsverity_init

# The init symlink must be a post install command of a file that is to TARGET_ROOT_OUT.

# Since init.rc is required for init and satisfies that requirement, we hijack it to create the symlink.

LOCAL_POST_INSTALL_CMD := ln -sf /system/bin/init $(TARGET_ROOT_OUT)/init

include $(BUILD_PREBUILT)

#######################################

# init-debug.rc

include $(CLEAR_VARS)

LOCAL_MODULE := init-debug.rc

LOCAL_SRC_FILES := $(LOCAL_MODULE)

LOCAL_MODULE_CLASS := ETC

LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)/init

include $(BUILD_PREBUILT)

以上部分主要是进行预编译init.rc和init-debug.rc,分别编译到out/target/product/产品名/root/init.rc和out/target/product/产品名/root/init/init-debug.rc,  rc脚本文件是Android启动非常重要的脚本文件,后期会单独分析

#######################################

# fsverity_init

include $(CLEAR_VARS)

LOCAL_MODULE:= fsverity_init

LOCAL_MODULE_CLASS := EXECUTABLES

LOCAL_SRC_FILES := fsverity_init.sh

include $(BUILD_PREBUILT)

#######################################

# init.environ.rc

include $(CLEAR_VARS)

LOCAL_MODULE_CLASS := ETC

LOCAL_MODULE := init.environ.rc

LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)

EXPORT_GLOBAL_ASAN_OPTIONS :=

ifneq ($(filter address,$(SANITIZE_TARGET)),)

  EXPORT_GLOBAL_ASAN_OPTIONS := export ASAN_OPTIONS include=/system/asan.options

  LOCAL_REQUIRED_MODULES := asan.options $(ASAN_OPTIONS_FILES) $(ASAN_EXTRACT_FILES)

endif

以上部分主要是进行预编译init.environ.rc到out/target/product/产品名/root/init.environ.rc中去, 后面加了编译选项, 是否支持asan相关选项,asan是编译cpp代码时会检测内存错误的功能, 使用ASan需要注意的是,ASan的CPU开销约为2倍,代码大小开销在50%到2倍之间,并且内存开销很大,约为2倍。官方建议对于64位ARM(一般现在的设备都是这个了),建议使用HWAddress Sanitizer

# create some directories (some are mount points) and symlinks

LOCAL_POST_INSTALL_CMD := mkdir -p $(addprefix $(TARGET_ROOT_OUT)/, \

    sbin dev proc sys system data odm oem acct config storage mnt apex debug_ramdisk $(BOARD_ROOT_EXTRA_FOLDERS)); \

    ln -sf /system/bin $(TARGET_ROOT_OUT)/bin; \

    ln -sf /system/etc $(TARGET_ROOT_OUT)/etc; \

    ln -sf /data/user_de/0/com.android.shell/files/bugreports $(TARGET_ROOT_OUT)/bugreports; \

    ln -sf /sys/kernel/debug $(TARGET_ROOT_OUT)/d; \

    ln -sf /storage/self/primary $(TARGET_ROOT_OUT)/sdcard

ifdef BOARD_USES_VENDORIMAGE

  LOCAL_POST_INSTALL_CMD += ; mkdir -p $(TARGET_ROOT_OUT)/vendor

else

  LOCAL_POST_INSTALL_CMD += ; ln -sf /system/vendor $(TARGET_ROOT_OUT)/vendor

endif

ifdef BOARD_USES_PRODUCTIMAGE

  LOCAL_POST_INSTALL_CMD += ; mkdir -p $(TARGET_ROOT_OUT)/product

else

  LOCAL_POST_INSTALL_CMD += ; ln -sf /system/product $(TARGET_ROOT_OUT)/product

endif

ifdef BOARD_USES_PRODUCT_SERVICESIMAGE

  LOCAL_POST_INSTALL_CMD += ; mkdir -p $(TARGET_ROOT_OUT)/product_services

else

  LOCAL_POST_INSTALL_CMD += ; ln -sf /system/product_services $(TARGET_ROOT_OUT)/product_services

endif

ifdef BOARD_USES_METADATA_PARTITION

  LOCAL_POST_INSTALL_CMD += ; mkdir -p $(TARGET_ROOT_OUT)/metadata

endif

以上编译规则主要完成如下事项:

     1, 新建out/target/product/产品名/root/, 并在目录下创建如下空的文件夹:

sbin dev proc sys system data odm oem acct config storage mnt apex debug_ramdisk

     2, out/target/product/产品名/root/中创建软连接:

     bin, etc, bugreports, d, sdcard, vendor, product, product_services

#对应第1,2点

# For /odm partition.

LOCAL_POST_INSTALL_CMD += ; mkdir -p $(TARGET_ROOT_OUT)/odm

LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/app $(TARGET_ROOT_OUT)/odm/app

LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/bin $(TARGET_ROOT_OUT)/odm/bin

LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/etc $(TARGET_ROOT_OUT)/odm/etc

LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/firmware $(TARGET_ROOT_OUT)/odm/firmware

LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/framework $(TARGET_ROOT_OUT)/odm/framework

LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/lib $(TARGET_ROOT_OUT)/odm/lib

LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/lib64 $(TARGET_ROOT_OUT)/odm/lib64

LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/overlay $(TARGET_ROOT_OUT)/odm/overlay

LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/priv-app $(TARGET_ROOT_OUT)/odm/priv-app

LOCAL_POST_INSTALL_CMD += ; ln -sf /vendor/odm/usr $(TARGET_ROOT_OUT)/odm/usr

#对应第3点

ifdef BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE

  LOCAL_POST_INSTALL_CMD += ; mkdir -p $(TARGET_ROOT_OUT)/cache

else

  LOCAL_POST_INSTALL_CMD += ; ln -sf /data/cache $(TARGET_ROOT_OUT)/cache

Endif

#对应第4点

ifdef BOARD_ROOT_EXTRA_SYMLINKS

# BOARD_ROOT_EXTRA_SYMLINKS is a list of <target>:<link_name>.

  LOCAL_POST_INSTALL_CMD += $(foreach s, $(BOARD_ROOT_EXTRA_SYMLINKS),\

    $(eval p := $(subst :,$(space),$(s)))\  #将字符串里面的:替换成空格

    ; mkdir -p $(dir $(TARGET_ROOT_OUT)/$(word 2,$(p))) \ # word函数此处为提取字符串中的第2个单词

    ; ln -sf $(word 1,$(p)) $(TARGET_ROOT_OUT)/$(word 2,$(p)))

endif

以上编译规则主要完成如下事项:

  1. 在out/target/product/产品名/root/下创建odm目录
  2. 并在odm目录下创建各种软连接, 链接到/vendor分区下的各个子目录
  3. 创建cache目录,如果系统不使用cache分区, 就新建软链接cache到/data/cache
  4. 源码中可以定义BOARD_ROOT_EXTRA_SYMLINKS, 格式为<target>:<link_name>, 那么就会新建目录out/target/product/产品名/root/link_name, 然后用新建目录软练级到<target>

$(LOCAL_BUILT_MODULE): $(LOCAL_PATH)/init.environ.rc.in

    @echo "Generate: $< -> $@"

@mkdir -p $(dir $@)

#将源文件中的所有%BOOTCLASSPATH%替换成PRODUCT_BOOTCLASSPATH的值

    $(hide) sed -e 's?%BOOTCLASSPATH%?$(PRODUCT_BOOTCLASSPATH)?g' $< >$@

    $(hide) sed -i -e 's?%DEX2OATBOOTCLASSPATH%?$(PRODUCT_DEX2OAT_BOOTCLASSPATH)?g' $@

    $(hide) sed -i -e 's?%SYSTEMSERVERCLASSPATH%?$(PRODUCT_SYSTEM_SERVER_CLASSPATH)?g' $@

    $(hide) sed -i -e 's?%EXPORT_GLOBAL_ASAN_OPTIONS%?$(EXPORT_GLOBAL_ASAN_OPTIONS)?g' $@

    $(hide) sed -i -e 's?%EXPORT_GLOBAL_GCOV_OPTIONS%?$(EXPORT_GLOBAL_GCOV_OPTIONS)?g' $@

    $(hide) sed -i -e 's?%EXPORT_GLOBAL_HWASAN_OPTIONS%?$(EXPORT_GLOBAL_HWASAN_OPTIONS)?g' $@

以上规则是将init.environ.rc.in作为源头, 根据产品实际情况,将文件中各个环境变量的值进行填充,可以快速看到实际结果:

cat system/core/rootdir/init.environ.rc.in

最后的结果:

cat  out/target/product/rk3399_roc_pc_plus/obj/ETC/init.environ.rc_intermediates/init.environ.rc

# Append PLATFORM_VNDK_VERSION to base name.

define append_vndk_version

$(strip \

  $(basename $(1)).$(PLATFORM_VNDK_VERSION)$(suffix $(1)) \

)

endef

#######################################

# ld.config.txt selection variables

#

_enforce_vndk_at_runtime := false

#文件中./device/rockchip/common/BoardConfig.mk:246:BOARD_VNDK_VERSION := current

#说明以下是成立的, _enforce_vndk_at_runtime为true

ifdef BOARD_VNDK_VERSION

  ifneq ($(BOARD_VNDK_RUNTIME_DISABLE),true)

    _enforce_vndk_at_runtime := true

  endif

endif

_enforce_vndk_lite_at_runtime := false

ifeq ($(_enforce_vndk_at_runtime),false)

  ifeq ($(PRODUCT_TREBLE_LINKER_NAMESPACES)|$(SANITIZE_TARGET),true|)

    _enforce_vndk_lite_at_runtime := true

  endif

endif

#######################################

# ld.config.txt

#

# For VNDK enforced devices that have defined BOARD_VNDK_VERSION, use

# "ld.config.txt" as a source file. This configuration includes strict VNDK

# run-time restrictions for vendor process.

#

# Other treblized devices, that have not defined BOARD_VNDK_VERSION or that

# have set BOARD_VNDK_RUNTIME_DISABLE to true, use "ld.config.vndk_lite.txt"

# as a source file. This configuration does not have strict VNDK run-time

# restrictions.

#

# If the device is not treblized, use "ld.config.legacy.txt" for legacy

# namespace configuration.

#

include $(CLEAR_VARS)

LOCAL_MODULE := ld.config.txt

LOCAL_MODULE_CLASS := ETC

LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)

以上编译规则中的注释说的很清楚, 主要针对VNDK模块, 编译ld.config.txt, 并且需要加上一个版本号, 以system/core/rootdir/etc/ld.config.txt为源, 实际最终生成为:

 out/target/product/rk3399_roc_pc_plus/system/etc/ld.config.29.txt

LOCAL_POST_INSTALL_CMD = mkdir -p $(TARGET_OUT)/usr && rm -rf $(TARGET_OUT)/usr/icu

LOCAL_POST_INSTALL_CMD += && ln -sf /apex/com.android.runtime/etc/icu $(TARGET_OUT)/usr/icu

# TODO(b/124106384): Clean up compat symlinks for ART binaries.

ART_BINARIES := \

  dalvikvm \

  dalvikvm32 \

  dalvikvm64 \

  dex2oat \

  dexdiag \

  dexdump \

  dexlist \

  dexoptanalyzer \

  oatdump \

  profman \

LOCAL_POST_INSTALL_CMD += && mkdir -p $(TARGET_OUT)/bin

$(foreach b,$(ART_BINARIES), \

  $(eval LOCAL_POST_INSTALL_CMD += \

    && ln -sf /apex/com.android.runtime/bin/$(b) $(TARGET_OUT)/bin/$(b)) \

 以上编译规则主要也是为了生成软连接, 针对APEX模块, 在实际生成结果如下:

out/target/product/rk3399_roc_pc_plus/system/bin

ifeq ($(_enforce_vndk_at_runtime),true)

# for VNDK enforced devices

LOCAL_MODULE_STEM := $(call append_vndk_version,$(LOCAL_MODULE))

include $(BUILD_SYSTEM)/base_rules.mk

ld_config_template := $(LOCAL_PATH)/etc/ld.config.txt

check_backward_compatibility := true

vndk_version := $(PLATFORM_VNDK_VERSION)

include $(LOCAL_PATH)/update_and_install_ld_config.mk

else ifeq ($(_enforce_vndk_lite_at_runtime),true)

# for treblized but VNDK lightly enforced devices

LOCAL_MODULE_STEM := ld.config.vndk_lite.txt

include $(BUILD_SYSTEM)/base_rules.mk

ld_config_template := $(LOCAL_PATH)/etc/ld.config.vndk_lite.txt

vndk_version := $(PLATFORM_VNDK_VERSION)

libz_is_llndk := true

include $(LOCAL_PATH)/update_and_install_ld_config.mk

else

# for legacy non-treblized devices

LOCAL_MODULE_STEM := $(LOCAL_MODULE)

LOCAL_SRC_FILES := etc/ld.config.legacy.txt

include $(BUILD_PREBUILT)

endif  # ifeq ($(_enforce_vndk_at_runtime),true)

 根据之前的分析, enforce_vndk_at_runtime为true, 所以最终生成文件为

out/target/product/rk3399_roc_pc_plus/system/etc/ld.config.29.txt

#######################################

# llndk.libraries.txt

include $(CLEAR_VARS)

LOCAL_MODULE := llndk.libraries.txt

LOCAL_MODULE_CLASS := ETC

LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)

LOCAL_MODULE_STEM := $(call append_vndk_version,$(LOCAL_MODULE))

include $(BUILD_SYSTEM)/base_rules.mk

$(LOCAL_BUILT_MODULE): PRIVATE_LLNDK_LIBRARIES := $(LLNDK_LIBRARIES)

$(LOCAL_BUILT_MODULE):

    @echo "Generate: $@"

    @mkdir -p $(dir $@)

    $(hide) echo -n > $@

    $(hide) $(foreach lib,$(PRIVATE_LLNDK_LIBRARIES), \

        echo $(lib).so >> $@;)

#######################################

# vndksp.libraries.txt

include $(CLEAR_VARS)

LOCAL_MODULE := vndksp.libraries.txt

LOCAL_MODULE_CLASS := ETC

LOCAL_MODULE_PATH := $(TARGET_OUT_ETC)

LOCAL_MODULE_STEM := $(call append_vndk_version,$(LOCAL_MODULE))

include $(BUILD_SYSTEM)/base_rules.mk

$(LOCAL_BUILT_MODULE): PRIVATE_VNDK_SAMEPROCESS_LIBRARIES := $(VNDK_SAMEPROCESS_LIBRARIES)

$(LOCAL_BUILT_MODULE):

    @echo "Generate: $@"

    @mkdir -p $(dir $@)

    $(hide) echo -n > $@

    $(hide) $(foreach lib,$(PRIVATE_VNDK_SAMEPROCESS_LIBRARIES), \

        echo $(lib).so >> $@;)

以上规则用于将llndk.libraries.txt和vndksp.libraries.txt作为模板,生成:

out/target/product/rk3399_roc_pc_plus/system/etc/llndk.libraries.29.txt

out/target/product/rk3399_roc_pc_plus/system/etc/vndksp.libraries.29.txt

里面的内容就是各种vndk的动态库名字。

 总结

通过分析了解rootdir目录Android.mk, 大部分技术你可以用我之前将的知识点来看懂,并能快速的理解Android根目录的构成。阅读系统中的源文件和编译规则, 会让你学习到更多的知识点。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/255817
推荐阅读
相关标签
  

闽ICP备14008679号