编译libdvm.so: makefile,mm

  操作系统:Ubuntu14.4  android版本:4.4  设备:nexus 5

 

  android系统的编译使用make来操作,那make呢是执行对应的makefile即android的编译系统看makefile。那什么是makefile看这里:

  makefile的语法及写法

  看完后应该对makefile有点了解了吧,你可以把它看成是一个脚本用来描述代码的编译关系。ok,不多说直接拿libdvm来练手,看对应的mankefile(/dalvik/vm/android.mk;这里只分析libdvm.so的编译):

LOCAL_PATH:= $(call my-dir)

#
# Build for the target (device).
#

ifeq ($(TARGET_CPU_SMP),true)
    target_smp_flag := -DANDROID_SMP=1
else
    target_smp_flag := -DANDROID_SMP=0
endif
host_smp_flag := -DANDROID_SMP=1

# Build the installed version (libdvm.so) first
WITH_JIT := true
include $(LOCAL_PATH)/ReconfigureDvm.mk

# Overwrite default settings
LOCAL_MODULE := libdvm
LOCAL_CFLAGS += $(target_smp_flag)

# Define WITH_ADDRESS_SANITIZER to build an ASan-instrumented version of the
# library in /system/lib/asan/libdvm.so.
ifneq ($(strip $(WITH_ADDRESS_SANITIZER)),)
    LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/asan
    LOCAL_ADDRESS_SANITIZER := true
    LOCAL_CFLAGS := $(filter-out $(CLANG_CONFIG_UNKNOWN_CFLAGS),$(LOCAL_CFLAGS))
endif

# TODO: split out the asflags.
LOCAL_ASFLAGS := $(LOCAL_CFLAGS)

include $(BUILD_SHARED_LIBRARY)

   有点迷糊是吧,因为android.mk中大量包含android宏所以有一点难理解啦。那我们再来学习下这篇文章:Android.mk 文件语法详解。我们是要生成libdvm.so对吧,那直接从结果往前推看makefile执行流程:

  include $(BUILD_SHARED_LIBRARY)    这行代码去生成libdvm.so,那我们直接看BUILD_SHARED_LIBRARY    

BUILD_SHARED_LIBRARY:是Build System提供的一个变量,指向一个GNU Makefile Script。

它负责收集自从上次调用 include $(CLEAR_VARS)  后的所有LOCAL_XXX信息。并决定编译为什么。

BUILD_STATIC_LIBRARY:编译为静态库。 
BUILD_SHARED_LIBRARY :编译为动态库 
BUILD_EXECUTABLE:编译为Native C可执行程序     

  看下BUILD_SHARED_LIBRARY究竟是个嘛玩意。在android源码主目录下有makefile:

### DO NOT EDIT THIS FILE ###
include build/core/main.mk
### DO NOT EDIT THIS FILE ###

  build/core/main.mk:

BUILD_SYSTEM := $(TOPDIR)build/core
# Set up various standard variables based on configuration # and host information. include $(BUILD_SYSTEM)
/config.mk

  config.mk:

BUILD_SHARED_LIBRARY:= $(BUILD_SYSTEM)/shared_library.mk
BUILD_EXECUTABLE:= $(BUILD_SYSTEM)/executable.mk

  原来BUILD_SHARED_LIBRARY就是build/core/share_library.mk文件:

ifeq (,$(call if-build-from-source,$(LOCAL_MODULE),$(LOCAL_PATH)))

  以上就是编译libdvm.so的makefile代码。既然有代码了,那我们执行的命令呢。

m 在源码树的根目录执行 make
mm Build 当前目录下的模块
mmm Build 指定目录下的模块

 

  下面来实践:

  其实跟编译源码步骤差不多

  1 source build/envsetup.sh

  2 lunch

   3 在源码的dalvik目录下执行mm生成新的libdvm.so或者直接make system.img生成system.img

  若生成的是libdvm.so,则按以下命令替换系统的libdvm.so(adb要有root):

  1 adb root

  2 adb remount

  3 adb push libdvm.so /data/local/tmp/libdvm.so

  4 cat /system/lib/libdvm.so > /data/local/tmp/libdvm.so-bak

  5 cat /data/local/tmp/libdvm.so > /system/lib/libdvm.so

 

 

makefile的语法及写法

Android.mk 文件语法详解

Dalvik 虚拟机编译脚本 和 源代码树

 

posted @ 2015-10-16 12:21  vendanner  阅读(1854)  评论(0编辑  收藏  举报