xref: /PHP-5.4/ext/ext_skel (revision 941c39bd)
1#!/bin/sh
2
3givup() {
4	echo $*
5	exit 1
6}
7
8usage() {
9echo "$0 --extname=module [--proto=file] [--stubs=file] [--xml[=file]]"
10echo "           [--skel=dir] [--full-xml] [--no-help]"
11echo ""
12echo "  --extname=module   module is the name of your extension"
13echo "  --proto=file       file contains prototypes of functions to create"
14echo "  --stubs=file       generate only function stubs in file"
15echo "  --xml              generate xml documentation to be added to phpdoc-cvs"
16echo "  --skel=dir         path to the skeleton directory"
17echo "  --full-xml         generate xml documentation for a self-contained extension"
18echo "                     (not yet implemented)"
19echo "  --no-help          don't try to be nice and create comments in the code"
20echo "                     and helper functions to test if the module compiled"
21exit 1
22}
23
24if test $# = 0; then
25  usage
26fi
27
28while test $# -gt 0; do
29  case "$1" in
30  -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
31  *) optarg= ;;
32  esac
33
34  case $1 in
35    --extname=?*)
36	extname=$optarg
37	EXTNAME=`echo $extname | tr "[:lower:]" "[:upper:]"`
38	;;
39    --proto=?*)
40	proto=$optarg
41	;;
42    --stubs=*)
43	stubs=yes
44	stubfile=$optarg
45	;;
46    --xml)
47	xml="yes"
48	;;
49    --xml=?*)
50	xml=$optarg
51	;;
52    --full-xml)
53	full_xml="yes"
54	;;
55    --no-help)
56	no_help="yes"
57	;;
58    --skel=?*)
59	skel_dir=$optarg
60	;;
61    *)
62	usage
63	;;
64  esac
65  shift
66done
67
68if test -d "$extname" ; then
69  givup "Directory $extname already exists."
70fi
71
72if test -z "$skel_dir"; then
73  skel_dir="skeleton"
74fi
75
76## convert skel_dir to full path
77skel_dir=`cd $skel_dir && pwd`
78
79test -d $skel_dir || givup "directory $skel_dir does not exist or is not directory"
80
81if echo '\c' | grep -s c >/dev/null 2>&1
82then
83    ECHO_N="echo -n"
84    ECHO_C=""
85else
86    ECHO_N="echo"
87    ECHO_C='\c'
88fi
89
90if test -z "$stubs"; then
91  echo "Creating directory $extname"
92  stubfile=$extname"/function_stubs"
93  mkdir $extname || givup "Cannot create directory $extname"
94fi
95
96if test -n "$proto"; then
97  cat $proto | awk -v extname=$extname -v stubs=$stubs -v stubfile=$stubfile -v xml=$xml -v full_xml=$full_xml -v i_know_what_to_do_shut_up_i_dont_need_your_help_mode=$no_help -f $skel_dir/create_stubs
98fi
99
100if test -z "$stubs"; then
101  cd $extname
102  chmod 755 .
103
104$ECHO_N "Creating basic files:$ECHO_C"
105
106$ECHO_N " config.m4$ECHO_C"
107cat >config.m4 <<eof
108dnl \$Id\$
109dnl config.m4 for extension $extname
110
111dnl Comments in this file start with the string 'dnl'.
112dnl Remove where necessary. This file will not work
113dnl without editing.
114
115dnl If your extension references something external, use with:
116
117dnl PHP_ARG_WITH($extname, for $extname support,
118dnl Make sure that the comment is aligned:
119dnl [  --with-$extname             Include $extname support])
120
121dnl Otherwise use enable:
122
123dnl PHP_ARG_ENABLE($extname, whether to enable $extname support,
124dnl Make sure that the comment is aligned:
125dnl [  --enable-$extname           Enable $extname support])
126
127if test "\$PHP_$EXTNAME" != "no"; then
128  dnl Write more examples of tests here...
129
130  dnl # --with-$extname -> check with-path
131  dnl SEARCH_PATH="/usr/local /usr"     # you might want to change this
132  dnl SEARCH_FOR="/include/$extname.h"  # you most likely want to change this
133  dnl if test -r \$PHP_$EXTNAME/\$SEARCH_FOR; then # path given as parameter
134  dnl   ${EXTNAME}_DIR=\$PHP_$EXTNAME
135  dnl else # search default path list
136  dnl   AC_MSG_CHECKING([for $extname files in default path])
137  dnl   for i in \$SEARCH_PATH ; do
138  dnl     if test -r \$i/\$SEARCH_FOR; then
139  dnl       ${EXTNAME}_DIR=\$i
140  dnl       AC_MSG_RESULT(found in \$i)
141  dnl     fi
142  dnl   done
143  dnl fi
144  dnl
145  dnl if test -z "\$${EXTNAME}_DIR"; then
146  dnl   AC_MSG_RESULT([not found])
147  dnl   AC_MSG_ERROR([Please reinstall the $extname distribution])
148  dnl fi
149
150  dnl # --with-$extname -> add include path
151  dnl PHP_ADD_INCLUDE(\$${EXTNAME}_DIR/include)
152
153  dnl # --with-$extname -> check for lib and symbol presence
154  dnl LIBNAME=$extname # you may want to change this
155  dnl LIBSYMBOL=$extname # you most likely want to change this 
156
157  dnl PHP_CHECK_LIBRARY(\$LIBNAME,\$LIBSYMBOL,
158  dnl [
159  dnl   PHP_ADD_LIBRARY_WITH_PATH(\$LIBNAME, \$${EXTNAME}_DIR/lib, ${EXTNAME}_SHARED_LIBADD)
160  dnl   AC_DEFINE(HAVE_${EXTNAME}LIB,1,[ ])
161  dnl ],[
162  dnl   AC_MSG_ERROR([wrong $extname lib version or lib not found])
163  dnl ],[
164  dnl   -L\$${EXTNAME}_DIR/lib -lm
165  dnl ])
166  dnl
167  dnl PHP_SUBST(${EXTNAME}_SHARED_LIBADD)
168
169  PHP_NEW_EXTENSION($extname, $extname.c, \$ext_shared)
170fi
171eof
172
173$ECHO_N " config.w32$ECHO_C"
174cat >config.w32 <<eof
175// \$Id\$
176// vim:ft=javascript
177
178// If your extension references something external, use ARG_WITH
179// ARG_WITH("$extname", "for $extname support", "no");
180
181// Otherwise, use ARG_ENABLE
182// ARG_ENABLE("$extname", "enable $extname support", "no");
183
184if (PHP_$EXTNAME != "no") {
185	EXTENSION("$extname", "$extname.c");
186}
187
188eof
189
190$ECHO_N " .svnignore$ECHO_C"
191cat >.svnignore <<eof
192.deps
193*.lo
194*.la
195eof
196
197$ECHO_N " $extname.c$ECHO_C"
198echo "s/extname/$extname/g" > sedscript
199echo "s/EXTNAME/$EXTNAME/g"  >> sedscript
200echo '/__function_entries_here__/r function_entries'  >> sedscript
201echo '/__function_stubs_here__/r function_stubs'  >> sedscript
202echo '/__header_here__/r ../../header'  >> sedscript
203echo '/__footer_here__/r ../../footer'  >> sedscript
204echo '/__function_entries_here__/D'  >> sedscript
205echo '/__function_stubs_here__/D'  >> sedscript
206echo '/__header_here__/D'  >> sedscript
207echo '/__footer_here__/D'  >> sedscript
208if [ ! -z "$no_help" ]; then
209    echo "/confirm_$extname_compiled/D" >> sedscript
210    echo '/Remove the following/,/^\*\//D' >> sedscript
211    echo 's/[[:space:]]\/\*.\+\*\///' >> sedscript
212    echo 's/^\/\*.*\*\/$//' >> sedscript
213    echo '/^[[:space:]]*\/\*/,/^[[:space:]]*\*\//D' >> sedscript
214fi
215
216sed -f sedscript < $skel_dir/skeleton.c > $extname.c
217
218
219$ECHO_N " php_$extname.h$ECHO_C"
220echo "s/extname/$extname/g" > sedscript
221echo "s/EXTNAME/$EXTNAME/g" >> sedscript
222echo '/__function_declarations_here__/r function_declarations' >> sedscript
223echo '/__header_here__/r ../../header'  >> sedscript
224echo '/__footer_here__/r ../../footer'  >> sedscript
225echo '/__function_declarations_here__/D' >> sedscript
226echo '/__header_here__/D'  >> sedscript
227echo '/__footer_here__/D'  >> sedscript
228if [ ! -z "$no_help" ]; then
229    echo "/confirm_$extname_compiled/D" >> sedscript
230    echo 's/[[:space:]]\/\*.\+\*\///' >> sedscript
231    echo 's/^\/\*.*\*\/$//' >> sedscript
232    echo '/^[[:space:]]*\/\*/,/^[[:space:]]*\*\//D' >> sedscript
233fi
234sed -f sedscript <$skel_dir/php_skeleton.h > php_$extname.h
235
236$ECHO_N " CREDITS$ECHO_C"
237echo "s/extname/$extname/g" > sedscript
238sed -f sedscript <$skel_dir/CREDITS > CREDITS
239
240$ECHO_N " EXPERIMENTAL$ECHO_C"
241echo "s/extname/$extname/g" > sedscript
242sed -f sedscript <$skel_dir/EXPERIMENTAL > EXPERIMENTAL
243
244$ECHO_N " tests/001.phpt$ECHO_C"
245mkdir tests || givup "Cannot create tests directory"
246chmod 755 tests
247sed -f sedscript <$skel_dir/tests/001.phpt > tests/001.phpt
248
249if test -z "$stubs" && test -z "$no_help"; then
250  $ECHO_N " $extname.php$ECHO_C"
251  sed \
252    -e "s/extname/$extname/g" \
253    <$skel_dir/skeleton.php \
254    > $extname.php
255fi
256
257rm sedscript
258
259if test -n "$proto"; then
260  if test -z "$stubs"; then
261    rm function_entries
262    rm function_declarations
263    rm function_stubs
264  fi
265  if test -f function_warning; then
266    rm function_warning
267    warning="
268NOTE! Because some arguments to functions were resources, the code generated
269cannot yet be compiled without editing. Please consider this to be step 4.5
270in the instructions above.
271"
272  fi
273fi
274
275find . -type f | xargs chmod 644
276find . -type d | xargs chmod 755
277fi
278
279echo " [done]."
280
281if test -z "$no_help" && test -z "$stubs"; then
282  cat <<eof
283
284To use your new extension, you will have to execute the following steps:
285
2861.  $ cd ..
2872.  $ vi ext/$extname/config.m4
2883.  $ ./buildconf
2894.  $ ./configure --[with|enable]-$extname
2905.  $ make
2916.  $ ./sapi/cli/php -f ext/$extname/$extname.php
2927.  $ vi ext/$extname/$extname.c
2938.  $ make
294
295Repeat steps 3-6 until you are satisfied with ext/$extname/config.m4 and
296step 6 confirms that your module is compiled into PHP. Then, start writing
297code and repeat the last two steps as often as necessary.
298$warning
299eof
300fi
301