2
# Copyright (C) 2004-2006 MySQL AB
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; version 2 of the License.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
# Utility functions to find files in a MySQL source or bindist
28
use base qw(Exporter);
29
our @EXPORT= qw(my_find_bin my_find_dir my_find_file NOT_REQUIRED);
33
my $bin_extension= ".exe" if IS_WINDOWS;
35
# Helper function to be used for fourth parameter to find functions
36
sub NOT_REQUIRED { return 0; }
39
# my_find_bin - find an executable with "name_1...name_n" in
40
# paths "path_1...path_n" and return the full path
43
# my $mysqld_exe= my_find_bin($basedir.
45
# ["mysqld", "mysqld-debug"]);
46
# my $mysql_exe= my_find_bin($basedir,
51
# To check if something exists, use the required parameter
52
# set to 0, the function will return an empty string if the
54
# my $mysql_exe= my_find_bin($basedir,
56
# "mysql", NOT_REQUIRED);
58
# NOTE: The function honours MTR_VS_CONFIG environment variable
62
my ($base, $paths, $names, $required)= @_;
63
croak "usage: my_find_bin(<base>, <paths>, <names>, [<required>])"
64
unless @_ == 4 or @_ == 3;
66
# -------------------------------------------------------
67
# Find and return the first executable
68
# -------------------------------------------------------
69
foreach my $path (my_find_paths($base, $paths, $names, $bin_extension)) {
70
return $path if ( -x $path or (IS_WINDOWS and -f $path) );
72
if (defined $required and $required == NOT_REQUIRED){
73
# Return empty string to indicate not found
76
find_error($base, $paths, $names);
81
# my_find_file - find a file with "name_1...name_n" in
82
# paths "path_1...path_n" and return the full path
85
# my $mysqld_exe= my_find_file($basedir.
90
# Also supports NOT_REQUIRED flag
92
# NOTE: The function honours MTR_VS_CONFIG environment variable
96
my ($base, $paths, $names, $required)= @_;
97
croak "usage: my_find_file(<base>, <paths>, <names>, [<required>])"
98
unless @_ == 4 or @_ == 3;
100
# -------------------------------------------------------
101
# Find and return the first executable
102
# -------------------------------------------------------
103
foreach my $path (my_find_paths($base, $paths, $names, $bin_extension)) {
104
return $path if ( -f $path );
106
if (defined $required and $required == NOT_REQUIRED){
107
# Return empty string to indicate not found
110
find_error($base, $paths, $names);
115
# my_find_dir - find the first existing directory in one of
119
# my $charset_set= my_find_dir($basedir,
120
# ["mysql/share","sql/share", "share"],
123
# my $charset_set= my_find_dir($basedir,
124
# ['client_release', 'client_debug',
127
# NOTE: The function honours MTR_VS_CONFIG environment variable
131
my ($base, $paths, $dirs, $required)= @_;
132
croak "usage: my_find_dir(<base>, <paths>[, <dirs>])"
133
unless (@_ == 3 or @_ == 2);
135
# -------------------------------------------------------
136
# Find and return the first directory
137
# -------------------------------------------------------
138
foreach my $path (my_find_paths($base, $paths, $dirs)) {
139
return $path if ( -d $path );
141
find_error($base, $paths, $dirs);
146
my ($base, $paths, $names, $extension)= @_;
148
# Convert the arguments into two normal arrays to ease
151
push(@names, ref $names eq "ARRAY" ? @$names : $names);
152
push(@paths, ref $paths eq "ARRAY" ? @$paths : $paths);
154
#print "base: $base\n";
155
#print "names: @names\n";
156
#print "paths: @paths\n";
158
# User can select to look in a special build dir
159
# which is a subdirectory of any of the paths
161
my $build_dir= $vs_config_dir || $ENV{MTR_VS_CONFIG} || $ENV{MTR_BUILD_DIR};
162
push(@extra_dirs, $build_dir) if defined $build_dir;
164
if (defined $extension){
165
# Append extension to names, if name does not already have extension
166
map { $_.=$extension unless /\.(.*)+$/ } @names;
169
# -------------------------------------------------------
171
# -------------------------------------------------------
173
# Add the default extra build dirs unless a specific one has
174
# already been selected
178
"debug")) if @extra_dirs == 0;
181
#print "extra_build_dir: @extra_dirs\n";
183
# -------------------------------------------------------
184
# Build cross product of "paths * extra_build_dirs"
185
# -------------------------------------------------------
186
push(@paths, map { my $path= $_;
187
map { "$path/$_" } @extra_dirs
189
#print "paths: @paths\n";
191
# -------------------------------------------------------
192
# Build cross product of "paths * names"
193
# -------------------------------------------------------
194
@paths= map { my $path= $_;
195
map { "$path/$_" } @names
197
#print "paths: @paths\n";
199
# -------------------------------------------------------
200
# Prepend base to all paths
201
# -------------------------------------------------------
202
@paths= map { "$base/$_" } @paths;
203
#print "paths: @paths\n";
205
# -------------------------------------------------------
206
# Glob all paths to expand wildcards
207
# -------------------------------------------------------
208
@paths= map { glob("$_") } @paths;
209
#print "paths: @paths\n";
211
# -------------------------------------------------------
212
# Return the list of paths
213
# -------------------------------------------------------
222
(@_ == 2) ? join(" or ", @_) :
223
join(", ", @_[0..($#_-1)], "or $_[-1]");
229
return map('\''.$_.'\'', @_);
234
my ($base, $paths, $names)= @_;
237
push(@names, ref $names eq "ARRAY" ? @$names : $names);
238
push(@paths, ref $paths eq "ARRAY" ? @$paths : $paths);
240
croak "** ERROR: Could not find ",
241
commify(fnuttify(@names)), " in ",
242
commify(fnuttify(my_find_paths($base, $paths, $names))), "\n";