• No results found

Building a PHP Extension on Windows

In document building-custom-php-extensions (Page 113-117)

After successfully building PHP on Windows, you can now begin the process of building your custom extension. Remember, the goal of this is to create a DLL file that can be dynamically loaded by PHP at runtime.

The quickest method for accomplishing this is to move your existing extension into the PHP source tree and then build the extension as a separate Visual C++

project. To do this, you will need a version of php.exe on the Windows machine and you will need Cygwin.

The first step is to create the Visual C++ project (.dsp) file. This is accomplished using the ext_skel_win32.php script in the ext directory of the PHP source. If you have already build your extension on a unix system, then you will need to make a small modification to the ext_skel_win32.php script before running it. The code for the script is shown below.

1 <?php

2 /* $Id: ext_skel_win32.php,v 1.1.2.1 2003/01/31 18:50:41 fmk Exp $ */

3

4 if (php_sapi_name() != "cli") {

5 echo "Please run this script using the CLI version of PHP\n";

6 exit;

7 } 8 /*

9 This script can be used on Win32 systems 10

11 1) Make sure you have CygWin installed

12 2) Adjust the $cygwin_path to match your installation 13 3) Change the environment cariable PATHEXT to include .PHP 14 4) run ext_skel --ext_name=...

15 the first time you run this script you will be asked to 16 associate it with a program. chooses the CLI version of php.

17 */

18

19 $cygwin_path = 'c:\cygwin\bin';

20

21 $path = getenv("PATH");

22 putenv("PATH=$cygwin_path;$path");

2324 array_shift($argv);

25 //system("sh ext_skel " . implode(" ", $argv));

2627 $extname = "";

28 $skel = "skeleton";

29 foreach($argv as $arg) {

30 if (strtolower(substr($arg, 0, 9)) == "--extname") {

31 $extname = substr($arg, 10);

32 }

33 if (strtolower(substr($arg, 0, 6)) == "--skel") {

34 $skel = substr($arg, 7);

35 }

36 } 37

38 $fp = fopen("$skel/skeleton.dsp", "rb");

39 if ($fp) {

40 $dsp_file = fread($fp, filesize("$skel/skeleton.dsp"));

41 fclose($fp);

4243 $dsp_file = str_replace("extname", $extname, $dsp_file);

44 $dsp_file = str_replace("EXTNAME", strtoupper($extname), $dsp_file);

45 $fp = fopen("$extname/$extname.dsp", "wb");

46 if ($fp) {

47 fwrite($fp, $dsp_file);

48 fclose($fp);

49 }

50 } 5152 ?>

Code Fragment 77: Source of the ext_skel_win32.php script.

The required change is to comment out line 25 of the script. Line 25 makes a system call to run the ext_skel script, but since you have already done this on a unix system, this step is uneccesary. You should run the script from within the Cygwin environment for simplicity. You must update some environment

variables before running the script. An example of using this script (for the GLPK extension) is shown below:

1 export PATHEXT=$PATHEXT';.PHP'

2 export PATH=$PATH':/cygdrive/c/php/source/php-4.3.2/release_ts/cli' 3 cd /path/to/php_source/ext

4 /path/to/php.exe ext_skel_win32.php –extname=glpk

After doing this, a new file extname.dsp will be found in the extname directory.

This is the Visual C++ project file required to build your custom extension. For reference, line 1 of the above steps adds the .PHP extension to the PATHEXT environment variable as required by the note in line 13 of the

ext_skel_win32.php script (see Code Fragment 77). Line 2 adds the full path of the php.exe binary to the PATH variable within the Cygwin environment. You will modify this line to reflect the actual location of php.exe on your system.

You are now ready to build your extension into a Windows DLL. Open the project file in Visual C++. You may need to edit your library dependencies (if you are linking against an external library, for example) or other build flags before building your project. In the case of the GLPK project, for example, it is first necessary to build the GLPK library on windows and then add the glpk.lib file to the list of linker dependencies.

I also recommend changing the ouput file name of the project. By default, the output file name is extname.dll whereas the typical file name for a dynamically loadable extension for PHP on Windows is php_extname.dll. You can make this change by selecting the Settings menu item from the Project menu and then by selecting the Link tab. The Output file name can be directly edited (see Figure 6).

Figure 6: Changing the output file name of the extension.

You can now build your extension. The resulting DLL file must be copied to the directory specified by the extension_dir parameter in your php.ini file. Once this has been done you can have the extension loaded by PHP at startup by

specifying the new DLL file in the extensions section of your php.ini file.

If your extension DLL statically links in its own dependencies, you need nothing more to begin testing your extension on Windows PHP. If your extension DLL requires any other external DLLs, you will need to make sure that those DLLs are located in the DLL search path.

Summary

Building extensions on Windows is slightly more complicated than building under a unix, but if you plan to redistribute your extension to the PHP community, you will likely need to provide a Windows port. The goal of this section was to show that this is a relatively straightforward process.

Conclusion

PHP is designed and has been designed from the beginning to provide a robust extension mechanism. Though not all of this functionality is documented thoroughly, the authors of PHP have obviously considered extension building a key part of PHP development and have therefore provided powerful tools for building the framework of a new extension.

This document has been an attempt to provide details for developing new extensions, customizing existing extensions and understanding the internals of PHP development. If you have questions or comments about this document, please visit the forums at http://www.php4devguide.com/.

All reasonable efforts will be made to provide updates to this document based on changes to the PHP internals and based on feedback from readers. If you feel that there is something significantly lacking in this document or if there are errors, please contact me via email at [email protected].

The source code for all the examples in this document can be downloaded from http://www.php4devguide.com/.

In document building-custom-php-extensions (Page 113-117)