| 1 |
########################################################################### |
| 2 |
# A module to find system files |
| 3 |
# |
| 4 |
# Copyright (C) 2016-2018 Andrey Ponomarenko's ABI Laboratory |
| 5 |
# |
| 6 |
# Written by Andrey Ponomarenko |
| 7 |
# |
| 8 |
# This library is free software; you can redistribute it and/or |
| 9 |
# modify it under the terms of the GNU Lesser General Public |
| 10 |
# License as published by the Free Software Foundation; either |
| 11 |
# version 2.1 of the License, or (at your option) any later version. |
| 12 |
# |
| 13 |
# This library is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
# Lesser General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU Lesser General Public |
| 19 |
# License along with this library; if not, write to the Free Software |
| 20 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 21 |
# MA 02110-1301 USA. |
| 22 |
########################################################################### |
| 23 |
use strict; |
| 24 |
|
| 25 |
my %Cache; |
| 26 |
|
| 27 |
my %DefaultBinPaths; |
| 28 |
|
| 29 |
my %OS_AddPath=( |
| 30 |
"macos"=>{ |
| 31 |
"bin"=>{"/Developer/usr/bin"=>1}}, |
| 32 |
"beos"=>{ |
| 33 |
"bin"=>{"/boot/common/bin"=>1,"/boot/system/bin"=>1,"/boot/develop/abi"=>1}} |
| 34 |
); |
| 35 |
|
| 36 |
sub getCmdPath($) |
| 37 |
{ |
| 38 |
my $Name = $_[0]; |
| 39 |
|
| 40 |
if(defined $Cache{"getCmdPath"}{$Name}) { |
| 41 |
return $Cache{"getCmdPath"}{$Name}; |
| 42 |
} |
| 43 |
my $Path = searchCmd($Name); |
| 44 |
if(not $Path and $In::Opt{"OS"} eq "windows") |
| 45 |
{ # search for *.exe file |
| 46 |
$Path = searchCmd($Name.".exe"); |
| 47 |
} |
| 48 |
if (not $Path) { |
| 49 |
$Path = searchCmd_Path($Name); |
| 50 |
} |
| 51 |
if($Path=~/\s/) { |
| 52 |
$Path = "\"".$Path."\""; |
| 53 |
} |
| 54 |
return ($Cache{"getCmdPath"}{$Name} = $Path); |
| 55 |
} |
| 56 |
|
| 57 |
sub searchCmd($) |
| 58 |
{ |
| 59 |
my $Name = $_[0]; |
| 60 |
|
| 61 |
if(defined $Cache{"searchCmd"}{$Name}) { |
| 62 |
return $Cache{"searchCmd"}{$Name}; |
| 63 |
} |
| 64 |
if(my $JdkPath = $In::Opt{"JdkPath"}) |
| 65 |
{ |
| 66 |
if(-x $JdkPath."/".$Name) { |
| 67 |
return ($Cache{"searchCmd"}{$Name} = $JdkPath."/".$Name); |
| 68 |
} |
| 69 |
|
| 70 |
if(-x $JdkPath."/bin/".$Name) { |
| 71 |
return ($Cache{"searchCmd"}{$Name} = $JdkPath."/bin/".$Name); |
| 72 |
} |
| 73 |
} |
| 74 |
if(my $DefaultPath = getCmdPath_Default($Name)) { |
| 75 |
return ($Cache{"searchCmd"}{$Name} = $DefaultPath); |
| 76 |
} |
| 77 |
return ($Cache{"searchCmd"}{$Name} = ""); |
| 78 |
} |
| 79 |
|
| 80 |
sub searchCmd_Path($) |
| 81 |
{ |
| 82 |
my $Name = $_[0]; |
| 83 |
|
| 84 |
if(defined $Cache{"searchCmd_Path"}{$Name}) { |
| 85 |
return $Cache{"searchCmd_Path"}{$Name}; |
| 86 |
} |
| 87 |
|
| 88 |
if(defined $In::Opt{"SysPaths"}{"bin"}) |
| 89 |
{ |
| 90 |
foreach my $Path (sort {length($a)<=>length($b)} keys(%{$In::Opt{"SysPaths"}{"bin"}})) |
| 91 |
{ |
| 92 |
if(-f $Path."/".$Name or -f $Path."/".$Name.".exe") { |
| 93 |
return ($Cache{"searchCmd_Path"}{$Name} = join_P($Path,$Name)); |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return ($Cache{"searchCmd_Path"}{$Name} = ""); |
| 99 |
} |
| 100 |
|
| 101 |
sub getCmdPath_Default($) |
| 102 |
{ # search in PATH |
| 103 |
if(defined $Cache{"getCmdPath_Default"}{$_[0]}) { |
| 104 |
return $Cache{"getCmdPath_Default"}{$_[0]}; |
| 105 |
} |
| 106 |
return ($Cache{"getCmdPath_Default"}{$_[0]} = getCmdPath_Default_I($_[0])); |
| 107 |
} |
| 108 |
|
| 109 |
sub getCmdPath_Default_I($) |
| 110 |
{ # search in PATH |
| 111 |
my $Name = $_[0]; |
| 112 |
|
| 113 |
my $TmpDir = $In::Opt{"Tmp"}; |
| 114 |
|
| 115 |
if($Name=~/find/) |
| 116 |
{ # special case: search for "find" utility |
| 117 |
if(`find \"$TmpDir\" -maxdepth 0 2>\"$TmpDir/null\"`) { |
| 118 |
return "find"; |
| 119 |
} |
| 120 |
} |
| 121 |
if(getVersion($Name)) { |
| 122 |
return $Name; |
| 123 |
} |
| 124 |
if($In::Opt{"OS"} eq "windows") |
| 125 |
{ |
| 126 |
if(`$Name /? 2>\"$TmpDir/null\"`) { |
| 127 |
return $Name; |
| 128 |
} |
| 129 |
} |
| 130 |
if($Name!~/which/) |
| 131 |
{ |
| 132 |
if(my $WhichCmd = getCmdPath("which")) |
| 133 |
{ |
| 134 |
if(`$WhichCmd $Name 2>\"$TmpDir/null\"`) { |
| 135 |
return $Name; |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
foreach my $Path (sort {length($a)<=>length($b)} keys(%DefaultBinPaths)) |
| 140 |
{ |
| 141 |
if(-f $Path."/".$Name) { |
| 142 |
return join_P($Path,$Name); |
| 143 |
} |
| 144 |
} |
| 145 |
return ""; |
| 146 |
} |
| 147 |
|
| 148 |
sub detectDefaultPaths($$) |
| 149 |
{ |
| 150 |
my ($Bin, $Java) = @_; |
| 151 |
|
| 152 |
if($Cache{"detectDefaultPaths"}{$Bin}{$Java}) |
| 153 |
{ # enter once |
| 154 |
return; |
| 155 |
} |
| 156 |
$Cache{"detectDefaultPaths"}{$Bin}{$Java} = 1; |
| 157 |
|
| 158 |
if(not keys(%{$In::Opt{"SysPaths"}})) |
| 159 |
{ # run once |
| 160 |
foreach my $Type (keys(%{$OS_AddPath{$In::Opt{"OS"}}})) |
| 161 |
{ # additional search paths |
| 162 |
foreach my $Path (keys(%{$OS_AddPath{$In::Opt{"OS"}}{$Type}})) |
| 163 |
{ |
| 164 |
next if(not -d $Path); |
| 165 |
$In::Opt{"SysPaths"}{$Type}{$Path} = $OS_AddPath{$In::Opt{"OS"}}{$Type}{$Path}; |
| 166 |
} |
| 167 |
} |
| 168 |
if($In::Opt{"OS"} ne "windows") |
| 169 |
{ |
| 170 |
foreach my $Type ("include", "lib", "bin") |
| 171 |
{ # autodetecting system "devel" directories |
| 172 |
foreach my $Path (cmdFind("/", "d", "*$Type*", 1)) { |
| 173 |
$In::Opt{"SysPaths"}{$Type}{$Path} = 1; |
| 174 |
} |
| 175 |
if(-d "/usr") |
| 176 |
{ |
| 177 |
foreach my $Path (cmdFind("/usr", "d", "*$Type*", 1)) { |
| 178 |
$In::Opt{"SysPaths"}{$Type}{$Path} = 1; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
if($Bin) |
| 186 |
{ |
| 187 |
detectBinDefaultPaths(); |
| 188 |
foreach my $Path (keys(%DefaultBinPaths)) { |
| 189 |
$In::Opt{"SysPaths"}{"bin"}{$Path} = $DefaultBinPaths{$Path}; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
if($Java) |
| 194 |
{ |
| 195 |
if(my $JavacCmd = getCmdPath("javac")) |
| 196 |
{ |
| 197 |
if(my $Ver = `$JavacCmd -version 2>&1`) |
| 198 |
{ |
| 199 |
if($Ver=~/javac\s+(.+)/) |
| 200 |
{ |
| 201 |
printMsg("INFO", "Using Java ".$1); |
| 202 |
$In::Opt{"CompilerVer"} = $1; |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
sub detectBinDefaultPaths() |
| 210 |
{ |
| 211 |
my $EnvPaths = $ENV{"PATH"}; |
| 212 |
if($In::Opt{"OS"} eq "beos") { |
| 213 |
$EnvPaths .= ":".$ENV{"BETOOLS"}; |
| 214 |
} |
| 215 |
elsif($In::Opt{"OS"} eq "windows" |
| 216 |
and my $JHome = $ENV{"JAVA_HOME"}) { |
| 217 |
$EnvPaths .= ";$JHome\\bin"; |
| 218 |
} |
| 219 |
my $Sep = ($In::Opt{"OS"} eq "windows")?";":":|;"; |
| 220 |
foreach my $Path (sort {length($a)<=>length($b)} split(/$Sep/, $EnvPaths)) |
| 221 |
{ |
| 222 |
$Path=~s/[\/\\]+\Z//g; |
| 223 |
if($Path) { |
| 224 |
$DefaultBinPaths{$Path} = 1; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
sub getArchivePaths($$) |
| 230 |
{ |
| 231 |
my ($Dest, $LVer) = @_; |
| 232 |
if(-f $Dest) { |
| 233 |
return ($Dest); |
| 234 |
} |
| 235 |
elsif(-d $Dest) |
| 236 |
{ |
| 237 |
$Dest=~s/[\/\\]+\Z//g; |
| 238 |
next if(not $Dest); |
| 239 |
|
| 240 |
my @Archives = (); |
| 241 |
foreach my $Path (cmdFind($Dest, "", "*\\.jar")) |
| 242 |
{ |
| 243 |
next if(ignorePath($Path, $Dest)); |
| 244 |
push(@Archives, realpath_F($Path)); |
| 245 |
} |
| 246 |
foreach my $Path (cmdFind($Dest, "", "*\\.jmod")) |
| 247 |
{ |
| 248 |
next if(ignorePath($Path, $Dest)); |
| 249 |
push(@Archives, realpath_F($Path)); |
| 250 |
} |
| 251 |
return @Archives; |
| 252 |
} |
| 253 |
return (); |
| 254 |
} |
| 255 |
|
| 256 |
return 1; |