Shell 作为一门脚本语言,作为用户和操作系统之间的桥梁。一些指令的使用经常给我们工作带来很大的便利,编译过程一些自动半自动脚本可以提升效率,具体阐述参加百度shell 编程。
本文分享一个半自动脚本。
主要功能:查找生成的动态库中的符号。
脚本概述:主要是运用编译器中的ld指令 查找动态库 需要使用到但是在依赖的库中又没有定义的符号,
在项目集成阶段作用比较明显。
ld指令使用格式如下:
(arm-linux-androideabi-)ld libraryspath/xxxxx.so -llog -lnetutils -lbinder -luditest -ldl -lm -lz -lutils -ldvm -lcutils -lc -lporting -L dependlibpath1 -L dependlibpath2 ... -L dependlibpathN
说明:ld 前缀视平台不一样而改变,libraryspath/xxxxx.so为生成的目标库,后面几个依赖的动态库,-L 后跟依赖库所在的一系列路径。
脚本如下:
#!/bin/sh
###########################
### 变量声明 ###
###########################
export WorkDir=${MMCP_HOME}
export TargetDir=${WorkDir}/out/${REAL_MMCP_PLATFORM}/${MMCP_COMPILING_MODE}
#Mstar 6A801平台
#export LD=arm-linux-androideabi-ld
#Hisi 3716c v200平台
export LD=arm-linux-gnueabi-ld
##定义四个库的名称,shell 数组
#export MMCPName_Ser=libmmcp_product.so
#export MMCPName_Cli=libmmcp_product_client.so
#export PROName_Ser=libmmcp_custom_product.so
#export PROPName_Cli=libmmcp_custom_product_client.so
export TargetName=(libmmcp_product.so libmmcp_product_client.so libmmcp_custom_product.so libmmcp_custom_product_client.so)
export IncludeLibary=
##放置依赖库路径
export IncludePath=${WorkDir}/linkLibrary/androidlib/${REAL_MMCP_PLATFORM}
GetLibrays()
{
echo "Enter Function to Get the Dependent libraries"
##依赖库的名称
export Librarys=$(find ${IncludePath} -name *.so ;)
for file in ${Librarys[*]}
do
tmp=$(basename ${file};)
##替换前缀lib为 "-l"
tmp=$(echo ${tmp/lib/ -l};)
##去掉后缀.so
tmp=$(echo ${tmp%.so};)
LibaryName+=${tmp}$" "
done
IncludeLibary=${LibaryName}
}
#echo ${IncludeLibary}
###########################
### 函数定义 ###
###########################
###################
# 信息提示 #
###################
ShowInfo()
{
echo "CheckoutSymbol Tips"
echo "Instructions for use :"
echo "(1) FIRST , run . bin/setupenv "
echo "(2) Second, run . CheckoutSymbol.sh"
# echo "(3) Check the result int the undefineSymbol.txt"
# nopara=yes
}
#CheckOutSymbol()
#{
#echo "Enter CheckOutSymbol"
#echo ${IncludePath}
#${LD} ${TargetDir}/${1} ${2} -L${IncludePath} -L{3}
#}
doselect()
{
GetLibrays
echo ${TargetName[$1-1]}
case $1 in
1)
# echo "1"
IncludeLibary=$(echo ${IncludeLibary/-lporting_client/ };)
echo ${IncludeLibary}
echo ${IncludePath}
${LD} ${TargetDir}/${TargetName[$1-1]} ${IncludeLibary} -L ${IncludePath}
;;
2)
# echo "2"
IncludeLibary=$(echo ${IncludeLibary/"-lporting "/" "};)
echo ${IncludeLibary}
echo ${IncludePath}
${LD} ${TargetDir}/${TargetName[$1-1]} ${IncludeLibary} -L ${IncludePath}
;;
3)
# echo "3"
IncludeLibary=$(echo ${IncludeLibary/-lporting_client/ };)
IncludeLibary=${IncludeLibary}$" -lmmcp_product"
echo ${IncludeLibary}
echo ${IncludePath}
${LD} ${TargetDir}/${TargetName[$1-1]} ${IncludeLibary} -L ${IncludePath} -L ${TargetDir}
;;
4)
# echo "4"
IncludeLibary=$(echo ${IncludeLibary/"-lporting "/" "};)
IncludeLibary=${IncludeLibary}$" -lmmcp_product_client"
echo ${IncludeLibary}
echo ${IncludePath}
${LD} ${TargetDir}/${TargetName[$1-1]} ${IncludeLibary} -L ${IncludePath} -L ${TargetDir}
;;
*)
echo "please select the library !"
;;
esac
}
######################
###判断是否设置环境###
######################
if [ "${MMCP_HOME}" = "" ];then
ShowInfo
elif [ x"${REAL_MMCP_PLATFORM}" = x ];then
ShowInfo
elif [ -z "${MMCP_COMPILING_MODE}" ];then
ShowInfo
else
echo "Will Checkout the undefined symbol"
echo "Please select the target library:"
echo "1)libmmcp_product.so 2)libmmcp_product_client.so 3)libmmcp_custom_product.so 4)libmmcp_custom_product_client.so"
read -p " Enter your anwser (default: linux) => " INPUT
doselect ${INPUT}
fi
#echo "(3) Check the result int the undefineSymbol.txt"
脚本比较粗糙,还可以进行优化,特定项目的可以做成自动化的。要做成通用类型的,就需要将ld 类型 ,目标库路径,依赖库路径等稍微封装一下
等用户自行输入,最后执行完自动列出未定义的符号。
附上一段打印说明一下:
luoting@stp-OptiPlex-7010:~/gitbranch/MMCP$ ./CheckoutSymbol.sh
Will Checkout the undefined symbol
Please select the target library:
1)libmmcp_product.so 2)libmmcp_product_client.so 3)libmmcp_custom_product.so 4)libmmcp_custom_product_client.so
Enter your anwser (default: linux) => 1
Enter Function to Get the Dependent libraries
libmmcp_product.so
-llog -lnetutils -lbinder -luditest -ldl -lm -lz -lutils -ldvm -lcutils -lporting -lc
/home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200
arm-linux-gnueabi-ld: warning: libstdc++.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/liblog.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libui.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libuditest.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libskia.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libuditest.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libsurfaceflinger_client.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libuditest.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libgui.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libuditest.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libandroid_runtime.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libuditest.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libnativehelper.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libuditest.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libdtv_getline.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libuditest.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libcurl.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libuditest.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libcrypto.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libuditest.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libssl.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libuditest.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libcorkscrew.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libdvm.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libstlport.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libdvm.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libGLESv2.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libhi_common.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libhi_msp.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libhi_pes.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: libhi_surface.so, needed by /home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so, not found (try using -rpath or -rpath-link)
arm-linux-gnueabi-ld: warning: cannot find entry symbol _start; defaulting to 00000000000082d0
/home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so: undefined reference to `HI_UNF_HDMI_DeInit'
/home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so: undefined reference to `HI_UNF_SND_Attach'
/home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so: undefined reference to `HI_UNF_DISP_SetFormat'
/home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so: undefined reference to `HI_UNF_PVR_RegisterEvent'
/home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so: undefined reference to `HI_UNF_DMX_AttachTSPort'
/home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so: undefined reference to `HI_UNF_VO_ResetWindow'
/home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so: undefined reference to `HI_UNF_DISP_GetFormat'
/home/luoting/gitbranch/MMCP/linkLibrary/androidlib/Android_Hi3716C_V200/libporting.so: undefined reference to `HI_UNF_HDMI_GetSinkCapability'
(1)很明显目标库,需要依赖的动态库libstdc++.so,libui.so,libskia.so,... libhi_surface.so ,在指定的几个路径下不存在;
(2)在当前几个依赖的库中有一些找不到的海思接口(undefined reference to HI_UNFxxxxxxx);
(3)依赖的动态库要尽量全,才能保证结果的精确性;