home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / CBuilder.pm < prev    next >
Encoding:
Perl POD Document  |  2004-01-12  |  7.0 KB  |  264 lines

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