子目录Makefile分析

Posted 四季帆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了子目录Makefile分析相关的知识,希望对你有一定的参考价值。

1. 前言

        鸿蒙暂时支持以下几种文件系统,以fat 文件系统为例,看看fat 文件系统是如何被编译到鸿蒙系统中,进而支持fat 文件系统的。

        

2. fat目录下的Makfile文件

include $(LITEOSTOPDIR)/config.mk         #包含顶层目录下的config.mk,即liteos_a/config.mk

MODULE_NAME := $(notdir $(shell pwd))     #打印当前路径然后去掉目录信息,即剩下fat

LOCAL_SRCS := $(wildcard os_adapt/*.c)    #获取工作目录下os_adapt/下的所有.c文件
LOCAL_SRCS += $(wildcard $(LITEOSTHIRDPARTY)/FatFs/source/*.c)  #获取OpenHarmony/third_party/FatFs/source/下的所有.c文件

LOCAL_INCLUDE := \\
    -I $(LITEOSTHIRDPARTY)/FatFs/source \\
    -I $(LITEOSTOPDIR)/fs/fat/virpart/include     #指定包含头文件的路径

LOCAL_FLAGS := $(LOCAL_INCLUDE) $(LITEOS_GCOV_OPTS)     #本地编译选项

include $(MODULE)                         #即包含liteos_a/tools/build/mk/module.mk

3. config.mk

        这个makefile中就是一些配置项,没什么东西。

-include $(LITEOSTOPDIR)/tools/build/mk/los_config.mk

TOP_LD_PATH      = $(LITEOSTOPDIR)
SCRIPTS_PATH     = $(LITEOS_SCRIPTPATH)
LITEOS_MK_PATH   = $(MK_PATH)
LITEOS_SUBDIRS   = $(LIB_SUBDIRS)
LIB_BIGODIR      = $(LITEOS_LIB_BIGODIR)

##### liteos tables -u ldflags #####
-include $(LITEOS_MK_PATH)/liteos_tables_ldflags.mk

LITEOS_COPTS   := $(LITEOS_COPTS_DEBUG) $(LITEOS_COPTS_BASE)  \\
                  $(LITEOS_COPTS_EXTRA) $(LITEOS_CORE_COPTS)
LITEOS_CXXOPTS  := $(LITEOS_CXXOPTS_BASE)
LITEOS_INCLUDE  := $(LITEOS_KERNEL_INCLUDE)  $(LITEOS_EXTKERNEL_INCLUDE) \\
                   $(LITEOS_COMPAT_INCLUDE)  $(LITEOS_FS_INCLUDE) \\
                   $(LITEOS_NET_INCLUDE)     $(LITEOS_LIB_INCLUDE) \\
                   $(LITEOS_DRIVERS_INCLUDE) $(LOSCFG_TOOLS_DEBUG_INCLUDE) \\
                   $(LITEOS_PLATFORM_INCLUDE) $(LITEOS_DFX_INCLUDE) \\
                   $(LITEOS_SECURITY_INCLUDE)
LITEOS_LIBDEP   := $(LITEOS_BASELIB)
LITEOS_ASFLAGS  := $(LITEOS_ASOPTS)      $(LITEOS_INCLUDE)
LITEOS_CFLAGS   := $(LITEOS_COPTS)       $(LITEOS_CMACRO) \\
                   $(LITEOS_CMACRO_TEST) $(LITEOS_IMAGE_MACRO) \\
                   $(LITEOS_INCLUDE)
LITEOS_CXXFLAGS := $(LITEOS_CXXOPTS)     $(LITEOS_CXXMACRO) \\
                   $(LITEOS_CMACRO)      $(LITEOS_CXXINCLUDE)
LITEOS_LDFLAGS  := $(LITEOS_LD_OPTS)     $(LITEOS_LD_PATH) \\
                   $(LITEOS_LD_SCRIPT)

4. module.mk

        这个Makefile文件中会出现第一个目标all,下面通过8个步骤的分析来描述如何将fat/目录下的.c文件编译为目标文件和库文件。

#3.LIBA即$(OUT)/lib/libfat.a
LIBSO :=
LIBA := $(OUT)/lib/lib$(MODULE_NAME).a    
#2.LIB即LIBA和LIBSO
LIB := $(LIBA) $(LIBSO)   

#6.把所有.c后缀的文件替换为.o后缀
LOCAL_COBJS := $(patsubst %.c,$(OBJOUT)/%.o,$(LOCAL_CSRCS))   
LOCAL_CPPOBJS := $(patsubst %.cpp,$(OBJOUT)/%.o,$(LOCAL_CPPSRCS))
LOCAL_ASMOBJS := $(patsubst %.S,$(OBJOUT)/%.o,$(LOCAL_ASMSRCS))
LOCAL_ASMOBJS2 := $(patsubst %.s,$(OBJOUT)/%.o,$(LOCAL_ASMSRCS2))
LOCAL_CCOBJS := $(patsubst %.cc,$(OBJOUT)/%.o,$(LOCAL_CCSRCS))

#5.LOCAL_OBJS依赖LOCAL_COBJS等
LOCAL_OBJS := $(LOCAL_COBJS) $(LOCAL_CPPOBJS) $(LOCAL_ASMOBJS) $(LOCAL_ASMOBJS2)$(LOCAL_CCOBJS)
LOCAL_CGCH := $(patsubst %.h,%.h.gch,$(LOCAL_CHS))
LOCAL_CPPGCH := $(patsubst %.h,%.h.gch,$(LOCAL_CPPHS))

#1.目标all依赖LIB
all : $(LIB)          

#7.将当前路径下的所有.c文件编译为.o文件
$(LOCAL_COBJS): $(OBJOUT)/%.o: %.c   
	$(HIDE)$(OBJ_MKDIR)
	$(HIDE)$(CC) $(LITEOS_CFLAGS) $(LOCAL_FLAGS) $(LOCAL_CFLAGS) -c $< -o $@

$(LOCAL_CPPOBJS): $(OBJOUT)/%.o: %.cpp
	$(HIDE)$(OBJ_MKDIR)
	$(HIDE)$(GPP) $(LITEOS_CXXFLAGS) $(LOCAL_FLAGS) $(LOCAL_CPPFLAGS) -c $< -o $@

$(LOCAL_ASMOBJS): $(OBJOUT)/%.o: %.S
	$(HIDE)$(OBJ_MKDIR)
	$(HIDE)$(CC) $(LITEOS_CFLAGS) $(LOCAL_FLAGS) $(LOCAL_ASFLAGS) -c $< -o $@

$(LOCAL_ASMOBJS2): $(OBJOUT)/%.o: %.s
	$(HIDE)$(OBJ_MKDIR)
	$(HIDE)$(AS) $(LITEOS_ASFLAGS) $(LOCAL_FLAGS) $(LOCAL_ASFLAGS) -c $< -o $@

$(LOCAL_CCOBJS): $(OBJOUT)/%.o: %.cc
	$(HIDE)$(OBJ_MKDIR)
	$(HIDE)$(GPP) $(LITEOS_CXXFLAGS) $(LOCAL_FLAGS) $(LOCAL_CPPFLAGS) -c $< -o $@

$(LOCAL_CGCH): %.h.gch : %.h
	$(HIDE)$(CC) $(LITEOS_CFLAGS) $(LOCAL_FLAGS) $(LOCAL_CFLAGS) $> $^

$(LOCAL_CPPGCH): %.h.gch : %.h
	$(HIDE)$(GPP) $(LITEOS_CXXFLAGS) $(LOCAL_FLAGS) $(LOCAL_CPPFLAGS) -x c++-header $> $^

LOCAL_GCH := $(LOCAL_CGCH) $(LOCAL_CPPGCH)

$(LOCAL_OBJS): $(LOCAL_GCH)
#4.生成LIBA需要依赖LOCAL_OBJS
$(LIBA): $(LOCAL_OBJS)          
	$(HIDE)$(OBJ_MKDIR)

ifeq ($(OS), Linux)
	$(HIDE)$(AR) $(ARFLAGS) $@ $(LOCAL_OBJS)
else
#8.将当前目录下的.o文件打包为libfat.so
ifeq ($(LOCAL_MODULES),)        
	$(HIDE)$(AR) $(ARFLAGS) $@ $(LOCAL_OBJS)
endif

clean:
	$(HIDE)$(RM) $(LIB) $(OBJOUT) $(LOCAL_GCH) *.bak *~

.PHONY: all clean

以上是关于子目录Makefile分析的主要内容,如果未能解决你的问题,请参考以下文章

主目录 Makefile 分析

redis源码分析--makefile和目录结构分析

U-boot主Makefile详尽分析

第3阶段——内核启动分析之make uImage编译内核

Makefile中export分析

Linux系统移植:U-Boot 顶层 Makefile 分析(下)