1use strict;
2use warnings;
3
4use lib "inc";
5use File::Spec::Functions qw(catdir catfile);
6use Config;
7use My::Utility qw(check_config_script check_prebuilt_binaries check_prereqs_libs check_prereqs_tools $source_packs
8                   check_perl_buildlibs);
9
10print "Welcome to Alien::SDL module installation\n";
11print "-----------------------------------------\n";
12print "checking operating system... $^O\n";
13$| = 1;
14print "checking for $My::Utility::cc... ";
15if( !check_prereqs_tools($My::Utility::cc) ) {
16  print "no\n";
17  for(qw(gcc cc lc)) {
18    next if $_ eq \$My::Utility::cc;
19    print "checking for $_... ";
20    if( check_prereqs_tools($_) ) {
21      $My::Utility::cc = $_;
22      print "yes\n";
23      last;
24    }
25    else {
26      print "no\n";
27    }
28  }
29}
30else {
31  print "yes\n";
32}
33print "checking build system type... $Config{archname}\n";
34
35#### we need the platform-specific module
36my %platforms =(
37       # Unix = default, thus not listing all UNIX like systems
38       MSWin32 => 'Windows',
39);
40my $package = 'My::Builder::' . ($platforms{$^O} || 'Unix');
41print "checking platform specific module... using '$package'\n";
42eval "require $package" or die "Require '$package' failed: $@\n";
43
44my $sdl_config;
45
46#### Stadard Module::Builder stuff
47my $build = $package->new(
48  module_name          => 'Alien::SDL',
49  all_from             => 'lib/Alien/SDL.pm',
50  dist_abstract        => 'Get, Build and Use SDL libraries',
51  dist_author          => 'Kartik Thakore <KTHAKORE@cpan.org>',
52  license              => 'perl',
53  requires             => {
54    'File::Spec'       => '0',
55    'File::Temp'       => '0',
56    'File::ShareDir'   => '0',
57    'File::Which'      => '0',
58    'ExtUtils::CBuilder' => '0',
59    'Capture::Tiny'    => '0',
60    'perl'             => '5.008000',
61  },
62  build_requires       => {  #need to have for running: ./Build (install|test)
63    'File::Spec'       => '0',
64    'File::Temp'       => '0',
65    'File::ShareDir'   => '0',
66    'ExtUtils::CBuilder' => '0',
67    'File::Path'       => '2.08',
68    'File::Fetch'      => '0.24',
69    'File::Find'       => '0',
70    'File::Which'      => '0',
71    'Digest::SHA'      => '0',
72    'Archive::Extract' => '0',
73    'Archive::Tar'     => '0',
74    'Archive::Zip'     => '0',
75    'Module::Build'    => '0.36',
76    'Text::Patch'      => '1.4',
77  },
78  configure_requires   => {  #need to have for running: perl Build.PL
79    'File::Spec'       => '0',
80    'File::Path'       => '2.08',
81    'File::Fetch'      => '0.24',
82    'File::Find'       => '0',
83    'File::Which'      => '0',
84    'Digest::SHA'      => '0',
85    'Archive::Extract' => '0',
86    'Module::Build'    => '0.36',
87    'Text::Patch'      => '1.4',
88    'File::ShareDir'   => '0',
89    'Capture::Tiny'    => '0',
90  },
91  meta_merge => {
92    resources  => {
93      bugtracker => 'http://github.com/PerlGameDev/SDL/issues?labels=Alien-SDL',
94      repository => 'http://github.com/PerlGameDev/Alien-SDL'
95    }
96  },
97  get_options => { 'with-sdl-config' => { qw(type :s  store) => \$sdl_config } },
98  dynamic_config => 1,
99  create_readme => 1,
100  share_dir => 'sharedir',
101  # sharedir is used for storing compiled/prebuilt binaries of SDL lib + related libraries
102  # avoid using 'share' name as M::B doe not handle well paths like /xx/yy/share/zz/ww/share/xx
103);
104
105my $choice;
106my %have_libs = ();
107my %perl_libs = ();
108
109if (defined $sdl_config) {
110  # handle --with-sdl-config (without params)
111  $sdl_config = 'sdl-config' if $sdl_config eq '';
112  # Don't prompt; just use specified location:
113  $choice = check_config_script($sdl_config)
114      or warn "###ERROR### Unable to use config script $sdl_config\n";
115}
116else {
117  $| = 1;
118
119  if( $My::Utility::cc eq 'cl' && $^O eq 'MSWin32' ) {
120    print "checking INCLUDE and LIB... ";
121    if( !$ENV{INCLUDE} || !$ENV{LIB} ) {
122      my @set = `\@vcvars32 & set`;
123      chomp @set;
124      my %set  = map /(\w+)=(.+)/, @set;
125      for( keys %set ) {
126        if( /^INCLUDE|LIB$/ ) {
127          $ENV{$_} = $set{$_};
128        }
129      }
130      print(($ENV{INCLUDE} && $ENV{LIB})
131        ? "yes, via vcvars32\n"
132        : "no\n");
133    }
134    else {
135      print "yes\n";
136    }
137  }
138  #### check what options we have for our platform
139  my $rv;
140  my @candidates = ();
141
142  print "checking SDL_INST_DIR env var... ";
143  if(defined($ENV{SDL_INST_DIR})) {
144    if (-d $ENV{SDL_INST_DIR}) {
145      print "yes, $ENV{SDL_INST_DIR}\n";
146      my @sdlinst = File::Spec->splitdir($ENV{SDL_INST_DIR});
147      if($rv = check_config_script(File::Spec->catdir(@sdlinst, 'bin', 'sdl-config'))) {
148        push @candidates, $rv;
149      }
150      elsif($rv = check_config_script(File::Spec->catdir(@sdlinst, 'sdl-config'))) {
151        push @candidates, $rv;
152      }
153    }
154    else {
155      print "directory '$ENV{SDL_INST_DIR}' does not exist";
156    }
157  }
158  print "no\n";
159
160  # sdl-config script
161  push @candidates, $rv if $rv = check_config_script("sdl-config");
162
163  if( $build->can_build_binaries_from_sources || scalar(@candidates) ) {
164    for(qw(pthread SDL
165           z jpeg tiff png SDL_image
166           ogg vorbis vorbisfile SDL_mixer
167           freetype SDL_ttf
168           SDL_gfx
169           pangoft2 pango gobject gmodule glib fontconfig expat SDL_Pango)) {
170      $have_libs{$_} = check_prereqs_libs($_);
171    }
172  }
173
174  $perl_libs{pthread} = check_perl_buildlibs('pthread') if $have_libs{pthread} && $^O eq 'openbsd';
175
176  # prebuilt binaries (windows only)
177  push @candidates, @{$rv} if $rv = check_prebuilt_binaries($build->os_type);
178
179  if($build->can_build_binaries_from_sources) {
180    for my $p ( @$source_packs ) {
181      $rv = { title => $p->{title}, members => [], buildtype => 'build_from_sources' };
182      for my $m (@{ $p->{members} }) {
183        next if $m->{pack} !~ /^SDL/ && $have_libs{ $m->{pack} }[0];
184        my $good = 1;
185        $good   &= $have_libs{$_} && $have_libs{$_}[0] ? 1 : 0 for @{ $m->{prereqs}->{libs} };
186        if( $good ) {
187          $have_libs{ $m->{pack} }[0] ||= 1;
188          push @{ $rv->{members} }, $m;
189          $rv->{title} .= "$m->{pack}(v$m->{version}) ";
190        }
191      }
192      push @candidates, $rv if scalar( @{ $rv->{members} } );
193    }
194  };
195
196  push @candidates, { title => 'Quit installation', buildtype => '' };
197
198  #### ask user what way to go
199  my $i                     = 1;
200  my $prompt_string         = "\nYou have the following options:\n";
201  my $recommended_candidate = 1;
202  foreach my $c (@candidates) {
203    $recommended_candidate = $i if $c->{buildtype} eq 'build_from_sources';
204
205    if( $c->{buildtype} eq 'use_config_script' ) {
206      $c->{title} .= "\n    ";
207      for(qw(SDL SDL_image SDL_mixer SDL_ttf SDL_gfx SDL_Pango)) {
208        $c->{title} .= "$_(v$have_libs{$_}->[0]) " if $have_libs{$_}[0];
209      }
210    }
211
212    $prompt_string .= "[" . $i++ . "] " . $c->{title} . "\n";
213  }
214} # end else search and prompt for build method
215
216#### store build params into 'notes'
217if($choice) {
218  print "Using \l$choice->{title}\n";
219  $build->notes('build_params', $choice);
220  $build->notes('env_include', $ENV{INCLUDE}) if $ENV{INCLUDE};
221  $build->notes('env_lib',     $ENV{LIB})     if $ENV{LIB};
222  $build->notes('have_libs',   \%have_libs);
223  $build->notes('perl_libs',   \%perl_libs);
224  $build->create_build_script();
225
226  #### clean build_done stamp; force rebuild when running 'Build'
227  $build->clean_build_done_marker;
228}
229else {
230  $build->notes('build_params', undef); # just to be sure
231  exit(0); # we want no reports from CPAN Testers in this case
232}
233