PHP拡張開発(一)-最初のPHP拡張を構築する

5615 ワード

まず、システムにgccコンパイラ、適切なバージョンのbisonなどがインストールされていることを確認する必要があります.次に、ソースコードからPHPをコンパイルインストールするために実行する基本的なコマンドです.
# cd php-src
# ./buildconf
# ./configure --enable-debug --enable-maintainer-zts --enable-cli
# make
# make install

基本的な拡張スケルトンの構築
PHP拡張開発では、ext_skelを使用して拡張構造スケルトンの作成を完了します.
$ ./ext_skel
./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
           [--skel=dir] [--full-xml] [--no-help]

  --extname=module      module         
  --proto=file          file              
  --stubs=file       generate only function stubs in file
  --xml              generate xml documentation to be added to phpdoc-cvs
  --skel=dir                  
  --full-xml         generate xml documentation for a self-contained extension (not yet implemented)
  --no-help          don't try to be nice and create comments in the code and helper functions to test if the module compiled

注:ext_skelコマンドファイルは、ソースファイルのextディレクトリの下にあります.
ここでの--extnameパラメータは、作成する拡張子名であり、拡張子は小文字+下線で構成され、extディレクトリで一意である必要があります.
たとえば、ここではext_demo_1というPHP拡張子を作成します.
/vagrant/ext$ ./ext_skel --extname=ext_demo_1
Creating directory ext_demo_1
Creating basic files: config.m4 config.w32 .svnignore ext_demo_1.c php_ext_demo_1.h CREDITS EXPERIMENTAL tests/001.phpt ext_demo_1.php [done].
To use your new extension, you will have to execute the following steps:
  • $ cd ..
  • $ vi ext/extdemo1/config.m4
  • $ ./buildconf
  • $ ./configure --[with|enable]-extdemo1
  • $ make
  • $ ./php -f ext/extdemo1/extdemo1.php
  • $ vi ext/extdemo1/extdemo1.c
  • $ make Repeat steps 3-6 until you are satisfied with ext/extdemo1/config.m4 andstep 6 confirms that your module is compiled into PHP. Then, start writingcode and repeat the last two steps as often as necessary.

  • extディレクトリの下に新しい拡張ディレクトリext_demo_1が表示されます.
    /vagrant/ext/ext_demo_1$ ls
    config.m4   CREDITS       ext_demo_1.c    php_ext_demo_1.h
    config.w32  EXPERIMENTAL  ext_demo_1.php  tests
    

    この場合、この拡張はコンパイルできません.config.m4ファイルを編集する必要があります.
    プロファイルconfig.m4コンフィギュレーション・ファイルconfig.m4は、UNIXビルド・システム拡張がサポートするconfigureオプションと、拡張に必要な追加のライブラリ、どのソース・ファイルを含むかなどを示します.このファイルは、GNUのautoconf構文を使用し、dnlで始まる動作注釈で、カッコ([および])に含まれる文字列を使用します.
    Autoconf構文はAUTOCNFドキュメントを参照
    PHP_ARG_ENABLE(ext_demo_1, whether to enable ext_demo_1 support,
    [  --enable-ext_demo_1           Enable ext_demo_1 support])
    
    if test "$PHP_EXT_DEMO_1" != "no"; then
      PHP_SUBST(EXT_DEMO_1_SHARED_LIBADD)
      PHP_NEW_EXTENSION(ext_demo_1, ext_demo_1.c, $ext_shared)
    fi
    

    上記のautoconfのプロファイルは、1番目のマクロPHP_ARG_ENABLEであり、3つのパラメータを含む.
  • extdemo 1これは最初のパラメータであり、./configureenable-ext_demo_1というオプション
  • が確立された.
  • の2番目のパラメータは、./configureコマンドが拡張プロファイルに処理すると、そのパラメータの内容
  • が表示される.
  • の3番目のパラメータは./configureコマンドのヘルプで、./configure --helpを使用すると
  • が表示されます.
    2番目のマクロはPHP_NEW_EXTENSIONで、拡張モジュールと拡張の一部としてコンパイルする必要があるソースファイルを宣言します.複数のソースファイルが必要な場合は、スペース区切り、3番目のパラメータ$ext_を使用します.sharedは呼び出しPHP_SUBST(EXT_DEMO_1_SHARED_LIBADD)に関係する.
    PHP_NEW_EXTENSION(ext_demo_1, ext_demo_1.c, $ext_shared)
    

    コンパイル拡張config.m4ファイルを修正した後、PHPと拡張子をコンパイルします.
    /vagrant$ ./configure --disable-libxml --enable-ext_demo_1 --disable-dom --disable-simplexml --disable-xml --disable-xmlreader --disable-xmlwriter --without-pear --prefix=/usr/local/php
    /vagrant$ make
    /vagrant$ sudo make install
    Installing PHP SAPI module:       cgi
    Installing PHP CGI binary: /usr/local/php/bin/
    Installing PHP CLI binary:        /usr/local/php/bin/
    Installing PHP CLI man page:      /usr/local/php/man/man1/
    Installing build environment:     /usr/local/php/lib/php/build/
    Installing header files:          /usr/local/php/include/php/
    Installing helper programs:       /usr/local/php/bin/
      program: phpize
      program: php-config
    Installing man pages:             /usr/local/php/man/man1/
      page: phpize.1
      page: php-config.1
    /vagrant/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
    ln -s -f /usr/local/php/bin/phar.phar /usr/local/php/bin/phar
    Installing PDO headers:          /usr/local/php/include/php/ext/pdo/
    

    このとき、PHPは/usr/local/phpディレクトリの下にインストールされ、このディレクトリに入ると、以下のファイルが表示されます.
    /usr/local/php$ ls
    bin  include  lib  man
    
    /usr/local/php/binディレクトリに入り、次のコマンドを実行します.
    /usr/local/php/bin$ ./php --info|grep demo
    Configure Command =>  './configure'  '--disable-libxml' '--enable-ext_demo_1' '--disable-dom' '--disable-simplexml' '--disable-xml' '--disable-xmlreader' '--disable-xmlwriter' '--without-pear' '--prefix=/usr/local/php'
    ext_demo_1
    ext_demo_1 support => enabled
    
    phpinfo()の拡張サポートが有効になっていることがわかります.上記の手順でインストールされた拡張には、confirm_ext_demo_1_compiled(arg)という名前の拡張機能が含まれています.実行結果は次のとおりです.
    /usr/local/php/bin$ ./php -r "echo confirm_ext_demo_1_compiled('mylxsw');"
    Congratulations! You have successfully modified ext/ext_demo_1/config.m4. Module mylxsw is now compiled into PHP.
    
    ext_demo_1の拡張インストールが成功したことがわかります.拡張開発の詳細については、AICODEに移動してください.CC.