ext_skel 8.4 KB

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