| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | package MooseX::Compile; | ||
| 4 | 2 | ▶ | use base qw(MooseX::Compile::Base); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 2 | ▶ | use constant DEBUG => MooseX::Compile::Base::DEBUG(); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use constant default_compiler_class => "MooseX::Compile::Compiler"; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 2 | ▶ | use Devel::INC::Sorted qw(inc_add_floating); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | |||
| 14 | BEGIN { | ||
| 15 | inc_add_floating(sub { | ||
| 16 | my ( $self, $file ) = @_; | ||
| 17 | |||
| 18 | if ( DEBUG ) { | ||
| 19 | foreach my $pkg qw( | ||
| 20 | Moose | ||
| 21 | Moose::Meta::Class | ||
| 22 | |||
| 23 | Class::MOP | ||
| 24 | Class::MOP::Class | ||
| 25 | |||
| 26 | metaclass | ||
| 27 | |||
| 28 | Moose::Util::TypeConstraints | ||
| 29 | Moose::Meta::TypeConstraint | ||
| 30 | Moose::Meta::TypeCoercion | ||
| 31 | ) { | ||
| 32 | ( my $pkg_file = "$pkg.pm" ) =~ s{::}{/}g; | ||
| 33 | require Carp and Carp::carp("loading $pkg") if $file eq $pkg_file; | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | if ( $ENV{MX_COMPILE_CLEAN} ) { | ||
| 38 | foreach my $dir ( grep { not ref } @INC ) { | ||
| 39 | my $full = "$dir/$file"; | ||
| 40 | |||
| 41 | my $pmc = "${full}c"; | ||
| 42 | ( my $mopc = $full ) =~ s/\.pm$/.mopc/; | ||
| 43 | |||
| 44 | if ( -e $pmc && -e $mopc ) { | ||
| 45 | warn "removing compiled class for file '$file'\n" if DEBUG; | ||
| 46 | unlink $pmc or die "Can't remove pmc file (unlink($pmc)): $!"; | ||
| 47 | unlink $mopc or die "Can't remove cached metaclass (unlink($mopc)): $!"; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | return; | ||
| 53 | }); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub import { | ||
| 57 | my ($self, @args) = @_; | ||
| 58 | |||
| 59 | my ( $class, $file ) = caller(); | ||
| 60 | |||
| 61 | if ( $MooseX::Compile::Bootstrap::known_pmc_files{$file} ) { | ||
| 62 | return $self->import_from_pmc( $class, $file, @args ); | ||
| 63 | } else { | ||
| 64 | warn "class '$class' requires compilation\n" if DEBUG; | ||
| 65 | |||
| 66 | require Check::UnitCheck; | ||
| 67 | Check::UnitCheck::unitcheckify(sub { | ||
| 68 | warn "compilation unit of class '$class' calling UNITCHECK\n" if DEBUG; | ||
| 69 | $self->unit_check( $class, $file, @args ); | ||
| 70 | }); | ||
| 71 | |||
| 72 | require Moose; | ||
| 73 | shift; unshift @_, "Moose"; | ||
| 74 | goto \&Moose::import; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | sub import_from_pmc { | ||
| 79 | |||
| 80 | } | ||
| 81 | |||
| 82 | sub unit_check { | ||
| 83 | my ( $self, $class, $file, @args ) = @_; | ||
| 84 | |||
| 85 | $self->compile_from_import( | ||
| 86 | class => $class, | ||
| 87 | file => $file, | ||
| 88 | @args, | ||
| 89 | ); | ||
| 90 | } | ||
| 91 | |||
| 92 | sub compile_from_import { | ||
| 93 | my ( $self, %args ) = @_; | ||
| 94 | |||
| 95 | if ( $ENV{MX_COMPILE_IMPLICIT_ANCESTORS} ) { | ||
| 96 | warn "implicitly compiling all ancestors of class '$args{class}'\n" if DEBUG; | ||
| 97 | $self->compile_ancestors( %args ); | ||
| 98 | } | ||
| 99 | |||
| 100 | $self->compile_class( %args ); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub compile_ancestors { | ||
| 104 | my ( $self, %args ) = @_; | ||
| 105 | |||
| 106 | my $class = $args{class}; | ||
| 107 | my $files = $args{files} || {}; | ||
| 108 | |||
| 109 | foreach my $superclass ( reverse $class->meta->linearized_isa ) { | ||
| 110 | next if $superclass eq $class; | ||
| 111 | warn "compiling '$class' superclass '$superclass'\n" if DEBUG; | ||
| 112 | $self->compile_class( %args, class => $superclass, file => $files->{$superclass} ); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | sub compile_class { | ||
| 117 | my ( $self, %args ) = @_; | ||
| 118 | |||
| 119 | my $compiler = $self->create_compiler( %args ); | ||
| 120 | |||
| 121 | $compiler->compile_class( %args ); | ||
| 122 | } | ||
| 123 | |||
| 124 | sub compiler_class { | ||
| 125 | my ( $self, %args ) = @_; | ||
| 126 | |||
| 127 | $args{compiler_class} || $self->default_compiler_class; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub create_compiler { | ||
| 131 | my ( $self, @args ) = @_; | ||
| 132 | |||
| 133 | my $compiler_class = $self->compiler_class(@args); | ||
| 134 | |||
| 135 | $self->load_classes($compiler_class); | ||
| 136 | |||
| 137 | $compiler_class->new( @args ); | ||
| 138 | } | ||
| 139 | |||
| 140 | __PACKAGE__; | ||
| 141 | |||
| 142 | __END__ | ||