1# $OpenBSD: Exec.pm,v 1.5 2018/08/26 19:09:55 naddy Exp $ 2 3# Copyright (c) 2007-2010 Steven Mestdagh <steven@openbsd.org> 4# Copyright (c) 2012 Marc Espie <espie@openbsd.org> 5# 6# Permission to use, copy, modify, and distribute this software for any 7# purpose with or without fee is hereby granted, provided that the above 8# copyright notice and this permission notice appear in all copies. 9# 10# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 18use strict; 19use warnings; 20use feature qw(say switch state); 21 22package LT::Exec; 23use LT::Trace; 24use LT::Util; 25 26my $dry = 0; 27my $verbose = 0; 28my $performed = 0; 29 30sub performed 31{ 32 return $performed; 33} 34 35sub dry_run 36{ 37 $dry = 1; 38} 39 40sub verbose_run 41{ 42 $verbose = 1; 43} 44 45sub silent_run 46{ 47 $verbose = 0; 48} 49 50sub new 51{ 52 my $class = shift; 53 bless {}, $class; 54} 55 56sub chdir 57{ 58 my ($self, $dir) = @_; 59 my $class = ref($self) || $self; 60 bless {dir => $dir}, $class; 61} 62 63sub compile 64{ 65 my ($self, @l) = @_; 66 $self->command("compile", @l); 67} 68 69sub execute 70{ 71 my ($self, @l) = @_; 72 $self->command("execute", @l); 73} 74 75sub install 76{ 77 my ($self, @l) = @_; 78 $self->command("install", @l); 79} 80 81sub link 82{ 83 my ($self, @l) = @_; 84 $self->command("link", @l); 85} 86 87sub command_run 88{ 89 my ($self, @l) = @_; 90 91 if ($self->{dir}) { 92 tprint {"cd $self->{dir} && "}; 93 } 94 tsay { "@l" }; 95 my $pid = fork(); 96 if (!defined $pid) { 97 die "Couldn't fork while running @l\n"; 98 } 99 if ($pid == 0) { 100 if ($self->{dir}) { 101 CORE::chdir($self->{dir}) or die "Can't chdir to $self->{dir}\n"; 102 } 103 exec(@l); 104 die "Exec failed @l\n"; 105 } else { 106 my $kid = waitpid($pid, 0); 107 if ($? != 0) { 108 shortdie "Error while executing @l\n"; 109 } 110 } 111} 112 113sub shell 114{ 115 my ($self, @cmds) = @_; 116 # create an object "on the run" 117 if (!ref($self)) { 118 $self = $self->new; 119 } 120 for my $c (@cmds) { 121 say $c if $verbose || $dry; 122 if (!$dry) { 123 $self->command_run($c); 124 } 125 } 126 $performed++; 127} 128 129sub command 130{ 131 my ($self, $mode, @l) = @_; 132 # create an object "on the run" 133 if (!ref($self)) { 134 $self = $self->new; 135 } 136 if ($mode eq "compile"){ 137 say "@l" if $verbose || $dry; 138 } else { 139 say "libtool: $mode: @l" if $verbose || $dry; 140 } 141 if (!$dry) { 142 $self->command_run(@l); 143 } 144 $performed++; 145} 146 1471; 148