/usr/lib/perl5/Glib/CodeGen.pm is in libglib-perl 3:1.304-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 | package Glib::CodeGen;
use strict;
use warnings;
use Carp;
use IO::File;
our $VERSION = '1.304';
# type handlers should look like this:
# sub gen_foo_stuff {
# my ($typemacro, $classname, $base, $package) = @_;
# ...
# }
#
# DO NOT manipulate this data structure directly. Use add_type_handler().
my %type_handler = (
GEnum => \&gen_enum_stuff,
GFlags => \&gen_flags_stuff,
GBoxed => \&gen_boxed_stuff,
GObject => \&gen_object_stuff,
# we treat GInterfaces as GObjects for these purposes.
GInterface => \&gen_object_stuff,
GError => \&gen_error_domain_stuff,
);
=head1 NAME
Glib::CodeGen - code generation utilities for Glib-based bindings.
=head1 SYNOPSIS
# usually in Makefile.PL
use Glib::CodeGen;
# most common, use all defaults
Glib::CodeGen->parse_maps ('myprefix');
Glib::CodeGen->write_boot;
# more exotic, change everything
Glib::CodeGen->parse_maps ('foo',
input => 'foo.maps',
header => 'foo-autogen.h',
typemap => 'foo.typemap',
register => 'register-foo.xsh');
Glib::CodeGen->write_boot (filename => 'bootfoo.xsh',
glob => 'Foo*.xs',
ignore => '^(Foo|Foo::Bar)$');
# add a custom type handler (rarely necessary)
Glib::CodeGen->add_type_handler (FooType => \&gen_foo_stuff);
# (see the section EXTENDING TYPE SUPPORT for more info.)
=head1 DESCRIPTION
This module packages some of the boilerplate code needed for performing code
generation typically used by perl bindings for gobject-based libraries, using
the Glib module as a base.
The default output filenames are in the subdirectory 'build', which usually
will be present if you are using ExtUtils::Depends (as most Glib-based
extensions probably should).
=head2 METHODS
=over
=item Glib::CodeGen->write_boot;
=item Glib::CodeGen->write_boot (KEY => VAL, ...)
Many GObject-based libraries to be bound to perl will be too large to put in
a single XS file; however, a single PM file typically only bootstraps one
XS file's code. C<write_boot> generates an XSH file to be included from
the BOOT section of that one bootstrapped module, calling the boot code for
all the other XS files in the project.
Options are passed to the function in a set of key/val pairs, and all options
may default.
filename the name of the output file to be created.
the default is 'build/boot.xsh'.
glob a glob pattern that specifies the names of
the xs files to scan for MODULE lines.
the default is 'xs/*.xs'.
xs_files use this to supply an explicit list of file
names (as an array reference) to use instead
of a glob pattern. the default is to use
the glob pattern.
ignore regular expression matching any and all
module names which should be ignored, i.e.
NOT included in the list of symbols to boot.
this parameter is extremely important for
avoiding infinite loops at startup; see the
discussion for an explanation and rationale.
the default is '^[^:]+$', or, any name that
contains no colons, i.e., any toplevel
package name.
This function performs a glob (using perl's builtin glob operator) on the
pattern specified by the 'glob' option to retrieve a list of file names.
It then scans each file in that list for lines matching the pattern
"^MODULE" -- that is, the MODULE directive in an XS file. The module
name is pulled out and matched against the regular expression specified
by the ignore parameter. If this module is not to be ignored, we next
check to see if the name has been seen. If not, the name will be converted
to a boot symbol (basically, s/:/_/ and prepend "boot_") and this symbol
will be added to a call to GPERL_CALL_BOOT in the generated file; it is then
marked as seen so we don't call it again.
What is this all about, you ask? In order to bind an XSub to perl, the C
function must be registered with the interpreter. This is the function of the
"boot" code, which is typically called in the bootstrapping process. However,
when multiple XS files are used with only one PM file, some other mechanism
must call the boot code from each XS file before any of the function therein
will be available.
A typical setup for a multiple-XS, single-PM module will be to call the
various bits of boot code from the BOOT: section of the toplevel module's
XS file.
To use Gtk2 as an example, when you do 'use Gtk2', Gtk2.pm calls bootstrap
on Gtk2, which calls the C function boot_Gtk2. This function calls the
boot symbols for all the other xs files in the module. The distinction
is that the toplevel module, Gtk2, has no colons in its name.
C<xsubpp> generates the boot function's name by replacing the
colons in the MODULE name with underscores and prepending "boot_".
We need to be careful not to include the boot code for the bootstrapped module,
(say Toplevel, or Gtk2, or whatever) because the bootstrap code in
Toplevel.pm will call boot_Toplevel when loaded, and boot_Toplevel
should actually include the file we are creating here.
The default value for the ignore parameter ignores any name not containing
colons, because it is assumed that this will be a toplevel module, and any
other packages/modules it boots will be I<below> this namespace, i.e., they
will contain colons. This assumption holds true for Gtk2 and Gnome2, but
obviously fails for something like Gnome2::Canvas. To boot that module
properly, you must use a regular expression such as "^Gnome2::Canvas$".
Note that you can, of course, match more than just one name, e.g.
"^(Foo|Foo::Bar)$", if you wanted to have Foo::Bar be included in the same
dynamically loaded object but only be booted when absolutely necessary.
(If you get that to work, more power to you.)
Also, since this code scans for ^MODULE, you must comment the MODULE section
out with leading # marks if you want to hide it from C<write_boot>.
=cut
sub write_boot {
my $class = shift;
my %opts = (
ignore => '^[^:]+$', # ignore package with no colons in it
filename => 'build/boot.xsh',
'glob' => 'xs/*.xs',
@_,
);
my $ignore = $opts{ignore};
my $file = IO::File->new (">$opts{filename}")
or carp "Cannot write $opts{filename}: $!";
print $file "\n\n/* This file is automatically generated, any changes made here will be lost! */\n\n";
my %boot=();
my @xs_files = 'ARRAY' eq ref $opts{xs_files}
? @{ $opts{xs_files} }
: glob $opts{'glob'};
foreach my $xsfile (@xs_files) {
my $in = IO::File->new ($xsfile)
or die "can't open $xsfile: $!\n";
while (<$in>) {
next unless m/^MODULE\s*=\s*(\S+)/;
#warn "found $1 in $&\n";
my $package = $1;
next if $package =~ m/$ignore/;
$package =~ s/:/_/g;
my $sym = "boot_$package";
print $file "GPERL_CALL_BOOT ($sym);\n"
unless $boot{$sym};
$boot{$sym}++;
}
close $in;
}
close $file;
}
=item Glib::CodeGen->parse_maps (PREFIX, [KEY => VAL, ...])
Convention within Glib/Gtk2 and friends is to use preprocessor macros in the
style of SvMyType and newSVMyType to get values in and out of perl, and to
use those same macros from both hand-written code as well as the typemaps.
However, if you have a lot of types in your library (such as the nearly 200
types in Gtk+ 2.x), then writing those macros becomes incredibly tedious,
especially so when you factor in all of the variants and such.
So, this function can turn a flat file containing terse descriptions of the
types into a header containing all the cast macros, a typemap file using them,
and an XSH file containing the proper code to register each of those types
(to be included by your module's BOOT code).
The I<PREFIX> is mandatory, and is used in some of the resulting filenames,
You can also override the defaults by providing key=>val pairs:
input input file name. default is 'maps'. if this
key's value is an array reference, all the
filenames in the array will be scanned.
header name of the header file to create, default is
build/$prefix-autogen.h
typemap name of the typemap file to create, default is
build/$prefix.typemap
register name of the xsh file to contain all of the
type registrations, default is build/register.xsh
the maps file is a table of type descriptions, one per line, with fields
separated by whitespace. the fields should be:
TYPE macro e.g., GTK_TYPE_WIDGET
class name e.g. GtkWidget, name of the C type
base type one of GObject, GBoxed, GEnum, GFlags.
To support other base types, see
EXTENDING TYPE SUPPORT for info on
on how to add a custom type handler.
package name of the perl package to which this
class name should be mapped, e.g.
Gtk2::Widget
As a special case, you can also use this same format to register error
domains; in this case two of the four columns take on slightly different
meanings:
domain macro e.g., GDK_PIXBUF_ERROR
enum type macro e.g., GDK_TYPE_PIXBUF_ERROR
base type GError
package name of the Perl package to which this
class name should be mapped, e.g.,
Gtk2::Gdk::Pixbuf::Error.
=back
=cut
# when we parse the maps, type handlers will call several helper functions
# to add header lines, typemaps, and boot lines. we store those here.
# these are private. see the add_foo functions, below.
# there
my (@header, @typemap, @input, @output, @boot);
sub parse_maps {
my $class = shift;
my $prefix = shift;
my %props = (
input => 'maps',
header => "build/$prefix-autogen.h",
typemap => "build/$prefix.typemap",
register => 'build/register.xsh',
@_,
);
local *IN;
local *OUT;
my %seen = ();
@header = ();
@typemap = ();
@input = ();
@output = ();
@boot = ();
my @files = 'ARRAY' eq ref $props{input}
? @{ $props{input} }
: $props{input};
foreach my $file (@files) {
open IN, "< $file"
or die "can't open $file for reading: $!\n";
my $n = 0;
while (<IN>) {
chomp;
s/#.*//;
my ($typemacro, $classname, $base, $package) = split;
next unless defined $package;
if (exists $type_handler{$base}) {
$type_handler{$base}->($typemacro, $classname,
$base, $package);
$seen{$base}++;
} else {
warn "unhandled type $typemacro $classname $base $package\n";
$seen{unhandled}++;
}
$n++;
}
close IN;
#print "Loaded $n type definitions from $file\n";
}
# create output
# the header
open OUT, "> $props{header}"
or die "can't open $props{header} for writing: $!\n";
print OUT join("\n",
"/* This file is automatically generated. Any changes made here will be lost. */\n",
"/* This header defines simple perlapi-ish macros for creating SV wrappers",
" * and extracting the GPerl value from SV wrappers. These macros are used",
" * by the autogenerated typemaps, and are defined here so that you can use",
" * the same logic anywhere in your code (e.g., if you handle the argument",
" * stack by hand instead of using the typemap). */\n",
@header,
);
close OUT;
# the typemaps
open OUT, "> $props{typemap}"
or die "can't open $props{typemap} for writing: $!\n";
print OUT join("\n",
"# This file is automatically generated. Any changes made here will be lost.",
"# This typemap is a trivial one-to-one mapping of each type, to avoid the",
"# need for bizarre typedefs and other tricks often used with XS.",
"TYPEMAP\n", @typemap,
"\nINPUT\n", @input,
"\nOUTPUT\n", @output);
close OUT;
# the boot code
open OUT, "> $props{register}"
or die "can't open $props{register} for writing: $!\n";
print OUT join("\n",
"/* This file is automatically generated. Any changes made here will be lost. */",
@boot,
);
print OUT "\n";
close OUT;
# mini report to stdout
# foreach (sort keys %seen) {
# printf " %3d %s\n", $seen{$_}, $_;
# }
# fin.
}
=head1 EXTENDING TYPE SUPPORT
C<parse_maps> uses the base type entry in each maps record to decide how to
generate output for that type. In the base module, type support is included
for the base types provided by Glib. It is easy to add support for your own
types, by merely adding a type handler. This type handler will call utility
functions to add typemaps, BOOT lines, and header lines.
=over
=item Glib::CodeGen->add_type_handler ($base_type => $handler)
=over
=item $base_type (string) C name of the base type to handle.
=item $handler (subroutine) Callback used to handle this type.
=back
Use I<$handler> to generate output for records whose base type is
I<$base_type>. I<$base_type> is the C type name as found in the third
column of a maps file entry.
I<$handler> will be called with the (possibly preprocessed) contents of the
current maps file record, and should call the C<add_typemap>, C<add_register>,
and C<add_header> functions to set up the necessary C/XS glue for that type.
For example:
Glib::CodeGen->add_type_handler (CoolThing => sub {
my ($typemacro, $classname, $base, $package) = @_;
# $typemacro is the C type macro, like COOL_TYPE_THING.
# $classname is the actual C type name, like CoolFooThing.
# $base is the C name of the base type. If CoolFooThing
# isa CoolThing, $base will be CoolThing. This
# parameter is useful when using the same type handler
# for multiple base types.
# $package is the package name that corresponds to
# $classname, as specified in the maps file.
...
});
=cut
sub add_type_handler {
my (undef, $root_type, $handler) = @_;
$type_handler{$root_type} = $handler;
}
=item add_typemap $type, $typemap [, $input, $output]
Add a typemap entry for C<$type>, named C<$typemap>. If I<$input> and/or
I<$output> are defined, their text will be used as the C<INPUT> and/or
C<OUTPUT> typemap implementations (respectively) for I<$typemap>. Note that in
general, you'll use C<T_GPERL_GENERIC_WRAPPER> or some other existing typemap
for I<$typemap>, so I<$input> and I<$output> are very rarely used.
Example:
# map $classname pointers and all their variants to the generic
# wrapper typemap.
add_typemap "$classname *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "const $classname *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "$classname\_ornull *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "const $classname\_ornull *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "$classname\_own *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "$classname\_copy *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "$classname\_own_ornull *", "T_GPERL_GENERIC_WRAPPER";
# custom code for an int-like enum:
add_typemap $class => T_FOO,
"\$var = foo_unwrap (\$arg);", # input
"\$arg = foo_wrap (\$var);"; # output
=cut
sub add_typemap {
my ($type, $typemap, $input, $output) = @_;
push @typemap, "$type\t$typemap" if defined $typemap;
push @input, $input if defined $input;
push @output, $output if defined $output;
}
=item add_register $text
Add I<$text> to the generated C<register.xsh>. This is usually used for
registering types with the bindings, e.g.:
add_register "#ifdef $typemacro\n"
. "gperl_register_object ($typemacro, \"$package\");\n"
. "#endif /* $typemacro */";
=cut
sub add_register { push @boot, shift; }
=item add_header $text
Add I<$text> to the generated C header. You'll put variant typedefs and
wrap/unwrap macros in the header, and will usually want to wrap the
declarations in C<#ifdef $typemacro> for safety.
=cut
sub add_header { push @header, shift; }
#
# generator subs
#
sub gen_enum_stuff {
my ($typemacro, $classname, undef, $package) = @_;
add_header "#ifdef $typemacro
/* GEnum $classname */
# define Sv$classname(sv) (($classname)gperl_convert_enum ($typemacro, sv))
# define newSV$classname(val) (gperl_convert_back_enum ($typemacro, val))
#endif /* $typemacro */
";
add_typemap $classname, "T_GPERL_GENERIC_WRAPPER";
add_register "#ifdef $typemacro
gperl_register_fundamental ($typemacro, \"$package\");
#endif /* $typemacro */"
unless $package eq '-';
}
sub gen_flags_stuff {
my ($typemacro, $classname, undef, $package) = @_;
add_header "#ifdef $typemacro
/* GFlags $classname */
# define Sv$classname(sv) (($classname)gperl_convert_flags ($typemacro, sv))
# define newSV$classname(val) (gperl_convert_back_flags ($typemacro, val))
#endif /* $typemacro */
";
add_typemap $classname, "T_GPERL_GENERIC_WRAPPER";
add_register "#ifdef $typemacro
gperl_register_fundamental ($typemacro, \"$package\");
#endif /* $typemacro */"
unless $package eq '-';
}
sub gen_boxed_stuff {
my ($typemacro, $classname, undef, $package) = @_;
add_header "#ifdef $typemacro
/* GBoxed $classname */
typedef $classname $classname\_ornull;
# define Sv$classname(sv) (($classname *) gperl_get_boxed_check ((sv), $typemacro))
# define Sv$classname\_ornull(sv) (gperl_sv_is_defined (sv) ? Sv$classname (sv) : NULL)
typedef $classname $classname\_own;
typedef $classname $classname\_copy;
typedef $classname $classname\_own_ornull;
# define newSV$classname(val) (gperl_new_boxed ((gpointer) (val), $typemacro, FALSE))
# define newSV$classname\_ornull(val) ((val) ? newSV$classname(val) : &PL_sv_undef)
# define newSV$classname\_own(val) (gperl_new_boxed ((gpointer) (val), $typemacro, TRUE))
# define newSV$classname\_copy(val) (gperl_new_boxed_copy ((gpointer) (val), $typemacro))
# define newSV$classname\_own_ornull(val) ((val) ? newSV$classname\_own(val) : &PL_sv_undef)
#endif /* $typemacro */
";
add_typemap "$classname *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "const $classname *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "$classname\_ornull *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "const $classname\_ornull *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "$classname\_own *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "$classname\_copy *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "$classname\_own_ornull *", "T_GPERL_GENERIC_WRAPPER";
add_register "#ifdef $typemacro
gperl_register_boxed ($typemacro, \"$package\", NULL);
#endif /* $typemacro */"
unless $package eq '-';
}
sub gen_object_stuff {
my ($typemacro, $classname, $root, $package) = @_;
my $get_wrapper = 'gperl_new_object (G_OBJECT (val), FALSE)';
my $header_text = "#ifdef $typemacro
/* $root derivative $classname */
# define Sv$classname(sv) (($classname*)gperl_get_object_check (sv, $typemacro))
# define newSV$classname(val) ($get_wrapper)
typedef $classname $classname\_ornull;
# define Sv$classname\_ornull(sv) (gperl_sv_is_defined (sv) ? Sv$classname(sv) : NULL)
# define newSV$classname\_ornull(val) (((val) == NULL) ? &PL_sv_undef : $get_wrapper)
";
add_typemap "$classname *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "const $classname *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "$classname\_ornull *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "const $classname\_ornull *", "T_GPERL_GENERIC_WRAPPER";
add_register "#ifdef $typemacro
gperl_register_object ($typemacro, \"$package\");
#endif /* $typemacro */";
if ($root eq 'GObject') {
# for GObjects, add a _noinc and a noinc_ornull variant for
# returning GObjects from constructors.
$header_text .= "typedef $classname $classname\_noinc;
#define newSV$classname\_noinc(val) (gperl_new_object (G_OBJECT (val), TRUE))
typedef $classname $classname\_noinc_ornull;
#define newSV$classname\_noinc_ornull(val) ((val) ? newSV$classname\_noinc(val) : &PL_sv_undef)
";
add_typemap "$classname\_noinc *", "T_GPERL_GENERIC_WRAPPER";
add_typemap "$classname\_noinc_ornull *", "T_GPERL_GENERIC_WRAPPER";
}
# close the header ifdef
$header_text .= "#endif /* $typemacro */\n";
add_header $header_text;
}
sub gen_error_domain_stuff {
my ($domain, $enum, undef, $package) = @_;
add_register "#if defined($domain) /* && defined($enum) */
gperl_register_error_domain ($domain, $enum, \"$package\");
#endif /* $domain */
";
}
1;
__END__
=back
=head1 BUGS
GInterfaces are mostly just ignored.
The code is ugly.
=head1 AUTHOR
muppet <scott at asofyet dot org>
=head1 COPYRIGHT
Copyright (C) 2003-2005, 2013 by the gtk2-perl team (see the file AUTHORS for
the full list)
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Library General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for
more details.
You should have received a copy of the GNU Library General Public License
along with this library; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
=cut
|