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="\033[31m""$msg""\033[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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值