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