xref: /openbsd-src/gnu/usr.bin/perl/dist/ExtUtils-CBuilder/lib/ExtUtils/CBuilder.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1898184e3Ssthenpackage ExtUtils::CBuilder;
29f11ffb7Safresh1
3898184e3Ssthenuse File::Spec ();
4898184e3Ssthenuse File::Path ();
5898184e3Ssthenuse File::Basename ();
6898184e3Ssthenuse Perl::OSType qw/os_type/;
7898184e3Ssthen
8b8851fccSafresh1use warnings;
9b8851fccSafresh1use strict;
10*3d61058aSafresh1our $VERSION = '0.280240'; # VERSION
119f11ffb7Safresh1our @ISA;
12898184e3Ssthen
13898184e3Ssthen# We only use this once - don't waste a symbol table entry on it.
14898184e3Ssthen# More importantly, don't make it an inheritable method.
15898184e3Ssthenmy $load = sub {
16898184e3Ssthen  my $mod = shift;
17898184e3Ssthen  eval "use $mod";
18898184e3Ssthen  die $@ if $@;
19898184e3Ssthen  @ISA = ($mod);
20898184e3Ssthen};
21898184e3Ssthen
22898184e3Ssthen{
23898184e3Ssthen  my @package = split /::/, __PACKAGE__;
24898184e3Ssthen
25898184e3Ssthen  my $ostype = os_type();
26898184e3Ssthen
27898184e3Ssthen  if (grep {-e File::Spec->catfile($_, @package, 'Platform', $^O) . '.pm'} @INC) {
28898184e3Ssthen      $load->(__PACKAGE__ . "::Platform::$^O");
29898184e3Ssthen
30b8851fccSafresh1  } elsif ( $ostype &&
31b8851fccSafresh1            grep {-e File::Spec->catfile($_, @package, 'Platform', $ostype) . '.pm'} @INC) {
32898184e3Ssthen      $load->(__PACKAGE__ . "::Platform::$ostype");
33898184e3Ssthen
34898184e3Ssthen  } else {
35898184e3Ssthen      $load->(__PACKAGE__ . "::Base");
36898184e3Ssthen  }
37898184e3Ssthen}
38898184e3Ssthen
39898184e3Ssthen1;
40898184e3Ssthen__END__
41898184e3Ssthen
42898184e3Ssthen=head1 NAME
43898184e3Ssthen
44898184e3SsthenExtUtils::CBuilder - Compile and link C code for Perl modules
45898184e3Ssthen
46898184e3Ssthen=head1 SYNOPSIS
47898184e3Ssthen
48898184e3Ssthen  use ExtUtils::CBuilder;
49898184e3Ssthen
50898184e3Ssthen  my $b = ExtUtils::CBuilder->new(%options);
51898184e3Ssthen  $obj_file = $b->compile(source => 'MyModule.c');
52898184e3Ssthen  $lib_file = $b->link(objects => $obj_file);
53898184e3Ssthen
54898184e3Ssthen=head1 DESCRIPTION
55898184e3Ssthen
56898184e3SsthenThis module can build the C portions of Perl modules by invoking the
57898184e3Ssthenappropriate compilers and linkers in a cross-platform manner.  It was
58898184e3Ssthenmotivated by the C<Module::Build> project, but may be useful for other
59898184e3Ssthenpurposes as well.  However, it is I<not> intended as a general
60898184e3Ssthencross-platform interface to all your C building needs.  That would
61898184e3Ssthenhave been a much more ambitious goal!
62898184e3Ssthen
63898184e3Ssthen=head1 METHODS
64898184e3Ssthen
65898184e3Ssthen=over 4
66898184e3Ssthen
67898184e3Ssthen=item new
68898184e3Ssthen
69898184e3SsthenReturns a new C<ExtUtils::CBuilder> object.  A C<config> parameter
70898184e3Ssthenlets you override C<Config.pm> settings for all operations performed
71898184e3Ssthenby the object, as in the following example:
72898184e3Ssthen
73898184e3Ssthen  # Use a different compiler than Config.pm says
74898184e3Ssthen  my $b = ExtUtils::CBuilder->new( config =>
75898184e3Ssthen                                   { ld => 'gcc' } );
76898184e3Ssthen
77898184e3SsthenA C<quiet> parameter tells C<CBuilder> to not print its C<system()>
78898184e3Ssthencommands before executing them:
79898184e3Ssthen
80898184e3Ssthen  # Be quieter than normal
81898184e3Ssthen  my $b = ExtUtils::CBuilder->new( quiet => 1 );
82898184e3Ssthen
83898184e3Ssthen=item have_compiler
84898184e3Ssthen
85898184e3SsthenReturns true if the current system has a working C compiler and
86898184e3Ssthenlinker, false otherwise.  To determine this, we actually compile and
87898184e3Ssthenlink a sample C library.  The sample will be compiled in the system
88898184e3Ssthentempdir or, if that fails for some reason, in the current directory.
89898184e3Ssthen
90898184e3Ssthen=item have_cplusplus
91898184e3Ssthen
92898184e3SsthenJust like have_compiler but for C++ instead of C.
93898184e3Ssthen
94898184e3Ssthen=item compile
95898184e3Ssthen
96898184e3SsthenCompiles a C source file and produces an object file.  The name of the
97898184e3Ssthenobject file is returned.  The source file is specified in a C<source>
98898184e3Ssthenparameter, which is required; the other parameters listed below are
99898184e3Ssthenoptional.
100898184e3Ssthen
101898184e3Ssthen=over 4
102898184e3Ssthen
103898184e3Ssthen=item C<object_file>
104898184e3Ssthen
105898184e3SsthenSpecifies the name of the output file to create.  Otherwise the
106898184e3SsthenC<object_file()> method will be consulted, passing it the name of the
107898184e3SsthenC<source> file.
108898184e3Ssthen
109898184e3Ssthen=item C<include_dirs>
110898184e3Ssthen
111898184e3SsthenSpecifies any additional directories in which to search for header
112898184e3Ssthenfiles.  May be given as a string indicating a single directory, or as
113898184e3Ssthena list reference indicating multiple directories.
114898184e3Ssthen
115898184e3Ssthen=item C<extra_compiler_flags>
116898184e3Ssthen
117898184e3SsthenSpecifies any additional arguments to pass to the compiler.  Should be
118898184e3Ssthengiven as a list reference containing the arguments individually, or if
119898184e3Ssthenthis is not possible, as a string containing all the arguments
120898184e3Ssthentogether.
121898184e3Ssthen
122898184e3Ssthen=item C<C++>
123898184e3Ssthen
124898184e3SsthenSpecifies that the source file is a C++ source file and sets appropriate
125898184e3Ssthencompiler flags
126898184e3Ssthen
127898184e3Ssthen=back
128898184e3Ssthen
129898184e3SsthenThe operation of this method is also affected by the
130898184e3SsthenC<archlibexp>, C<cccdlflags>, C<ccflags>, C<optimize>, and C<cc>
131898184e3Ssthenentries in C<Config.pm>.
132898184e3Ssthen
133898184e3Ssthen=item link
134898184e3Ssthen
135898184e3SsthenInvokes the linker to produce a library file from object files.  In
136898184e3Ssthenscalar context, the name of the library file is returned.  In list
137898184e3Ssthencontext, the library file and any temporary files created are
138898184e3Ssthenreturned.  A required C<objects> parameter contains the name of the
139898184e3Ssthenobject files to process, either in a string (for one object file) or
140898184e3Ssthenlist reference (for one or more files).  The following parameters are
141898184e3Ssthenoptional:
142898184e3Ssthen
143898184e3Ssthen
144898184e3Ssthen=over 4
145898184e3Ssthen
146898184e3Ssthen=item lib_file
147898184e3Ssthen
148898184e3SsthenSpecifies the name of the output library file to create.  Otherwise
149898184e3Ssthenthe C<lib_file()> method will be consulted, passing it the name of
150898184e3Ssthenthe first entry in C<objects>.
151898184e3Ssthen
152898184e3Ssthen=item module_name
153898184e3Ssthen
154898184e3SsthenSpecifies the name of the Perl module that will be created by linking.
155898184e3SsthenOn platforms that need to do prelinking (Win32, OS/2, etc.) this is a
156898184e3Ssthenrequired parameter.
157898184e3Ssthen
158898184e3Ssthen=item extra_linker_flags
159898184e3Ssthen
160898184e3SsthenAny additional flags you wish to pass to the linker.
161898184e3Ssthen
162898184e3Ssthen=back
163898184e3Ssthen
164898184e3SsthenOn platforms where C<need_prelink()> returns true, C<prelink()>
165898184e3Ssthenwill be called automatically.
166898184e3Ssthen
167898184e3SsthenThe operation of this method is also affected by the C<lddlflags>,
168898184e3SsthenC<shrpenv>, and C<ld> entries in C<Config.pm>.
169898184e3Ssthen
170898184e3Ssthen=item link_executable
171898184e3Ssthen
172898184e3SsthenInvokes the linker to produce an executable file from object files.  In
173898184e3Ssthenscalar context, the name of the executable file is returned.  In list
174898184e3Ssthencontext, the executable file and any temporary files created are
175898184e3Ssthenreturned.  A required C<objects> parameter contains the name of the
176898184e3Ssthenobject files to process, either in a string (for one object file) or
177898184e3Ssthenlist reference (for one or more files).  The optional parameters are
178898184e3Ssthenthe same as C<link> with exception for
179898184e3Ssthen
180898184e3Ssthen
181898184e3Ssthen=over 4
182898184e3Ssthen
183898184e3Ssthen=item exe_file
184898184e3Ssthen
185898184e3SsthenSpecifies the name of the output executable file to create.  Otherwise
186898184e3Ssthenthe C<exe_file()> method will be consulted, passing it the name of the
187898184e3Ssthenfirst entry in C<objects>.
188898184e3Ssthen
189898184e3Ssthen=back
190898184e3Ssthen
191898184e3Ssthen=item object_file
192898184e3Ssthen
193898184e3Ssthen my $object_file = $b->object_file($source_file);
194898184e3Ssthen
195898184e3SsthenConverts the name of a C source file to the most natural name of an
196898184e3Ssthenoutput object file to create from it.  For instance, on Unix the
197898184e3Ssthensource file F<foo.c> would result in the object file F<foo.o>.
198898184e3Ssthen
199898184e3Ssthen=item lib_file
200898184e3Ssthen
201898184e3Ssthen my $lib_file = $b->lib_file($object_file);
202898184e3Ssthen
203898184e3SsthenConverts the name of an object file to the most natural name of a
204898184e3Ssthenoutput library file to create from it.  For instance, on Mac OS X the
205898184e3Ssthenobject file F<foo.o> would result in the library file F<foo.bundle>.
206898184e3Ssthen
207898184e3Ssthen=item exe_file
208898184e3Ssthen
209898184e3Ssthen my $exe_file = $b->exe_file($object_file);
210898184e3Ssthen
211898184e3SsthenConverts the name of an object file to the most natural name of an
212898184e3Ssthenexecutable file to create from it.  For instance, on Mac OS X the
213898184e3Ssthenobject file F<foo.o> would result in the executable file F<foo>, and
214898184e3Ssthenon Windows it would result in F<foo.exe>.
215898184e3Ssthen
216898184e3Ssthen
217898184e3Ssthen=item prelink
218898184e3Ssthen
219898184e3SsthenOn certain platforms like Win32, OS/2, VMS, and AIX, it is necessary
220898184e3Ssthento perform some actions before invoking the linker.  The
221898184e3SsthenC<ExtUtils::Mksymlists> module does this, writing files used by the
222898184e3Ssthenlinker during the creation of shared libraries for dynamic extensions.
223898184e3SsthenThe names of any files written will be returned as a list.
224898184e3Ssthen
225898184e3SsthenSeveral parameters correspond to C<ExtUtils::Mksymlists::Mksymlists()>
226898184e3Ssthenoptions, as follows:
227898184e3Ssthen
228898184e3Ssthen    Mksymlists()   prelink()          type
229898184e3Ssthen   -------------|-------------------|-------------------
230898184e3Ssthen    NAME        |  dl_name          | string (required)
231898184e3Ssthen    DLBASE      |  dl_base          | string
232898184e3Ssthen    FILE        |  dl_file          | string
233898184e3Ssthen    DL_VARS     |  dl_vars          | array reference
234898184e3Ssthen    DL_FUNCS    |  dl_funcs         | hash reference
235898184e3Ssthen    FUNCLIST    |  dl_func_list     | array reference
236898184e3Ssthen    IMPORTS     |  dl_imports       | hash reference
237898184e3Ssthen    VERSION     |  dl_version       | string
238898184e3Ssthen
239898184e3SsthenPlease see the documentation for C<ExtUtils::Mksymlists> for the
240898184e3Ssthendetails of what these parameters do.
241898184e3Ssthen
242898184e3Ssthen=item need_prelink
243898184e3Ssthen
244898184e3SsthenReturns true on platforms where C<prelink()> should be called
245898184e3Ssthenduring linking, and false otherwise.
246898184e3Ssthen
247898184e3Ssthen=item extra_link_args_after_prelink
248898184e3Ssthen
249898184e3SsthenReturns list of extra arguments to give to the link command; the arguments
250898184e3Ssthenare the same as for prelink(), with addition of array reference to the
251898184e3Ssthenresults of prelink(); this reference is indexed by key C<prelink_res>.
252898184e3Ssthen
253898184e3Ssthen=back
254898184e3Ssthen
255898184e3Ssthen=head1 TO DO
256898184e3Ssthen
257898184e3SsthenCurrently this has only been tested on Unix and doesn't contain any of
258898184e3Ssthenthe Windows-specific code from the C<Module::Build> project.  I'll do
259898184e3Ssthenthat next.
260898184e3Ssthen
261898184e3Ssthen=head1 HISTORY
262898184e3Ssthen
263898184e3SsthenThis module is an outgrowth of the C<Module::Build> project, to which
264898184e3Ssthenthere have been many contributors.  Notably, Randy W. Sims submitted
265898184e3Ssthenlots of code to support 3 compilers on Windows and helped with various
266898184e3Ssthenother platform-specific issues.  Ilya Zakharevich has contributed
267898184e3Ssthenfixes for OS/2; John E. Malmberg and Peter Prymmer have done likewise
268898184e3Ssthenfor VMS.
269898184e3Ssthen
270898184e3Ssthen=head1 SUPPORT
271898184e3Ssthen
272898184e3SsthenExtUtils::CBuilder is maintained as part of the Perl 5 core.  Please
273898184e3Ssthensubmit any bug reports via the F<perlbug> tool included with Perl 5.
274898184e3SsthenBug reports will be included in the Perl 5 ticket system at
27556d68f1eSafresh1L<https://rt.perl.org>.
276898184e3Ssthen
27756d68f1eSafresh1The Perl 5 source code is available at L<https://perl5.git.perl.org/perl.git>
278898184e3Ssthenand ExtUtils-CBuilder may be found in the F<dist/ExtUtils-CBuilder> directory
279898184e3Ssthenof the repository.
280898184e3Ssthen
281898184e3Ssthen=head1 AUTHOR
282898184e3Ssthen
283898184e3SsthenKen Williams, kwilliams@cpan.org
284898184e3Ssthen
285898184e3SsthenAdditional contributions by The Perl 5 Porters.
286898184e3Ssthen
287898184e3Ssthen=head1 COPYRIGHT
288898184e3Ssthen
289898184e3SsthenCopyright (c) 2003-2005 Ken Williams.  All rights reserved.
290898184e3Ssthen
291898184e3SsthenThis library is free software; you can redistribute it and/or
292898184e3Ssthenmodify it under the same terms as Perl itself.
293898184e3Ssthen
294898184e3Ssthen=head1 SEE ALSO
295898184e3Ssthen
296898184e3Ssthenperl(1), Module::Build(3)
297898184e3Ssthen
298898184e3Ssthen=cut
299