README.SELF-CONTAINED-EXTENSIONS 4.9 KB

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