]> git.imager.perl.org - imager.git/blob - lib/Imager/ExtUtils.pm
clarify that FORMATGUESS is only used when writing to a file.
[imager.git] / lib / Imager / ExtUtils.pm
1 package Imager::ExtUtils;
2 use strict;
3
4 use vars qw($VERSION);
5
6 $VERSION = "1.000";
7
8 =head1 NAME
9
10 Imager::ExtUtils - functions handy in writing Imager extensions
11
12 =head1 SYNOPSIS
13
14   # make Imager easier to use with Inline
15   # perldoc Imager::Inline
16   use Inline with => 'Imager';
17
18 =head1 DESCRIPTION
19
20 =over
21
22 =item base_dir
23
24 Returns the base directory where Imager is installed.
25
26 =cut
27
28 # figure out where Imager is installed
29 sub base_dir {
30   for my $dir (@INC) {
31     if (-e "$dir/Imager.pm") {
32       return $dir;
33     }
34   }
35
36   die "Cannot locate an installed Imager!";
37 }
38
39 =item inline_config
40
41 Implements Imager's Inline::C C<with> hook.
42
43 =cut
44
45 sub inline_config {
46   my ($class) = @_;
47   my $base = base_dir();
48
49   return
50     {
51      INC => $class->includes,
52      TYPEMAPS => $class->typemap,
53      AUTO_INCLUDE => <<'CODE',
54 #include "imext.h"
55 #include "imperl.h"
56 DEFINE_IMAGER_CALLBACKS;
57 CODE
58      BOOT => 'PERL_INITIALIZE_IMAGER_CALLBACKS;',
59      FILTERS => \&_inline_filter,
60     };
61 }
62
63 my @inline_replace =
64   qw(
65    Imager::ImgRaw
66    Imager::Color::Float
67    Imager::Color
68    Imager::IO
69   );
70
71 my %inline_replace =
72   map { (my $tmp = $_) =~ s/::/__/g; $_ => $tmp } @inline_replace;
73
74 my $inline_replace_re = "\\b(" . join('|', @inline_replace) . ")\\b";
75
76 sub _inline_filter {
77   my $code = shift;
78
79   $code =~ s/$inline_replace_re/$inline_replace{$1}/g;
80
81   $code;
82 }
83
84 =item includes
85
86 Returns -I options suitable for use with ExtUtils::MakeMaker's INC
87 option.
88
89 =cut
90
91 sub includes {
92   my $class = shift;
93   my $base = $class->base_dir();
94
95   "-I" . $base . '/Imager/include',
96 }
97
98 =item typemap
99
100 Returns the full path to Imager's installed typemap.
101
102 =cut
103
104 sub typemap {
105   my $class = shift;
106   my $base = $class->base_dir();
107
108   $base . '/Imager/typemap';
109 }
110
111 1;
112
113 __END__
114
115 =back
116
117 =head1 AUTHOR
118
119 Tony Cook <tony@imager.perl.org>
120
121 =head1 REVISION
122
123 $Revision$
124
125 =head1 SEE ALSO
126
127 Imager, Imager::API, Imager::Inline, Imager::APIRef.
128
129 =cut