PHPをソースコードからビルド
試験環境
環境1
- OS: Windows XP SP2
- コンパイラ: Visual Studio 2005 SP1
- 作業ディレクトリ: F:\work
- PHPバージョン:5.2.4
環境2
- OS: Windows XP SP1
- コンパイラ:Visual Studio 6.0 SP6
- 作業ディレクトリ: C:\work
- PHPバーション:5.2.6
- Webサーバ:Apache 2.0.61
- DB:MySQL 5.0.51b
環境の準備
1. コンパイラの準備
- Visual Studio 2005を利用の場合、SP1を適応させておく
⇒ http://www.microsoft.com/japan/msdn/vstudio/downloads/sp/vs2005/sp1/ - Visual Studio 6.0を利用の場合、SP6を適応させておく
⇒ http://www.microsoft.com/japan/msdn/vstudio/downloads/sp/VS6SP6.aspx
※因みに、PHPのソースコードはVC6で作成されており*1、旧版のPHPマニュアルでもVC6が推奨環境であると記載されていた。
(現版のマニュアルでは、既にVC6が入手困難なためか、その文言は削除されている)
2. ソースコードの準備
- http://www.php.net/downloads.php よりPHPのソースコードをDL*2
⇒ F:\work\php-5.2.*に展開
※旧版を利用したい場合は、DLページのURLのバージョン部分の数字を変更することでページにアクセス可能。
ex) 最新版:http://jp2.php.net/get/php-5.2.9.tar.bz2/from/a/mirror ↓ 5.2.6 :http://jp2.php.net/get/php-5.2.6.tar.bz2/from/a/mirror 5.2.4 :http://jp2.php.net/get/php-5.2.4.tar.bz2/from/a/mirror ※中には既に提供されていないバージョンもある。
3. 必要な拡張モジュールの準備
- http://pecl.php.net/ より必要な拡張モジュールを検索、DL
⇒ 解凍した拡張モジュールのフォルダ名からバージョン名を取り除いて、F:\work\php-5.2.*\ext内にフォルダを格納
※DLした拡張モジュールのフォルダ内にconfig.w32が存在することを確認する。
⇒ このファイルが存在しないとコンパイル対象のモジュールとして認識されず、コンパイルできない。
4. ビルド用ファイルを保存するフォルダの用意
必要なフォルダは、
- バイナリツール(.exe)などを保存する”bin”フォルダ
- ヘッダファイル(.h)などのインクルードファイルを保存する”include”フォルダ
- ライブラリファイル(.lib)を保存する”lib”フォルダ
⇒ これらを格納する親フォルダとして、F:\work\php_buildを作成
※この時点で、以下のような構成になる。
ルートドライブ(F:\) ┗work ┣php-5.2.* ┃ ┣ext ┃ ┃ ┣bcmath ┃ ┃ ┣calender ┃ ┃ : : ┃ : ┗php_build ┣bin ┣include ┗lib
5. バイナリツールの準備
- http://pecl2.php.net/downloads/php-windows-builds/php-libs/binary-tools.zip よりバイナリツールをDL
⇒ 解凍し、binの中身をF:\work\php_build\binにコピー
6. ライブラリおよびインクルードファイルの準備
- Visual Studio 2005を利用の場合
⇒ http://pecl2.php.net/downloads/php-windows-builds/php-libs/VC8/x86/ より必要と思われるファイルをDL - Visual Studio 6.0を利用の場合
⇒ http://pecl2.php.net/downloads/php-windows-builds/php-libs/VC6/x86/ より必要と思われるファイルをDL
※最低限、bindlib / libiconv / libxml2 / zlibは必要。
※取りあえず、全てDLしておくことをおすすめする。
コンパイルの実行
8. コマンドプロンプトの起動
- Visual Studio 2005を利用の場合
⇒ スタートメニュー > すべてのプログラム > Microsoft Visual Studio 2005 > Visual Studio Tools > Visual Studio 2005コマンド プロンプトから起動 - Visual Studio 6.0を利用の場合
⇒ C:\Program Files\Microsoft Visual Studio\VC98\Bin\VCVARS32.BATを通常のコマンドプロンプトから実行
cf. http://oshiete1.goo.ne.jp/qa129968.html
9. 環境変数の設定
- set "PATH=F:\work\php_build\bin;%PATH%"
- set "PATH=F:\work\php_build\include;%PATH%"
- set "PATH=F:\work\php_build\lib;%PATH%"
を実行する。
11. オプションの設定
※ここから先は簡単には上手くいかないことが殆どなので、実行するに当たっては根気を以って臨むこと!
- ここでは、自分が実際に設定したオプションの例を提示する。基本的な方針については以下の通り。
- 基本方針
-
- 必要ないWebサーバ対応およびデータベース対応はすべてキャンセル。
- また、然程必要性がなく、ライブラリおよびインクルードファイルの収集が困難と判断したものについてもキャンセル。
- 拡張モジュールのID3を独自にインストール
- Visual Studio 2005を利用の場合
”cscript /nologo configure.js --with-extra-includes="F:\work\php_build\include" --with-extra-libs="F:\work\php_build\lib" --enable-object-out-dir=.. --without-aolserver --disable-apache --without-apache-includes --without-apache-libs --disable-apache2-2filter --disable-apache2-2handler --enable-apache2handler --without-pi3web --without-dba --without-fbsql --without-fdf --with-gd=shared --without-t1lib --without-gettext --without-gmp --enable-id3 --without-interbase --enable-mbstring --without-mhash --with-mime-magic --without-ming --without-dblib --with-mysql --with-mysqli --without-oci8 --disable-odbc --without-pgsql --without-pspell --without-snmp --without-sqlite --without-sybase-ct --without-pdo-firebird --without-pdo-oci --without-pdo-oci8 --without-pdo-odbc --without-pdo-pgsql --without-pdo-sqlite --without-pdo-sqlite-external --enable-zip --enable-exif --with-xsl”を実行 - Visual Studio 6.0を利用の場合
”cscript /nologo configure.js --with-extra-includes="C:\work\php_build\include" --with-extra-libs="C:\work\php_build\lib" --enable-object-out-dir=.. --without-aolserver --disable-apache --without-apache-includes --without-apache-libs --disable-apache2-2filter --disable-apache2-2handler --enable-apache2handler --without-pi3web --without-curl --without-dba --without-fbsql --without-fdf --with-gd=shared --without-t1lib --without-gmp --enable-id3 --without-interbase --enable-mbstring --without-mhash --with-mime-magic --without-ming --without-msql --without-dblib --with-mysql --with-mysqli --without-oci8 --disable-odbc --without-pgsql --without-pspell --without-snmp --without-sqlite --without-sybase-ct --without-pdo-firebird --without-pdo-oci --without-pdo-oci8 --without-pdo-odbc --without-pdo-pgsql --without-pdo-sqlite --without-pdo-sqlite-external --enable-zip --enable-exif --with-xsl”を実行
-
- 各オプションの詳細については、”cscript /nologo configure.js --help”で確認する。
- Warningや
が出た場合は、原因となった必要なファイルを探してきて、先ほどパスを通したフォルダ(bin / include / lib)内に格納する。 - 場合によっては、”lib*****_a.lib”となっているファイル名の”lib”や”_a”の部分を削除してみる。
⇒ libiconv、olber32など - 解決が困難と判断した場合は、該当ファイルを必要とするオプションをキャンセルする。
(--withとなっている部分を--withoutに、--enableとなっている部分を--disableと指定する) - lemon.exeが無いといわれることがあるが、とりあえず、無視しても大丈夫な模様。
(そもそも何の実行ファイルか不明)
- 場合によっては、”lib*****_a.lib”となっているファイル名の”lib”や”_a”の部分を削除してみる。
12. コンパイルの実行
- ”nmake”を実行
※途中でエラーが発生した場合、エラー箇所を修正後、再度”nmake”によって再開できる。
⇒ 修正不能、再開ができない場合は、一旦、”nmake clean”を実行の後、再度”buildconf”からやり直す。
トラブルシューティング
以下に自分の経験したエラーとその対処法について記すので、参考までに。- ”main\spprintf.c(184) : error C2491: 'strnlen' : dllimport 関数 の定義は許されません。”
と出た場合は、F:\work\php-5.2.*\main\config_w32.hの末尾に”#define HAVE_STRNLEN 1
”を追加 - ext\calender\jewish.cでエラーが出た場合は、文字コードをUTF-8で読み込み → UTF-8Nで保存をする。
- フォルダパスでエラーが出た場合は、適宜ファイルを指定の場所にコピーするなどして対処
⇒ bcmathなどで発生する - ”fatal error C1900: 'P1' Version '20060201' と 'P2' Version '20050411' が一致しません。”
といわれる場合、Visual Studio 2005にSP1を適応すれば解決される。
cf. http://mobile.seisyun.net/cgi/read.cgi/pc11/pc11_tech_1196663772/503 - 旧バージョンのPHPをコンパイルする場合で、”libxml.obj : error LNK2001: 外部シンボル "_xmlDllMain" は未解決です。”
と出る場合は、ext\libxml\config.w32を最新のものにバージョンアップする。 - ”libcurl.lib(ldap.obj) : error LINK2019:”
が出た時は、http://curl.haxx.se/download.htmlより、最新版のlibcurlをDLしてきて、その中に含まれるlibcurl_a.libをlibcurl.libに改名する。 - ”php5ts.dll.def :error LNK2001 : 外部シンボル compressBound は未解決です。
php5ts.dll.def :error LNK2001 : 外部シンボル deflateBound は未解決です。
php5ts.dll.def :error LNK2001 : 外部シンボル deflatePrime は未解決です。
php5ts.dll.def :error LNK2001 : 外部シンボル gzclearerr は未解決です。
php5ts.dll.def :error LNK2001 : 外部シンボル gzungetc は未解決です。
php5ts.dll.def :error LNK2001 : 外部シンボル inflateBack は未解決です。
php5ts.dll.def :error LNK2001 : 外部シンボル inflateBackEnd は未解決です。
php5ts.dll.def :error LNK2001 : 外部シンボル inflateBackInit_ は未解決です。
php5ts.dll.def :error LNK2001 : 外部シンボル inflateCopy は未解決です。
php5ts.dll.def :error LNK2001 : 外部シンボル zlibCompileFlags は未解決です。”
などと表示される場合は、zlibのインクルードおよびライブラリファイルを全て最新のものに更新する。
cf. http://translate.google.com/translate?hl=ja&sl=ru&u=http://diamondz.land.ru/OpenSourceForWindows.txt&ei=R8uzSbnLLMPVkAWLyYzZBA&sa=X&oi=translate&resnum=1&ct=result&prev=/search%3Fq%3Dphp5ts.dll.def%2B:%2Berror%2BLNK2001:%2B%2BcompressBound%26hl%3Dja%26lr%3D%26sa%3DG
13. snapshotの作成
- オプションの設定
- Visual Studio 2005を利用の場合
”cscript /nologo configure.js --with-extra-includes="F:\work\php_build\include" --with-extra-libs="F:\work\php_build\lib" --enable-snapshot-build --without-aolserver --disable-apache --without-apache-includes --without-apache-libs --disable-apache2-2filter --disable-apache2-2handler --enable-apache2handler --without-pi3web --without-dba --without-fbsql --without-fdf --with-gd=shared --without-t1lib --without-gettext --without-gmp --enable-id3 --without-interbase --enable-mbstring --without-mhash --with-mime-magic --without-ming --without-dblib --with-mysql --with-mysqli --without-oci8 --disable-odbc --without-pgsql --without-pspell --without-snmp --without-sqlite --without-sybase-ct --without-pdo-firebird --without-pdo-oci --without-pdo-oci8 --without-pdo-odbc --without-pdo-pgsql --without-pdo-sqlite --without-pdo-sqlite-external --enable-zip --enable-exif --with-xsl”を実行 - Visual Studio 6.0を利用の場合
”cscript /nologo configure.js --with-extra-includes="C:\work\php_build\include" --with-extra-libs="C:\work\php_build\lib" --enable-snapshot-build --without-aolserver --disable-apache --without-apache-includes --without-apache-libs --disable-apache2-2filter --disable-apache2-2handler --enable-apache2handler --disable-nsapi --without-nsapi-includes --without-nsapi-libs --without-pi3web --without-curl --without-dba --without-fbsql --without-fdf --with-gd=shared --without-t1lib --without-gmp --enable-id3 --without-interbase --enable-mbstring --without-mhash --with-mime-magic --without-ming --without-msql --without-dblib --with-mysql --with-mysqli --without-oci8 --disable-odbc --without-pgsql --without-pspell --without-snmp --without-sqlite --without-sybase-ct --without-pdo-firebird --without-pdo-oci --without-pdo-oci8 --without-pdo-odbc --without-pdo-pgsql --without-pdo-sqlite --without-pdo-sqlite-external --enable-zip --enable-exif --with-xsl”を実行
- Visual Studio 2005を利用の場合
⇒ 必要に応じてインクルードファイル、ライブラリファイルを取得してF:\work\php_build\includeまたはF:\work\php_build\libに格納する。
参考リンク
- MySQLソースDL:
http://downloads.mysql.com/archives.php?p=mysql-5.0&o=-win-src - mingソース:
http://www.libming.org/Releases - mhashソース:
http://mhash.sourceforge.net/の”Here you can download mhash source code”より - pspellソース:
http://aspell.net/→落としたソースフォルダのinterface\cc内にある - net-snmpソース:
http://www.net-snmp.org/ - その他:
http://pecl2.php.net/downloads/php-windows-builds/source/
※php-5.2.9では”WARNING: you don't have a snapshot template”と表示されsnapshot作成が完了できない場合がある
⇒ 何らかのsnapshotのtemplateが必要になるようだが詳細は不明
14. 実行テスト
- 生成された実行ファイルの存在するフォルダに移動し、”php -v”などを実行。
⇒- ”--enable-object-out-dir=..”オプションを付けていた場合:
cd F:\work\Release_TS\ - 付けていない場合:
cd F:\work\php-5.2.*\Release_TS\
- ”--enable-object-out-dir=..”オプションを付けていた場合:
- コマンドラインからスクリプトを実行する場合
”php -r phpinfo();”
といった形で実行する。
因みに、”php -r phpinfo(); > info.txt”としておくと、生成したPHPの構成情報をテキストファイルに保存できる。
※”Error in my_thread_global_end(): 1 threads didn't exit”
と表示された場合は、利用するMySQLと同じバージョンのlibmysql.dllをC:\WINDOWS\SYSTEM32\にコピーする。
⇒ デフォルトではMySQLのインストールディレクトリ内のbinフォルダに存在する。
インストール
PHPのインストールについては別途記述予定。