README.SELF-CONTAINED-EXTENSIONS 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. $Id$
  2. =============================================================================
  3. HOW TO CREATE A SELF-CONTAINED PHP EXTENSION
  4. A self-contained extension can be distributed independently of
  5. the PHP source. To create such an extension, two things are
  6. required:
  7. - Configuration file (config.m4)
  8. - Source code for your module
  9. We will describe now how to create these and how to put things
  10. together.
  11. PREPARING YOUR SYSTEM
  12. While the result will run on any system, a developer's setup needs these
  13. tools:
  14. GNU autoconf
  15. GNU automake
  16. GNU libtool
  17. GNU m4
  18. All of these are available from
  19. ftp://ftp.gnu.org/pub/gnu/
  20. CONVERTING AN EXISTING EXTENSION
  21. Just to show you how easy it is to create a self-contained
  22. extension, we will convert an embedded extension into a
  23. self-contained one. Install PHP and execute the following
  24. commands.
  25. $ mkdir /tmp/newext
  26. $ cd /tmp/newext
  27. You now have an empty directory. We will copy the files from
  28. the mysql extension:
  29. $ cp -rp php-4.0.X/ext/mysql/* .
  30. It is time to finish the module. Run:
  31. $ phpize
  32. You can now ship the contents of the directory - the extension
  33. can live completely on its own.
  34. The user instructions boil down to
  35. $ ./configure \
  36. [--with-php-config=/path/to/php-config] \
  37. [--with-mysql=MYSQL-DIR]
  38. $ make install
  39. The MySQL module will either use the embedded MySQL client
  40. library or the MySQL installation in MYSQL-DIR.
  41. DEFINING THE NEW EXTENSION
  42. Our demo extension is called "foobar".
  43. It consists of two source files "foo.c" and "bar.c"
  44. (and any arbitrary amount of header files, but that is not
  45. important here).
  46. The demo extension does not reference any external
  47. libraries (that is important, because the user does not
  48. need to specify anything).
  49. LTLIBRARY_SOURCES specifies the names of the sources files. You can
  50. name an arbitrary number of source files here.
  51. CREATING THE M4 CONFIGURATION FILE
  52. The m4 configuration can perform additional checks. For a
  53. self-contained extension, you do not need more than a few
  54. macro calls.
  55. ------------------------------------------------------------------------------
  56. PHP_ARG_ENABLE(foobar,whether to enable foobar,
  57. [ --enable-foobar Enable foobar])
  58. if test "$PHP_FOOBAR" != "no"; then
  59. PHP_NEW_EXTENSION(foobar, foo.c bar.c, $ext_shared)
  60. fi
  61. ------------------------------------------------------------------------------
  62. PHP_ARG_ENABLE will automatically set the correct variables, so
  63. that the extension will be enabled by PHP_NEW_EXTENSION in shared mode.
  64. The first argument of PHP_NEW_EXTENSION describes the name of the
  65. extension. The second names the source-code files. The third passes
  66. $ext_shared which is set by PHP_ARG_ENABLE/WITH to PHP_NEW_EXTENSION.
  67. Please use always PHP_ARG_ENABLE or PHP_ARG_WITH. Even if you do not
  68. plan to distribute your module with PHP, these facilities allow you
  69. to integrate your module easily into the main PHP module framework.
  70. CREATING SOURCE FILES
  71. ext_skel can be of great help when creating the common code for all modules
  72. in PHP for you and also writing basic function definitions and C code for
  73. handling arguments passed to your functions. See README.EXT_SKEL for further
  74. information.
  75. As for the rest, you are currently alone here. There are a lot of existing
  76. modules, use a simple module as a starting point and add your own code.
  77. CREATING THE SELF-CONTAINED EXTENSION
  78. Put config.m4 and the source files into one directory. Then, run phpize
  79. (this is installed during make install by PHP 4.0).
  80. For example, if you configured PHP with --prefix=/php, you would run
  81. $ /php/bin/phpize
  82. This will automatically copy the necessary build files and create
  83. configure from your config.m4.
  84. And that's it. You now have a self-contained extension.
  85. INSTALLING A SELF-CONTAINED EXTENSION
  86. An extension can be installed by running:
  87. $ ./configure \
  88. [--with-php-config=/path/to/php-config]
  89. $ make install
  90. ADDING SHARED MODULE SUPPORT TO A MODULE
  91. In order to be useful, a self-contained extension must be loadable
  92. as a shared module. I will explain now how you can add shared module
  93. support to an existing module called foo.
  94. 1. In config.m4, use PHP_ARG_WITH/PHP_ARG_ENABLE. Then you will
  95. automatically be able to use --with-foo=shared[,..] or
  96. --enable-foo=shared[,..].
  97. 2. In config.m4, use PHP_NEW_EXTENSION(foo,.., $ext_shared) to enable
  98. building the extension.
  99. 3. Add the following lines to your C source file:
  100. #ifdef COMPILE_DL_FOO
  101. ZEND_GET_MODULE(foo)
  102. #endif
  103. PECL SITE CONFORMITY
  104. If you plan to release an extension to the PECL website, there are several
  105. points to be regarded.
  106. 1. Add LICENSE or COPYING to the package.xml
  107. 2. The following should be defined in one of the extension header files
  108. #define PHP_FOO_VERSION "1.2.3"
  109. This macros has to be used within your foo_module_entry to indicate the
  110. extension version.