openbcm

Git mirror of https://github.com/Broadcom-Network-Switching-Software/OpenBCM
git clone git://git.finwo.net/mirror/broadcom/openbcm
Log | Files | Refs | README

mktool.pl (3734B)


      1 #!/usr/bin/env perl
      2 #
      3 # 
      4 # This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file.
      5 # 
      6 # Copyright 2007-2019 Broadcom Inc. All rights reserved.
      7 #
      8 # mktool.pl
      9 #
     10 
     11 use File::Path;
     12 use File::Find;
     13 use File::Copy;
     14 use Cwd;
     15 
     16 ($prog = $0) =~ s/.*\///;
     17 
     18 SWITCH:
     19 {
     20     $op = shift;
     21 
     22     if ($op eq "-rm") { mktool_rm(@ARGV); last SWITCH; }
     23     if ($op eq "-cp") { mktool_cp(@ARGV); last SWITCH; }
     24     if ($op eq "-md") { mktool_md(@ARGV); last SWITCH; }
     25     if ($op eq "-ln") { mktool_ln(@ARGV); last SWITCH; }
     26     if ($op eq "-foreach") { mktool_foreach(@ARGV); last SWITCH; }
     27     if ($op eq "-dep") { mktool_makedep(@ARGV); last SWITCH; }
     28     if ($op eq "-echo") { mktool_echo(@ARGV); last SWITCH; }
     29     if ($op eq "-beep") { mktool_beep(@ARGV); last SWITCH; }
     30     die("$prog: unknown option '$op'\n");
     31 }
     32 
     33 exit 0;
     34 
     35 
     36 
     37 
     38 #
     39 # mktool_execute
     40 #
     41 # Executes a command, returns exist status.
     42 # Performs token special translation before execution.
     43 #
     44 
     45 sub mktool_execute
     46 {
     47     my $token = shift;
     48     my @cmds = @_;
     49 
     50 #    printf("mktool_execute: token = '$token'\n");
     51     foreach $cmd (@cmds)
     52     {
     53 	#printf("mktool_execute: cmd = '$cmd'\n");
     54 	$cmd =~ s/\#\#/$token/g;
     55 	if($cmd =~ /^-p/)
     56 	{
     57 	    $cmd =~ s/^-p//;
     58 	    printf("$cmd\n");
     59 	}
     60 	else
     61 	{
     62 	    system($cmd);
     63 	    my $excode = ($? >> 8);
     64 	    exit $excode if $excode;
     65 	}
     66     }
     67 }
     68 
     69 
     70 $find_regexp = "";
     71 @find_cmd;
     72 
     73 #
     74 # mktool_foreach
     75 #
     76 sub mktool_foreach
     77 {
     78     if($_[0] eq "-find")
     79     {
     80 	shift;
     81 	$find_dir = shift;
     82 	$find_regexp = shift;
     83 	@find_cmds = @_;
     84 
     85 	if(!($find_dir =~ /^\//))
     86 	{
     87 	    $find_dir = cwd() . "/" . $find_dir;
     88 	}
     89 	find(\&_mktool_foreach_find_wanted, $find_dir);
     90     }
     91     else
     92     {
     93 	my $subdir = 0;
     94 	if($_[0] eq "-subdir")
     95 	{
     96 	    $subdir = 1;
     97 	    shift;
     98 	}
     99 
    100 	my @thingies = split(' ', shift);
    101 
    102 	foreach $thingy (@thingies)
    103 	{
    104 	    chdir $thingy unless $subdir == 0;
    105 	    mktool_execute($thingy, @_);
    106 	    chdir ".." unless $subdir == 0;
    107 	}
    108     }
    109 }
    110 
    111 
    112 
    113 sub _mktool_foreach_find_wanted
    114 {
    115     my $expr = "\$File::Find::name =~ /\^$find_regexp\$/";
    116 
    117     if(eval($expr))
    118     {
    119 	mktool_execute($File::Find::name, @find_cmds);
    120 	exit $excode if $excode;
    121     }
    122 }
    123 
    124 
    125 #
    126 # rm
    127 #
    128 # Removes a list of objects
    129 #
    130 sub mktool_rm
    131 {
    132     my($f);
    133 
    134     foreach $f (@_) {
    135 	eval { rmtree($f) };
    136 	if ($@) {
    137 	    die "$prog $op: failed to remove $f: $@\n";
    138 	}
    139     }
    140 }
    141 
    142 #
    143 # md
    144 #
    145 # Makes a list of directories
    146 #
    147 sub mktool_md
    148 {
    149     my($dir);
    150 
    151     foreach $dir (@_) {
    152 	$dir =~ s!/+$!!;
    153 	eval { mkpath($dir) };
    154 	if ($@) {
    155 	    die "$prog $op: failed to make directory $dir: $@\n";
    156         }
    157     }
    158 }
    159 
    160 
    161 sub mktool_cp
    162 {
    163     my($from, $to) = @_;
    164 
    165     if (@_ != 2) {
    166 	    die "$prog $op: must have two arguments\n";
    167     }
    168     copy($from, $to) ||
    169 	die "$prog $op: failed to copy $from to $to: $!\n";
    170 }
    171 
    172 sub mktool_ln
    173 {
    174     my($old, $new) = @_;
    175 
    176     if (@_ != 2) {
    177 	    die "$prog $op: must have two arguments\n";
    178     }
    179     link ($old, $new) ||
    180 	die "$prog $op: failed to link $new to $old: $!\n";
    181 }
    182 
    183 
    184 #	@echo "$@ \\" > ${BLDDIR}/$(notdir $@)
    185 #	@if ($(DEPEND)) >> $(BLDDIR)/$(notdir $@); then	\
    186 #	    exit 0;				\
    187 #	else					\
    188 #	    rm -f ${BLDDIR}/$(notdir $@);	\
    189 #	    exit 1;				\
    190 #	fi
    191 
    192 #	$(MAKEDEP) "$@" "$(BLDDIR)/$(notdir $@)" "$(DEPEND)"
    193 
    194 sub mktool_makedep
    195 {
    196     my ($source, $target, $cmd) = @_;
    197     my $result = `$cmd`;
    198     if(!$?)
    199     {
    200 	$result =~ s/^#.*\n//g;		# some makedeps produce comments
    201 	$result =~ s/\n+$//;		# toss trailing newlines
    202 
    203 	open (TARGET, ">$target") ||
    204 	    die("$prog $op: cannot open '$target' for writing: $!\n");
    205 	print TARGET "$source \\\n\${BLDDIR}/$result\n";
    206 	close(TARGET);
    207     }
    208 }
    209 
    210 sub mktool_echo
    211 {
    212     print "@_\n";
    213 }
    214 
    215 sub mktool_beep
    216 {
    217     -t STDOUT && defined $ENV{MAKEBEEP} && print "\007";
    218 }