1# Copyright (c) 2006, 2016, Oracle and/or its affiliates. All rights reserved.
2# Copyright (c) 2017, 2019, MariaDB Corporation.
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; version 2 of the License.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
16
17# This is the CMakeLists for InnoDB
18
19INCLUDE(CheckFunctionExists)
20INCLUDE(CheckCSourceCompiles)
21INCLUDE(CheckCSourceRuns)
22INCLUDE(lz4.cmake)
23INCLUDE(lzo.cmake)
24INCLUDE(lzma.cmake)
25INCLUDE(bzip2.cmake)
26INCLUDE(snappy.cmake)
27INCLUDE(numa)
28INCLUDE(TestBigEndian)
29
30MYSQL_CHECK_LZ4()
31MYSQL_CHECK_LZO()
32MYSQL_CHECK_LZMA()
33MYSQL_CHECK_BZIP2()
34MYSQL_CHECK_SNAPPY()
35MYSQL_CHECK_NUMA()
36
37INCLUDE(${MYSQL_CMAKE_SCRIPT_DIR}/compile_flags.cmake)
38
39IF(CMAKE_CROSSCOMPILING)
40  # Use CHECK_C_SOURCE_COMPILES instead of CHECK_C_SOURCE_RUNS when
41  # cross-compiling. Not as precise, but usually good enough.
42  # This only make sense for atomic tests in this file, this trick doesn't
43  # work in a general case.
44  MACRO(CHECK_C_SOURCE SOURCE VAR)
45    CHECK_C_SOURCE_COMPILES("${SOURCE}" "${VAR}")
46  ENDMACRO()
47ELSE()
48  MACRO(CHECK_C_SOURCE SOURCE VAR)
49    CHECK_C_SOURCE_RUNS("${SOURCE}" "${VAR}")
50  ENDMACRO()
51ENDIF()
52
53# OS tests
54IF(UNIX)
55  IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
56
57    ADD_DEFINITIONS("-DUNIV_LINUX -D_GNU_SOURCE=1")
58
59    CHECK_INCLUDE_FILES (libaio.h HAVE_LIBAIO_H)
60    CHECK_LIBRARY_EXISTS(aio io_queue_init "" HAVE_LIBAIO)
61
62    IF(HAVE_LIBAIO_H AND HAVE_LIBAIO)
63      ADD_DEFINITIONS(-DLINUX_NATIVE_AIO=1)
64      LINK_LIBRARIES(aio)
65    ENDIF()
66    IF(HAVE_LIBNUMA)
67      LINK_LIBRARIES(numa)
68    ENDIF()
69  ELSEIF(CMAKE_SYSTEM_NAME MATCHES "HP*")
70    ADD_DEFINITIONS("-DUNIV_HPUX")
71  ELSEIF(CMAKE_SYSTEM_NAME STREQUAL "AIX")
72    ADD_DEFINITIONS("-DUNIV_AIX")
73  ELSEIF(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
74    ADD_DEFINITIONS("-DUNIV_SOLARIS")
75  ENDIF()
76ENDIF()
77
78OPTION(INNODB_COMPILER_HINTS "Compile InnoDB with compiler hints" ON)
79MARK_AS_ADVANCED(INNODB_COMPILER_HINTS)
80
81IF(INNODB_COMPILER_HINTS)
82   ADD_DEFINITIONS("-DCOMPILER_HINTS")
83ENDIF()
84ADD_FEATURE_INFO(INNODB_COMPILER_HINTS INNODB_COMPILER_HINTS "InnoDB compiled with compiler hints")
85
86SET(MUTEXTYPE "event" CACHE STRING "Mutex type: event, sys or futex")
87
88IF(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
89# After: WL#5825 Using C++ Standard Library with MySQL code
90#       we no longer use -fno-exceptions
91#	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
92
93# Add -Wconversion if compiling with GCC
94## As of Mar 15 2011 this flag causes 3573+ warnings. If you are reading this
95## please fix them and enable the following code:
96#SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wconversion")
97
98  IF (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR
99      CMAKE_SYSTEM_PROCESSOR MATCHES "i386" AND
100      CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
101    INCLUDE(CheckCXXCompilerFlag)
102    CHECK_CXX_COMPILER_FLAG("-fno-builtin-memcmp" HAVE_NO_BUILTIN_MEMCMP)
103    IF (HAVE_NO_BUILTIN_MEMCMP)
104      # Work around http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052
105      SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/rem/rem0cmp.cc
106	PROPERTIES COMPILE_FLAGS -fno-builtin-memcmp)
107    ENDIF()
108  ENDIF()
109ENDIF()
110
111# Enable InnoDB's UNIV_DEBUG in debug builds
112SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DUNIV_DEBUG")
113
114OPTION(WITH_INNODB_AHI "Include innodb_adaptive_hash_index" ON)
115OPTION(WITH_INNODB_ROOT_GUESS "Cache index root block descriptors" ON)
116IF(WITH_INNODB_AHI)
117  ADD_DEFINITIONS(-DBTR_CUR_HASH_ADAPT -DBTR_CUR_ADAPT)
118  IF(NOT WITH_INNODB_ROOT_GUESS)
119    MESSAGE(WARNING "WITH_INNODB_AHI implies WITH_INNODB_ROOT_GUESS")
120    SET(WITH_INNODB_ROOT_GUESS ON)
121  ENDIF()
122ELSEIF(WITH_INNODB_ROOT_GUESS)
123  ADD_DEFINITIONS(-DBTR_CUR_ADAPT)
124ENDIF()
125ADD_FEATURE_INFO(INNODB_AHI WITH_INNODB_AHI "InnoDB Adaptive Hash Index")
126ADD_FEATURE_INFO(INNODB_ROOT_GUESS WITH_INNODB_ROOT_GUESS
127                 "Cache index root block descriptors in InnoDB")
128
129OPTION(WITH_INNODB_EXTRA_DEBUG "Enable extra InnoDB debug checks" OFF)
130IF(WITH_INNODB_EXTRA_DEBUG)
131  ADD_DEFINITIONS(-DUNIV_ZIP_DEBUG)
132ENDIF()
133ADD_FEATURE_INFO(INNODB_EXTRA_DEBUG WITH_INNODB_EXTRA_DEBUG "Extra InnoDB debug checks")
134
135
136CHECK_FUNCTION_EXISTS(sched_getcpu  HAVE_SCHED_GETCPU)
137IF(HAVE_SCHED_GETCPU)
138 ADD_DEFINITIONS(-DHAVE_SCHED_GETCPU=1)
139ENDIF()
140
141CHECK_FUNCTION_EXISTS(nanosleep HAVE_NANOSLEEP)
142IF(HAVE_NANOSLEEP)
143 ADD_DEFINITIONS(-DHAVE_NANOSLEEP=1)
144ENDIF()
145
146IF(HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE)
147 ADD_DEFINITIONS(-DHAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE=1)
148ENDIF()
149
150IF(NOT MSVC)
151  # Work around MDEV-18417, MDEV-18656, MDEV-18417
152  IF(WITH_ASAN AND CMAKE_COMPILER_IS_GNUCC AND
153     CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
154    SET_SOURCE_FILES_PROPERTIES(trx/trx0rec.cc PROPERTIES COMPILE_FLAGS -O1)
155  ENDIF()
156
157# Only use futexes on Linux if GCC atomics are available
158IF(NOT MSVC AND NOT CMAKE_CROSSCOMPILING)
159  CHECK_C_SOURCE_RUNS(
160  "
161  #include <stdio.h>
162  #include <unistd.h>
163  #include <errno.h>
164  #include <assert.h>
165  #include <linux/futex.h>
166  #include <unistd.h>
167  #include <sys/syscall.h>
168
169   int futex_wait(int* futex, int v) {
170	return(syscall(SYS_futex, futex, FUTEX_WAIT_PRIVATE, v, NULL, NULL, 0));
171   }
172
173   int futex_signal(int* futex) {
174	return(syscall(SYS_futex, futex, FUTEX_WAKE, 1, NULL, NULL, 0));
175   }
176
177  int main() {
178	int	ret;
179	int	m = 1;
180
181	/* It is setup to fail and return EWOULDBLOCK. */
182	ret = futex_wait(&m, 0);
183	assert(ret == -1 && errno == EWOULDBLOCK);
184	/* Shouldn't wake up any threads. */
185	assert(futex_signal(&m) == 0);
186
187	return(0);
188  }"
189  HAVE_IB_LINUX_FUTEX)
190ENDIF()
191
192IF(HAVE_IB_LINUX_FUTEX)
193  ADD_DEFINITIONS(-DHAVE_IB_LINUX_FUTEX=1)
194ENDIF()
195
196ENDIF(NOT MSVC)
197
198CHECK_FUNCTION_EXISTS(vasprintf  HAVE_VASPRINTF)
199
200SET(MUTEXTYPE "event" CACHE STRING "Mutex type: event, sys or futex")
201
202IF(MUTEXTYPE MATCHES "event")
203  ADD_DEFINITIONS(-DMUTEX_EVENT)
204ELSEIF(MUTEXTYPE MATCHES "futex" AND DEFINED HAVE_IB_LINUX_FUTEX)
205  ADD_DEFINITIONS(-DMUTEX_FUTEX)
206ELSE()
207   ADD_DEFINITIONS(-DMUTEX_SYS)
208ENDIF()
209
210OPTION(WITH_INNODB_DISALLOW_WRITES "InnoDB freeze writes patch from Google" ${WITH_WSREP})
211IF (WITH_INNODB_DISALLOW_WRITES)
212  ADD_DEFINITIONS(-DWITH_INNODB_DISALLOW_WRITES)
213ENDIF()
214ADD_FEATURE_INFO(INNODB_DISALLOW_WRITES WITH_INNODB_DISALLOW_WRITES "Expose innodb_disallow_writes switch to stop innodb from writing to disk")
215
216
217# Include directories under innobase
218INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/storage/innobase/include
219		    ${CMAKE_SOURCE_DIR}/storage/innobase/handler)
220
221# Sun Studio bug with -xO2
222IF(CMAKE_CXX_COMPILER_ID MATCHES "SunPro"
223	AND CMAKE_CXX_FLAGS_RELEASE MATCHES "O2"
224	AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
225	# Sun Studio 12 crashes with -xO2 flag, but not with higher optimization
226	# -xO3
227	SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/rem/rem0rec.cc
228    PROPERTIES COMPILE_FLAGS -xO3)
229ENDIF()
230
231# Avoid generating Hardware Capabilities due to crc32 instructions
232IF(CMAKE_SYSTEM_NAME MATCHES "SunOS" AND CMAKE_SYSTEM_PROCESSOR MATCHES "i386")
233  MY_CHECK_CXX_COMPILER_FLAG("-Wa,-nH")
234  IF(have_CXX__Wa__nH)
235    ADD_COMPILE_FLAGS(
236      ut/ut0crc32.cc
237      COMPILE_FLAGS "-Wa,-nH"
238    )
239  ENDIF()
240ENDIF()
241
242IF(MSVC)
243  # Avoid "unreferenced label" warning in generated file
244  GET_FILENAME_COMPONENT(_SRC_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
245  SET_SOURCE_FILES_PROPERTIES(${_SRC_DIR}/pars/pars0grm.c
246          PROPERTIES COMPILE_FLAGS "/wd4102")
247  SET_SOURCE_FILES_PROPERTIES(${_SRC_DIR}/pars/lexyy.c
248          PROPERTIES COMPILE_FLAGS "/wd4003")
249ENDIF()
250
251# Include directories under innobase
252INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/storage/innobase/include
253                    ${CMAKE_SOURCE_DIR}/storage/innobase/handler
254                    ${CMAKE_SOURCE_DIR}/libbinlogevents/include )
255