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, version 2.0,
8# as published by the Free Software Foundation.
9#
10# This program is also distributed with certain software (including
11# but not limited to OpenSSL) that is licensed under separate terms,
12# as designated in a particular file or component or in included license
13# documentation.  The authors of MySQL hereby grant you an additional
14# permission to link the program and your derivative works with the
15# separately licensed software that they have included with MySQL.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20# GNU General Public License, version 2.0, for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
25
26##############################################################################
27#
28#  This script reports various configuration settings that may be needed
29#  when using the MySQL client library.
30#
31#  This script try to match the shell script version as close as possible,
32#  but in addition being compatible with ActiveState Perl on Windows.
33#
34#  All unrecognized arguments to this script are passed to mysqld.
35#
36#  NOTE: This script will only be used on Windows until solved how to
37#        handle @LIBS@ and other strings inserted that might contain
38#        several arguments, possibly with spaces in them.
39#
40#  NOTE: This script was deliberately written to be as close to the shell
41#        script as possible, to make the maintenance of both in parallel
42#        easier.
43#
44##############################################################################
45
46use File::Basename;
47use Getopt::Long;
48use Cwd;
49use strict;
50
51my $cwd = cwd();
52my $basedir;
53
54my $socket  = '@MYSQL_UNIX_ADDR@';
55my $version = '@VERSION@';
56
57sub which
58{
59  my $file = shift;
60
61  my $IFS = $^O eq "MSWin32" ? ";" : ":";
62
63  foreach my $dir ( split($IFS, $ENV{PATH}) )
64  {
65    if ( -f "$dir/$file" or -f "$dir/$file.exe" )
66    {
67      return "$dir/$file";
68    }
69  }
70  print STDERR "which: no $file in ($ENV{PATH})\n";
71  exit 1;
72}
73
74# ----------------------------------------------------------------------
75# If we can find the given directory relatively to where mysql_config is
76# we should use this instead of the incompiled one.
77# This is to ensure that this script also works with the binary MySQL
78# version
79# ----------------------------------------------------------------------
80
81sub fix_path
82{
83  my $default = shift;
84  my @dirs = @_;
85
86  foreach my $dirname ( @dirs )
87  {
88    my $path = "$basedir/$dirname";
89    if ( -d $path )
90    {
91      return $path;
92    }
93  }
94  return $default;
95}
96
97sub get_full_path
98{
99  my $file = shift;
100
101  # if the file is a symlink, try to resolve it
102  if ( $^O ne "MSWin32" and -l $file )
103  {
104    $file = readlink($file);
105  }
106
107  if ( $file =~ m,^/, )
108  {
109    # Do nothing, absolute path
110  }
111  elsif ( $file =~ m,/, )
112  {
113    # Make absolute, and remove "/./" in path
114    $file = "$cwd/$file";
115    $file =~ s,/\./,/,g;
116  }
117  else
118  {
119    # Find in PATH
120    $file = which($file);
121  }
122
123  return $file;
124}
125
126##############################################################################
127#
128#  Form a command line that can handle spaces in paths and arguments
129#
130##############################################################################
131
132sub quote_options {
133  my @cmd;
134  foreach my $opt ( @_ )
135  {
136    next unless $opt;           # If undefined or empty, just skip
137    push(@cmd, "\"$opt\"");       # Quote argument
138  }
139  return join(" ", @cmd);
140}
141
142##############################################################################
143#
144#  Main program
145#
146##############################################################################
147
148my $me = get_full_path($0);
149$basedir = dirname(dirname($me)); # Remove "/bin/mysql_config" part
150
151my $ldata   = '@localstatedir@';
152my $execdir = '@libexecdir@';
153my $bindir  = '@bindir@';
154
155# ----------------------------------------------------------------------
156# If installed, search for the compiled in directory first (might be "lib64")
157# ----------------------------------------------------------------------
158
159my $pkglibdir = fix_path('@pkglibdir@',"libmysql/relwithdebinfo",
160                         "libmysql/release","libmysql/debug","lib/mysql","lib");
161
162my $pkgincludedir = fix_path('@pkgincludedir@', "include/mysql", "include");
163
164# Assume no argument with space in it
165my @ldflags = split(" ",'@LDFLAGS@');
166
167my $port;
168if ( '@MYSQL_TCP_PORT_DEFAULT@' == 0 ) {
169  $port = 0;
170} else {
171  $port = '@MYSQL_TCP_PORT@';
172}
173
174# ----------------------------------------------------------------------
175# Create options
176# ----------------------------------------------------------------------
177
178my(@lib_opts,@lib_e_opts);
179if ( $^O eq "MSWin32" )
180{
181  my $linkpath   = "$pkglibdir";
182  @lib_opts   = ("$linkpath/@LIBMYSQL_OS_OUTPUT_NAME@");
183  @lib_e_opts = ("$linkpath/@LIBEMBED_OS_OUTPUT_NAME@");
184}
185else
186{
187  my $linkpath   = "-L$pkglibdir@RPATH_OPTION@";
188  @lib_opts   = ($linkpath,"-l@LIBMYSQL_OS_OUTPUT_NAME@");
189  @lib_e_opts = ($linkpath,"-l@LIBEMBED_OS_OUTPUT_NAME@");
190}
191
192
193my $flags;
194$flags->{libs} = [@lib_opts, qw(@CONFIG_CLIENT_LIBS@)];
195$flags->{embedded_libs} = [@lib_e_opts, qw(@CONFIG_EMBEDD_LIBS@)];
196
197$flags->{include} = ["-I$pkgincludedir"];
198$flags->{cflags}  = [@{$flags->{include}},split(" ",'@CFLAGS@')];
199$flags->{cxxflags}= [@{$flags->{include}},split(" ",'@CXXFLAGS@')];
200
201my $include =       quote_options(@{$flags->{include}});
202my $cflags  =       quote_options(@{$flags->{cflags}});
203my $cxxflags=       quote_options(@{$flags->{cxxflags}});
204my $libs    =       quote_options(@{$flags->{libs}});
205my $embedded_libs = quote_options(@{$flags->{embedded_libs}});
206
207##############################################################################
208#
209#  Usage information, output if no option is given
210#
211##############################################################################
212
213sub usage
214{
215  print <<EOF;
216Usage: $0 [OPTIONS]
217Options:
218        --cflags         [$cflags]
219        --cxxflags       [$cxxflags]
220        --include        [$include]
221        --libs           [$libs]
222        --libs_r         [$libs]
223        --socket         [$socket]
224        --port           [$port]
225        --version        [$version]@LIBMYSQLD_LIBS_USAGE@
226EOF
227  exit 1;
228}
229
230@ARGV or usage();
231
232##############################################################################
233#
234#  Get options and output the values
235#
236##############################################################################
237
238GetOptions(
239           "cflags"  => sub { print "$cflags\n" },
240           "cxxflags"=> sub { print "$cxxflags\n" },
241           "include" => sub { print "$include\n" },
242           "libs"    => sub { print "$libs\n" },
243           "libs_r"  => sub { print "$libs\n" },
244           "socket"  => sub { print "$socket\n" },
245           "port"    => sub { print "$port\n" },
246           "version" => sub { print "$version\n" },
247           "embedded-libs|embedded|libmysqld-libs" =>
248             sub { @DISABLE_EMBEDDED_PL@ print "$embedded_libs\n" },
249           ) or usage();
250
251exit 0
252