PageRenderTime 37ms CodeModel.GetById 15ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/inc/My/Builder/Darwin.pm

http://github.com/PerlGameDev/SDL
Perl | 127 lines | 110 code | 15 blank | 2 comment | 17 complexity | e94c5213d64725cdf3fb6b86304b5938 MD5 | raw file
  1#!/usr/bin/env perl
  2package My::Builder::Darwin;
  3
  4use strict;
  5use warnings;
  6use Alien::SDL;
  7use File::Find qw(find);
  8use Data::Dumper;
  9use Cwd;
 10use Config;
 11use File::Copy;
 12use File::Spec;
 13use base 'My::Builder';
 14
 15sub special_build_settings {
 16    my $self = shift;
 17    $self->{c_source}     = [ 'src', 'main.c' ];
 18    $self->{c_sources}    = 'MacOSX';
 19    $self->{install_base} = "SDLPerl.app/Contents/Resources";
 20}
 21
 22sub build_bundle {
 23    my $self = shift;
 24
 25    system "mkdir -p blib/script";
 26    my $Perl = ( $ENV{'FULLPERL'} or $^X or 'perl' );
 27    my $cflags =
 28      `$Perl -MExtUtils::Embed -e ccopts` . ' ' . Alien::SDL->config('cflags');
 29    my $libs =
 30        `$Perl -MExtUtils::Embed -e ldopts` . ' '
 31      . Alien::SDL->config('libs')
 32      . ' -lSDLmain';
 33    my $arch    = '';
 34    my $sdl_lib = '';
 35    $sdl_lib =
 36         Alien::SDL->config('ld_shlib_map')
 37      && Alien::SDL->config('ld_shlib_map')->{SDL}
 38      ? Alien::SDL->config('ld_shlib_map')->{SDL}
 39      : _find_SDL_lib();
 40    $arch = $1 if $sdl_lib && `lipo -info $sdl_lib` =~ m/\s(\w+)s*$/;
 41    $arch = $ENV{SDL_ARCH} if $ENV{SDL_ARCH};
 42
 43    if ($arch) {
 44        $cflags =~ s/\b-arch \w+\s//g;
 45        $libs   =~ s/\b-arch \w+\s//g;
 46        $arch = "-arch $arch";
 47    }
 48
 49    my $cmd =
 50      "gcc $arch -o \"blib/script/SDLPerl\" MacOSX/main.c $cflags $libs";
 51    $cmd =~ s/\s+/ /g;
 52    unless(-e 'blib/script/SDLPerl') {
 53        print STDERR $cmd . "\n";
 54        system($cmd);
 55    }
 56}
 57
 58sub ACTION_test {
 59    my $self = shift;
 60    $self->depends_on('build');
 61    $self->build_bundle() if !( -e 'blib/script/SDLPerl' );
 62    if( $ENV{SDL_RELEASE_TESTING} ) {
 63        if ( $ENV{SDL_PERL_TEST} ) {
 64            $self->Module::Build::ACTION_test;
 65            $ENV{SDL_PERL_TEST} = 0;    #unset it again
 66        }
 67        else {
 68            my $cmd = 'blib/script/SDLPerl Build test';
 69            $ENV{SDL_PERL_TEST} = 1;
 70            system( split ' ', $cmd );
 71            die 'Errors in Testing. Can\'t continue' if $?;
 72        }
 73    }
 74    else {
 75        $self->Module::Build::ACTION_test;
 76    }
 77}
 78
 79sub _find_SDL_lib {
 80    my $inc_lib_candidates = {
 81        '/usr/local/include' => '/usr/local/lib',
 82        '/usr/include'       => '/usr/lib',
 83        '/usr/X11R6/include' => '/usr/X11R6/lib',
 84    };
 85
 86    if ( -e '/usr/lib64' && $Config{'myarchname'} =~ /64/ ) {
 87        $inc_lib_candidates->{'/usr/include'} = '/usr/lib64';
 88    }
 89
 90    if ( exists $ENV{SDL_LIB} && exists $ENV{SDL_INC} ) {
 91        $inc_lib_candidates->{ $ENV{SDL_INC} } = $ENV{SDL_LIB};
 92    }
 93
 94    foreach ( keys %$inc_lib_candidates ) {
 95        my $ld = $inc_lib_candidates->{$_};
 96        next unless -d $ld;
 97        my ($found_lib) = _find_file( $ld,
 98            qr/[\/\\]lib\QSDLmain\E[\-\d\.]*\.(so|dylib|bundle[\d\.]*|a|dll.a)$/
 99        );
100        return $found_lib if $found_lib;
101    }
102
103    return 0;
104}
105
106sub _find_file {
107    my ( $dir, $re ) = @_;
108    my @files;
109    $re ||= qr/.*/;
110    {
111
112#hide warning "Can't opendir(...): Permission denied - fix for http://rt.cpan.org/Public/Bug/Display.html?id=57232
113        no warnings;
114        find(
115            {
116                wanted => sub { push @files, File::Spec->rel2abs($_) if /$re/ },
117                follow => 1,
118                no_chdir    => 1,
119                follow_skip => 2
120            },
121            $dir
122        );
123    };
124    return @files;
125}
126
1271;