Add libwx-perl
[pkg-perl] / deb-src / libwx-perl / libwx-perl-0.96 / build / Wx / build / Options.pm
1 package Wx::build::Options;
2
3 use strict;
4
5 =head1 NAME
6
7 Wx::build::Options - retrieve wxWidgets/wxPerl build options
8
9 =head1 METHODS
10
11 =cut
12
13 use Getopt::Long;
14 Getopt::Long::Configure( 'pass_through' );
15
16 my $help         = 0;
17 my $mksymlinks   = 0;
18 my $extra_libs   = '';
19 my $extra_cflags = '';
20 my $alien_key    = '';
21 my %subdirs      = ();
22 my %wx           = ();
23 my $options;
24
25 sub _wx_version {
26     my( $o, $v ) = @_;
27
28     $v =~ m/(\d+)\.(\d+)(?:\.(\d+))?/
29       or die 'Invalid version specification: ', $v, "\n";
30
31     if( defined $3 ) {
32         $wx{version} = [ $1 + ( $2 + $3 / 1000 ) / 1000,
33                          $1 + ( $2 + ( $3 + 1 ) / 1000 ) / 1000 ];
34     } else {
35         $wx{version} = [ $1 + $2 / 1000,
36                          $1 + ( $2 + 1 ) / 1000 ];
37     }
38 }
39
40 sub _load_options {
41   return if $options;
42
43   $options = do 'Wx/build/Opt.pm';
44   die "Unable to load options: $@" unless $options;
45
46   ( $extra_cflags, $extra_libs, $alien_key )
47     = @{$options}{qw(extra_cflags extra_libs alien_key)};
48
49   require Alien::wxWidgets;
50   Alien::wxWidgets->load( key => $alien_key );
51 }
52
53 my $parsed = 0;
54 my @argv;
55
56 sub _parse_options {
57   return if $parsed;
58
59   $parsed = 1;
60
61   my $result = GetOptions( 'help'           => \$help,
62                            'mksymlinks'     => \$mksymlinks,
63                            'extra-libs=s'   => \$extra_libs,
64                            'extra-cflags=s' => \$extra_cflags,
65                            # for Alien::wxWidgets
66                            'wx-debug!'      => \($wx{debug}),
67                            'wx-unicode!'    => \($wx{unicode}),
68                            'wx-mslu!'       => \($wx{mslu}),
69                            'wx-version=s'   => \&_wx_version,
70                            'wx-toolkit=s'   => \($wx{toolkit}),
71                            '<>'             => \&_process_options,
72                          );
73
74   @ARGV = @argv; @argv = ();
75
76   if( !$result || $help ) {
77     print <<HELP;
78 Usage: perl Makefile.PL [options]
79   --enable/disable-foo where foo is one of: dnd filesys grid help
80                        html mdi print xrc stc docview calendar datetime 
81   --help               you are reading it
82   --mksymlinks         create a symlink tree
83   --extra-libs=libs    specify extra linking flags
84   --extra-cflags=flags specify extra compilation flags
85
86   --[no-]wx-debug      [Non-] debugging wxWidgets
87   --[no-]wx-unicode    [Non-] Unicode wxWidgets
88   --[no-]wx-mslu       [Non-] MSLU wxWidgets (Windows only)
89   --wx-version=2.6[.1] 
90   --wx-toolkit=msw|gtk|gtk2|motif|mac|wce|...
91 HELP
92
93     exit !$result;
94   }
95
96   if( $wx{toolkit} && $wx{toolkit} eq 'wce' ) {
97     $wx{compiler_kind} = 'evc';
98   }
99
100   if( Alien::wxWidgets->can( 'load' ) ) {
101       Alien::wxWidgets->load( map  { $_ => $wx{$_} }
102                               grep { defined $wx{$_} }
103                                    keys %wx );
104       $alien_key = Alien::wxWidgets->key;
105   }
106 }
107
108 sub _process_options {
109   my $i = shift;
110
111   unless( $i =~ m/^-/ ) {
112     push @argv, $i;
113     return;
114   }
115
116   if( $i =~ m/^--(enable|disable)-(\w+)$/ ) {
117     $subdirs{$2} = ( $1 eq 'enable' ? 1 : 0 );
118   } else {
119     die "invalid option $i";
120   }
121 }
122
123 =head2 get_makemaker_options
124
125   my %mm_options = Wx::build::Options->get_makemaker_options;
126
127 Returns options meaningful at wxPerl building time.
128
129   my %options = ( mksymlinks   => 0,
130                   extra_libs   => '',
131                   extra_cflags => '',
132                   subdirs      => { stc => 1,
133                                     xrc => 0 } )
134
135 =cut
136
137 sub get_makemaker_options {
138   my $ref = shift;
139   my $from = shift || '';
140
141   if( $from eq 'saved' ) {
142     _load_options();
143   } else {
144     _parse_options();
145   }
146
147   return ( mksymlinks   => $mksymlinks,
148            extra_libs   => $extra_libs,
149            extra_cflags => $extra_cflags,
150            subdirs      => \%subdirs );
151 }
152
153 =head2 write_config_file
154
155   my $ok = Wx::build::Options->write_config_file( '/path/to/file' );
156
157 Writes a machine-readable representation of command-line options given to
158 top-level Makefile.PL
159
160 =cut
161
162 sub write_config_file {
163   my $class = shift;
164   my $file = shift;
165
166   require Data::Dumper;
167   my $str = Data::Dumper->Dump( [ { extra_libs   => $extra_libs,
168                                     extra_cflags => $extra_cflags,
169                                     alien_key    => $alien_key,
170                                   } ] );
171
172   Wx::build::Utils::write_string( $file, $str );
173 }
174
175 1;
176
177 # local variables:
178 # mode: cperl
179 # end: