-
Notifications
You must be signed in to change notification settings - Fork 497
/
fix-include-path.sh
executable file
·131 lines (113 loc) · 3.06 KB
/
fix-include-path.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#! /bin/bash
# source file types, you may need to change it, using regex
SOURCE_TYPES=".*[.]\(c\|h\|cc\|hh\|cpp\|hpp\)$"
CONVERT_LOG="convert.log"
SKIP_LOG="skip.log"
WARNING_LOG="warning.log"
function _warning()
{
if [ -t 2 ]; then
echo -e "\033[1;33m$@\033[m" >&2
else
echo -e "$@" >&2
fi
}
function _info()
{
if [ -t 2 ]; then
echo -e "\033[1;36m$@\033[m" >&2
else
echo -e "$@" >&2
fi
}
_find_project_root ()
{
local dir
dir=$PWD;
while [ "$dir" != "/" ]; do
if [ -f "$dir/BLADE_ROOT" ]; then
echo "$dir"
return 0
fi;
dir=`dirname "$dir"`
done
return 1
}
absolute_path()
{
dir=`dirname $1`
base=`basename $1`
if [[ $base == '.' || $base == '/' ]] ; then
echo `cd $dir; pwd`
else
echo "`cd $dir; pwd`/$base"
fi
}
escape_slash ()
{
echo ${1////\\/}
}
# convert_headers root
# @param root: the path of source root (original root)
convert_headers ()
{
root="$1"
srcs=`find . -regex "$SOURCE_TYPES"`
for src in $srcs ; do
src_dir=`dirname $src`
_info "Checking $src"
lines=`awk '
/^[[:blank:]]*#[[:blank:]]*include[[:blank:]]*"[[:alnum:].\-_][[:alnum:]/.\-_]*"/ {
match($0, /"[[:alnum:]/.\-_]*"/, m)
hdr=gensub(/"([[:alnum:]/.\-_]*)"/, "\\\\1", "", m[0])
printf "%s,%s\n", NR, hdr
}' $src`
sed_cmd=""
for line in $lines ; do
lineno=${line%,*}
hdr=${line#*,}
hdr_path=""
# Ignore *.pb.h
if [[ "$hdr" =~ \.pb\.h$ ]]; then
continue
fi
# Already good
if [ -f "$root/$hdr" ] ; then
continue
fi
if [ -f "$src_dir/$hdr" ] ; then
hdr_path=$(absolute_path "$src_dir/$hdr")
else
search_dir="`find $root | grep -E "/$hdr$"`"
if [[ $(echo "$search_dir" | wc -l) -gt 1 ]]; then
_warning "Ambiguous file: '$hdr', can't be fixed automatically:\n$search_dir"
continue
fi
if [ -f "$search_dir" ] ; then
hdr_path=${search_dir#$root/}
fi
fi
# check if header file exists from source root
if [[ $hdr_path != "" ]] ; then
new_hdr=${hdr_path#$root/}
if [[ "$hdr" != "$new_hdr" ]] ; then
_info "Replace '$hdr' to '$new_hdr'"
sed_cmd=${sed_cmd}"$lineno {s|$(escape_slash $hdr)|$(escape_slash $new_hdr)|g}"$'\n'
fi
fi
done
if [ -n "$sed_cmd" ]; then
_info "Converting $src, backup file is $src.bak"
sed -i.bak "$sed_cmd" $src
fi
done
}
if [ $# -ne 1 ]; then
_warning "Fix the source file include path"
_warning "Usage: $0 <dir>"
_warning "Example: $0 ."
exit 1
fi
cd $1 || exit 1
ROOT=$(_find_project_root)
convert_headers $ROOT