有这样一个工程,包含了多个模块,需要把每个模块编译成动态库,即.so 文件,那就需要这样一个脚本放置在工程目录下,执行的时候会到每个模块下进行编译。但前提是每个模块下要有对应的Makefile文件,即怎么编译是每个模块的事情。如有模块:
以 Json 模块为例,其目录下的Makefile如下:
PLATFORM=
CROSS_COMPILE=
ifeq ($(platform), x86)
PLATFORM=x86
else
PLATFORM=$(platform)
CROSS_COMPILE=arm-himix200-linux-
endif
$(info "platform = $(PLATFORM)")
COMPILE_DIR = compile
LIB_DIR = lib
CC = $(CROSS_COMPILE)g++ -std=c++11
CFLAGS = -Werror
LIB = -pthread
INCLUDE = -I/home/tianyexing/Documents/code/working/basiclibrary/Include -I./Src
ifneq ($(PLATFORM), x86)
INCLUDE += -I/opt/arm-openssl/include
endif
#源码目录
SRCS_PATH = Src
LIB_SRCS_CPP = $(foreach dir, $(SRCS_PATH), $(wildcard $(dir)/*.cpp))
#创建源码所在层级目录
$(foreach dir, $(SRCS_PATH), $(shell if [ ! -d $(COMPILE_DIR)/$(PLATFORM)/$(dir) ]; then mkdir -p $(COMPILE_DIR)/$(PLATFORM)/$(dir); fi))
OBJS = $(patsubst %.cpp, $(COMPILE_DIR)/$(PLATFORM)/%.o, $(LIB_SRCS_CPP))
DEP = $(patsubst %.o, %.d, $(OBJS))
# $(shell if [ ! -d $(COMPILE_DIR) ]; then mkdir $(COMPILE_DIR); fi)
$(shell if [ ! -d $(LIB_DIR)/$(PLATFORM) ]; then mkdir -p $(LIB_DIR)/$(PLATFORM); fi)
SHAREDLIB=libjson.so
all: $(SHAREDLIB)
-include $(DEP)
$(SHAREDLIB): $(OBJS)
@$(CC) $(INCLUDE) -shared -o $@ $^ $(LIB)
@cp $@ $(LIB_DIR)/$(PLATFORM)/
@mv $@ /home/tianyexing/Documents/code/working/basiclibrary/Build/Lib/Debug/$(PLATFORM)
$(COMPILE_DIR)/$(PLATFORM)/%.o: %.cpp $(COMPILE_DIR)/$(PLATFORM)/%.d
$(CC) $(INCLUDE) -fPIC -c $< -o $@
$(COMPILE_DIR)/$(PLATFORM)/%.d: %.cpp
$(CC) $(CFLAGS) $(INCLUDE) -fPIC -MM -E -c $< -o $@
@sed 's/.*\.o/$(subst /,\/,$(dir $@))&/g' $@ > $@.tmp
@mv $@.tmp $@
.PHONY: clean
clean:
rm -rf $(LIB_DIR)/$(PLATFORM) $(COMPILE_DIR)/$(PLATFORM)
然后我工程目录下有一个shell 脚本,主要用到了 Makefile -C 参数,它会先切到指定的目录下执行 Makefile 然后再返回到当前目录继续执行。shell 脚本如下:
#!/bin/bash
moduleList="Json Logger UBase NFramework User"
platformlist="dv300 x86"
#先转成列表再取长度
module_list=($moduleList)
module_list_len=${#module_list[@]}
platform_list=($platformlist)
platform_list_len=${#platformlist[@]}
function make_module_list()
{
for modul in ${moduleList}
do
make -C ${modul} platform=$1 #函数里的 $1 是函数的入参,并非命令行传入的参数
done
}
function clean_module_list()
{
for modul in ${moduleList}
do
make -C ${modul} platform=$1 clean
done
}
function make_single_mudule()
{
module=$1
module=${module%/} #去掉最后一个/,按Tab自动完成时带的/,这里不能直接用$1,用了一个临时变量module进行操作
make -C ${module} platform=$2
}
function clean_single_module()
{
module=$1
module=${module%/} #去掉最后一个/,按Tab自动完成时带的/,这里不能直接用$1,用了一个临时变量module进行操作
make -C ${module} platform=$2 clean
}
function is_valid_module()
{
module=$1
realModule=${module%/} #去掉最后一个/,按Tab自动完成时带的/,这里不能直接用$1,用了一个临时变量module进行操作
modulTotal=0
for moduleName in ${moduleList}
do
if [ ${moduleName} == ${realModule} ]; then
return 1
else
let modulTotal++
fi
done
#一个都没匹配到,不是有效的模块名称
if [ ${module_list_len} == ${modulTotal} ]; then
return 0
fi
return 1
}
function is_valid_platform()
{
platformTotal=0
for platform in ${platformlist}
do
if [ ${platform} == $1 ]; then
return 1
else
let modulTotal++
fi
done
#一个都没匹配到,不是有效的平台名称
if [ ${platform_list_len} == ${platformTotal} ]; then
return 0
fi
return 1;
}
if [ $# -eq 0 ]; then
echo "Usage: $0 [module|clean] platform"
exit
elif [ $# -eq 1 ]; then #一个参数时,必须跟平台类型
echo -e "\033[33m"
echo "module list: ${moduleList}"
echo -e "\033[0m"
#函数调用,参数为平台类型
is_valid_platform $1
if [ $? -eq 0 ]; then
echo "no such platform"
echo "$0 [module|clean] platform"
exit
else
make_module_list $1
fi
elif [ $# -eq 2 ]; then
if [ "clean" == $1 ]; then
is_valid_platform $2
if [ $? -eq 0 ]; then
echo "$2 is not a platform, usage: $0 [module|clean] platform"
fi
clean_module_list $2
else
is_valid_module $1
if [ $? -eq 0 ]; then
echo "no such module, Usage: $0 [module|clean] platform"
exit
fi
is_valid_platform $2
if [ $? -eq 1 ]; then
make_single_mudule $1 $2
fi
fi
exit
elif [ $# -eq 3 ]; then
is_valid_module $1
if [ $? -eq 0 ]; then
echo "no such module, Usage: $0 [module|clean] platform"
exit
fi
if [ $2 != "clean" ]; then
echo "sync error, usage: $0 [module|clean] platform"
exit
fi
is_valid_platform $3
if [ $? -eq 0 ]; then
echo "no such platform, $0 [module|clean] platform"
exit
fi
clean_single_module $1 $3
else
echo "not support more than 3 parameters"
exit
fi
脚本里指定了格式,只要按照格式输入即可执行对应的操作。当我需要编译指定模块时,输入如:
./makeModule.sh Json dv300
当不带参数时,则编译脚本中所列举的所有模块,如:
清空指定模块,再编译指定模块: