Integrate version.pm-0.77 into bleadperl
[p5sagit/p5-mst-13.2.git] / lib / ExtUtils / CBuilder.pm
CommitLineData
6b09c160 1package ExtUtils::CBuilder;
2
3use File::Spec ();
4use File::Path ();
5use File::Basename ();
6
7use vars qw($VERSION @ISA);
673223b5 8$VERSION = '0.25';
6b09c160 9$VERSION = eval $VERSION;
10
11# Okay, this is the brute-force method of finding out what kind of
12# platform we're on. I don't know of a systematic way. These values
13# came from the latest (bleadperl) perlport.pod.
14
15my %OSTYPES = qw(
16 aix Unix
17 bsdos Unix
18 dgux Unix
19 dynixptx Unix
20 freebsd Unix
21 linux Unix
22 hpux Unix
23 irix Unix
24 darwin Unix
25 machten Unix
26 next Unix
27 openbsd Unix
28 netbsd Unix
29 dec_osf Unix
30 svr4 Unix
31 svr5 Unix
32 sco_sv Unix
33 unicos Unix
34 unicosmk Unix
35 solaris Unix
36 sunos Unix
37 cygwin Unix
38 os2 Unix
92760223 39 gnu Unix
40 gnukfreebsd Unix
df00ff3b 41 haiku Unix
6b09c160 42
43 dos Windows
44 MSWin32 Windows
45
46 os390 EBCDIC
47 os400 EBCDIC
48 posix-bc EBCDIC
49 vmesa EBCDIC
50
51 MacOS MacOS
52 VMS VMS
53 VOS VOS
54 riscos RiscOS
55 amigaos Amiga
56 mpeix MPEiX
57 );
58
59# We only use this once - don't waste a symbol table entry on it.
60# More importantly, don't make it an inheritable method.
61my $load = sub {
62 my $mod = shift;
63 eval "use $mod";
64 die $@ if $@;
65 @ISA = ($mod);
66};
67
68{
69 my @package = split /::/, __PACKAGE__;
70
71 if (grep {-e File::Spec->catfile($_, @package, 'Platform', $^O) . '.pm'} @INC) {
72 $load->(__PACKAGE__ . "::Platform::$^O");
73
74 } elsif (exists $OSTYPES{$^O} and
75 grep {-e File::Spec->catfile($_, @package, 'Platform', $OSTYPES{$^O}) . '.pm'} @INC) {
76 $load->(__PACKAGE__ . "::Platform::$OSTYPES{$^O}");
77
78 } else {
79 $load->(__PACKAGE__ . "::Base");
80 }
81}
82
83sub os_type { $OSTYPES{$^O} }
84
851;
86__END__
87
88=head1 NAME
89
90ExtUtils::CBuilder - Compile and link C code for Perl modules
91
92=head1 SYNOPSIS
93
94 use ExtUtils::CBuilder;
95
96 my $b = ExtUtils::CBuilder->new(%options);
97 $obj_file = $b->compile(source => 'MyModule.c');
98 $lib_file = $b->link(objects => $obj_file);
99
100=head1 DESCRIPTION
101
102This module can build the C portions of Perl modules by invoking the
103appropriate compilers and linkers in a cross-platform manner. It was
104motivated by the C<Module::Build> project, but may be useful for other
105purposes as well. However, it is I<not> intended as a general
106cross-platform interface to all your C building needs. That would
107have been a much more ambitious goal!
108
109=head1 METHODS
110
111=over 4
112
113=item new
114
115Returns a new C<ExtUtils::CBuilder> object. A C<config> parameter
116lets you override C<Config.pm> settings for all operations performed
117by the object, as in the following example:
118
119 # Use a different compiler than Config.pm says
120 my $b = ExtUtils::CBuilder->new( config =>
121 { ld => 'gcc' } );
122
ea2e6518 123A C<quiet> parameter tells C<CBuilder> to not print its C<system()>
124commands before executing them:
125
126 # Be quieter than normal
127 my $b = ExtUtils::CBuilder->new( quiet => 1 );
128
6b09c160 129=item have_compiler
130
131Returns true if the current system has a working C compiler and
132linker, false otherwise. To determine this, we actually compile and
133link a sample C library.
134
135=item compile
136
137Compiles a C source file and produces an object file. The name of the
138object file is returned. The source file is specified in a C<source>
139parameter, which is required; the other parameters listed below are
140optional.
141
142=over 4
143
144=item C<object_file>
145
146Specifies the name of the output file to create. Otherwise the
147C<object_file()> method will be consulted, passing it the name of the
148C<source> file.
149
150=item C<include_dirs>
151
152Specifies any additional directories in which to search for header
153files. May be given as a string indicating a single directory, or as
154a list reference indicating multiple directories.
155
156=item C<extra_compiler_flags>
157
158Specifies any additional arguments to pass to the compiler. Should be
159given as a list reference containing the arguments individually, or if
160this is not possible, as a string containing all the arguments
161together.
162
163=back
164
165The operation of this method is also affected by the
345dbb93 166C<archlibexp>, C<cccdlflags>, C<ccflags>, C<optimize>, and C<cc>
6b09c160 167entries in C<Config.pm>.
168
169=item link
170
171Invokes the linker to produce a library file from object files. In
172scalar context, the name of the library file is returned. In list
173context, the library file and any temporary files created are
174returned. A required C<objects> parameter contains the name of the
175object files to process, either in a string (for one object file) or
176list reference (for one or more files). The following parameters are
177optional:
178
179
180=over 4
181
182=item lib_file
183
184Specifies the name of the output library file to create. Otherwise
185the C<lib_file()> method will be consulted, passing it the name of
186the first entry in C<objects>.
187
188=item module_name
189
190Specifies the name of the Perl module that will be created by linking.
191On platforms that need to do prelinking (Win32, OS/2, etc.) this is a
192required parameter.
193
194=item extra_linker_flags
195
196Any additional flags you wish to pass to the linker.
197
198=back
199
200On platforms where C<need_prelink()> returns true, C<prelink()>
201will be called automatically.
202
203The operation of this method is also affected by the C<lddlflags>,
204C<shrpenv>, and C<ld> entries in C<Config.pm>.
205
206=item link_executable
207
208Invokes the linker to produce an executable file from object files. In
209scalar context, the name of the executable file is returned. In list
210context, the executable file and any temporary files created are
211returned. A required C<objects> parameter contains the name of the
212object files to process, either in a string (for one object file) or
213list reference (for one or more files). The optional parameters are
214the same as C<link> with exception for
215
216
217=over 4
218
219=item exe_file
220
221Specifies the name of the output executable file to create. Otherwise
222the C<exe_file()> method will be consulted, passing it the name of the
223first entry in C<objects>.
224
225=back
226
227=item object_file
228
229 my $object_file = $b->object_file($source_file);
230
231Converts the name of a C source file to the most natural name of an
232output object file to create from it. For instance, on Unix the
233source file F<foo.c> would result in the object file F<foo.o>.
234
235=item lib_file
236
237 my $lib_file = $b->lib_file($object_file);
238
239Converts the name of an object file to the most natural name of a
240output library file to create from it. For instance, on Mac OS X the
241object file F<foo.o> would result in the library file F<foo.bundle>.
242
243=item exe_file
244
245 my $exe_file = $b->exe_file($object_file);
246
247Converts the name of an object file to the most natural name of an
248executable file to create from it. For instance, on Mac OS X the
249object file F<foo.o> would result in the executable file F<foo>, and
250on Windows it would result in F<foo.exe>.
251
252
253=item prelink
254
255On certain platforms like Win32, OS/2, VMS, and AIX, it is necessary
256to perform some actions before invoking the linker. The
257C<ExtUtils::Mksymlists> module does this, writing files used by the
258linker during the creation of shared libraries for dynamic extensions.
259The names of any files written will be returned as a list.
260
261Several parameters correspond to C<ExtUtils::Mksymlists::Mksymlists()>
262options, as follows:
263
264 Mksymlists() prelink() type
265 -------------|-------------------|-------------------
266 NAME | dl_name | string (required)
267 DLBASE | dl_base | string
268 FILE | dl_file | string
269 DL_VARS | dl_vars | array reference
270 DL_FUNCS | dl_funcs | hash reference
271 FUNCLIST | dl_func_list | array reference
272 IMPORTS | dl_imports | hash reference
ce87d91c 273 VERSION | dl_version | string
6b09c160 274
275Please see the documentation for C<ExtUtils::Mksymlists> for the
276details of what these parameters do.
277
278=item need_prelink
279
280Returns true on platforms where C<prelink()> should be called
281during linking, and false otherwise.
282
ce87d91c 283=item extra_link_args_after_prelink
284
285Returns list of extra arguments to give to the link command; the arguments
286are the same as for prelink(), with addition of array reference to the
287results of prelink(); this reference is indexed by key C<prelink_res>.
288
6b09c160 289=back
290
291=head1 TO DO
292
293Currently this has only been tested on Unix and doesn't contain any of
294the Windows-specific code from the C<Module::Build> project. I'll do
295that next.
296
297=head1 HISTORY
298
299This module is an outgrowth of the C<Module::Build> project, to which
300there have been many contributors. Notably, Randy W. Sims submitted
301lots of code to support 3 compilers on Windows and helped with various
ce87d91c 302other platform-specific issues. Ilya Zakharevich has contributed
303fixes for OS/2; John E. Malmberg and Peter Prymmer have done likewise
304for VMS.
6b09c160 305
306=head1 AUTHOR
307
308Ken Williams, kwilliams@cpan.org
309
310=head1 COPYRIGHT
311
312Copyright (c) 2003-2005 Ken Williams. All rights reserved.
313
314This library is free software; you can redistribute it and/or
315modify it under the same terms as Perl itself.
316
317=head1 SEE ALSO
318
319perl(1), Module::Build(3)
320
321=cut