Linux编译中的头文件路径大小写校验脚本

跨平台编译中,Windows开发的代码路径大小写不正确并不导致编译出错,但在Linux下编译时文件名是严格区分大小写的所以会出现很多因大小写问题导致的编译错误。所以通过编译前检测所有大小写问题,批量修改可以提升编译的效率。

思路上,通过grep工具检出所有的包含,然后逐个做locate校验,如果包含路径书写不规范也会导致存在性校验失败,可以对校验条件稍作修改。完整代码如下:

#!/bin/bash

workdir=.
modify=0

while [ $# -gt 0 ]
do
    case $1 in
    "--modify")
        modify=1
        ;;
    "--work-dir")
        shift
        workdir=$1
        ;;
    *)
        echo "USAGE: $0 [--work-dir <work-dir>] [--modify] [--help]"
        exit 1
    esac
    shift
done

echo "work-dir: $workdir, modify: $modify"

#1 更新db,时间会稍长10多分钟
#updatedb

#2 去重并导出所有待检测的头文件
touch channel
grep -IPrsnH '^\s*#\s*include' --include=*.{h,cpp,ui} $workdir | while read -e line;
do 
	header=$(echo $line | grep -Po '#include[\s"<]*\K([^>"]*)')
	if [ -z $header ] || [[ $header =~ "ui_" ]]; then
		continue 
	fi
	
	contained=0	
	for h in ${includes[*]};
	do 
		if [ $h == $header ]; then 
			contained=1
		fi
	done
	if [ $contained -eq 0 ]; then
		includes[$k]=$header
		(echo $line) >> channel
		k=$(($k+1))
	fi
done 

#3 对所有头文件做存在性校验,输出不存在的包含路径上下文
cat channel | while read ln; do 
	header=$(echo $ln | grep -Po '#include[\s"<]*\K([^>"]*)' | sed -r 's/[/\\]+/\//g')
	if [ $(locate $header | wc -l) -eq 0 ]
	then 
		echo $ln
	fi
done
if [ -f "channel" ]; then
	rm "channel"
fi
echo Done!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值