| 1 | package App::PPK; | ||
| 2 | |||
| 3 | # Copyright (c) 2018, cPanel, L.L.C. | ||
| 4 | # All rights reserved. | ||
| 5 | # http://cpanel.net/ | ||
| 6 | # | ||
| 7 | # This is free software; you can redistribute it and/or modify it under the same | ||
| 8 | # terms as Perl itself. See the LICENSE file for further details. | ||
| 9 | |||
| 10 | use strict; | ||
| 11 | use warnings; | ||
| 12 | |||
| 13 | use Getopt::Long qw(:config no_ignore_case no_auto_abbrev); | ||
| 14 | use Build::PPK (); | ||
| 15 | |||
| 16 | use Carp ('confess'); | ||
| 17 | |||
| 18 | sub usage { | ||
| 19 | my ($message) = @_; | ||
| 20 | |||
| 21 | print STDERR "$message\n" if $message; | ||
| 22 | print STDERR "usage: $0 main.pl [options] [-o output]\n"; | ||
| 23 | print STDERR " $0 main.pl [options] -c\n"; | ||
| 24 | |||
| 25 | exit 1; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub read_depfile { | ||
| 29 | my ( $class, $file ) = @_; | ||
| 30 | my %modules; | ||
| 31 | my @dists; | ||
| 32 | |||
| 33 | my $fh; | ||
| 34 | |||
| 35 | if ( $file eq '-' ) { | ||
| 36 | $fh = \*STDIN; | ||
| 37 | } | ||
| 38 | else { | ||
| 39 | open( $fh, '<', $file ) or confess("Unable to open dependency file $file for reading: $!"); | ||
| 40 | } | ||
| 41 | |||
| 42 | while ( my $line = readline($fh) ) { | ||
| 43 | chomp $line; | ||
| 44 | |||
| 45 | next if $line =~ /^\s*(#|$)/; | ||
| 46 | |||
| 47 | my ( $command, $args ) = split /\s+/, $line, 2; | ||
| 48 | |||
| 49 | if ( $command eq 'module' ) { | ||
| 50 | my ( $module, $file ) = split /\s+/, $args, 2; | ||
| 51 | $modules{$module} = $file; | ||
| 52 | } | ||
| 53 | elsif ( $command eq 'dist' ) { | ||
| 54 | push @dists, $args; | ||
| 55 | } | ||
| 56 | else { | ||
| 57 | confess("Syntax error: Unknown specifier '$command'"); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | close $fh unless $file eq '-'; | ||
| 62 | |||
| 63 | return { | ||
| 64 | 'modules' => \%modules, | ||
| 65 | 'dists' => \@dists | ||
| 66 | }; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub parse_opts { | ||
| 70 | my ($class) = @_; | ||
| 71 | my @modules; | ||
| 72 | |||
| 73 | my $opts = { | ||
| 74 | 'output' => 'ppk.out', | ||
| 75 | 'modules' => {}, | ||
| 76 | 'dists' => [] | ||
| 77 | }; | ||
| 78 | |||
| 79 | GetOptions( | ||
| 80 | 'check|c' => \$opts->{'check'}, | ||
| 81 | 'output|o=s' => \$opts->{'output'}, | ||
| 82 | 'dists|d=s@{,}' => \$opts->{'dists'}, | ||
| 83 | 'deps-from=s' => \$opts->{'depfile'}, | ||
| 84 | 'header|H=s' => \$opts->{'header'}, | ||
| 85 | 'desc|D=s' => \$opts->{'desc'}, | ||
| 86 | 'modules|m=s@{,}' => \@modules | ||
| 87 | ) or usage(); | ||
| 88 | |||
| 89 | usage('No main entry point script specified') unless $opts->{'main'} = $ARGV[0]; | ||
| 90 | usage('Cannot check a script while compiling') if $opts->{'check'} && $opts->{'output'}; | ||
| 91 | usage('Desc can only be passed when a header is provided') if $opts->{'desc'} && !$opts->{'header'}; | ||
| 92 | |||
| 93 | # | ||
| 94 | # If --deps-from is specified, then open the file passed and parse each line | ||
| 95 | # in a special format described in the POD for ppk(1). | ||
| 96 | # | ||
| 97 | if ( $opts->{'depfile'} ) { | ||
| 98 | my $deps = $class->read_depfile( $opts->{'depfile'} ); | ||
| 99 | my $modules = $deps->{'modules'}; | ||
| 100 | my $dists = $deps->{'dists'}; | ||
| 101 | |||
| 102 | push @{ $opts->{'dists'} }, @{$dists}; | ||
| 103 | %{ $opts->{'modules'} } = %{$modules}; | ||
| 104 | } | ||
| 105 | |||
| 106 | # | ||
| 107 | # Any module dependencies specified from a --deps-from file can of course | ||
| 108 | # be overridden here. | ||
| 109 | # | ||
| 110 | foreach my $dependency (@modules) { | ||
| 111 | my ( $file, $module ) = split /=/, $dependency, 2; | ||
| 112 | |||
| 113 | usage("No module name was specified for module file $file") unless $module; | ||
| 114 | |||
| 115 | $opts->{'modules'}->{$module} = $file; | ||
| 116 | } | ||
| 117 | |||
| 118 | return $opts; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub run { | ||
| 122 | my ($class) = @_; | ||
| 123 | my $opts = $class->parse_opts; | ||
| 124 | |||
| 125 | Build::PPK->build( $opts->{'main'}, $opts ); | ||
| 126 | |||
| 127 | return 0; | ||
| 128 | } | ||
| 129 | |||
| 130 | 1; |