我是靠谱客的博主 美好音响,最近开发中收集的这篇文章主要介绍openwrt自动生成patch脚本,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

 带2个参数,第1个参数为包名称,第2个参数为patch文件名称

#!/bin/sh
g_pwd=`pwd` #当前目录,必须是openwrt工程目录
g_packageName="$1"
g_patchName="$2"
g_linux_version=''
#内核版本,比如linux-3.14.18
########################################
# 描述: 将要传入的信息转换成红色
# 入参: $1 - 传入的信息
# 出参: 无
# 返回: 转换成红色的信息
########################################
GenerateErrorMsg()
{
local msg=$1
local errorMsg="33[31m""$msg""33[0m" #生成红色字体
echo -n $errorMsg
}
########################################
# 描述: 根据包名称,获取包所在的路径
# 入参: 无
# 出参: 无
# 返回: 包路径,比如package/kernel/mac80211
########################################
GetPackageDir()
{
local packageDir=""
if [ "${g_packageName}" = "linux" ]; then
packageDir=`find target/linux -type d -name "patches-${g_linux_version}"`
else
#先在package下找,然后在feeds下找
packageDir=`find package -type d -name "${g_packageName}" |grep -v files` #忽略files目录
if [ -z "$packageDir" ]; then
packageDir=`find feeds -type d -name "${g_packageName}" |grep -v files` #忽略files目录
fi
fi
echo -n $packageDir
}
########################################
# 描述: 根据包名称,获取包的编译路径
# 入参: 无
# 出参: 无
# 返回: 包编译路径,比如build_dir/target-mips_24kc_musl/linux-ar71xx_generic/backports-2017-11-01
########################################
GetBuildDir()
{
local buildDir=""
local packageDir=""
local buildDirPartName=""
if [ "${g_packageName}" = "linux" ]; then
buildDir=`find build_dir/linux* -maxdepth 1 -type d -name linux-[0-9]*`
if [ -n "$buildDir" ]; then
g_linux_version=`echo ${buildDir##*linux-}`
fi
else
#先在build_dir/target-mips_24kc_musl下找,然后在build_dir/target-mips_24kc_musl/linux-ar71xx_generic下找
buildDir=`find build_dir/target* -maxdepth 1 -type d -name "${g_packageName}*"`
if [ -z "$buildDir" ]; then
buildDir=`find build_dir/target*/linux* -maxdepth 1 -type d -name "${g_packageName}*"`
fi
fi
#如果仍然没找到,说明package名称和编译路径不一样
if [ -z "$buildDir" ]; then
packageDir=$(GetPackageDir)
# 截取PKG_BUILD_DIR的最低级目录中,横杠前面的内容,比如$(KERNEL_BUILD_DIR)/backports-$(PKG_VERSION),截取之后就是backports
buildDirPartName=`grep 'PKG_BUILD_DIR:=' ${packageDir} -r | awk -F'/' '{print $NF}' | awk -F'-' '{print $1}'`
#先在build_dir/target-mips_24kc_musl下找,然后在build_dir/target-mips_24kc_musl/linux-ar71xx_generic下找
buildDir=`find build_dir/target* -maxdepth 1 -type d -name "${buildDirPartName}*"`
if [ -z "$buildDir" ]; then
buildDir=`find build_dir/target*/linux* -maxdepth 1 -type d -name "${buildDirPartName}*"`
fi
fi
echo -n $buildDir
}
########################################
# 描述: 在当前用户根目录下创建patch目录,比如patch_backports
#
并在此目录下创建old和new目录
# 入参: 无
# 出参: 无
# 返回: 无
########################################
MakePatchDir()
{
local patchDir
echo -e "nmake patch dir..."
patchDir=~/patch_${g_packageName}
if [ -d $patchDir ] ; then
echo -e "t--->Delete existed patch dir"
rm -rf $patchDir
fi
mkdir $patchDir
mkdir $patchDir/old
mkdir $patchDir/new
if [ -d $patchDir -a -d $patchDir/old -a -d $patchDir/new ] ; then
echo -e "t--->ok"
else
echo -e $(GenerateErrorMsg 't--->make patch dir failed')
fi
}
########################################
# 描述: 从编译目录拷贝文件到patch目录
# 入参: $1 - new指拷贝新文件,old指拷贝旧文件
# 出参: 无
# 返回: 无
########################################
CopyFiles()
{
local fileList
local sourceFileList
local headFileList
local txtFileList
local makefileList
local kconfigList
local i
local patchDir=~/patch_${g_packageName}
local newOrOld=$1
local buildDir=$(GetBuildDir)
echo -e "ncopy $newOrOld files..."
if [ -z "$buildDir" ]; then
echo -e $(GenerateErrorMsg 't--->find package build dir failed')
exit 1
fi
# 在编译目录下,拷贝所有文件到patch目录下
echo -e "t--->build dir is $buildDir"
cd $buildDir
sourceFileList=`find . -name '*.c' ! -name '*.mod.c'` #.c文件,但忽略mod.c
headFileList=`find . -name '*.h'` # .h文件
txtFileList=`find . -name '*.txt'` # .txt文件
if [ "${g_packageName}" = "linux" ]; then
makefileList=`find . -name 'Makefile'` # Makefile文件
kconfigList=`find . -name 'Kconfig'` # Kconfig文件
fi
fileList="${sourceFileList} ${headFileList} ${txtFileList} ${makefileList} ${kconfigList}" #中间用空格分开
for i in $fileList
do
cp $i $patchDir/$newOrOld --parents
done
#回到openwrt根目录
cd $g_pwd
echo -e "t--->ok"
}
CompileOldPackage()
{
echo -e "ncompile old package..."
if [ "${g_packageName}" = "linux" ]; then
echo -e "t--->clean linux"
make target/$g_packageName/clean > /dev/null 2>&1
echo -e "t--->compile linux"
make target/$g_packageName/compile > /dev/null 2>&1
else
echo -e "t--->clean package"
make package/$g_packageName/clean > /dev/null 2>&1
echo -e "t--->compile package"
make package/$g_packageName/compile > /dev/null 2>&1
fi
echo -e "t--->ok"
}
GeneratePatch()
{
local patchDir=~/patch_${g_packageName}
echo -e "ngenerate patch..."
cd $patchDir
diff -Nur old new >
$g_patchName
#回到openwrt根目录
cd $g_pwd
echo -e "t--->ok"
}
CopyPatch()
{
local patchDir=~/patch_${g_packageName}
local packageDir=$(GetPackageDir)
echo -e "ncopy patch..."
if [ -z "$packageDir" ]; then
echo -e $(GenerateErrorMsg 't--->find package dir failed')
exit 1
fi
echo -e "t--->package dir is $packageDir"
if [ "${g_packageName}" != "linux" ]; then
if [ ! -d $packageDir/patches ]; then
echo -e "t--->make patch dir, path=$packageDir/patches"
mkdir -p $packageDir/patches
fi
echo -e "t--->copy patch to $packageDir/patches"
cp $patchDir/$g_patchName $packageDir/patches/
else
echo -e "t--->copy patch to $packageDir"
cp $patchDir/$g_patchName $packageDir/
fi
echo -e "t--->ok"
}
CheckPatch()
{
local diff
local patchDir=~/patch_${g_packageName}
local buildDir=$(GetBuildDir)
echo -e "ncheck patch..."
if [ "${g_packageName}" = "linux" ]; then
echo -e "t--->clean linux"
make target/$g_packageName/clean > /dev/null 2>&1
echo -e "t--->compile linux"
make target/$g_packageName/compile > /dev/null 2>&1
else
echo -e "t--->clean package"
make package/$g_packageName/clean > /dev/null 2>&1
echo -e "t--->compile package"
make package/$g_packageName/compile > /dev/null 2>&1
fi
diff=`diff -ur $patchDir/new $buildDir |grep -v Only`
if [ -n "$diff" ]; then
echo -e $(GenerateErrorMsg 't--->check patch failed')
exit 1
fi
echo -e "t--->ok"
}
################################## main ################################
#生成patch目录
MakePatchDir
#拷贝新文件
CopyFiles new
#编译旧的版本
CompileOldPackage
#拷贝旧文件
CopyFiles old
#生成patch
GeneratePatch
#拷贝patch
CopyPatch
#检查patch
CheckPatch

 

最后

以上就是美好音响为你收集整理的openwrt自动生成patch脚本的全部内容,希望文章能够帮你解决openwrt自动生成patch脚本所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部