1. 程式人生 > 其它 >【makefile】02 經典應用示例

【makefile】02 經典應用示例

經典應用示例

1. 檔案結構:

.
├── common
│ └── common.h
├── dir1
│ ├── a1.c
│ ├── a1.h
│ ├── b1.c
│ ├── b1.h
│ ├── c1.c
│ ├── c1.h
│ └── dir1.mk
├── dir2
│ ├── a2.c
│ ├── a2.h
│ ├── b2.c
│ ├── b2.h
│ ├── c2.c
│ ├── c2.h
│ └── dir2.mk
├── dir3
│ ├── a3.c
│ ├── a3.h
│ ├── b3.c
│ ├── b3.h
│ ├── c3.c
│ ├── c3.h
│ └── dir3.mk
├── LICENSE
├── makefile
├── README.md
└── sys
-make ├── config │ ├── build.config │ └── version.config ├── gcc.mk ├── rules.mk └── tools.mk 6 directories, 30 files

2. makefile檔案:

a. dir1.mk

# Sub-folder makefile
#
#    @File:      dir1.mk
#    @Author:    How.Chen
#    @History:
#    VER        DATE            Change
#    
1.0 27/May/2017 init version # 1.1 12/Oct/2020 update suffix # define module name for compile use MODULE = dir1 # define expected lib MOD_LIB = $(LIB_PREFIX)$(MODULE).$(LIB_SUFFIX) CC_DEFS := # modify sys-make/config/build.config to control ifeq '$(filter FEATURE_1, $(FEATURE))
' 'FEATURE_1' CC_DEFS += dir1_DEF_1 CC_DEFS += dir1_DEF_2 CC_DEFS += dir1_DEF_3 endif # add srouce files, which would like to compile SRC_FILES = SRC_FILES += a1.c SRC_FILES += b1.c # modify sys-make/config/build.config to control ifeq '$(filter FEATURE_1, $(FEATURE))' 'FEATURE_1' SRC_FILES += c1.c endif # add include search path INC_PATH = INC_PATH += $(TOP_DIR)/common # add source file search path together with vpath SRC_PATH = SRC_PATH += $(TOP_DIR)/$(MODULE) SRC_PATH += $(TOP_DIR)/$(MODULE)/sub_dir1 vpath %.c $(SRC_PATH) # use general tools setting, compiler and compile rules include $(MKFILE_DIR)/tools.mk include $(MKFILE_DIR)/gcc.mk include $(MKFILE_DIR)/rules.mk

b. dir2.mk

MODULE = dir2

MOD_LIB = $(LIB_PREFIX)$(MODULE).$(LIB_SUFFIX)

CC_DEFS :=
ifeq '$(filter FEATURE_2, $(FEATURE))' 'FEATURE_2'
    CC_DEFS += dir2_DEF_1
    CC_DEFS += dir2_DEF_2
    CC_DEFS += dir2_DEF_3
endif

SRC_FILES =
SRC_FILES += a2.c
SRC_FILES += b2.c
SRC_FILES += c2.c

INC_PATH =
INC_PATH += $(TOP_DIR)/common

SRC_PATH =
SRC_PATH += $(TOP_DIR)/$(MODULE)
SRC_PATH += $(TOP_DIR)/$(MODULE)/sub_dir2

vpath %.c $(SRC_PATH)

include $(MKFILE_DIR)/tools.mk
include $(MKFILE_DIR)/gcc.mk
include $(MKFILE_DIR)/rules.mk

c. dir3.mk

MODULE = dir3

MOD_LIB = $(LIB_PREFIX)$(MODULE).$(LIB_SUFFIX)

CC_DEFS :=
ifeq '$(filter FEATURE_3, $(FEATURE))' 'FEATURE_3'
    CC_DEFS += dir3_DEF_1
    CC_DEFS += dir3_DEF_2
    CC_DEFS += dir3_DEF_3
endif

SRC_FILES =
SRC_FILES += a3.c
SRC_FILES += b3.c
SRC_FILES += c3.c

INC_PATH =
INC_PATH += $(TOP_DIR)/common

SRC_PATH =
SRC_PATH += $(TOP_DIR)/$(MODULE)
SRC_PATH += $(TOP_DIR)/$(MODULE)/sub_dir3

vpath %.c $(SRC_PATH)

include $(MKFILE_DIR)/tools.mk
include $(MKFILE_DIR)/gcc.mk
include $(MKFILE_DIR)/rules.mk

d. makefile

# main makefile
#
#    @File:      makefile
#    @Author:    How.Chen
#    @History:
#    VER        DATE            Change
#    1.0        27/Apr/2017        init commit
#    1.1        12/Oct/2020        change suffix name andadd tools.mk

# define useful directory path
TOP_DIR = $(PWD)
MKFILE_DIR = $(TOP_DIR)/sys-make
CFG_DIR = $(MKFILE_DIR)/config
OUTPUT_DIR = $(TOP_DIR)/output

# define useful prefix/postfix
LIB_PREFIX = lib
LIB_SUFFIX = a

# export var, which need be known by sub-makefile
export TOP_DIR MKFILE_DIR OUTPUT_DIR
export LIB_PREFIX LIB_SUFFIX

all: obj link

obj:
    @$(MAKE) -f $(TOP_DIR)/dir1/dir1.mk
    @$(MAKE) -f $(TOP_DIR)/dir2/dir2.mk
    @$(MAKE) -f $(TOP_DIR)/dir3/dir3.mk

# link workaround
# pass link to rules.mk to trigger link
link:
    @$(MAKE) -f $(MKFILE_DIR)/rules.mk link

# check
# to display each module build info
check:
    @$(MAKE) -f $(TOP_DIR)/dir1/dir1.mk check
    @$(MAKE) -f $(TOP_DIR)/dir2/dir2.mk check
    @$(MAKE) -f $(TOP_DIR)/dir3/dir3.mk check

# remove ouyput
clean:
    @$(MAKE) -f $(TOP_DIR)/dir1/dir1.mk clean
    @$(MAKE) -f $(TOP_DIR)/dir2/dir2.mk clean
    @$(MAKE) -f $(TOP_DIR)/dir3/dir3.mk clean
    -$(RM) -rf $(OUTPUT_DIR)

# include build configuration
# FEATURE define in it
include $(MKFILE_DIR)/tools.mk
include $(CFG_DIR)/build.config
include $(CFG_DIR)/version.config

3. 執行:

[root@centos8 makefile]# make
make[1]: Entering directory '/root/makefile'
CC: a1.c
CC: b1.c
CC: c1.c
Generate lib: libdir1.a
make[1]: Leaving directory '/root/makefile'
make[1]: Entering directory '/root/makefile'
CC: a2.c
CC: b2.c
CC: c2.c
Generate lib: libdir2.a
make[1]: Leaving directory '/root/makefile'
make[1]: Entering directory '/root/makefile'
CC: a3.c
CC: b3.c
CC: c3.c
Generate lib: libdir3.a
make[1]: Leaving directory '/root/makefile'
make[1]: Entering directory '/root/makefile'
Link executable: test_makefile_2021.42.3.23.02
full objs: /root/makefile/output/dir2/b2.o /root/makefile/output/dir2/a2.o /root/makefile/output/dir2/c2.o /root/makefile/output/dir3/b3.o /root/makefile/output/dir3/c3.o /root/makefile/output/dir3/a3.o /root/makefile/output/dir1/b1.o /root/makefile/output/dir1/a1.o /root/makefile/output/dir1/c1.o
make[1]: Leaving directory '/root/makefile'

參考資料

1. makefile【howhow】

2. 跟我一起學makefile