1#!@PERL_PATH@
2# -*- cperl -*-
3#
4# Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; version 2 of the License.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1335  USA
18
19##############################################################################
20#
21#  This script reports various configuration settings that may be needed
22#  when using the MySQL client library.
23#
24#  This script try to match the shell script version as close as possible,
25#  but in addition being compatible with ActiveState Perl on Windows.
26#
27#  All unrecognized arguments to this script are passed to mysqld.
28#
29#  NOTE: This script will only be used on Windows until solved how to
30#        handle @LIBS@ and other strings inserted that might contain
31#        several arguments, possibly with spaces in them.
32#
33#  NOTE: This script was deliberately written to be as close to the shell
34#        script as possible, to make the maintenance of both in parallel
35#        easier.
36#
37##############################################################################
38
39use File::Basename;
40use Getopt::Long;
41use Cwd;
42use strict;
43
44my @exclude_cflags =
45  qw/DDBUG_OFF DSAFE_MUTEX DUNIV_MUST_NOT_INLINE DFORCE_INIT_OF_VARS
46     DEXTRA_DEBUG DHAVE_valgrind O O[0-9] xO[0-9] W[-A-Za-z]*
47     Xa xstrconst xc99=none
48     unroll2 ip mp restrict/;
49
50my @exclude_libs = qw/lmtmalloc static-libcxa i-static static-intel/;
51
52my $cwd = cwd();
53my $basedir;
54
55my $socket  = '@MYSQL_UNIX_ADDR@';
56my $version = '@VERSION@';
57
58sub which
59{
60  my $file = shift;
61
62  my $IFS = $^O eq "MSWin32" ? ";" : ":";
63
64  foreach my $dir ( split($IFS, $ENV{PATH}) )
65  {
66    if ( -f "$dir/$file" or -f "$dir/$file.exe" )
67    {
68      return "$dir/$file";
69    }
70  }
71  print STDERR "which: no $file in ($ENV{PATH})\n";
72  exit 1;
73}
74
75# ----------------------------------------------------------------------
76# If we can find the given directory relatively to where mysql_config is
77# we should use this instead of the incompiled one.
78# This is to ensure that this script also works with the binary MySQL
79# version
80# ----------------------------------------------------------------------
81
82sub fix_path
83{
84  my $default = shift;
85  my @dirs = @_;
86
87  foreach my $dirname ( @dirs )
88  {
89    my $path = "$basedir/$dirname";
90    if ( -d $path )
91    {
92      return $path;
93    }
94  }
95  return $default;
96}
97
98sub get_full_path
99{
100  my $file = shift;
101
102  # if the file is a symlink, try to resolve it
103  if ( $^O ne "MSWin32" and -l $file )
104  {
105    $file = readlink($file);
106  }
107
108  if ( $file =~ m,^/, )
109  {
110    # Do nothing, absolute path
111  }
112  elsif ( $file =~ m,/, )
113  {
114    # Make absolute, and remove "/./" in path
115    $file = "$cwd/$file";
116    $file =~ s,/\./,/,g;
117  }
118  else
119  {
120    # Find in PATH
121    $file = which($file);
122  }
123
124  return $file;
125}
126
127##############################################################################
128#
129#  Form a command line that can handle spaces in paths and arguments
130#
131##############################################################################
132
133sub quote_options {
134  my @cmd;
135  foreach my $opt ( @_ )
136  {
137    next unless $opt;           # If undefined or empty, just skip
138    push(@cmd, "\"$opt\"");       # Quote argument
139  }
140  return join(" ", @cmd);
141}
142
143##############################################################################
144#
145#  Main program
146#
147##############################################################################
148
149my $me = get_full_path($0);
150$basedir = dirname(dirname($me)); # Remove "/bin/mysql_config" part
151
152my $ldata   = '@localstatedir@';
153my $execdir = '@libexecdir@';
154my $bindir  = '@bindir@';
155
156# ----------------------------------------------------------------------
157# If installed, search for the compiled in directory first (might be "lib64")
158# ----------------------------------------------------------------------
159
160my $pkglibdir = fix_path('@pkglibdir@',"libmysql/relwithdebinfo",
161                         "libmysql/release","libmysql/debug","lib/mysql","lib");
162
163my $pkgincludedir = fix_path('@pkgincludedir@', "include/mysql", "include");
164
165# Assume no argument with space in it
166my @ldflags = split(" ",'@LDFLAGS@');
167
168my $port;
169if ( '@MYSQL_TCP_PORT_DEFAULT@' == 0 ) {
170  $port = 0;
171} else {
172  $port = '@MYSQL_TCP_PORT@';
173}
174
175# ----------------------------------------------------------------------
176# Create options
177# We intentionally add a space to the beginning and end of lib strings, simplifies replace later
178# ----------------------------------------------------------------------
179
180my (@lib_opts,@lib_r_opts,@lib_e_opts);
181if ( $^O eq "MSWin32" )
182{
183  my $linkpath   = "$pkglibdir";
184  # user32 is only needed for debug or embedded
185  my @winlibs = ("wsock32.lib","advapi32.lib","user32.lib");
186  @lib_opts   = ("$linkpath/mysqlclient.lib",@winlibs);
187  @lib_r_opts = @lib_opts;
188  @lib_e_opts = ("$linkpath/mysqlserver.lib",@winlibs);
189}
190else
191{
192  my $linkpath   = "-L$pkglibdir @RPATH_OPTION@";
193  @lib_opts   = ($linkpath,"-lmysqlclient");
194  @lib_r_opts = ($linkpath,"-lmysqlclient_r");
195  @lib_e_opts = ($linkpath,"-lmysqld");
196}
197
198my $flags;
199$flags->{libs} =
200  [@ldflags,@lib_opts,'@ZLIB_DEPS@','@NON_THREADED_LIBS@','@openssl_libs@','@STATIC_NSS_FLAGS@'];
201$flags->{libs_r} =
202  [@ldflags,@lib_r_opts,'@ZLIB_DEPS@','@LIBS@','@openssl_libs@'];
203$flags->{embedded_libs} =
204  [@ldflags,@lib_e_opts,'@CMAKE_DL_LIBS@','@ZLIB_DEPS@','@LIBS@','@WRAPLIBS@','@openssl_libs@'];
205
206$flags->{include} = ["-I$pkgincludedir"];
207$flags->{cflags}  = [@{$flags->{include}},split(" ",'@CFLAGS@')];
208
209# ----------------------------------------------------------------------
210# Remove some options that a client doesn't have to care about
211# FIXME until we have a --cxxflags, we need to remove -Xa
212#       and -xstrconst to make --cflags usable for Sun Forte C++
213# ----------------------------------------------------------------------
214
215my $filter = join("|", @exclude_cflags);
216my @tmp = @{$flags->{cflags}};          # Copy the flag list
217$flags->{cflags} = [];                  # Clear it
218foreach my $cflag ( @tmp )
219{
220  push(@{$flags->{cflags}}, $cflag) unless $cflag =~ m/^($filter)$/o;
221}
222
223# Same for --libs(_r)
224$filter = join("|", @exclude_libs);
225foreach my $lib_type ( "libs","libs_r","embedded_libs" )
226{
227  my @tmp = @{$flags->{$lib_type}};          # Copy the flag list
228  $flags->{$lib_type} = [];                  # Clear it
229  foreach my $lib ( @tmp )
230  {
231    push(@{$flags->{$lib_type}}, $lib) unless $lib =~ m/^($filter)$/o;
232  }
233}
234
235my $include =       quote_options(@{$flags->{include}});
236my $cflags  =       quote_options(@{$flags->{cflags}});
237my $libs    =       quote_options(@{$flags->{libs}});
238my $libs_r  =       quote_options(@{$flags->{libs_r}});
239my $embedded_libs = quote_options(@{$flags->{embedded_libs}});
240
241##############################################################################
242#
243#  Usage information, output if no option is given
244#
245##############################################################################
246
247sub usage
248{
249  print <<EOF;
250Usage: $0 [OPTIONS]
251Options:
252        --cflags         [$cflags]
253        --include        [$include]
254        --libs           [$libs]
255        --libs_r         [$libs_r]
256        --socket         [$socket]
257        --port           [$port]
258        --version        [$version]
259        --libmysqld-libs [$embedded_libs]
260EOF
261  exit 0;
262}
263
264@ARGV or usage();
265
266##############################################################################
267#
268#  Get options and output the values
269#
270##############################################################################
271
272GetOptions(
273           "cflags"  => sub { print "$cflags\n" },
274           "include" => sub { print "$include\n" },
275           "libs"    => sub { print "$libs\n" },
276           "libs_r"  => sub { print "$libs_r\n" },
277           "socket"  => sub { print "$socket\n" },
278           "port"    => sub { print "$port\n" },
279           "version" => sub { print "$version\n" },
280           "embedded-libs|embedded|libmysqld-libs" =>
281             sub { print "$embedded_libs\n" },
282           ) or usage();
283
284exit 0
285