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