-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeconfig.sh
More file actions
288 lines (258 loc) · 7.27 KB
/
Copy pathmakeconfig.sh
File metadata and controls
288 lines (258 loc) · 7.27 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/bin/bash
#configure文件自动生成器
if [ "$(uname)" = "Darwin" ];then
SED_CMD="gsed"
else
SED_CMD="sed"
fi
#帮助文件
proj_help(){
echo "Usage: makeconfig [options...][operate]"
echo "Options:"
echo -e "-o <file>\tPlace the output into <file>"
echo -e "-v <value>\tSpecify software version <value>"
echo -e "-l <library>\tlibraries to pass to the linker, e.g. -l<library>"
echo -e "-I <include dir>\t C preprocessor flags: include dir, e.g. -I<include dir>"
echo -e "-D <value>\t Define a MACRO, e.g. -DDEBUG=true"
echo -e "-F <value>\t linker flags, e.g. -export-dynamic -lmemcached"
echo "operate:"
echo -e "clean\tClean all generated files"
echo -e "<file>\tSpecify a file name,replace -o <file>"
}
#执行清理
clean_all(){
if [[ -n "$1" ]];then
rm -rf stamp-h1 autoscan.log aclocal.m4 autom4te.cache >/dev/null
else
make clean >/dev/null 2>&1
rm -rf stamp-h1 *.o *.Po .deps .DS_Store config.* autoscan.log Makefile* configure* aclocal.m4 autom4te.cache >/dev/null
rm -f compile depcomp install-sh missing >/dev/null
for curdir in `ls | tr "\n" " "`;do
if [[ -d "$curdir" ]];then
rm -rf $curdir/Makefile* $curdir/.deps $curdir/.DS_Store
fi
done
fi
}
#获取程序名称
get_proj_name(){
tmp_contents=""
for cur_file in `ls *.c* 2>/dev/null` ;do
tmp_contents=$(grep "^\s*int\s\s*main\s*(.*)" $cur_file 2>/dev/null)
if [[ -z "$tmp_contents" ]];then
tmp_contents=$(grep "^\s*void\s\s*main\s*(.*)" $cur_file 2>/dev/null)
fi
if [[ -z "$tmp_contents" ]];then
tmp_contents=$(grep "^\s*main\s*(.*)" $cur_file 2>/dev/null)
fi
if [[ -n "$tmp_contents" ]];then
echo ${cur_file%.c*}
break
fi
done
unset tmp_contents
}
#获取项目子目录
get_sub_dirs(){
tmp_file_ext="*.c*"
if [[ -n "$*" ]];then
tmp_file_ext=$*
fi
for curdir in `ls | tr "\n" " "`;do
if [[ -d "$curdir" ]];then
filenames=$(ls $curdir/$tmp_file_ext 2>/dev/null | tr "\n" " ")
if [[ -n "$filenames" ]];then
echo $curdir
fi
fi
done
unset tmp_file_ext
}
#生成configure.ac文件
make_configure_ac(){
sub_makefile=""
for curdir in `get_sub_dirs`;do
sub_makefile="${sub_makefile} ${curdir}\/Makefile"
done
$SED_CMD -i "s/\[FULL-PACKAGE-NAME\]/${proj_name%.*}/g" configure.scan
$SED_CMD -i "s/\[VERSION\]/${proj_version}/g" configure.scan
$SED_CMD -i "s/\[BUG-REPORT-ADDRESS\]/${author_email}/g" configure.scan
$SED_CMD -i "s/AC_CONFIG_SRCDIR/AM_INIT_AUTOMAKE(${proj_name%.*}, ${proj_version})\nAC_PROG_RANLIB\nAC_CONFIG_SRCDIR/g" configure.scan
if [[ $proj_type = "shared" ]];then
$SED_CMD -i "s/AC_OUTPUT/AC_PROG_LIBTOOL\nAC_CONFIG_FILES([Makefile${sub_makefile}])\nAC_OUTPUT/g" configure.scan
else
$SED_CMD -i "s/AC_OUTPUT/AC_CONFIG_FILES([Makefile${sub_makefile}])\nAC_OUTPUT/g" configure.scan
fi
mv configure.scan configure.ac
}
#生成Makefile.am文件
make_makefile_am(){
tmp_proj_source=$(ls *.c* 2>/dev/null | tr "\n" " ")
tmp_proj_head=$(ls *.h* include/*.h* 2>/dev/null | tr "\n" " ")
subdirs=""
sublibs=""
tmp_sub_head=""
ctmp_sub_head=""
#在各个子目录下生成Makefile.am文件
for cursrcdir in `get_sub_dirs`;do
sourcefiles=$(ls $cursrcdir/*.c* 2>/dev/null | tr "\n" " ")
subdirs="${subdirs} ${cursrcdir}"
sublibs="${sublibs} ${cursrcdir}/lib${cursrcdir}.a"
echo "noinst_LIBRARIES=lib${cursrcdir}.a" > $cursrcdir/Makefile.am
echo "lib${cursrcdir}_a_SOURCES=${sourcefiles//$cursrcdir\/}" >> $cursrcdir/Makefile.am
if [[ -n "$external_cppflags" ]];then
echo "lib${cursrcdir}_a_CPPFLAGS=${external_cppflags}" >> $cursrcdir/Makefile.am
fi
tmp_sub_head=$(ls include/${cursrcdir}/*.h* 2>/dev/null | tr "\n" " ")
ctmp_sub_head=$(ls ${cursrcdir}/*.h* 2>/dev/null | tr "\n" " ")
if [[ -n "${tmp_sub_head}" ]];then
tmp_sub_head=${tmp_sub_head//include\//../include/}
fi
if [[ -n "${ctmp_sub_head}" ]];then
ctmp_sub_head=${ctmp_sub_head//${cursrcdir}\//}
fi
echo "${cursrcdir}includedir=\$(includedir)/${cursrcdir}" >> $cursrcdir/Makefile.am
echo "${cursrcdir}include_HEADERS=${tmp_sub_head} ${ctmp_sub_head}" >> $cursrcdir/Makefile.am
done
unset tmp_sub_head
unset ctmp_sub_head
#在顶层目录生成Makefile.am文件
echo "AUTOMAKE_OPTIONS=foreign" > Makefile.am
if [[ -n "$subdirs" ]];then
echo "SUBDIRS=${subdirs}" >> Makefile.am
fi
if [[ $proj_type = "shared" ]];then
echo "lib_PROGRAMS=${1}" >> Makefile.am
else
echo "bin_PROGRAMS=${1}" >> Makefile.am
fi
echo "${1//./_}_SOURCES=${tmp_proj_source}" >> Makefile.am
if [[ -n "$external_incs" ]];then
echo "INCLUDES = ${external_incs}" >> Makefile.am
fi
if [[ -n "$sublibs" ]];then
echo "${1//./_}_LDADD=${sublibs}" >> Makefile.am
fi
if [[ -n "$external_cppflags" ]];then
echo "${1//./_}_CPPFLAGS=${external_cppflags}" >> Makefile.am
fi
if [[ -n "$external_ldflags" ]];then
echo "${1//./_}_LDFLAGS=${external_ldflags}" >> Makefile.am
fi
if [[ -n "${tmp_proj_head}" ]];then
echo "include_HEADERS=${tmp_proj_head}" >> Makefile.am
fi
if [[ -n "${external_libs}" ]];then
echo "LIBS=${external_libs}" >> Makefile.am
fi
unset tmp_proj_source
unset tmp_proj_head
}
#生成configure文件
make_configure(){
#如果项目名称为空,则退出
if [[ -z "${proj_name}" ]];then
echo "Error: File name must be verified! "
proj_help
exit
fi
#生成configure.scan
autoscan
#生成configure.ac
make_configure_ac
#创建 Makefile.am
make_makefile_am "$proj_name"
#生成aclocal.m4
aclocal
#生成动态库
if [[ $proj_type = "shared" ]];then
libtoolize -f -c
fi
#生成 configure
autoconf
#生成 config.h.in
autoheader
#生成Makefile.in
automake --add-missing >/dev/null 2>&1
}
#初始化变量
proj_name=$(get_proj_name)
proj_version="1.0.0"
proj_type="bin"
author_email="spring.wind2006@163.com"
external_libs=""
external_incs=""
external_ldflags=""
external_cppflags=""
#自动获取参数
main_params=$*
while getopts ":o:l:shv:D:F:I:" temp_opt
do
case $temp_opt in
o )
proj_name=$OPTARG
main_params=${main_params/-o*$OPTARG/}
;;
s )
proj_type="shared"
main_params=${main_params/-s/}
;;
l )
external_libs="${external_libs} -l ${OPTARG}"
main_params=${main_params/-l*$OPTARG/}
;;
v )
proj_version=$OPTARG
main_params=${main_params/-v*$OPTARG/}
;;
D )
external_cppflags="${external_cppflags} -D${OPTARG}"
main_params=${main_params/-D*$OPTARG/}
;;
F )
external_ldflags="${external_ldflags} ${OPTARG}"
main_params=${main_params/-F*$OPTARG/}
;;
I )
external_incs="${external_incs} -I${OPTARG}"
main_params=${main_params/-I*$OPTARG/}
;;
h )
proj_help
exit
;;
esac
done
main_params="${main_params// /}"
#直接生成文件支持,例如:makeconfig spring
if [[ -n "$main_params" ]];then
proj_name=$main_params
fi
if [[ $proj_type = "shared" ]];then
external_ldflags="${external_ldflags} –fPIC –shared"
if [[ -n "${proj_name}" ]];then
echo "Tips: you are making a shared library..."
proj_name="lib${proj_name}.so"
fi
fi
#处理主程序
case $main_params in
clean )
clean_all
;;
*)
#清理文件
echo "Clean up the generated file......finished!"
clean_all
echo "Generate \"configure\" file......please wait!"
#生成configure文件
make_configure
if [[ "$?" = "-1" ]];then
echo "failed! please check you code!"
else
echo "Success! you can run \"./configure && make\" to build!"
fi
#清理中间文件
clean_all other
;;
esac