PHP extension in D

"Rémy Mouëza" firstname.name at gmail.com
Thu Apr 11 13:30:28 PDT 2013


On Thursday, 11 April 2013 at 11:28:05 UTC, gedaiu wrote:
> Hi,
>
> I want to extend Php with an extension written in D, and wrap 
> some D classes in php. My questions are:
>
> 1. How I can build a static library(I am using eclipse with ddt)
> 2. How I can create methods and create objects in c++ from the 
> D library.
>
>
> Thanks,
> Bogdan


I once tried to make a proof of concept PHP extension in D on an
Ubuntu Linux. I chose to make it "simple" by letting Swig
(www.swig.org) do the heavy wrapping work for me. It's then
becomes quite as much "difficult" as calling some D code from C.

Below is an outline of the steps it took me to wrap a D function
for PHP.
You'll still have to figure out how to make it work nicely with
Eclipse and on your particular platform, nonetheless it should
help you getting started.

Note that this approach could also be used to call some D code
from Java, C# or any other language supported by Swig.


1. Write / get the D code (speedup.d).
> import std.stdio , std.string ; import std.conv : to;
> string speedUp (string msg) {
>     writefln ("-- %s --", msg);
>     return `sped up "%s"`.format (msg);
> }


2. make a C API for that code, declaring the functions of that
API with the "extern (C)" qualifier.
> extern (C) {
>     immutable (char) * d_speedUp (char * msg) {
>         return toStringz (speedUp (to!string (msg)));
>     }
> }


3. Create a Swig interface file for that C API (speedup.i). Also
add a declaration to the "rt_init" function from the druntime.
> %module speedup
> char rt_init (long long);
> const char * d_speedUp (char * msg);

4. run Swig: `swig -php speedup.i`. This will generate several
files:
       - speedup_wrap.c
       - php_speedup.h
       - speedup.php

Now we've got to compile all those parts together: we want to
generate a "speedup.so" dynamic library object from all that we
got.

5. From what I researched, dmd (at least on Linux) needs to see a
main function to properly generate all the code of the druntime
within the dynamic library/shared object it generates.  We will
trick it by adding a fake main function (dfakemain.d):
> void main () {}

6. compile dfakemain.d:
> dmd -c dfakemain.d

7. compile speedup.o:
> dmd -fPIC -c -L-shared dfakemain.o speedup.d

8. compile speedup_wrap.o, you'll need to have everything needed
to compile extension for PHP on your platform:
> gcc `php-config --includes` -fpic -c speedup_wrap.c

9. compile speedup.so, our target:
> dmd -shared speedup_wrap.o dfakemain.o speedup.o -ofspeedup.so

10. If everything went well so far, you can now try your
extension within a script (dmd_speedup.php):
> <?php rt_init (0); // initialize the D runtime. echo d_speedUp 
> ("hello from php"), "\n";

You can launch the script with extension loading enabled with a
command line such like this one:
> php -d enable_dl=1 -d extension=`pwd`/speedup.so dmd_speedup.php

It should then output:
> -- hello from php --
> sped up "hello from php"


Now, one could imagine to get a bit further by automating all
that, like using the json output from dmd to generate a swig
interface file and a nice OOP interface on the PHP side, but this
would be much more work.

Have fun!


More information about the Digitalmars-d mailing list