xref: /PHP-7.1/ext/ext_skel (revision 447b41f6)
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-svn"
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
123PHP_ARG_ENABLE($extname, whether to enable $extname support,
124dnl Make sure that the comment is aligned:
125[  --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/\$PHP_LIBDIR, ${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/\$PHP_LIBDIR -lm
165  dnl ])
166  dnl
167  dnl PHP_SUBST(${EXTNAME}_SHARED_LIBADD)
168
169  PHP_NEW_EXTENSION($extname, $extname.c, \$ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
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
182ARG_ENABLE("$extname", "enable $extname support", "no");
183
184if (PHP_$EXTNAME != "no") {
185	EXTENSION("$extname", "$extname.c", PHP_EXTNAME_SHARED, "/DZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
186}
187
188eof
189
190$ECHO_N " .gitignore$ECHO_C"
191cat >.gitignore <<eof
192*.lo
193*.la
194.libs
195acinclude.m4
196aclocal.m4
197autom4te.cache
198build
199config.guess
200config.h
201config.h.in
202config.log
203config.nice
204config.status
205config.sub
206configure
207configure.in
208include
209install-sh
210libtool
211ltmain.sh
212Makefile
213Makefile.fragments
214Makefile.global
215Makefile.objects
216missing
217mkinstalldirs
218modules
219run-tests.php
220tests/*/*.diff
221tests/*/*.out
222tests/*/*.php
223tests/*/*.exp
224tests/*/*.log
225tests/*/*.sh
226eof
227
228$ECHO_N " $extname.c$ECHO_C"
229echo "s/extname/$extname/g" > sedscript
230echo "s/EXTNAME/$EXTNAME/g"  >> sedscript
231echo '/__function_entries_here__/r function_entries'  >> sedscript
232echo '/__function_stubs_here__/r function_stubs'  >> sedscript
233echo '/__header_here__/r ../../header'  >> sedscript
234echo '/__footer_here__/r ../../footer'  >> sedscript
235echo '/__function_entries_here__/D'  >> sedscript
236echo '/__function_stubs_here__/D'  >> sedscript
237echo '/__header_here__/D'  >> sedscript
238echo '/__footer_here__/D'  >> sedscript
239if [ ! -z "$no_help" ]; then
240    echo "/confirm_$extname_compiled/D" >> sedscript
241    echo '/Remove the following/,/^\*\//D' >> sedscript
242    echo 's/[[:space:]]\/\*.\+\*\///' >> sedscript
243    echo 's/^\/\*.*\*\/$//' >> sedscript
244    echo '/^[[:space:]]*\/\*/,/^[[:space:]]*\*\//D' >> sedscript
245fi
246
247sed -f sedscript < $skel_dir/skeleton.c > $extname.c
248
249
250$ECHO_N " php_$extname.h$ECHO_C"
251echo "s/extname/$extname/g" > sedscript
252echo "s/EXTNAME/$EXTNAME/g" >> sedscript
253echo '/__function_declarations_here__/r function_declarations' >> sedscript
254echo '/__header_here__/r ../../header'  >> sedscript
255echo '/__footer_here__/r ../../footer'  >> sedscript
256echo '/__function_declarations_here__/D' >> sedscript
257echo '/__header_here__/D'  >> sedscript
258echo '/__footer_here__/D'  >> sedscript
259if [ ! -z "$no_help" ]; then
260    echo "/confirm_$extname_compiled/D" >> sedscript
261    echo 's/[[:space:]]\/\*.\+\*\///' >> sedscript
262    echo 's/^\/\*.*\*\/$//' >> sedscript
263    echo '/^[[:space:]]*\/\*/,/^[[:space:]]*\*\//D' >> sedscript
264fi
265sed -f sedscript <$skel_dir/php_skeleton.h > php_$extname.h
266
267$ECHO_N " CREDITS$ECHO_C"
268echo "s/extname/$extname/g" > sedscript
269sed -f sedscript <$skel_dir/CREDITS > CREDITS
270
271$ECHO_N " EXPERIMENTAL$ECHO_C"
272echo "s/extname/$extname/g" > sedscript
273sed -f sedscript <$skel_dir/EXPERIMENTAL > EXPERIMENTAL
274
275$ECHO_N " tests/001.phpt$ECHO_C"
276mkdir tests || givup "Cannot create tests directory"
277chmod 755 tests
278sed -f sedscript <$skel_dir/tests/001.phpt > tests/001.phpt
279
280if test -z "$stubs" && test -z "$no_help"; then
281  $ECHO_N " $extname.php$ECHO_C"
282  sed \
283    -e "s/extname/$extname/g" \
284    <$skel_dir/skeleton.php \
285    > $extname.php
286fi
287
288rm sedscript
289
290if test -n "$proto"; then
291  if test -z "$stubs"; then
292    rm function_entries
293    rm function_declarations
294    rm function_stubs
295  fi
296  if test -f function_warning; then
297    rm function_warning
298    warning="
299NOTE! Because some arguments to functions were resources, the code generated
300cannot yet be compiled without editing. Please consider this to be step 4.5
301in the instructions above.
302"
303  fi
304fi
305
306find . -type f | xargs chmod 644
307find . -type d | xargs chmod 755
308fi
309
310echo " [done]."
311
312if test -z "$no_help" && test -z "$stubs"; then
313  cat <<eof
314
315To use your new extension, you will have to execute the following steps:
316
3171.  $ cd ..
3182.  $ vi ext/$extname/config.m4
3193.  $ ./buildconf
3204.  $ ./configure --[with|enable]-$extname
3215.  $ make
3226.  $ ./sapi/cli/php -f ext/$extname/$extname.php
3237.  $ vi ext/$extname/$extname.c
3248.  $ make
325
326Repeat steps 3-6 until you are satisfied with ext/$extname/config.m4 and
327step 6 confirms that your module is compiled into PHP. Then, start writing
328code and repeat the last two steps as often as necessary.
329$warning
330eof
331fi
332