1a4703828SSergey Avseyev#!/usr/bin/env perl
2e1ad8c50SMark Nunberg
3e1ad8c50SMark Nunberg# This script will eventually become the top-level configure directory ONCE
4e1ad8c50SMark Nunberg# we've completely migrated away from autotools
5e1ad8c50SMark Nunberg
6e1ad8c50SMark Nunberguse strict;
7e1ad8c50SMark Nunberguse warnings;
8e1ad8c50SMark Nunberguse Getopt::Long;
9e1ad8c50SMark Nunberguse File::Path;
1000236339SMark Nunberguse File::Basename qw(dirname);
11e1ad8c50SMark Nunberguse Cwd qw(getcwd realpath);
1200236339SMark Nunberguse FindBin qw($Bin $RealBin);
13e1ad8c50SMark Nunberg
14e1ad8c50SMark Nunberg# Format here is:
15e1ad8c50SMark Nunberg# [ Argument Name,
16e1ad8c50SMark Nunberg#   Option format string,
17e1ad8c50SMark Nunberg#   Option description,
18e1ad8c50SMark Nunberg#   Default format string,
19e1ad8c50SMark Nunberg#   Default value (ref)
20e1ad8c50SMark Nunberg# ]
21e1ad8c50SMark Nunberg
22e1ad8c50SMark Nunbergmy @HELP_DESCRIPTIONS = (
23e1ad8c50SMark Nunberg    [ "enable-debug", "",
24e1ad8c50SMark Nunberg        "Create a debug build",
25e1ad8c50SMark Nunberg        "%d", \(my $DEBUG = 0) ],
26e1ad8c50SMark Nunberg
27493aa4dbSSergey Avseyev    [ "enable-profiler", "",
28493aa4dbSSergey Avseyev        "Build with profiler support (gperftools)",
29493aa4dbSSergey Avseyev        "%d", \(my $USE_PROFILER = 0) ],
30493aa4dbSSergey Avseyev
31e1ad8c50SMark Nunberg    [ "with-ninja", "s",
32e1ad8c50SMark Nunberg        "Generate Ninja Makefiles",
33e1ad8c50SMark Nunberg        "%s", \(my $USE_NINJA = "") ],
34e1ad8c50SMark Nunberg
35e1ad8c50SMark Nunberg    [ "prefix", "s",
36e1ad8c50SMark Nunberg        "installation prefix",
37e1ad8c50SMark Nunberg        "%s", \(my $PREFIX = "/usr/local") ],
38e1ad8c50SMark Nunberg
39e1ad8c50SMark Nunberg    [ "disable-tools", "",
40e1ad8c50SMark Nunberg        "Don't build tools",
41e1ad8c50SMark Nunberg        "%d", \(my $NO_TOOLS = 0)],
42e1ad8c50SMark Nunberg
43e1ad8c50SMark Nunberg    [ "disable-tests", "",
44e1ad8c50SMark Nunberg        "Don't build tests",
45e1ad8c50SMark Nunberg        "%d", \(my $NO_TESTS = 0)],
46e1ad8c50SMark Nunberg
47e1ad8c50SMark Nunberg    [ "with-libuv", "s",
48e1ad8c50SMark Nunberg        "Path to LIBUV installation",
49e1ad8c50SMark Nunberg        "%s", \(my $LIBUV_ROOT = "")],
50e1ad8c50SMark Nunberg
51e1ad8c50SMark Nunberg    [ "with-libev", "s",
52e1ad8c50SMark Nunberg        "Path to LIBEV installation",
53e1ad8c50SMark Nunberg        "%s", \(my $LIBEV_ROOT = "")],
54e1ad8c50SMark Nunberg
55e1ad8c50SMark Nunberg    [ "with-libevent", "s",
56e1ad8c50SMark Nunberg        "Path to LIBEVENT installation",
57e1ad8c50SMark Nunberg        "%s", \(my $LIBEVENT_ROOT = "")],
58e1ad8c50SMark Nunberg
59e1ad8c50SMark Nunberg    [ "with-cmake", "s",
60e1ad8c50SMark Nunberg        "Executable to use as cmake",
61e1ad8c50SMark Nunberg        "%s", \(my $CMAKE_EXE = "cmake")],
62e1ad8c50SMark Nunberg
63378e2fa9SMark Nunberg    [ "with-openssl", "s",
64378e2fa9SMark Nunberg        "Path to OpenSSL installation",
65378e2fa9SMark Nunberg        "%s", \(my $OPENSSL_ROOT = "")],
66378e2fa9SMark Nunberg
67e1ad8c50SMark Nunberg    [ "disable-plugins", "",
68e1ad8c50SMark Nunberg        "Disable building of external plugins",
69e1ad8c50SMark Nunberg        "%d", \(my $NO_PLUGINS = 0)],
70e1ad8c50SMark Nunberg
71e1ad8c50SMark Nunberg    [ "disable-cxx", "",
72e1ad8c50SMark Nunberg        "Disable C++ targets",
73e1ad8c50SMark Nunberg        "%d", \(my $NO_CXX = 0) ],
7475532fc0SMark Nunberg    [ "disable-couchbasemock", "",
7575532fc0SMark Nunberg        "Don't run tests with Mock",
7675532fc0SMark Nunberg        "%d", \(my $NO_MOCK = 0) ],
77e1ad8c50SMark Nunberg    [ "enable-maintainer-mode", "",
78e1ad8c50SMark Nunberg        "Maintainer mode. Everything is strict",
79e1ad8c50SMark Nunberg        "%d", \(my $MAINTAINER_MODE = 0)],
80e1ad8c50SMark Nunberg
81e1ad8c50SMark Nunberg    [ "enable-fat-binary", "",
82e1ad8c50SMark Nunberg        "Build universal binaries on OS X",
83e1ad8c50SMark Nunberg        "%d", \(my $OSX_UNIVERSAL_BUILD = 0)],
84e1ad8c50SMark Nunberg
85e1ad8c50SMark Nunberg    [ "cmake-verbose", "",
86e1ad8c50SMark Nunberg        "Verbose CMake output",
87e1ad8c50SMark Nunberg        "%d", \(my $CMAKE_VERBOSE = 0)],
88e1ad8c50SMark Nunberg
896ccbb8abSMark Nunberg    [ "host", "s",
906ccbb8abSMark Nunberg        "Set host target for Win32 cross compilation",
916ccbb8abSMark Nunberg        "%s", \(my $CMAKE_HOST = "")],
926ccbb8abSMark Nunberg
93f1e0c2dbSMark Nunberg    [ "enable-static", "",
94f1e0c2dbSMark Nunberg        "Build static libraries",
95f1e0c2dbSMark Nunberg        "%d", \(my $STATIC_LIBS = 0)],
965f19b421SMark Nunberg    [ "enable-embedded-libevent-plugin", "",
975f19b421SMark Nunberg        "Embed the libevent plugin into the library's core",
985f19b421SMark Nunberg        "%d", \(my $EMBED_LIBEVENT_PLUGIN = 0) ],
99378e2fa9SMark Nunberg    [ "cc", "s",
100378e2fa9SMark Nunberg        "C Compiler",
101378e2fa9SMark Nunberg        "%s", \(my $CC = "") ],
102378e2fa9SMark Nunberg
103378e2fa9SMark Nunberg    [ "cxx", "s",
104378e2fa9SMark Nunberg        "C++ Compiler",
105378e2fa9SMark Nunberg        "%s", \(my $CXX = "") ],
106378e2fa9SMark Nunberg    [ "disable-ssl", "",
107378e2fa9SMark Nunberg        "Do not build SSL support",
108378e2fa9SMark Nunberg        "%d", \(my $NO_SSL = 0)],
1093b68ede6SMark Nunberg    [ "enable-asan", "",
1103b68ede6SMark Nunberg        "Enable LLVM AddressSanitizer support",
1113b68ede6SMark Nunberg        "%d", \(my $LCB_USE_ASAN = 0)],
112ad23b248SMark Nunberg    [ "enable-coverage", "",
113ad23b248SMark Nunberg        "Compile with code coverage instrumentation",
114ad23b248SMark Nunberg        "%d", \(my $LCB_USE_COVERAGE = 0)],
1150eb8dfe4SMark Nunberg    [ "enable-examples", "",
1160eb8dfe4SMark Nunberg        "Build examples",
1170eb8dfe4SMark Nunberg        "%s", \(my $BUILD_EXAMPLES = 0)],
11800236339SMark Nunberg    [ "build-dir", "s",
11900236339SMark Nunberg        "Directory to build in",
12000236339SMark Nunberg        "%s", \(my $BUILD_DIR = "build") ],
12132409ce1SSergey Avseyev    [ "skip-git-version", "",
12232409ce1SSergey Avseyev        "Skip version detection using git",
12332409ce1SSergey Avseyev        "%d", \(my $SKIP_GIT_VERSION = 0) ],
12432409ce1SSergey Avseyev
125e1ad8c50SMark Nunberg    [ "help", "",
126e1ad8c50SMark Nunberg        "This message", "%s", \(my $WANT_HELP = 0) ]
127e1ad8c50SMark Nunberg);
128e1ad8c50SMark Nunberg
129e1ad8c50SMark Nunbergmy @HELPLINES = ();
130e1ad8c50SMark Nunbergmy %GETOPT_ARGS = ();
131e1ad8c50SMark Nunberg
132e1ad8c50SMark Nunbergforeach my $optdesc (@HELP_DESCRIPTIONS) {
133e1ad8c50SMark Nunberg    my ($name, $vfmt, $desc, $deflfmt, $vref) = @$optdesc;
134e1ad8c50SMark Nunberg    my $go_key = $name;
135e1ad8c50SMark Nunberg    if ($vfmt) {
136e1ad8c50SMark Nunberg        $go_key .= "=$vfmt";
137e1ad8c50SMark Nunberg    }
138e1ad8c50SMark Nunberg    $GETOPT_ARGS{$go_key} = $vref;
139e1ad8c50SMark Nunberg}
140e1ad8c50SMark Nunberg
141e1ad8c50SMark NunbergGetOptions(%GETOPT_ARGS);
142e1ad8c50SMark Nunberg
143e1ad8c50SMark Nunbergif ($WANT_HELP) {
144e1ad8c50SMark Nunberg    printf("configure.pl ... options\n");
145e1ad8c50SMark Nunberg    foreach my $optdesc (@HELP_DESCRIPTIONS) {
146e1ad8c50SMark Nunberg        my ($name, $vfmt, $desc, $fmt, $deflref) = @$optdesc;
147e1ad8c50SMark Nunberg        printf("  --%-20s", $name);
148e1ad8c50SMark Nunberg        if (length($name) > 18) {
149e1ad8c50SMark Nunberg            printf("\n");
150e1ad8c50SMark Nunberg            printf("%-20s%s\n","", $desc);
151e1ad8c50SMark Nunberg        } else {
152e1ad8c50SMark Nunberg            printf("%s.", $desc);
153e1ad8c50SMark Nunberg        }
154e1ad8c50SMark Nunberg        printf(" Default=".$fmt."\n", ${$deflref});
155e1ad8c50SMark Nunberg    }
156e1ad8c50SMark Nunberg
157e1ad8c50SMark Nunberg    exit(0);
158e1ad8c50SMark Nunberg}
159e1ad8c50SMark Nunberg
160e1ad8c50SMark Nunbergmy $origdir = getcwd();
16100236339SMark Nunbergmy $srcdir = realpath("$RealBin/..");
16200236339SMark Nunberg
163e1ad8c50SMark Nunbergif ($origdir eq $srcdir) {
16400236339SMark Nunberg    printf("Detected in-source build. Making '$BUILD_DIR' directory\n");
16500236339SMark Nunberg    mkpath($BUILD_DIR);
16600236339SMark Nunberg    chdir($BUILD_DIR);
167e1ad8c50SMark Nunberg}
168e1ad8c50SMark Nunberg
169e1ad8c50SMark Nunbergif ($NO_CXX) {
170e1ad8c50SMark Nunberg    $NO_TESTS=1;
171e1ad8c50SMark Nunberg    $NO_TOOLS=1;
172e1ad8c50SMark Nunberg}
173e1ad8c50SMark Nunberg
174e1ad8c50SMark Nunbergmy @CM_OPTIONS = ();
175378e2fa9SMark Nunberg
177378e2fa9SMark Nunbergmy %ENVOPTIONS = ();
178378e2fa9SMark Nunbergforeach my $var (@VARNAMES) {
179378e2fa9SMark Nunberg    $ENVOPTIONS{$var} = $ENV{$var};
180378e2fa9SMark Nunberg}
181378e2fa9SMark Nunberg
182378e2fa9SMark Nunbergforeach my $arg (@ARGV) {
183378e2fa9SMark Nunberg    my ($k,$v) = $arg =~ m/([^=]+)=(.*)/;
184378e2fa9SMark Nunberg    unless ($k && $v && exists $ENVOPTIONS{$k}) {
185378e2fa9SMark Nunberg        die("Invalid variable $arg");
186378e2fa9SMark Nunberg    }
187378e2fa9SMark Nunberg    $ENVOPTIONS{$k} = $v;
188378e2fa9SMark Nunberg}
189378e2fa9SMark Nunberg
190378e2fa9SMark Nunbergmy $flags_common = "";
191378e2fa9SMark Nunbergmy $flags_cxx = "";
192378e2fa9SMark Nunbergmy $flags_cc = "";
193378e2fa9SMark Nunberg
194378e2fa9SMark Nunbergif ($ENVOPTIONS{CC}) { $CC = $ENVOPTIONS{CC}; }
195378e2fa9SMark Nunbergif ($ENVOPTIONS{CXX}) { $CXX = $ENVOPTIONS{CXX}; }
196378e2fa9SMark Nunbergif ($ENVOPTIONS{CPPFLAGS}) {
197378e2fa9SMark Nunberg    $flags_common = $ENVOPTIONS{CPPFLAGS};
198378e2fa9SMark Nunberg}
199378e2fa9SMark Nunberg$flags_cxx = $flags_common;
200378e2fa9SMark Nunberg$flags_cc = $flags_common;
201378e2fa9SMark Nunbergif ($ENVOPTIONS{CFLAGS}) {
202378e2fa9SMark Nunberg    $flags_cc .= " " . $ENVOPTIONS{CFLAGS};
203378e2fa9SMark Nunberg}
204378e2fa9SMark Nunbergif ($ENVOPTIONS{CXXFLAGS}) {
205378e2fa9SMark Nunberg    $flags_cxx .= " ".$ENVOPTIONS{CXXFLAGS};
206378e2fa9SMark Nunberg}
207378e2fa9SMark Nunbergif ($flags_cc) {
208378e2fa9SMark Nunberg    push @CM_OPTIONS, "-DCMAKE_C_FLAGS=$flags_cc";
209378e2fa9SMark Nunberg}
210378e2fa9SMark Nunbergif ($flags_cxx) {
211378e2fa9SMark Nunberg    push @CM_OPTIONS, "-DCMAKE_CXX_FLAGS=$flags_cxx";
212378e2fa9SMark Nunberg}
213378e2fa9SMark Nunbergif ($ENVOPTIONS{LDFLAGS}) {
214378e2fa9SMark Nunberg    # Flags for the linker
215378e2fa9SMark Nunberg    my $flags = $ENVOPTIONS{LDFLAGS};
216378e2fa9SMark Nunberg    foreach my $var (qw(EXE_LINKER_FLAGS SHARED_LINKER_FLAGS)) {
217378e2fa9SMark Nunberg        push @CM_OPTIONS, "-DCMAKE_${var}=$flags";
218378e2fa9SMark Nunberg    }
219378e2fa9SMark Nunberg}
220378e2fa9SMark Nunbergif ($ENVOPTIONS{LIBS}) {
221378e2fa9SMark Nunberg    warn("Don't know how to convert autotools 'LIBS' into CMake");
222378e2fa9SMark Nunberg}
223378e2fa9SMark Nunberg
224e1ad8c50SMark Nunbergif ($DEBUG) {
225e1ad8c50SMark Nunberg    push @CM_OPTIONS, "-DCMAKE_BUILD_TYPE=DEBUG";
226e1ad8c50SMark Nunberg} else {
227e1ad8c50SMark Nunberg    push @CM_OPTIONS, "-DCMAKE_BUILD_TYPE=RelWithDebInfo"
228e1ad8c50SMark Nunberg}
229e1ad8c50SMark Nunberg
230493aa4dbSSergey Avseyevif ($USE_PROFILER) {
231493aa4dbSSergey Avseyev    push @CM_OPTIONS, "-DLCB_USE_PROFILER=1";
232493aa4dbSSergey Avseyev}
233493aa4dbSSergey Avseyev
234e1ad8c50SMark Nunbergif ($PREFIX) {
235e1ad8c50SMark Nunberg    push @CM_OPTIONS, "-DCMAKE_INSTALL_PREFIX=$PREFIX";
236e1ad8c50SMark Nunberg}
237e1ad8c50SMark Nunbergif ($NO_TESTS) {
238e1ad8c50SMark Nunberg    push @CM_OPTIONS, "-DLCB_NO_TESTS=1";
239e1ad8c50SMark Nunberg}
240e1ad8c50SMark Nunbergif ($NO_TOOLS) {
241e1ad8c50SMark Nunberg    push @CM_OPTIONS, "-DLCB_NO_TOOLS=1";
242e1ad8c50SMark Nunberg}
243e1ad8c50SMark Nunberg
24432409ce1SSergey Avseyevif ($SKIP_GIT_VERSION) {
24532409ce1SSergey Avseyev    push @CM_OPTIONS, "-DLCB_SKIP_GIT_VERSION=1"
24632409ce1SSergey Avseyev}
24732409ce1SSergey Avseyev
248e1ad8c50SMark Nunbergif ($NO_PLUGINS) {
249e1ad8c50SMark Nunberg    push @CM_OPTIONS, "-DLCB_NO_PLUGINS=1";
250e1ad8c50SMark Nunberg}
251e1ad8c50SMark Nunbergif ($LIBUV_ROOT) {
252e1ad8c50SMark Nunberg    push @CM_OPTIONS, "-DLIBUV_ROOT=$LIBUV_ROOT";
253e1ad8c50SMark Nunberg}
254e1ad8c50SMark Nunbergif ($LIBEV_ROOT) {
255e1ad8c50SMark Nunberg    push @CM_OPTIONS, "-DLIBEV_ROOT=$LIBEV_ROOT";
256e1ad8c50SMark Nunberg}
257e1ad8c50SMark Nunbergif ($MAINTAINER_MODE) {
258e1ad8c50SMark Nunberg    push @CM_OPTIONS, "-DLCB_MAINTAINER_MODE=1";
259e1ad8c50SMark Nunberg}
260e1ad8c50SMark Nunbergif ($OSX_UNIVERSAL_BUILD) {
261e1ad8c50SMark Nunberg    push @CM_OPTIONS, "-DLCB_UNIVERSAL_BINARY=1";
262e1ad8c50SMark Nunberg}
263e1ad8c50SMark Nunbergif ($CMAKE_VERBOSE) {
264e1ad8c50SMark Nunberg    push @CM_OPTIONS, "--debug-output";
265e1ad8c50SMark Nunberg}
266f1e0c2dbSMark Nunbergif ($STATIC_LIBS) {
267f1e0c2dbSMark Nunberg    push @CM_OPTIONS, "-DBUILD_SHARED_LIBS=OFF", "-DLCB_BUILD_STATIC=1";
268f1e0c2dbSMark Nunberg}
269378e2fa9SMark Nunbergif ($CC) {
270378e2fa9SMark Nunberg    push @CM_OPTIONS, "-DCMAKE_C_COMPILER=$CC";
271378e2fa9SMark Nunberg}
272378e2fa9SMark Nunbergif ($CXX) {
273378e2fa9SMark Nunberg    push @CM_OPTIONS, "-DCMAKE_CXX_COMPILER=$CXX";
274378e2fa9SMark Nunberg}
275e1ad8c50SMark Nunbergif ($USE_NINJA) {
276e1ad8c50SMark Nunberg    push @CM_OPTIONS, "-DCMAKE_MAKE_PROGRAM=$USE_NINJA";
277e1ad8c50SMark Nunberg    push @CM_OPTIONS, "-GNinja";
2784d3eeec3SMark Nunberg} else {
2794d3eeec3SMark Nunberg    push @CM_OPTIONS, "-GUnix Makefiles";
280e1ad8c50SMark Nunberg}
281378e2fa9SMark Nunbergif ($NO_SSL) {
282378e2fa9SMark Nunberg    push @CM_OPTIONS, "-DLCB_NO_SSL=$NO_SSL";