1#!/usr/bin/perl
2
3# Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License, version 2.0,
7# as published by the Free Software Foundation.
8#
9# This program is also distributed with certain software (including
10# but not limited to OpenSSL) that is licensed under separate terms,
11# as designated in a particular file or component or in included license
12# documentation.  The authors of MySQL hereby grant you an additional
13# permission to link the program and your derivative works with the
14# separately licensed software that they have included with MySQL.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19# GNU General Public License, version 2.0, for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24
25use strict;
26use Cwd 'abs_path';
27use File::Basename;
28
29my $cmakeargs = "";
30
31# Find source root directory
32# Assume this script is in <srcroot>/cmake
33my $srcdir = dirname(dirname(abs_path($0)));
34my $cmake_install_prefix="";
35
36# Sets installation directory,  bindir, libdir, libexecdir etc
37# the equivalent CMake variables are given without prefix
38# e.g if --prefix is /usr and --bindir is /usr/bin
39# then cmake variable (INSTALL_BINDIR) must be just "bin"
40
41sub set_installdir
42{
43   my($path, $varname) = @_;
44   my $prefix_length = length($cmake_install_prefix);
45   if (($prefix_length > 0) && (index($path,$cmake_install_prefix) == 0))
46   {
47      # path is under the prefix, so remove the prefix and maybe following "/"
48      $path = substr($path, $prefix_length);
49      if(length($path) > 0)
50      {
51        my $char = substr($path, 0, 1);
52        if($char eq "/")
53        {
54          $path= substr($path, 1);
55        }
56      }
57      if(length($path) > 0)
58      {
59        $cmakeargs = $cmakeargs." -D".$varname."=".$path;
60      }
61   }
62}
63
64# CMake understands CC and CXX env.variables correctly, if they  contain 1 or 2 tokens
65# e.g CXX=g++ and CXX="ccache g++" are ok. However it could have a problem if there
66# (recognizing g++) with more tokens ,e.g CXX="ccache g++ --pipe".
67# The problem is simply fixed by splitting compiler and flags, e.g
68# CXX="ccache g++ --pipe" => CXX=ccache g++ CXXFLAGS=--pipe
69
70sub check_compiler
71{
72  my ($varname, $flagsvarname) = @_;
73  my @tokens = split(/ /,$ENV{$varname});
74  if($#tokens >= 2)
75  {
76    $ENV{$varname} = $tokens[0]." ".$tokens[1];
77    my $flags;
78
79    for(my $i=2; $i<=$#tokens; $i++)
80    {
81      $flags= $flags." ".$tokens[$i];
82    }
83    if(defined $ENV{$flagsvarname})
84    {
85      $flags = $flags." ".$ENV{$flagsvarname};
86    }
87    $ENV{$flagsvarname}=$flags;
88    print("$varname=$ENV{$varname}\n");
89    print("$flagsvarname=$ENV{$flagsvarname}\n");
90  }
91}
92
93check_compiler("CC", "CFLAGS");
94check_compiler("CXX", "CXXFLAGS");
95
96if(defined $ENV{"CXX"} and $ENV{"CXX"} =~ m/gcc/)
97{
98  my $old_cxx= $ENV{"CXX"};
99  $ENV{"CXX"} =~ s/gcc/g++/;
100  print("configure.pl : switching CXX compiler from $old_cxx to $ENV{CXX}\n");
101}
102
103if(defined $ENV{"CXXFLAGS"} and $ENV{"CXXFLAGS"} =~ "-fno-exceptions")
104{
105  $ENV{"CXXFLAGS"} =~ s/-fno-exceptions//;
106  print("configure.pl : stripping off -fno-exceptions CXXFLAGS=$ENV{CXXFLAGS}\n");
107}
108
109foreach my $option (@ARGV)
110{
111  if (substr ($option, 0, 2) eq "--")
112  {
113    $option = substr($option, 2);
114  }
115  else
116  {
117    # This must be environment variable
118    my @v  = split('=', $option);
119    my $name = shift(@v);
120    if(@v)
121    {
122      $ENV{$name} = join('=', @v);
123    }
124    next;
125  }
126  if($option =~ /srcdir/)
127  {
128    $srcdir = substr($option,7);
129    next;
130  }
131  if($option =~ /help/)
132  {
133    system("cmake ${srcdir} -LH");
134    exit(0);
135  }
136  if($option =~ /with-plugins=/)
137  {
138    my @plugins= split(/,/, substr($option,13));
139    foreach my $p (@plugins)
140    {
141      $p =~ s/-/_/g;
142      $cmakeargs = $cmakeargs." -DWITH_".uc($p)."=1";
143    }
144    next;
145  }
146  if($option =~ /with-extra-charsets=/)
147  {
148    my $charsets= substr($option,20);
149    $cmakeargs = $cmakeargs." -DWITH_EXTRA_CHARSETS=".$charsets;
150    next;
151  }
152  if($option =~ /without-plugin=/)
153  {
154    $cmakeargs = $cmakeargs." -DWITHOUT_".uc(substr($option,15))."=1";
155    next;
156  }
157  if($option =~ /with-zlib-dir=bundled/)
158  {
159    $cmakeargs = $cmakeargs." -DWITH_ZLIB=bundled";
160    next;
161  }
162  if($option =~ /with-zlib-dir=/)
163  {
164    $cmakeargs = $cmakeargs." -DWITH_ZLIB=system";
165    next;
166  }
167  if($option =~ /with-libevent=/)
168  {
169    $cmakeargs = $cmakeargs." -DWITH_LIBEVENT=system";
170    next;
171  }
172  if($option =~ /with-libevent/)
173  {
174    $cmakeargs = $cmakeargs." -DWITH_LIBEVENT=bundled";
175    next;
176  }
177  if($option =~ /with-ssl=/)
178  {
179    $cmakeargs = $cmakeargs." -DWITH_SSL=yes";
180    next;
181  }
182  if($option =~ /with-ssl/)
183  {
184    $cmakeargs = $cmakeargs." -DWITH_SSL=bundled";
185    next;
186  }
187  if($option =~ /prefix=/)
188  {
189    $cmake_install_prefix= substr($option, 7);
190    $cmakeargs = $cmakeargs." -DCMAKE_INSTALL_PREFIX=".$cmake_install_prefix;
191    next;
192  }
193  if($option =~/bindir=/)
194  {
195    set_installdir(substr($option,7), "INSTALL_BINDIR");
196    next;
197  }
198  if($option =~/libdir=/)
199  {
200    set_installdir(substr($option,7), "INSTALL_LIBDIR");
201    next;
202  }
203  if($option =~/libexecdir=/)
204  {
205    set_installdir(substr($option,11), "INSTALL_SBINDIR");
206    next;
207  }
208  if($option =~/includedir=/)
209  {
210    set_installdir(substr($option,11), "INSTALL_INCLUDEDIR");
211    next;
212  }
213  if ($option =~ /extra-charsets=all/)
214  {
215    $cmakeargs = $cmakeargs." -DWITH_CHARSETS=all";
216    next;
217  }
218  if ($option =~ /extra-charsets=complex/)
219  {
220    $cmakeargs = $cmakeargs." -DWITH_CHARSETS=complex";
221    next;
222  }
223  if ($option =~ /localstatedir=/)
224  {
225    $cmakeargs = $cmakeargs." -DMYSQL_DATADIR=".substr($option,14);
226    next;
227  }
228  if ($option =~ /with-comment=/)
229  {
230    $cmakeargs = $cmakeargs." \"-DWITH_COMMENT=".substr($option,13)."\"";
231    next;
232  }
233  if ($option =~ /layout=/)
234  {
235    $cmakeargs = $cmakeargs." -DINSTALL_LAYOUT=".substr($option,7);
236    next;
237  }
238  if ($option =~ /with-unix-socket-path=/)
239  {
240    $cmakeargs = $cmakeargs." -DMYSQL_UNIX_ADDR=".substr($option,22);
241    next;
242  }
243  if ($option =~ /mysql-maintainer-mode/)
244  {
245    $cmakeargs = $cmakeargs." -DMYSQL_MAINTAINER_MODE=" .
246                 ($option =~ /enable/ ? "1" : "0");
247    next;
248  }
249  if ($option =~ /with-comment=/)
250  {
251    $cmakeargs = $cmakeargs." \"-DWITH_COMMENT=".substr($option,13)."\"";
252    next;
253  }
254  if ($option =~ /with-gcov/)
255  {
256      $cmakeargs = $cmakeargs." -DENABLE_GCOV=ON";
257      next;
258  }
259  if ($option =~ /with-client-ldflags/)
260  {
261      print("configure.pl : ignoring $option\n");
262      next;
263  }
264  if ($option =~ /with-mysqld-ldflags=/)
265  {
266      print("configure.pl : ignoring $option\n");
267      next;
268  }
269
270  $option = uc($option);
271  $option =~ s/-/_/g;
272  $cmakeargs = $cmakeargs." -D".$option."=1";
273}
274
275print("configure.pl : calling cmake $srcdir $cmakeargs\n");
276unlink("CMakeCache.txt");
277my $rc = system("cmake $srcdir $cmakeargs");
278exit($rc);
279