mktool.pl (5074B)
1 # 2 # mktool.pl 3 # 4 # 5 # 6 # This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 7 # 8 # Copyright 2007-2019 Broadcom Inc. All rights reserved. 9 10 use File::Path; 11 use File::Find; 12 use File::Copy; 13 use Cwd; 14 15 ($prog = $0) =~ s/.*\///; 16 17 SWITCH: 18 { 19 $op = shift; 20 21 if ($op eq "-rm") { mktool_rm(@ARGV); last SWITCH; } 22 if ($op eq "-cp") { mktool_cp(@ARGV); last SWITCH; } 23 if ($op eq "-md") { mktool_md(@ARGV); last SWITCH; } 24 if ($op eq "-ln") { mktool_ln(@ARGV); last SWITCH; } 25 if ($op eq "-foreach") { mktool_foreach(@ARGV); last SWITCH; } 26 if ($op eq "-dep") { mktool_makedep(@ARGV); last SWITCH; } 27 if ($op eq "-echo") { mktool_echo(@ARGV); last SWITCH; } 28 if ($op eq "-beep") { mktool_beep(@ARGV); last SWITCH; } 29 die("$prog: unknown option '$op'\n"); 30 } 31 32 exit 0; 33 34 35 36 37 # 38 # mktool_execute 39 # 40 # Executes a command, returns exist status. 41 # Performs token special translation before execution. 42 # 43 44 sub mktool_execute 45 { 46 my $token = shift; 47 my @cmds = @_; 48 49 # printf("mktool_execute: token = '$token'\n"); 50 foreach $cmd (@cmds) 51 { 52 #printf("mktool_execute: cmd = '$cmd'\n"); 53 $cmd =~ s/\#\#/$token/g; 54 if($cmd =~ /^-p/) 55 { 56 $cmd =~ s/^-p//; 57 printf("$cmd\n"); 58 } 59 else 60 { 61 system($cmd); 62 my $excode = ($? >> 8); 63 exit $excode if $excode; 64 } 65 } 66 } 67 68 69 $find_regexp = ""; 70 @find_cmd; 71 72 # 73 # mktool_foreach 74 # 75 sub mktool_foreach 76 { 77 if($_[0] eq "-find") 78 { 79 shift; 80 $find_dir = shift; 81 $find_regexp = shift; 82 @find_cmds = @_; 83 84 if(!($find_dir =~ /^\//)) 85 { 86 $find_dir = cwd() . "/" . $find_dir; 87 } 88 find(\&_mktool_foreach_find_wanted, $find_dir); 89 } 90 else 91 { 92 my $subdir = 0; 93 if($_[0] eq "-subdir") 94 { 95 $subdir = 1; 96 shift; 97 } 98 99 my @thingies = split(' ', shift); 100 101 foreach $thingy (@thingies) 102 { 103 chdir $thingy unless $subdir == 0; 104 mktool_execute($thingy, @_); 105 chdir ".." unless $subdir == 0; 106 } 107 } 108 } 109 110 111 112 sub _mktool_foreach_find_wanted 113 { 114 my $expr = "\$File::Find::name =~ /\^$find_regexp\$/"; 115 116 if(eval($expr)) 117 { 118 mktool_execute($File::Find::name, @find_cmds); 119 exit $excode if $excode; 120 } 121 } 122 123 124 # 125 # rm 126 # 127 # Removes a list of objects 128 # 129 sub mktool_rm 130 { 131 my($f); 132 133 foreach $f (@_) { 134 eval { rmtree($f) }; 135 if ($@) { 136 die "$prog $op: failed to remove $f: $@\n"; 137 } 138 } 139 } 140 141 # 142 # md 143 # 144 # Makes a list of directories 145 # 146 sub mktool_md 147 { 148 my($dir); 149 150 foreach $dir (@_) { 151 $dir =~ s!/+$!!; 152 eval { mkpath($dir) }; 153 if ($@) { 154 die "$prog $op: failed to make directory $dir: $@\n"; 155 } 156 } 157 } 158 159 160 sub mktool_cp 161 { 162 my($from, $to) = @_; 163 164 if (@_ != 2) { 165 die "$prog $op: must have two arguments\n"; 166 } 167 copy($from, $to) || 168 die "$prog $op: failed to copy $from to $to: $!\n"; 169 } 170 171 sub mktool_ln 172 { 173 my($old, $new) = @_; 174 175 if (@_ != 2) { 176 die "$prog $op: must have two arguments\n"; 177 } 178 link ($old, $new) || 179 die "$prog $op: failed to link $new to $old: $!\n"; 180 } 181 182 183 # @echo "$@ \\" > ${BLDDIR}/$(notdir $@) 184 # @if ($(DEPEND)) >> $(BLDDIR)/$(notdir $@); then \ 185 # exit 0; \ 186 # else \ 187 # rm -f ${BLDDIR}/$(notdir $@); \ 188 # exit 1; \ 189 # fi 190 191 # $(MAKEDEP) "$@" "$(BLDDIR)/$(notdir $@)" "$(DEPEND)" 192 193 sub mktool_makedep 194 { 195 my ($source, $target, $cmd, $curdir) = @_; 196 my @result = `$cmd`; 197 my $sdk = $ENV{'SDK'}; 198 my $count; 199 my $tmp; 200 local $resultant; 201 202 ## Comman $cmd 203 #Command $cmd 204 print <<MKTOOL; 205 206 mktool.pl:: 207 curdir $curdir 208 209 MKTOOL 210 211 # derive the path to the target 212 $curdir = "$sdk/$curdir"; 213 214 # save the basename 215 $dirName = substr($target,0,rindex($target,"\/")); 216 217 # prepare the top line of the %.d file 218 $resultant="$source \\\n\${BLDDIR}/"; 219 if(!$?) 220 { 221 foreach $line (@result) 222 { 223 $line =~ s/^#.*\n//g; # some makedeps produce comments 224 $line =~ s/\n+$//; # toss trailing newlines 225 $line =~ s/(\s+)(\.\/)/ /g; # remove leading ./ 226 227 # insert SDK path before ../ 228 $line =~ s/(\s+)(\.\.\/)/${1}$curdir\/${2}/g; 229 230 # insert SDK path 231 $line =~ s/(\s+)(\w+)/${1}$curdir\/${2}/g; 232 233 $count=0; 234 $tmp=$line; 235 while( (index($line,"..")>-1) & ($count < 20) ) 236 { 237 $line=~s/\/\w+\/\.\.//; 238 # if we hit a major recursion, revert the line, report 239 # this to the output and drop out of the loop, but do 240 # continue, this should not halt generation 241 if($count++>19) 242 { 243 print "mktool.pl: could not process $line \n\n"; 244 print ":: curdir $curdir\n"; 245 print ":: target $target\n"; 246 print ":: cmd $cmd\n"; 247 $line=$tmp; 248 } 249 } 250 251 # set all the paths to use the $SDK variable 252 $line =~ s/$ENV{'SDK'}/\$\{SDK\}/g; 253 $resultant=$resultant . $line; 254 } 255 256 # some compilers return extra newlines 257 $resultant=~s/\n//g; 258 259 # now clean up the result 260 $resultant=~s/\\/\\\n/g; 261 262 mktool_md($dirName) unless (-d $dirName); 263 open (TARGET, ">$target") || 264 die("$prog $op: cannot open '$target' for writing: $!\n"); 265 print TARGET "$resultant\n"; 266 close(TARGET); 267 } 268 } 269 270 sub mktool_echo 271 { 272 print "@_\n"; 273 } 274 275 sub mktool_beep 276 { 277 -t STDOUT && defined $ENV{MAKEBEEP} && print "\007"; 278 }