Android系统编译流程详解(二)

目录

第一篇:android源码编译(ubuntu16.04 64位)
第二篇:编译补充(关于下载代码和内存不足问题)
第三篇:Android系统编译流程详解(一)
第四篇:Android系统编译流程详解(二)

编译源码步骤

google给出的编译步骤如下:

  1. source build/envsetup.sh:加载命令
  2. lunch:选择平台编译选项
  3. make:执行编译
    那么每一步都做了什么呢?

build/envsetup.sh

打开build/envsetup.sh文件:
(Android P版本下)


image.png

可以看到有许多的函数.

函数名 含义
hmm() 显示帮助信息
lunch 配置lunch
tapas tapas
croot 回到根目录
m make from top
mm Builds all of the modules in the current directory,
mma Builds all of the modules in the current directory
mmma Builds all of the modules in the supplied directories,
cgrep 查找c/c++文件
ggrep 查找所有的文件
jgrep 查找java文件
resgrep 查找xml文件
mangrep 查找Manifest.xml文件
mgrep 查找所有的mk文件
sepgrep 查找sepolicy文件
sgrep 查找源码文件
godir 跳转指定目录
get_abs_build_var 获取绝对变量
get_build_var 获取绝对变量
check_product 检查product
check_variant 检查变量
setpaths 设置文件路径
printconfig 打印配置
set_stuff_for_environment 设置环境变量
set_sequence_number 设置序号
settitle 设置标题
choosetype 设置type
chooseproduct 设置product
choosevariant 设置variant
tapas 功能同choosecombo
choosecombo 设置编译参数

有兴趣的可以看一下源码.O(∩_∩)O

执行:
source build/envsetup.sh 

在脚本最后,执行以下代码,来加载各个区域的vendorsetup.sh文件.

 if [ "x$SHELL" != "x/bin/bash" ]; then
     case `ps -o command -p $$` in
         *bash*)
             ;;
         *)
             echo "WARNING: Only bash is supported, use of other shell would lead to err     oneous results"
             ;;
     esac
 fi
 
 # Execute the contents of any vendorsetup.sh files we can find.
 for f in `test -d device && find -L device -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/n     ull | sort` \
          `test -d vendor && find -L vendor -maxdepth 4 -name 'vendorsetup.sh' 2> /dev/n     ull | sort` \
          `test -d product && find -L product -maxdepth 4 -name 'vendorsetup.sh' 2> /dev     /null | sort`
 do
     echo "including $f"
     . $f
 done

查看下device/google/marlin/vendorsetup.sh文件,可以看到:

image.png

文件中调用了add_lunch_combo命令.
add_lunch_combo命令的定义在文件build/envsetup.sh中:

  function add_lunch_combo()
  {
      local new_combo=$1
      local c
      for c in ${LUNCH_MENU_CHOICES[@]} ; do
          if [ "$new_combo" = "$c" ] ; then
              return
          fi
      done
      LUNCH_MENU_CHOICES=(${LUNCH_MENU_CHOICES[@]} $new_combo)
  }

可以看出,编译时,就是通过add_lunch_combo将编译选项传递给lunch的.

lunch

先来看下lunch的实现:

function lunch()
{
    local answer

    if [ "$1" ] ; then
        answer=$1
    else
        print_lunch_menu
        echo -n "Which would you like? [aosp_arm-eng] "
        read answer
    fi

    local selection=

    if [ -z "$answer" ]
    then
        selection=aosp_arm-eng
    elif (echo -n $answer | grep -q -e "^[0-9][0-9]*$")
    then
        if [ $answer -le ${#LUNCH_MENU_CHOICES[@]} ]
        then
            selection=${LUNCH_MENU_CHOICES[$(($answer-1))]}
        fi
    else
        selection=$answer
    fi

    export TARGET_BUILD_APPS=

    local product variant_and_version variant version

    product=${selection%%-*} # Trim everything after first dash
    variant_and_version=${selection#*-} # Trim everything up to first dash
    if [ "$variant_and_version" != "$selection" ]; then
        variant=${variant_and_version%%-*}
        if [ "$variant" != "$variant_and_version" ]; then
            version=${variant_and_version#*-}
        fi
    fi

    if [ -z "$product" ]
    then
        echo
        echo "Invalid lunch combo: $selection"
        return 1
    fi

    TARGET_PRODUCT=$product \
    TARGET_BUILD_VARIANT=$variant \
    TARGET_PLATFORM_VERSION=$version \
    build_build_var_cache
    if [ $? -ne 0 ]
    then
        return 1
    fi

    export TARGET_PRODUCT=$(get_build_var TARGET_PRODUCT)
    export TARGET_BUILD_VARIANT=$(get_build_var TARGET_BUILD_VARIANT)
    export TARGET_PLATFORM_VERSION=$(get_build_var TARGET_PLATFORM_VERSION)
    export TARGET_BUILD_TYPE=release

    echo

    set_stuff_for_environment
    printconfig
    destroy_build_var_cache
}

lunch命令是envsetup.sh里定义的一个命令,用来让用户选择编译项,来定义Product和编译过程中用到的全局量

liunch大致实现了导出一些重要的环境变量,从而影响编译系统的编译结果。


image.png

接下来就是make了.

make的内容比较多,明天再继续make.O(∩_∩)O

打完收工.

参考

Android编译过程详解(一)

你可能感兴趣的:(Android系统编译流程详解(二))