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