将linux和uboot集成到Android编译框架中

span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0; }.CodeMirror-line::-moz-selection, .CodeMirror-line > span::-moz-selection, .CodeMirror-line > span > span::-moz-selection { background: #d7d4f0; }.cm-searching {background: #ffa; background: rgba(255, 255, 0, .4);}.cm-force-border { padding-right: .1px; }@media print { .CodeMirror div.CodeMirror-cursors {visibility: hidden;}}.cm-tab-wrap-hack:after { content: ""; }span.CodeMirror-selectedtext { background: none; }.CodeMirror-activeline-background, .CodeMirror-selected {transition: visibility 0ms 100ms;}.CodeMirror-blur .CodeMirror-activeline-background, .CodeMirror-blur .CodeMirror-selected {visibility:hidden;}.CodeMirror-blur .CodeMirror-matchingbracket {color:inherit !important;outline:none !important;text-decoration:none !important;}.CodeMirror-sizer {min-height:auto !important;}
-->
li {list-style-type:decimal;}.wiz-editor-body ol.wiz-list-level2 > li {list-style-type:lower-latin;}.wiz-editor-body ol.wiz-list-level3 > li {list-style-type:lower-roman;}.wiz-editor-body li.wiz-list-align-style {list-style-position: inside; margin-left: -1em;}.wiz-editor-body blockquote {padding: 0 12px;}.wiz-editor-body blockquote > :first-child {margin-top:0;}.wiz-editor-body blockquote > :last-child {margin-bottom:0;}.wiz-editor-body img {border:0;max-width:100%;height:auto !important;margin:2px 0;}.wiz-editor-body table {border-collapse:collapse;border:1px solid #bbbbbb;}.wiz-editor-body td,.wiz-editor-body th {padding:4px 8px;border-collapse:collapse;border:1px solid #bbbbbb;min-height:28px;word-break:break-word;box-sizing: border-box;}.wiz-editor-body td > div:first-child {margin-top:0;}.wiz-editor-body td > div:last-child {margin-bottom:0;}.wiz-editor-body img.wiz-svg-image {box-shadow:1px 1px 4px #E8E8E8;}.wiz-hide {display:none !important;}
-->

环境

Android-7.1
Linux-4.14
u-boot-2018-09
 

概述

        用过高通平台的都知道,人家把Linux内核代码放到了Android代码根目录下的kernel路径下,在编译Android的时候,会连同Linux内核一块编译,最后会将编译出来的Linux内核镜像连同ramdisk.img一块打包到boot.img镜像中。下面我们仿照高通的做法,也将Linux内核代码和uboot代码集成到Android代码中。
        Android代码在编译时没有采用递归的方式,而是通过include,在Android代码根目录下生成一个巨大的Makefile。可以参考如下博客:
 
 

正文

一、建立一个新的产品

下面我们首先添加一个新产品,这个新产品是基于ARMv8架构的。

1、创建如下路径

mkdir -p device/mycompany/myproduct
mkdir -p device/mycompany/myproduct

2、在myproduct下添加如下几个文件

将linux和uboot集成到Android编译框架中
  • Android.mk
ifneq ($(filter myproduct, $(TARGET_DEVICE)),)

LOCAL_PATH := $(call my-dir)

# if some modules are built directly from this directory (not subdirectories),
# their rules should be written here. include $(call all-makefiles-under,$(LOCAL_PATH))
endif
ifneq ($(filter myproduct, $(TARGET_DEVICE)),)

LOCAL_PATH := $(call my-dir)

# if some modules are built directly from this directory (not subdirectories),
# their rules should be written here.

include $(call all-makefiles-under,$(LOCAL_PATH))
endif

  • AndroidProducts.mk
#
# This file should set PRODUCT_MAKEFILES to a list of product makefiles
# to expose to the build system. LOCAL_DIR will already be set to
# the directory containing this file.
#
# This file may not rely on the value of any variable other than
# LOCAL_DIR; do not use any conditionals, and do not look up the
# value of any variable that isn't set in this file or in a file that
# it includes.
# PRODUCT_MAKEFILES := \
$(LOCAL_DIR)/myproduct.mk
#
# This file should set PRODUCT_MAKEFILES to a list of product makefiles
# to expose to the build system. LOCAL_DIR will already be set to
# the directory containing this file.
#
# This file may not rely on the value of any variable other than
# LOCAL_DIR; do not use any conditionals, and do not look up the
# value of any variable that isn't set in this file or in a file that
# it includes.
#

PRODUCT_MAKEFILES := \
$(LOCAL_DIR)/myproduct.mk

  • myproduct.mk
#
# Inherit the full_base and device configurations
$(call inherit-product, $(SRC_TARGET_DIR)/product/full_base.mk) #
# Overrides
PRODUCT_NAME := myproduct
PRODUCT_DEVICE := myproduct
#
# Inherit the full_base and device configurations
$(call inherit-product, $(SRC_TARGET_DIR)/product/full_base.mk)

#
# Overrides
PRODUCT_NAME := myproduct
PRODUCT_DEVICE := myproduct

  • BoardConfig.mk
TARGET_NO_BOOTLOADER := true
TARGET_NO_KERNEL := true
TARGET_NO_RECOVERY := true # Primary Arch
TARGET_ARCH := arm64
TARGET_ARCH_VARIANT := armv8-a
TARGET_CPU_VARIANT := generic
TARGET_CPU_ABI := arm64-v8a # Secondary Arch
TARGET_2ND_ARCH := arm
TARGET_2ND_ARCH_VARIANT := armv7-a-neon
TARGET_2ND_CPU_VARIANT := cortex-a15
TARGET_2ND_CPU_ABI := armeabi-v7a
TARGET_2ND_CPU_ABI2 := armeabi TARGET_USES_64_BIT_BINDER := true
TARGET_SUPPORTS_32_BIT_APPS := true
TARGET_SUPPORTS_64_BIT_APPS := true TARGET_BOARD_PLATFORM := myproduct
WITH_DEXPREOPT ?= true
BUILD_EMULATOR_OPENGL := true
USE_OPENGL_RENDERER := true
ANDROID_ENABLE_RENDERSCRIPT := true # Some framework code requires this to enable BT
BOARD_HAVE_BLUETOOTH := true
BOARD_BLUETOOTH_BDROID_BUILDCFG_INCLUDE_DIR := device/generic/common/bluetooth BOARD_USES_GENERIC_AUDIO := true
USE_CAMERA_STUB := true TARGET_USERIMAGES_USE_EXT4 := true
BOARD_SYSTEMIMAGE_PARTITION_SIZE :=
BOARD_USERDATAIMAGE_PARTITION_SIZE :=
BOARD_CACHEIMAGE_PARTITION_SIZE :=
BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
BOARD_FLASH_BLOCK_SIZE :=
TARGET_NO_BOOTLOADER := true
TARGET_NO_KERNEL := true
TARGET_NO_RECOVERY := true

# Primary Arch
TARGET_ARCH := arm64
TARGET_ARCH_VARIANT := armv8-a
TARGET_CPU_VARIANT := generic
TARGET_CPU_ABI := arm64-v8a

# Secondary Arch
TARGET_2ND_ARCH := arm
TARGET_2ND_ARCH_VARIANT := armv7-a-neon
TARGET_2ND_CPU_VARIANT := cortex-a15
TARGET_2ND_CPU_ABI := armeabi-v7a
TARGET_2ND_CPU_ABI2 := armeabi

TARGET_USES_64_BIT_BINDER := true
TARGET_SUPPORTS_32_BIT_APPS := true
TARGET_SUPPORTS_64_BIT_APPS := true

TARGET_BOARD_PLATFORM := myproduct
WITH_DEXPREOPT ?= true
BUILD_EMULATOR_OPENGL := true
USE_OPENGL_RENDERER := true
ANDROID_ENABLE_RENDERSCRIPT := true

# Some framework code requires this to enable BT
BOARD_HAVE_BLUETOOTH := true
BOARD_BLUETOOTH_BDROID_BUILDCFG_INCLUDE_DIR := device/generic/common/bluetooth

BOARD_USES_GENERIC_AUDIO := true
USE_CAMERA_STUB := true

TARGET_USERIMAGES_USE_EXT4 := true
BOARD_SYSTEMIMAGE_PARTITION_SIZE := 1610612736
BOARD_USERDATAIMAGE_PARTITION_SIZE := 1595915776
BOARD_CACHEIMAGE_PARTITION_SIZE := 268435456
BOARD_CACHEIMAGE_FILE_SYSTEM_TYPE := ext4
BOARD_FLASH_BLOCK_SIZE := 512

  • vendorsetup.sh
#
# Copyright (C) 2013 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# This file is executed by build/envsetup.sh, and can use anything
# defined in envsetup.sh.
#
# In particular, you can add lunch options with the add_lunch_combo
# function: add_lunch_combo generic-eng

add_lunch_combo myproduct-userdebug

#
# Copyright (C) The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# # This file is executed by build/envsetup.sh, and can use anything
# defined in envsetup.sh.
#
# In particular, you can add lunch options with the add_lunch_combo
# function: add_lunch_combo generic-eng add_lunch_combo myproduct-userdebug

3、编译

  • build/envsetup.sh
将linux和uboot集成到Android编译框架中
  • lunch
pengdonglin@pengdonglin-dell:~/Android7_1$ lunch

You're building on Linux

Lunch menu... pick a combo:
. aosp_arm-eng
. aosp_arm64-eng
. aosp_mips-eng
. aosp_mips64-eng
. aosp_x86-eng
. aosp_x86_64-eng
. full_fugu-userdebug
. aosp_fugu-userdebug
. mini_emulator_arm64-userdebug
. m_e_arm-userdebug
. m_e_mips64-eng
. m_e_mips-userdebug
. mini_emulator_x86_64-userdebug
. mini_emulator_x86-userdebug
. aosp_dragon-userdebug
. aosp_dragon-eng
. aosp_marlin-userdebug
. aosp_sailfish-userdebug
. aosp_flounder-userdebug
. aosp_angler-userdebug
. aosp_bullhead-userdebug
. hikey-userdebug
. aosp_shamu-userdebug
. myproduct-userdebug Which would you like? [aosp_arm-eng] ============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.
TARGET_PRODUCT=myproduct
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv7-a-neon
TARGET_2ND_CPU_VARIANT=cortex-a15
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.15.--generic-x86_64-with-Ubuntu-18.04-bionic
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=N2G48H
OUT_DIR=out
============================================
pengdonglin@pengdonglin-dell:~/Android7_1$ lunch

You're building on Linux

Lunch menu... pick a combo:
1. aosp_arm-eng
2. aosp_arm64-eng
3. aosp_mips-eng
4. aosp_mips64-eng
5. aosp_x86-eng
6. aosp_x86_64-eng
7. full_fugu-userdebug
8. aosp_fugu-userdebug
9. mini_emulator_arm64-userdebug
10. m_e_arm-userdebug
11. m_e_mips64-eng
12. m_e_mips-userdebug
13. mini_emulator_x86_64-userdebug
14. mini_emulator_x86-userdebug
15. aosp_dragon-userdebug
16. aosp_dragon-eng
17. aosp_marlin-userdebug
18. aosp_sailfish-userdebug
19. aosp_flounder-userdebug
20. aosp_angler-userdebug
21. aosp_bullhead-userdebug
22. hikey-userdebug
23. aosp_shamu-userdebug
24. myproduct-userdebug

Which would you like? [aosp_arm-eng] 24

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.2
TARGET_PRODUCT=myproduct
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv7-a-neon
TARGET_2ND_CPU_VARIANT=cortex-a15
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.15.0-48-generic-x86_64-with-Ubuntu-18.04-bionic
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=N2G48H
OUT_DIR=out
============================================

  • make
pengdonglin@pengdonglin-dell:~/Android7_1$ make
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.2
TARGET_PRODUCT=myproduct
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv7-a-neon
TARGET_2ND_CPU_VARIANT=cortex-a15
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.15.0-48-generic-x86_64-with-Ubuntu-18.04-bionic
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=N2G48H
OUT_DIR=out
============================================
Running kati to generate build-myproduct.ninja...
out/build-myproduct.ninja is missing, regenerating...
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.2
TARGET_PRODUCT=myproduct
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv7-a-neon
TARGET_2ND_CPU_VARIANT=cortex-a15
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.15.0-48-generic-x86_64-with-Ubuntu-18.04-bionic
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=N2G48H
OUT_DIR=out
============================================
FindEmulator: find: File system loop detected; `./linux-4.14/out64/source' is part of the same file system loop as `./linux-4.14'.
FindEmulator: find: File system loop detected; `./linux-4.14/out64/source' is part of the same file system loop as `./linux-4.14'.
including ./abi/cpp/Android.mk ...
including ./art/Android.mk ...
including ./bionic/Android.mk ...
including ./bootable/recovery/Android.mk ...
including ./build/libs/host/Android.mk ...
... ...
pengdonglin@pengdonglin-dell:~/Android7_1$ make
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.
TARGET_PRODUCT=myproduct
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv7-a-neon
TARGET_2ND_CPU_VARIANT=cortex-a15
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.15.--generic-x86_64-with-Ubuntu-18.04-bionic
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=N2G48H
OUT_DIR=out
============================================
Running kati to generate build-myproduct.ninja...
out/build-myproduct.ninja is missing, regenerating...
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.
TARGET_PRODUCT=myproduct
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv7-a-neon
TARGET_2ND_CPU_VARIANT=cortex-a15
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.15.--generic-x86_64-with-Ubuntu-18.04-bionic
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=N2G48H
OUT_DIR=out
============================================
FindEmulator: find: File system loop detected; `./linux-4.14/out64/source' is part of the same file system loop as `./linux-4.14'.
FindEmulator: find: File system loop detected; `./linux-4.14/out64/source' is part of the same file system loop as `./linux-4.14'.
including ./abi/cpp/Android.mk ...
including ./art/Android.mk ...
including ./bionic/Android.mk ...
including ./bootable/recovery/Android.mk ...
including ./build/libs/host/Android.mk ...
... ...

二、集成Linux内核源码到Android代码中

1、将Linux内核代码拷贝到Android代码根目录下并重命名为kernel

将linux和uboot集成到Android编译框架中
 

2、添加AndroidKernel.mk到kernel中

AndroidKernel.mk可以参考:AndroidKernel.mk
内容如下:
#Android makefile to build kernel as a part of Android Build
PERL = perl KERNEL_TARGET := $(strip $(INSTALLED_KERNEL_TARGET))
ifeq ($(KERNEL_TARGET),)
INSTALLED_KERNEL_TARGET := $(PRODUCT_OUT)/kernel
endif TARGET_KERNEL_ARCH := $(strip $(TARGET_KERNEL_ARCH))
ifeq ($(TARGET_KERNEL_ARCH),)
KERNEL_ARCH := arm
else
KERNEL_ARCH := $(TARGET_KERNEL_ARCH)
endif TARGET_KERNEL_HEADER_ARCH := $(strip $(TARGET_KERNEL_HEADER_ARCH))
ifeq ($(TARGET_KERNEL_HEADER_ARCH),)
KERNEL_HEADER_ARCH := $(KERNEL_ARCH)
else
$(warning Forcing kernel header generation only for '$(TARGET_KERNEL_HEADER_ARCH)')
KERNEL_HEADER_ARCH := $(TARGET_KERNEL_HEADER_ARCH)
endif KERNEL_HEADER_DEFCONFIG := $(strip $(KERNEL_HEADER_DEFCONFIG))
ifeq ($(KERNEL_HEADER_DEFCONFIG),)
KERNEL_HEADER_DEFCONFIG := $(KERNEL_DEFCONFIG)
endif # Force -bit binder IPC for 64bit kernel with 32bit userspace
ifeq ($(KERNEL_ARCH),arm64)
ifeq ($(TARGET_ARCH),arm)
KERNEL_CONFIG_OVERRIDE := CONFIG_ANDROID_BINDER_IPC_32BIT=y
endif
endif TARGET_KERNEL_CROSS_COMPILE_PREFIX := $(strip $(TARGET_KERNEL_CROSS_COMPILE_PREFIX))
ifeq ($(TARGET_KERNEL_CROSS_COMPILE_PREFIX),)
KERNEL_CROSS_COMPILE := arm-eabi-
else
KERNEL_CROSS_COMPILE := $(TARGET_KERNEL_CROSS_COMPILE_PREFIX)
endif ifeq ($(TARGET_PREBUILT_KERNEL),) KERNEL_GCC_NOANDROID_CHK := $(shell (echo "int main() {return 0;}" | $(KERNEL_CROSS_COMPILE)gcc -E -mno-android - > /dev/null >& ; echo $$?))
ifeq ($(strip $(KERNEL_GCC_NOANDROID_CHK)),)
KERNEL_CFLAGS := KCFLAGS=-mno-android
endif KERNEL_OUT := $(TARGET_OUT_INTERMEDIATES)/KERNEL_OBJ
KERNEL_CONFIG := $(KERNEL_OUT)/.config ifeq ($(KERNEL_DEFCONFIG)$(wildcard $(KERNEL_CONFIG)),)
$(error Kernel configuration not defined, cannot build kernel)
else ifeq ($(TARGET_USES_UNCOMPRESSED_KERNEL),true)
$(info Using uncompressed kernel)
TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/Image
else
ifeq ($(KERNEL_ARCH),arm64)
TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/Image.gz
else
TARGET_PREBUILT_INT_KERNEL := $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/zImage
endif
endif ifeq ($(TARGET_KERNEL_APPEND_DTB), true)
$(info Using appended DTB)
TARGET_PREBUILT_INT_KERNEL := $(TARGET_PREBUILT_INT_KERNEL)-dtb
endif KERNEL_HEADERS_INSTALL := $(KERNEL_OUT)/usr
KERNEL_MODULES_INSTALL := system
KERNEL_MODULES_OUT := $(TARGET_OUT)/lib/modules TARGET_PREBUILT_KERNEL := $(TARGET_PREBUILT_INT_KERNEL) define mv-modules
mdpath=`find $(KERNEL_MODULES_OUT) -type f -name modules.dep`;\
if [ "$$mdpath" != "" ];then\
mpath=`dirname $$mdpath`;\
ko=`find $$mpath/kernel -type f -name *.ko`;\
for i in $$ko; do mv $$i $(KERNEL_MODULES_OUT)/; done;\
fi
endef define clean-module-folder
mdpath=`find $(KERNEL_MODULES_OUT) -type f -name modules.dep`;\
if [ "$$mdpath" != "" ];then\
mpath=`dirname $$mdpath`; rm -rf $$mpath;\
fi
endef $(KERNEL_OUT):
mkdir -p $(KERNEL_OUT) $(KERNEL_CONFIG): $(KERNEL_OUT)
$(MAKE) -C kernel O=../$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(KERNEL_DEFCONFIG)
$(hide) if [ ! -z "$(KERNEL_CONFIG_OVERRIDE)" ]; then \
echo "Overriding kernel config with '$(KERNEL_CONFIG_OVERRIDE)'"; \
echo $(KERNEL_CONFIG_OVERRIDE) >> $(KERNEL_OUT)/.config; \
$(MAKE) -C kernel O=../$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) oldconfig; fi $(TARGET_PREBUILT_INT_KERNEL): $(KERNEL_OUT) $(KERNEL_HEADERS_INSTALL)
$(hide) echo "Building kernel..."
$(hide) rm -rf $(KERNEL_OUT)/arch/$(KERNEL_ARCH)/boot/dts
$(MAKE) -C kernel O=../$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(KERNEL_CFLAGS)
$(MAKE) -C kernel O=../$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(KERNEL_CFLAGS) modules
$(MAKE) -C kernel O=../$(KERNEL_OUT) INSTALL_MOD_PATH=../../$(KERNEL_MODULES_INSTALL) INSTALL_MOD_STRIP= ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) modules_install
$(mv-modules)
$(clean-module-folder) $(KERNEL_HEADERS_INSTALL): $(KERNEL_OUT)
$(hide) if [ ! -z "$(KERNEL_HEADER_DEFCONFIG)" ]; then \
$(hide) rm -f ../$(KERNEL_CONFIG); \
$(MAKE) -C kernel O=../$(KERNEL_OUT) ARCH=$(KERNEL_HEADER_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(KERNEL_HEADER_DEFCONFIG); \
$(MAKE) -C kernel O=../$(KERNEL_OUT) ARCH=$(KERNEL_HEADER_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) headers_install; fi
$(hide) if [ "$(KERNEL_HEADER_DEFCONFIG)" != "$(KERNEL_DEFCONFIG)" ]; then \
echo "Used a different defconfig for header generation"; \
$(hide) rm -f ../$(KERNEL_CONFIG); \
$(MAKE) -C kernel O=../$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) $(KERNEL_DEFCONFIG); fi
$(hide) if [ ! -z "$(KERNEL_CONFIG_OVERRIDE)" ]; then \
echo "Overriding kernel config with '$(KERNEL_CONFIG_OVERRIDE)'"; \
echo $(KERNEL_CONFIG_OVERRIDE) >> $(KERNEL_OUT)/.config; \
$(MAKE) -C kernel O=../$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) oldconfig; fi kerneltags: $(KERNEL_OUT) $(KERNEL_CONFIG)
$(MAKE) -C kernel O=../$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) tags kernelconfig: $(KERNEL_OUT) $(KERNEL_CONFIG)
env KCONFIG_NOTIMESTAMP=true \
$(MAKE) -C kernel O=../$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) menuconfig
env KCONFIG_NOTIMESTAMP=true \
$(MAKE) -C kernel O=../$(KERNEL_OUT) ARCH=$(KERNEL_ARCH) CROSS_COMPILE=$(KERNEL_CROSS_COMPILE) savedefconfig
cp $(KERNEL_OUT)/defconfig kernel/arch/$(KERNEL_ARCH)/configs/$(KERNEL_DEFCONFIG) endif
endif

3、在device/mycompany/myproduct/中添加配置文件AndroidBoard.mk

这个AndroidBoard.mk文件会被build/target/board/Android.mk包含。
内容如下:
TARGET_KERNEL_ARCH := arm64
TARGET_KERNEL_CROSS_COMPILE_PREFIX := aarch64-linux-android-
TARGET_USES_UNCOMPRESSED_KERNEL := true

KERNEL_DEFCONFIG := defconfig

include kernel/AndroidKernel.mk

$(INSTALLED_KERNEL_TARGET): $(TARGET_PREBUILT_KERNEL) | $(ACP)
$(transform-prebuilt-to-target)

TARGET_KERNEL_ARCH := arm64
TARGET_KERNEL_CROSS_COMPILE_PREFIX := aarch64-linux-android-
TARGET_USES_UNCOMPRESSED_KERNEL := true KERNEL_DEFCONFIG := defconfig include kernel/AndroidKernel.mk $(INSTALLED_KERNEL_TARGET): $(TARGET_PREBUILT_KERNEL) | $(ACP)
$(transform-prebuilt-to-target)

4、修改device/mycompany/myproduct/BoardConfig.mk

将TARGET_NO_KERNEL的值修改为false。
将linux和uboot集成到Android编译框架中

5、编译

. build/envsetup.sh
lunch 24
make bootimage

. build/envsetup.sh
lunch 24
make bootimage

将linux和uboot集成到Android编译框架中

6、添加kernelclean目标

在进行clean的时候,没有必要include那些Android.mk,可以加快执行速度,在build/core/main.mk中提供了一个变量dont_bother_goals,其中的目标在编译时会略过加载Android.mk。
修改build/core/main.mk:
diff --git a/core/main.mk b/core/main.mk
index a612f835d..e333a17d5 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -87,6 +87,8 @@ dont_bother_goals := clean clobber dataclean installclean \
recoveryimage-nodeps \
product-graph dump-products

+dont_bother_goals += kernel-clean
+
ifneq ($(filter $(dont_bother_goals), $(MAKECMDGOALS)),)
dont_bother := true
endif
@@ -1115,6 +1117,11 @@ clean:
@rm -rf $(OUT_DIR)/*
@echo "Entire build directory removed."

+.PHONY: kernel-clean
+kernel-clean:
+ @echo "Cleaning kernel..."
+ @rm -rf $(PRODUCT_OUT)/obj/KERNEL_OBJ/
+
.PHONY: clobber
clobber: clean

diff --git a/core/main.mk b/core/main.mk
index a612f835d..e333a17d5
--- a/core/main.mk
+++ b/core/main.mk
@@ -, +, @@ dont_bother_goals := clean clobber dataclean installclean \
recoveryimage-nodeps \
product-graph dump-products +dont_bother_goals += kernel-clean
+
ifneq ($(filter $(dont_bother_goals), $(MAKECMDGOALS)),)
dont_bother := true
endif
@@ -, +, @@ clean:
@rm -rf $(OUT_DIR)/*
@echo "Entire build directory removed." +.PHONY: kernel-clean
+kernel-clean:
+ @echo "Cleaning kernel..."
+ @rm -rf $(PRODUCT_OUT)/obj/KERNEL_OBJ/
+
.PHONY: clobber
clobber: clean
 
测试
pengdonglin@pengdonglin-dell:~/Android7_1$ make kernel-clean
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.2
TARGET_PRODUCT=myproduct
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv7-a-neon
TARGET_2ND_CPU_VARIANT=cortex-a15
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.15.0-50-generic-x86_64-with-Ubuntu-18.04-bionic
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=N2G48H
OUT_DIR=out
============================================
Running kati to generate build-myproduct-kernel-clean.ninja...
No need to regenerate ninja file
Starting build with ninja
ninja: Entering directory `.'
[100% 1/1] Cleaning kernel...

#### make completed successfully (2 seconds) ####

pengdonglin@pengdonglin-dell:~/Android7_1$ make kernel-clean
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.
TARGET_PRODUCT=myproduct
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv7-a-neon
TARGET_2ND_CPU_VARIANT=cortex-a15
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.15.--generic-x86_64-with-Ubuntu-18.04-bionic
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=N2G48H
OUT_DIR=out
============================================
Running kati to generate build-myproduct-kernel-clean.ninja...
No need to regenerate ninja file
Starting build with ninja
ninja: Entering directory `.'
[% /] Cleaning kernel... #### make completed successfully ( seconds) ####

三、将U-Boot代码集成到Android编译系统中

1、将u-boot-2018.09拷贝到bootable下,并重命名为u-boot

2、在u-boot下添加AndroidU-boot.mk

U-BOOT_TARGET := $(strip $(INSTALLED_U-BOOT_TARGET))
ifeq ($(U-BOOT_TARGET),)
INSTALLED_U-BOOT_TARGET := $(PRODUCT_OUT)/u-boot.bin
endif

TARGET_U-BOOT_ARCH := $(strip $(TARGET_U-BOOT_ARCH))

TARGET_U-BOOT_CROSS_COMPILE_PREFIX := $(strip $(TARGET_U-BOOT_CROSS_COMPILE_PREFIX))

U-BOOT_DEFCONFIG := $(strip $(U-BOOT_DEFCONFIG))
U-BOOT_OUT := $(TARGET_OUT_INTERMEDIATES)/U-BOOT_OBJ
U-BOOT_CONFIG := $(U-BOOT_OUT)/.config

TARGET_PREBUILT_INT_U-BOOT := $(U-BOOT_OUT)/u-boot.bin

TARGET_PREBUILT_U-BOOT := $(TARGET_PREBUILT_INT_U-BOOT)

$(U-BOOT_OUT):
mkdir -p $(U-BOOT_OUT)

$(U-BOOT_CONFIG): $(U-BOOT_OUT)
$(MAKE) -C bootable/u-boot O=../../$(U-BOOT_OUT) ARCH=$(TARGET_U-BOOT_ARCH) CROSS_COMPILE=$(TARGET_U-BOOT_CROSS_COMPILE_PREFIX) $(U-BOOT_DEFCONFIG)

$(TARGET_PREBUILT_INT_U-BOOT): $(U-BOOT_OUT) $(U-BOOT_CONFIG)
$(hide) echo "Building u-boot..."
$(MAKE) -C bootable/u-boot O=../../$(U-BOOT_OUT) ARCH=$(TARGET_U-BOOT_ARCH) CROSS_COMPILE=$(TARGET_U-BOOT_CROSS_COMPILE_PREFIX)

U-BOOT_TARGET := $(strip $(INSTALLED_U-BOOT_TARGET))
ifeq ($(U-BOOT_TARGET),)
INSTALLED_U-BOOT_TARGET := $(PRODUCT_OUT)/u-boot.bin
endif TARGET_U-BOOT_ARCH := $(strip $(TARGET_U-BOOT_ARCH)) TARGET_U-BOOT_CROSS_COMPILE_PREFIX := $(strip $(TARGET_U-BOOT_CROSS_COMPILE_PREFIX)) U-BOOT_DEFCONFIG := $(strip $(U-BOOT_DEFCONFIG))
U-BOOT_OUT := $(TARGET_OUT_INTERMEDIATES)/U-BOOT_OBJ
U-BOOT_CONFIG := $(U-BOOT_OUT)/.config TARGET_PREBUILT_INT_U-BOOT := $(U-BOOT_OUT)/u-boot.bin TARGET_PREBUILT_U-BOOT := $(TARGET_PREBUILT_INT_U-BOOT) $(U-BOOT_OUT):
mkdir -p $(U-BOOT_OUT) $(U-BOOT_CONFIG): $(U-BOOT_OUT)
$(MAKE) -C bootable/u-boot O=../../$(U-BOOT_OUT) ARCH=$(TARGET_U-BOOT_ARCH) CROSS_COMPILE=$(TARGET_U-BOOT_CROSS_COMPILE_PREFIX) $(U-BOOT_DEFCONFIG) $(TARGET_PREBUILT_INT_U-BOOT): $(U-BOOT_OUT) $(U-BOOT_CONFIG)
$(hide) echo "Building u-boot..."
$(MAKE) -C bootable/u-boot O=../../$(U-BOOT_OUT) ARCH=$(TARGET_U-BOOT_ARCH) CROSS_COMPILE=$(TARGET_U-BOOT_CROSS_COMPILE_PREFIX)

3、修改device/mycompany/myproduct/AndroidBoard.mk

追加如下内容:
TARGET_U-BOOT_ARCH := arm
TARGET_U-BOOT_CROSS_COMPILE_PREFIX := aarch64-linux-android-
U-BOOT_DEFCONFIG := qemu_arm64_defconfig

include bootable/u-boot/AndroidU-boot.mk

$(INSTALLED_U-BOOT_TARGET): $(TARGET_PREBUILT_U-BOOT) | $(ACP)
$(transform-prebuilt-to-target)

TARGET_U-BOOT_ARCH := arm
TARGET_U-BOOT_CROSS_COMPILE_PREFIX := aarch64-linux-android-
U-BOOT_DEFCONFIG := qemu_arm64_defconfig include bootable/u-boot/AndroidU-boot.mk $(INSTALLED_U-BOOT_TARGET): $(TARGET_PREBUILT_U-BOOT) | $(ACP)
$(transform-prebuilt-to-target)

4、修改device/mycompany/myproduct/BoardConfig.mk

添加如下内容:
TARGET_NO_U-BOOT := false
TARGET_NO_U-BOOT := false

5、修改build/core/Makefile

diff --git a/core/Makefile b/core/Makefile
index d8b4ef89b..4b8f8f559 100644
--- a/core/Makefile
+++ b/core/Makefile
@@ -464,6 +464,11 @@ ifneq ($(strip $(TARGET_NO_KERNEL)),true)
else
INSTALLED_KERNEL_TARGET :=
endif
+ifneq ($(strip $(TARGET_NO_U-BOOT)),true)
+ INSTALLED_U-BOOT_TARGET := $(PRODUCT_OUT)/u-boot.bin
+else
+ INSTALLED_U-BOOT_TARGET :=
+endif

# -----------------------------------------------------------------
# the ramdisk

diff --git a/core/Makefile b/core/Makefile
index d8b4ef89b..4b8f8f559
--- a/core/Makefile
+++ b/core/Makefile
@@ -, +, @@ ifneq ($(strip $(TARGET_NO_KERNEL)),true)
else
INSTALLED_KERNEL_TARGET :=
endif
+ifneq ($(strip $(TARGET_NO_U-BOOT)),true)
+ INSTALLED_U-BOOT_TARGET := $(PRODUCT_OUT)/u-boot.bin
+else
+ INSTALLED_U-BOOT_TARGET :=
+endif # -----------------------------------------------------------------
# the ramdisk

6、修改build/core/main.mk

diff --git a/core/main.mk b/core/main.mk
index a612f835d..bac8ef18d 100644
--- a/core/main.mk
+++ b/core/main.mk
@@ -87,6 +87,9 @@ dont_bother_goals := clean clobber dataclean installclean \
recoveryimage-nodeps \
product-graph dump-products

+dont_bother_goals += u-boot-clean
+dont_bother_goals += kernel-clean
+
ifneq ($(filter $(dont_bother_goals), $(MAKECMDGOALS)),)
dont_bother := true
endif
@@ -907,6 +910,9 @@ endif
.PHONY: ramdisk
ramdisk: $(INSTALLED_RAMDISK_TARGET)

+.PHONY: u-boot
+u-boot: $(INSTALLED_U-BOOT_TARGET)
+
.PHONY: systemtarball
systemtarball: $(INSTALLED_SYSTEMTARBALL_TARGET)

@@ -1115,6 +1121,16 @@ clean:
@rm -rf $(OUT_DIR)/*
@echo "Entire build directory removed."

+.PHONY: u-boot-clean
+u-boot-clean:
+ @echo "Cleaning u-boot..."
+ @rm -rf $(PRODUCT_OUT)/obj/U-BOOT_OBJ/
+
+.PHONY: kernel-clean
+kernel-clean:
+ @echo "Cleaning kernel..."
+ @rm -rf $(PRODUCT_OUT)/obj/KERNEL_OBJ/
+
.PHONY: clobber
clobber: clean

diff --git a/core/main.mk b/core/main.mk
index a612f835d..bac8ef18d
--- a/core/main.mk
+++ b/core/main.mk
@@ -, +, @@ dont_bother_goals := clean clobber dataclean installclean \
recoveryimage-nodeps \
product-graph dump-products +dont_bother_goals += u-boot-clean
+dont_bother_goals += kernel-clean
+
ifneq ($(filter $(dont_bother_goals), $(MAKECMDGOALS)),)
dont_bother := true
endif
@@ -, +, @@ endif
.PHONY: ramdisk
ramdisk: $(INSTALLED_RAMDISK_TARGET) +.PHONY: u-boot
+u-boot: $(INSTALLED_U-BOOT_TARGET)
+
.PHONY: systemtarball
systemtarball: $(INSTALLED_SYSTEMTARBALL_TARGET) @@ -, +, @@ clean:
@rm -rf $(OUT_DIR)/*
@echo "Entire build directory removed." +.PHONY: u-boot-clean
+u-boot-clean:
+ @echo "Cleaning u-boot..."
+ @rm -rf $(PRODUCT_OUT)/obj/U-BOOT_OBJ/
+
+.PHONY: kernel-clean
+kernel-clean:
+ @echo "Cleaning kernel..."
+ @rm -rf $(PRODUCT_OUT)/obj/KERNEL_OBJ/
+
.PHONY: clobber
clobber: clean

7、测试

. build/envsetup.sh
lunch 24
make u-boot
OBJCOPY examples/standalone/hello_world.srec
OBJCOPY examples/standalone/hello_world.bin
LD u-boot
OBJCOPY u-boot.srec
OBJCOPY u-boot-nodtb.bin
SYM u-boot.sym
start=$(aarch64-linux-android-nm u-boot | grep __rel_dyn_start | cut -f 1 -d ' '); end=$(aarch64-linux-android-nm u-boot | grep __rel_dyn_end | cut -f 1 -d ' '); tools/relocate-rela u-boot-nodtb.bin 0x00000000 $start $end
COPY u-boot.bin
CFGCHK u-boot.cfg
make[1]: Leaving directory '/home/pengdonglin/Android7_1/out/target/product/myproduct/obj/U-BOOT_OBJ'
make: Leaving directory '/home/pengdonglin/Android7_1/bootable/u-boot'
[100% 4/4] target Prebuilt: (out/target/product/myproduct/u-boot.bin)
OBJCOPY examples/standalone/hello_world.srec
OBJCOPY examples/standalone/hello_world.bin
LD u-boot
OBJCOPY u-boot.srec
OBJCOPY u-boot-nodtb.bin
SYM u-boot.sym
start=$(aarch64-linux-android-nm u-boot | grep __rel_dyn_start | cut -f -d ' '); end=$(aarch64-linux-android-nm u-boot | grep __rel_dyn_end | cut -f -d ' '); tools/relocate-rela u-boot-nodtb.bin 0x00000000 $start $end
COPY u-boot.bin
CFGCHK u-boot.cfg
make[]: Leaving directory '/home/pengdonglin/Android7_1/out/target/product/myproduct/obj/U-BOOT_OBJ'
make: Leaving directory '/home/pengdonglin/Android7_1/bootable/u-boot'
[% /] target Prebuilt: (out/target/product/myproduct/u-boot.bin)
 
make u-boot-clean
pengdonglin@pengdonglin-dell:~/Android7_1$ make u-boot-clean
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.2
TARGET_PRODUCT=myproduct
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv7-a-neon
TARGET_2ND_CPU_VARIANT=cortex-a15
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.15.0-50-generic-x86_64-with-Ubuntu-18.04-bionic
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=N2G48H
OUT_DIR=out
============================================
Running kati to generate build-myproduct-u-boot-clean.ninja...
No need to regenerate ninja file
Starting build with ninja
ninja: Entering directory `.'
[100% 1/1] Cleaning u-boot...

#### make completed successfully ####

pengdonglin@pengdonglin-dell:~/Android7_1$ make u-boot-clean
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.
TARGET_PRODUCT=myproduct
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv7-a-neon
TARGET_2ND_CPU_VARIANT=cortex-a15
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.15.--generic-x86_64-with-Ubuntu-18.04-bionic
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=N2G48H
OUT_DIR=out
============================================
Running kati to generate build-myproduct-u-boot-clean.ninja...
No need to regenerate ninja file
Starting build with ninja
ninja: Entering directory `.'
[% /] Cleaning u-boot... #### make completed successfully ####
完。
上一篇:Delphi XE4 TStringHelper用法详解


下一篇:IO多路复用之epoll总结