1#
2# This is the toplevel CMakeLists.txt that is used to build Couchbase
3#
4CMAKE_MINIMUM_REQUIRED(VERSION 3.12)
5
6STRING(REGEX REPLACE "[ ]" "a" modified_source_dir "${CMAKE_SOURCE_DIR}")
7IF (NOT modified_source_dir STREQUAL CMAKE_SOURCE_DIR)
8  MESSAGE(FATAL_ERROR "Couchbase cannot be built in a source directory containing a space character")
9ENDIF (NOT modified_source_dir STREQUAL CMAKE_SOURCE_DIR)
10
11STRING(REGEX REPLACE "[ ]" "a" modified_binary_dir "${CMAKE_BINARY_DIR}")
12IF (NOT modified_binary_dir STREQUAL CMAKE_BINARY_DIR)
13  MESSAGE(FATAL_ERROR "Couchbase cannot be built in a build directory containing a space character")
14ENDIF (NOT modified_binary_dir STREQUAL CMAKE_BINARY_DIR)
15
16# The PROJECT() macro sets it's own default build type, so need to specify
17# ours first.
18IF (NOT DEFINED CMAKE_BUILD_TYPE)
19   SET(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING
20       "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel."
21       FORCE)
22ENDIF ()
23
24# Include our custom build type (DebugOptimized) in the list.
25SET(CMAKE_BUILD_TYPE "${CMAKE_BUILD_TYPE}" CACHE STRING
26    "Choose the type of build, options are: Debug DebugOptimized Release RelWithDebInfo MinSizeRel."
27    FORCE)
28
29#
30# Choose deployment target on macOS.
31# As of Mad-Hatter, minimum supported is High Sierra (10.13).
32#
33SET(CMAKE_OSX_DEPLOYMENT_TARGET "10.13" CACHE STRING
34    "Minimum supported version of MacOS X")
35
36PROJECT("Couchbase")
37
38OPTION(COUCHBASE_KV_COMMIT_VALIDATION
39       "Only build modules used by KV commit validation"
40       OFF)
41
42# This option should be be enabled in the early phases of development
43# (e.g. when developing off master) and is used to guard additional
44# runtime checks which are desirable to report during testing; but
45# should not trigger in production.  As such this option should be set
46# to OFF before we move into the Beta/pre-release phase.
47#
48# Example usage - asserting that statistical counters are of an
49# expected range (don't underflow) - we would like to detect such
50# bugs; but they arn't serious enough to warrant crashing in a
51# customer environment.
52OPTION(CB_DEVELOPMENT_ASSERTS
53       "Enable additional asserts which abort execution if they fail."
54       OFF)
55
56# Always run with assertions for CV
57IF(COUCHBASE_KV_COMMIT_VALIDATION)
58    SET(CB_DEVELOPMENT_ASSERTS ON)
59ENDIF(COUCHBASE_KV_COMMIT_VALIDATION)
60
61IF (CB_DEVELOPMENT_ASSERTS)
62   MESSAGE(STATUS "Enabling CB_DEVELOPMENT_ASSERTS")
63   ADD_DEFINITIONS(-DCB_DEVELOPMENT_ASSERTS)
64ENDIF (CB_DEVELOPMENT_ASSERTS)
65
66IF ($ENV{JETBRAINS_IDE})
67   SET(JETBRAINS_CLION_IDE True CACHE BOOL "Build Couchbase under CLion")
68ENDIF ($ENV{JETBRAINS_IDE})
69
70IF (DEFINED JETBRAINS_CLION_IDE)
71   # CLion tries to autodetect all of the different configuration
72   # types and generate projects for them. That cause a race condition
73   # with the population pf the precompiled binaries we've got
74   # making an incomplete configuration. To work around that,
75   # let's just remove all of the other configurations and just
76   # build a debug build
77   SET(CMAKE_CONFIGURATION_TYPES Debug CACHE STRING "" FORCE)
78   ADD_DEFINITIONS(-DJETBRAINS_CLION_IDE)
79ENDIF (DEFINED JETBRAINS_CLION_IDE)
80
81IF (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
82  SET(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/install" CACHE STRING
83      "The install location" FORCE)
84  LIST(APPEND CMAKE_PREFIX_PATH "${CMAKE_INSTALL_PREFIX}")
85ENDIF (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
86
87LIST (APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/tlm/cmake/Modules/")
88SET (COUCHBASE_SERVER_BUILD True CACHE BOOL "Indicating this is a server build")
89
90# Try to download the prebuilt 3rd-party dependencies by default
91IF (NOT DEFINED CB_DOWNLOAD_DEPS)
92   SET(CB_DOWNLOAD_DEPS True
93       CACHE BOOL "Download prebuilt dependencies by default")
94ENDIF (NOT DEFINED CB_DOWNLOAD_DEPS)
95
96# Have to add deps first, before any other part of the build tries to
97# FIND anything
98ADD_SUBDIRECTORY(tlm/deps)
99
100INCLUDE(CouchbaseDefaultValues)
101
102# If the user has synced any closed-source code, assume building EE
103IF (EXISTS "${PROJECT_SOURCE_DIR}/analytics")
104  SET (_ee_found ON)
105ELSE ()
106  SET (_ee_found OFF)
107ENDIF()
108OPTION (BUILD_ENTERPRISE "Building Enterprise Edition" ${_ee_found})
109
110# Build 'developer builds' by default
111OPTION (CB_DEVELOPER_BUILD "Produce Developer builds" ON)
112
113# 'Production builds' are those built by the official Couchbase build jobs
114OPTION (CB_PRODUCTION_BUILD "Official production build" OFF)
115
116# Add option to build tools only
117SET(BUILD_ONLY_TOOLS False)
118IF ($ENV{CB_TOOLS_ONLY})
119  SET(BUILD_ONLY_TOOLS True)
120ENDIF ($ENV{CB_TOOLS_ONLY})
121
122MESSAGE(STATUS "Using cmake version: ${CMAKE_VERSION}")
123MESSAGE(STATUS "Installing to ${CMAKE_INSTALL_PREFIX}")
124MESSAGE(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
125
126INCLUDE(CouchbaseCompilerOptions)
127INCLUDE(CouchbaseSanitizers)
128INCLUDE(CouchbaseAsciiDocs)
129INCLUDE(CouchbaseMemoryAllocator)
130INCLUDE(CouchbaseCompression)
131
132INCLUDE(FindCouchbaseBoost)
133INCLUDE(FindCouchbaseFolly)
134INCLUDE(FindCouchbaseOpenSSL)
135INCLUDE(FindCouchbaseLibevent)
136INCLUDE(FindCouchbaseCurl)
137INCLUDE(FindCouchbaseLua)
138INCLUDE(FindCouchbasePythonInterp)
139INCLUDE(FindCouchbaseErlang)
140INCLUDE(FindCouchbaseDtrace)
141INCLUDE(FindCouchbaseGo)
142INCLUDE(FindCouchbaseBreakpad)
143INCLUDE(FindCouchbaseFlatbuffers)
144INCLUDE(FindCouchbaseValgrind)
145INCLUDE(FindCouchbaseV8)
146INCLUDE(FindCouchbaseIcu)
147INCLUDE(FindCouchbaseNlohmannJson)
148INCLUDE(FindCouchbaseRocksDB)
149INCLUDE(FindCouchbaseOpentracing)
150INCLUDE(FindCouchbasePCRE)
151
152if (BUILD_ENTERPRISE)
153    # libuv is currently only used by the enterpise builds
154    include(FindCouchbaseLibuv)
155endif()
156
157# Declare PRODUCT_VERSION in cache, but then override user-defined value
158# if it is an empty string
159SET (PRODUCT_VERSION "" CACHE STRING "Product version with build number")
160IF ("${PRODUCT_VERSION}" STREQUAL "")
161  SET (_product_version)
162  IF (PYTHON_EXECUTABLE)
163    # Try to determine product version from manifest
164    EXECUTE_PROCESS(COMMAND ${PYTHON_EXECUTABLE} tlm/scripts/get_version.py
165      WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
166      OUTPUT_VARIABLE _product_version
167      ERROR_QUIET
168      OUTPUT_STRIP_TRAILING_WHITESPACE)
169  ENDIF ()
170  IF (NOT _product_version)
171    SET (_product_version "0.0.0-9999")
172  ENDIF ()
173  SET (PRODUCT_VERSION ${_product_version} CACHE STRING "Product version with build number" FORCE)
174ENDIF ("${PRODUCT_VERSION}" STREQUAL "")
175
176IF (BUILD_ENTERPRISE)
177  MESSAGE (STATUS "Building Couchbase Server ${PRODUCT_VERSION} Enterprise Edition")
178  ADD_DEFINITIONS(-DCOUCHBASE_ENTERPRISE_EDITION=1)
179ELSE ()
180  MESSAGE (STATUS "Building Couchbase Server ${PRODUCT_VERSION} Community Edition")
181ENDIF ()
182
183IF (WIN32)
184   SET(COUCHBASE_NETWORK_LIBS "Ws2_32")
185ELSEIF ("${CMAKE_SYSTEM_NAME}" STREQUAL "SunOS")
186   SET(COUCHBASE_NETWORK_LIBS socket nsl)
187ENDIF (WIN32)
188MESSAGE(STATUS "Linking with network libraries: ${COUCHBASE_NETWORK_LIBS}")
189
190IF (NOT WIN32)
191   SET(COUCHBASE_MATH_LIBS m)
192ENDIF(NOT WIN32)
193
194INCLUDE(CouchbaseCodeCoverage)
195
196INCLUDE(TestBigEndian)
197TEST_BIG_ENDIAN(WORDS_BIG_ENDIAN)
198IF (WORDS_BIG_ENDIAN)
199  MESSAGE(STATUS "Builing on big endian system")
200  ADD_DEFINITIONS(-DWORDS_BIGENDIAN=1)
201ENDIF(WORDS_BIG_ENDIAN)
202
203ENABLE_TESTING()
204
205IF (NOT BUILD_ONLY_TOOLS)
206  INCLUDE_DIRECTORIES(BEFORE ${CMAKE_BINARY_DIR}/platform/include)
207  INCLUDE_DIRECTORIES(AFTER SYSTEM ${NLOHMANN_JSON_INCLUDE_DIR})
208ENDIF()
209
210# Add a quick (and safe) way to nuke everything
211ADD_CUSTOM_TARGET(reset
212                  COMMAND repo forall -c "git clean -dfx"
213                  VERBATIM)
214
215SET (gtest_force_shared_crt ON CACHE BOOL
216     "Use shared (DLL) run-time lib even when Google Test is built as static lib.")
217ADD_SUBDIRECTORY(googletest EXCLUDE_FROM_ALL)
218
219ADD_SUBDIRECTORY(benchmark EXCLUDE_FROM_ALL)
220
221IF (NOT BUILD_ONLY_TOOLS)
222  ADD_SUBDIRECTORY(tlm/python)
223  ADD_SUBDIRECTORY(phosphor)
224ENDIF()
225
226OPTION(PHOSPHOR_DISABLE "Disable compilation of Phosphor trace points into components" OFF)
227
228IF (PHOSPHOR_DISABLE)
229    ADD_DEFINITIONS(-DPHOSPHOR_DISABLED=1)
230    SET(PHOSPHOR_LIB "")
231ENDIF (PHOSPHOR_DISABLE)
232
233ADD_SUBDIRECTORY(third_party EXCLUDE_FROM_ALL)
234
235IF (NOT BUILD_ONLY_TOOLS)
236  ADD_SUBDIRECTORY(platform)
237  ADD_SUBDIRECTORY(subjson)
238  ADD_SUBDIRECTORY(kv_engine/include)
239  ADD_SUBDIRECTORY(couchstore)
240  IF (BUILD_ENTERPRISE AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/magma")
241    ADD_SUBDIRECTORY(magma)
242  ENDIF()
243ENDIF()
244
245IF (NOT COUCHBASE_KV_COMMIT_VALIDATION)
246    ADD_SUBDIRECTORY(forestdb)
247    SET (FORESTDB_INCLUDE_DIR "${ForestDB_SOURCE_DIR}/include")
248    SET(FORESTDB_LIBRARY_DIR "${ForestDB_BINARY_DIR}")
249    SET (FORESTDB_TARGET forestdb)
250ENDIF()
251
252IF (NOT BUILD_ONLY_TOOLS)
253  ADD_SUBDIRECTORY(kv_engine)
254  ADD_SUBDIRECTORY(sigar)
255ENDIF()
256
257IF (NOT BUILD_ONLY_TOOLS)
258  ADD_SUBDIRECTORY(product-texts/couchbase-server)
259ENDIF()
260
261IF (NOT COUCHBASE_KV_COMMIT_VALIDATION)
262    IF (NOT BUILD_ONLY_TOOLS)
263      ADD_SUBDIRECTORY(couchbase-cli)
264      ADD_SUBDIRECTORY(couchdb)
265      ADD_SUBDIRECTORY(geocouch)
266      ADD_SUBDIRECTORY(query-ui)
267    ENDIF()
268
269    SET (GODEPSDIR "${CMAKE_CURRENT_SOURCE_DIR}/godeps")
270    IF (NOT BUILD_ONLY_TOOLS)
271      ADD_SUBDIRECTORY(ns_server ${CMAKE_CURRENT_SOURCE_DIR}/ns_server/build)
272
273      ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/query)
274      ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/indexing)
275      ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/goxdcr)
276      ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/gometa)
277      ADD_SUBDIRECTORY(godeps/src/github.com/couchbase/cbauth)
278
279      ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/cbft)
280      ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/docloader)
281      ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/mobile-service)
282
283      # Analytics CBGT connector
284      IF (BUILD_ENTERPRISE AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/goproj/src/github.com/couchbase/cbas")
285        ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/cbas)
286      ENDIF ()
287
288      # Analytics Maven project
289      IF (BUILD_ENTERPRISE AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/analytics/CMakeLists.txt")
290        ADD_SUBDIRECTORY (analytics)
291      ENDIF ()
292
293      # Analytics UI
294      IF (BUILD_ENTERPRISE AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/cbas-ui")
295        ADD_SUBDIRECTORY (cbas-ui)
296      ENDIF ()
297    ENDIF()
298
299    IF (EXISTS "${PROJECT_SOURCE_DIR}/goproj/src/github.com/couchbase/backup")
300      ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/backup)
301    ENDIF()
302
303    IF (BUILD_ENTERPRISE AND EXISTS "${PROJECT_SOURCE_DIR}/goproj/src/github.com/couchbase/plasma" AND NOT BUILD_ONLY_TOOLS)
304      ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/plasma)
305    ENDIF()
306
307    IF (BUILD_ENTERPRISE AND EXISTS "${PROJECT_SOURCE_DIR}/libcouchbase")
308      SET (LCB_NO_PLUGINS ON CACHE BOOL "Don't build plugins" FORCE)
309      SET (LCB_NO_TESTS ON CACHE BOOL "Don't run tests" FORCE)
310      SET (LCB_BUILD_DTRACE OFF CACHE BOOL "Turn off tracing" FORCE)
311      SET (LCB_INSTALL_HEADERS OFF CACHE BOOL "Don't install headers" FORCE)
312      SET (LCB_INSTALL_PKGCONFIG OFF CACHE BOOL "Don't install libcouchbase.pc" FORCE)
313      SET (CMAKE_INSTALL_LIBDIR "lib" CACHE STRING "Override libdir to violate FHS" FORCE)
314      ADD_SUBDIRECTORY(libcouchbase)
315    ENDIF()
316
317    IF (NOT BUILD_ONLY_TOOLS)
318      IF (BUILD_ENTERPRISE)
319        ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/eventing)
320        ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/eventing-ee)
321      ENDIF()
322
323      IF (BUILD_ENTERPRISE AND EXISTS "${PROJECT_SOURCE_DIR}/goproj/src/github.com/couchbase/cbsummary")
324        ADD_SUBDIRECTORY(goproj/src/github.com/couchbase/cbsummary)
325      ENDIF()
326    ENDIF ()
327
328    IF (EXISTS "${PROJECT_SOURCE_DIR}/voltron/CMakeLists.txt")
329      ADD_SUBDIRECTORY(voltron)
330    ENDIF ()
331ENDIF (NOT COUCHBASE_KV_COMMIT_VALIDATION)
332
333IF (NOT WIN32)
334   CONFIGURE_FILE(tlm/memcached-wrapper.in ${CMAKE_BINARY_DIR}/tlm/memcached-wrapper)
335ENDIF (NOT WIN32)
336