-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo
executable file
·182 lines (157 loc) · 3.48 KB
/
go
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
#!/bin/sh
#
# XXX move to make/cmake/whatever
#
BASE_PATH="./ext"
LOG="$( pwd )/$( mktemp --tmpdir=. build.log.XXX )"
PROTO_PATH="${BASE_PATH}/proto"
PHP_MODULE_DIR="/usr/lib/php/modules"
HTTPD_USER="http"
HTTPD_GROUP="http"
PROTOC_C=$( which protoc-c )
EGREP=$( which egrep )
SED=$( which sed )
PHPIZE=$( which phpize )
MAKE=$( which make )
INSTALL=$( which install )
#
# print a relative list of files in $PROTO_PATH, matching a pattern
#
get_proto_files()
{
find $PROTO_PATH -type f -name $1 -printf '%f\n'
}
#
# compile a proto file in/to $PROTO_PATH
#
compile_proto_file()
{
if [ -f "${PROTO_PATH}/$1" ]; then
cd $PROTO_PATH
if ! $PROTOC_C --c_out=. $1 >> $LOG 2>&1; then
cd - > /dev/null 2>&1
return 1
else
cd - > /dev/null 2>&1
return 0
fi
fi
return 42 # usage foo
}
#
# extracts all descriptors from a compiled header file
#
extract_descriptors()
{
$EGREP -o '\&([a-z_]+__descriptor)' $PROTO_PATH/$1 | $SED 's,&,,g'
}
#
# generates a phproto_message[] array from all extracted descriptors
#
generate_descriptor_array()
{
for FILE in $( get_proto_files "*.h" ); do
echo "#include \"proto/${FILE}\""
done
echo
echo "static const ProtobufCMessageDescriptor* phproto_messages[] = {"
for FILE in $( get_proto_files "*.h" ); do
for DESCRIPTOR in $( extract_descriptors $FILE ); do
echo " &${DESCRIPTOR}, "
done
done
echo "};"
}
#
# prepare for build
#
prepare()
{
# compile all .proto files
for FILE in $( get_proto_files "*.proto" ); do
echo -n "compiling $FILE.. "
if ! compile_proto_file $FILE; then
echo "FAILED"
return 1
else
echo "SUCCESS"
fi
done
# generate phproto.h from template and descriptors
echo -n "generating phproto.h.. "
if ! cp $BASE_PATH/phproto.h.template $BASE_PATH/phproto.h >> $LOG 2>&1; then
echo "FAILED"
return 1
else
generate_descriptor_array >> $BASE_PATH/phproto.h
echo "#endif" >> $BASE_PATH/phproto.h
echo "SUCCESS"
fi
# generate config.m4 by adding compiled .c files
echo -n "generating config.m4.. "
PROTOFILES=$( get_proto_files '*.c' | $SED 's,^,proto/,g' | tr '\n' ' ' )
if ! $SED "s,{PROTOFILES},${PROTOFILES},g" $BASE_PATH/config.m4.template > $BASE_PATH/config.m4 2>&1; then
echo "FAILED"
return 1
else
echo "SUCCESS"
fi
}
#
# build
#
build()
{
cd $BASE_PATH
echo -n "PHPizing source tree.. "
if ! $PHPIZE >> $LOG 2>&1; then
echo "FAILED"
return 1
else
echo "SUCCESS"
fi
echo -n "configuring.. "
if ! ./configure --enable-phproto >> $LOG 2>&1; then
echo "FAILED"
return 1
else
echo "SUCCESS"
fi
echo -n "building.. "
NUM_PROCS=$(( $( grep processor /proc/cpuinfo | wc -l ) * 2))
if ! $MAKE -j$NUM_PROCS >> $LOG 2>&1; then
echo "FAILED"
return 1
else
echo "SUCCESS"
fi
cd - > /dev/null 2>&1
return 0
}
#
# "deploy" :)
#
deploy()
{
echo -n "deploying.. "
if ! $INSTALL -o$HTTPD_USER -g$HTTPD_GROUP -m0644 $BASE_PATH/modules/phproto.so \
$PHP_MODULE_DIR/. >> $LOG 2>&1; then
echo "FAILED"
return 1
else
echo "SUCCESS"
fi
return 0
}
#
#------------- main
#
if ! prepare; then
exit 1
fi
if ! build; then
exit 1
fi
if ! deploy; then
exit 1;
fi