XSBuilder - Autogenerating XS-glue Code
O'Reilly OpenSource Convention 2002
Gerald Richter
ecos gmbh
http://www.ecos.de
XSBuilder - What is it?
It's purpose is to automaticly generate a Perl interface to C Code
Solves the problem, that the Perl interface is not always uptodate with the C interface
Saves a lot of Copy&Paste work
Systematical changes have to be done only once
For example changes in the memory management of strings.
Is part of mod_perl 2.0 build system
Most code is developed by Doug MacEachern. Additionaly I have
- abstracted the code from mod_perl so it's useable for any C code
- added new features like callbacks and the ability to parse comments
- Replaced C::Scan with a Parse::RecDescent to be platform and compiler independend
Goal is to replace the current mod_perl XS generation code with XSBuilder
Inline versus XSBuilder
- Inline: embed C-Code into Perl
- XSBuilder: Create interface for existing C-libraries/applicationen
XSBuilder - What does it do?
Create Perl functions/methods for every C function
The function can be assigned to different packages, also automaticly by inspecting the first parameter
Create a Perl class for every C structure
Every element of structure becomes a Perl method to get/set it's value. The object can be either a scalar reference (used by mod_perl) or a reference to a hash (use by Embperl), which allows to store extra data by the Perl code into this hash.
Create glue code to handle callbacks
There several sorts of callback, not all are implemented right now
Create Perl constant subs
Coming soon...
XSBuilder - How does it work?
Parse the C header files
Extract
- Functions, their arguments and return types
- Structures and it's members
- Constants
- Callbacks
and create four tables which contains the results
Create the XS code
Input is
- The source tables
- Mapfiles which contains the mapping from C to Perl
- Addtional C and Perl code that can be used to customize the interface
Output is
- The XS files (one form every generated class)
- Makefile.PL for every class
- pm files
Parse the source
Create your own ParseSource class and override methods...
package Apache::DAV::ParseSource;use strict;use vars qw{@ISA $VERSION} ;use ExtUtils::XSBuilder::ParseSource ;@ISA = ('ExtUtils::XSBuilder::ParseSource') ;$VERSION = '0.01';my $dav_dir = 'C:\perl\msrc\cvs\mod_dav' ;my $ap_dir = 'c:\programme\apache group\apache' ;# ============================================================================sub find_includes {my $self = shift;return $self->{includes} if $self->{includes};my @includes = ("$ap_dir/include/ap_alloc.h", "$dav_dir/mod_dav.h") ;return $self->{includes} = $self -> sort_includes (\@includes) ;}# ============================================================================sub package { 'Apache::DAV' }# ============================================================================sub preprocess {my $self = shift ;$_[0] =~ s/(?:API_EXPORT)(?:_NONSTD)?\s*\(\s*(.*?)\s*\)/$1/g ;}1;
...run it
use FindBin ;use lib ($FindBin::Bin) ;require ParseSource ;Apache::DAV::ParseSource -> run ;
...and you get
C:\perl\msrc\davint>perl xsbuilder\source_scan.plWill use mod_dav in C:\perl\msrc\cvs\mod_davWill use Apache in c:\programme\apache group\apacheInitialize parserscan c:\programme\apache group\apache/include/ap_alloc.h ...constant: APACHE_ALLOC_Hfunc: ap_init_allocfunc: ap_cleanup_allocfunc: ap_make_sub_poolfunc: ap_destroy_poolconstant: ap_pool_joinfunc: ap_pool_joinfunc: ap_find_poolfunc: ap_pool_is_ancestorfunc: ap_clear_poolfunc: ap_cleanup_for_execfunc: ap_pallocfunc: ap_pcallocfunc: ap_pstrdupfunc: ap_pstrndupfunc: ap_pstrcatfunc: ap_pvsprintfvaluefield: ap_pool * : poolvaluefield: int : elt_sizevaluefield: int : neltsvaluefield: int : nallocvaluefield: char * : eltsstruct: (type=array_header)...
The result is stored in four tables
- xsbuilder/tables/Apache/DAV/FuntionTable.pm
-
Contains all function, it arguments and comments
- xsbuilder/tables/Apache/DAV/ConstantTable.pm
-
Contains all constants
- xsbuilder/tables/Apache/DAV/StructureTable.pm
-
Contains all structures, it's members and their comments
- xsbuilder/tables/Apache/DAV/CallbackTable.pm
-
Contains all callback function definitions
Create the map files
Mapfiles are used to tell XSBuilder how C datatypes, structures and function aruments should be mapped into Perl ones.
Create your own WrapXS class and override methods
package Apache::DAV::WrapXS ;use strict;use vars qw{@ISA $VERSION} ;use ExtUtils::XSBuilder::WrapXS ;@ISA = ('ExtUtils::XSBuilder::WrapXS') ;$VERSION = '0.01';# ============================================================================sub new_parsesource { [ Apache::DAV::ParseSource->new ] }# ============================================================================sub my_xs_prefix { 'davxs_' }# ============================================================================sub h_filename_prefix { 'moddav_xs_' }# ============================================================================sub xs_includes {my $self = shift ;my $i = $self -> SUPER::xs_includes ;my @i = grep (!/ap_alloc/, @$i) ;return \@i ;}
XSBuilder can create/update initial maps for you
use FindBin ;use lib ($FindBin::Bin) ;require ParseSource ;require WrapXS ;Apache::DAV::WrapXS->checkmaps (' ');
run it
C:\perl\msrc\davint>perl xsbuilder\xs_check.plWill use mod_dav in C:\perl\msrc\cvs\mod_davWill use Apache in c:\programme\apache group\apacheParse xsbuilder\maps/_types.map...WARNING: No *_function.map file found in xsbuilder\mapsWARNING: No *_callback.map file found in xsbuilder\mapsWARNING: No *_structure.map file found in xsbuilder\mapsWrite xsbuilder\maps/new_function.map...Write xsbuilder\maps/new_callback.map...Write xsbuilder\maps/new_structure.map...Write xsbuilder\maps/new_type.map...
Now we have four map files
- new_types.map
-
Contains the mapping from C type to Perl classes
- new_functions.map
-
Contains the mapping form C functions to Perl functions. Can be used to reorder arguments, tell XSBuilder which arguments are actualy return values and in which Perl package the function will be created.
- new_structures.map
-
Contains the mapping from C structures to Perl classes and defines for which members a access methods should be created. You can also specify if you want a
"new"method for the class.
- new_callbacks.map
-
Contains the mapping form C callback functions to Perl callback functions. Can be used to reorder arguments, tell XSBuilder which arguments are actualy return values and in which Perl package the function will be created.
It's a good idea to rename the prefix from "new_" to something unique, here we use "dav"
Everytime you rerun checkmaps, XSBuilder will create new_* files with the items that are not already part of the other maps.
Next step is to customize the maps...
type map
autogenerated dav_type.map
DIR |FILE |HANDLE |array_header |dav_buffer |dav_dyn_context |dav_dyn_hooks |dav_dyn_module |dav_dyn_provider |dav_error |dav_get_props_result |dav_hooks_liveprop |dav_hooks_locks |dav_hooks_repository |dav_if_header |dav_if_state_type |...
Add Perl classes
struct array_header | Apache::Arraystruct dav_buffer |struct dav_datum | Apache::DAV::Datumstruct dav_dyn_context | Apache::DAV::DynContextstruct dav_dyn_hooks | Apache::DAV::DynHooksstruct dav_dyn_module | Apache::DAV::DynModulestruct dav_dyn_provider | Apache::DAV::DynProviderstruct dav_error | Apache::DAV::Errorstruct dav_get_props_result | Apache::DAV::PropsResultstruct dav_hooks_db | Apache::DAV::HooksDbstruct dav_hooks_liveprop | Apache::DAV::HooksLivepropstruct dav_hooks_locks | Apache::DAV::HooksLocksstruct dav_hooks_repository | Apache::DAV::HooksRepositorystruct dav_hooks_vsn |struct dav_if_header | Apache::DAV::IfHeaderstruct dav_if_state_list | Apache::DAV::StateList...struct pool | Apache::Poolstruct request_rec | Apache::struct server_rec | Apache::Server...
Defines the mapping from C datatypes to Perl datatypes and classes and tells XSBuilder which datatype are (like) structures
function map
Function map defines the mapping from C functions arguments to Perl arguments
- Tell XSBuilder where to place functions and which prefix to strip
-
MODULE=Apache::DAV PACKAGE=guess PREFIX=dav_
- Simple entries in the function map will be mapped 1:1 from C to Perl
-
dav_add_responsedav_buffer_appenddav_buffer_initdav_buffer_placedav_buffer_place_memdav_check_bufsizedav_close_propdbdav_collect_liveprop_urisdav_dyn_module_adddav_empty_elem...
- The following map file entry tells XSBuilder that the value of "res_p" should be returned
-
dav_get_resource | | r, <res_pThe C function
int dav_get_resource(request_rec *r, dav_resource **res_p) ;How to call it from Perl
# get a sub request of the actual document$subr = $r -> lookup_uri($uri);# get a mod_dav resource objectmy ($err, $resource) = $subr->get_resource;
- You can let XSBuilder insert your custom code, for the interface
-
If you call
"get_props"from Perl"glue_dav_get_props"will be called, which can adjust the arguments and return types as necessary. The actual code for"glue_dav_get_props"will be taken from separate include file.dav_get_props | glue_dav_get_allprops | glue_xsinclude\Apache\DAV\PropResult\Apache__DAV__PropResults.h
dav_get_props_result * dav_glue_get_props(dav_propdb * db, dav_xml_doc *doc){dav_get_props_result * result = (dav_get_props_result *)ap_palloc (db -> p, sizeof (dav_get_props_result)) ;*result = dav_get_props(db, doc) ;return result ;}
- Arguments can be replaced
-
MODULE=Apache::Array PACKAGE=Apache::Array PREFIX=ap_ap_make_array | ap_make_array(r->pool, nelts, elt_size) | request_rec *:r, nelts, elt_sizeap_make_array requires a pool a it's first parameter, we pass the request_rec from Perl and XSBuilder will take the pool from the request_rec.
structure map
MALLOC=strdup:$dest = ($type)strdup($src)FREE=strdup:free($src)<dav_xml_elem>namenslangfirst_cdatafollowing_cdataparentnextfirst_childattrlast_childns_scopepropidproviderns_mapnew</dav_xml_elem>MALLOC=strdup:$dest = ($type)ap_pstrdup(obj -> pool, $src)<array_header>poolelt_sizeneltsnalloceltsprivate</array_header>
Create a accessor functions for every element and, if requested, a new method
$setprop = Apache::DAV::XMLElem -> new ({name => 'prop'}) ;$elem = Apache::DAV::XMLElem -> new ({name => $name, ns => $namespaces}) ;$setprop -> first_child($elem) ;$first = $setprop -> first_child ;
some datatypes, like strings, requires dynamic allocated memory
From _types.map,which conatins a set of standard types
int | IVint * | UNDEFINEDunsigned int | UVsigned int | IVlong | IVlong int | IVunsigned long | UVunsigned | UVchar * | PV | | | strdupconst char * | PV | | | strdupconst char ** | UNDEFINEDchar const * | PV | | | strdupunsigned char * | PV | | | strdupconst unsigned char * | PV | | | strdup...
callback maps
Callback maps have the same options a function maps
# dav_hooks_db -> opendav_error *(*)(pool * p,const dav_resource * resource,int ro,dav_db * * pdb) | p, resource, ro=0, <pdb# dav_hooks_lock -> has_locksdav_error *(*)(dav_lockdb * lockdb,const dav_resource * resource,int * locks_present)
Generate the XS files
use FindBin ;use lib ($FindBin::Bin) ;require ParseSource ;require WrapXS ;Apache::DAV::WrapXS->run;
...and run...
C:\perl\msrc\davint>perl xsbuilder\xs_generate.plWill use mod_dav in C:\perl\msrc\cvs\mod_davWill use Apache in c:\programme\apache group\apacheParse xsbuilder\maps/_types.map...Parse xsbuilder\maps/dav_type.map...mkdir xswriting...xs//typemapParse xsbuilder\maps/dav_functions.map...WARNING: Cannot map type int(*)(void * ,const char * ,const char * ) for function ap_table_doWARNING: Cannot map type dav_buffer * for function dav_buffer_appendWARNING: Cannot map type dav_buffer * for function dav_buffer_initWARNING: Cannot map type dav_buffer * for function dav_buffer_placeWARNING: Cannot map type dav_buffer * for function dav_buffer_place_memWARNING: Cannot map type dav_buffer * for function dav_check_bufsizeWARNING: Cannot map return type int * for function dav_collect_liveprop_urisWARNING: Cannot map type dav_resource * * for function dav_ensure_resource_writableWARNING: Cannot map type dav_buffer * for function dav_lock_get_activelockWARNING: Cannot map type dav_buffer * for function dav_set_bufsizeWARNING: Cannot map type int * for function dav_xml2textstruct array_header...Parse xsbuilder\maps/dav_structure.map...elt_size...nelts...nalloc...elts...struct dav_buffer...struct dav_datum...dptr...dsize...struct dav_dyn_context...
Makefile.PL
We need create a top level Makefile.PL
use ExtUtils::MakeMaker ();my $apdir = '/path/to/apache';my $davdir = '/path/to/moddav';%MMARGS = ('INC' => "-I\"$davdir\" -I\"$apdir/include\" -I\"$apdir/os/unix\" -I\"$dir/xs\" -I\"$dir/xsinclude\"",) ;open FH, ">xs/mmargs.pl" or die "Cannot open xs/mmargs.pl ($!)" ;print FH Data::Dumper -> Dump ([\%MMARGS], ['MMARGS']) ;close FH ;ExtUtils::MakeMaker::WriteMakefile('NAME' => 'Apache::DAV','VERSION' => '0.13',%MMARGS,);
Makefile.PL's for all class are generated automaticly
C:\perl\msrc\davint>perl Makefile.PLWill use Apache in c:\programme\apache group\apacheWill use mod_dav in C:\perl\msrc\cvs\mod_davChecking if your kit is complete...Looks goodWriting Makefile for Apache::ArrayWriting Makefile for Apache::DAV::DatumWriting Makefile for Apache::DAV::DynContextWriting Makefile for Apache::DAV::DynHooksWriting Makefile for Apache::DAV::DynModuleWriting Makefile for Apache::DAV::DynProviderWriting Makefile for Apache::DAV::ErrorWriting Makefile for Apache::DAV::HooksDbWriting Makefile for Apache::DAV::HooksLivepropWriting Makefile for Apache::DAV::HooksLocksWriting Makefile for Apache::DAV::HooksRepositoryWriting Makefile for Apache::DAV::IfHeaderWriting Makefile for Apache::DAV::LockWriting Makefile for Apache::DAV::LockDBWriting Makefile for Apache::DAV::LockTokenListWriting Makefile for Apache::DAV::LockupResultWriting Makefile for Apache::DAV::PropCtxWriting Makefile for Apache::DAV::PropsResultWriting Makefile for Apache::DAV::ResourceWriting Makefile for Apache::DAV::ResponseWriting Makefile for Apache::DAV::StateListWriting Makefile for Apache::DAV::TextWriting Makefile for Apache::DAV::TextHeaderWriting Makefile for Apache::DAV::WalkerCtxWriting Makefile for Apache::DAV::XMLAttrWriting Makefile for Apache::DAV::XMLDocWriting Makefile for Apache::DAV::XMLElemWriting Makefile for Apache::DAVWriting Makefile for Apache::TableEntryWriting Makefile for ApacheWriting Makefile for WrapXSWriting Makefile for Apache::DAV
and now compile...
How does it go on...
Generating documentation
XSBuilder already extracts source comments for functions and structures. It also parses doxygen comments, which are used in Apache 2.0. Lyle Brooks has started on automaticly createing POD files from this information.
Improving callbacks
Callbacks are the main area that needs improvement.