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()
36TEST_BIG_ENDIAN(IS_BIG_ENDIAN)
37
38INCLUDE(${MYSQL_CMAKE_SCRIPT_DIR}/compile_flags.cmake)
39
40IF(CMAKE_CROSSCOMPILING)
41  # Use CHECK_C_SOURCE_COMPILES instead of CHECK_C_SOURCE_RUNS when
42  # cross-compiling. Not as precise, but usually good enough.
43  # This only make sense for atomic tests in this file, this trick doesn't
44  # work in a general case.
45  MACRO(CHECK_C_SOURCE SOURCE VAR)
46    CHECK_C_SOURCE_COMPILES("${SOURCE}" "${VAR}")
47  ENDMACRO()
48ELSE()
49  MACRO(CHECK_C_SOURCE SOURCE VAR)
50    CHECK_C_SOURCE_RUNS("${SOURCE}" "${VAR}")
51  ENDMACRO()
52ENDIF()
53
54# OS tests
55IF(UNIX)
56  IF(CMAKE_SYSTEM_NAME STREQUAL "Linux")
57
58    ADD_DEFINITIONS("-DUNIV_LINUX -D_GNU_SOURCE=1")
59
60    CHECK_INCLUDE_FILES (libaio.h HAVE_LIBAIO_H)
61    CHECK_LIBRARY_EXISTS(aio io_queue_init "" HAVE_LIBAIO)
62
63    IF(HAVE_LIBAIO_H AND HAVE_LIBAIO)
64      ADD_DEFINITIONS(-DLINUX_NATIVE_AIO=1)
65      LINK_LIBRARIES(aio)
66    ENDIF()
67    IF(HAVE_LIBNUMA)
68      LINK_LIBRARIES(numa)
69    ENDIF()
70  ELSEIF(CMAKE_SYSTEM_NAME MATCHES "HP*")
71    ADD_DEFINITIONS("-DUNIV_HPUX")
72  ELSEIF(CMAKE_SYSTEM_NAME STREQUAL "AIX")
73    ADD_DEFINITIONS("-DUNIV_AIX")
74  ELSEIF(CMAKE_SYSTEM_NAME STREQUAL "SunOS")
75    ADD_DEFINITIONS("-DUNIV_SOLARIS")
76  ENDIF()
77ENDIF()
78
79OPTION(INNODB_COMPILER_HINTS "Compile InnoDB with compiler hints" ON)
80MARK_AS_ADVANCED(INNODB_COMPILER_HINTS)
81
82IF(INNODB_COMPILER_HINTS)
83   ADD_DEFINITIONS("-DCOMPILER_HINTS")
84ENDIF()
85ADD_FEATURE_INFO(INNODB_COMPILER_HINTS INNODB_COMPILER_HINTS "InnoDB compiled with compiler hints")
86
87SET(MUTEXTYPE "event" CACHE STRING "Mutex type: event, sys or futex")
88
89IF(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
90# After: WL#5825 Using C++ Standard Library with MySQL code
91#       we no longer use -fno-exceptions
92#	SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-exceptions")
93
94# Add -Wconversion if compiling with GCC
95## As of Mar 15 2011 this flag causes 3573+ warnings. If you are reading this
96## please fix them and enable the following code:
97#SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wconversion")
98
99  IF (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR
100      CMAKE_SYSTEM_PROCESSOR MATCHES "i386" AND
101      CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.6)
102    INCLUDE(CheckCXXCompilerFlag)
103    CHECK_CXX_COMPILER_FLAG("-fno-builtin-memcmp" HAVE_NO_BUILTIN_MEMCMP)
104    IF (HAVE_NO_BUILTIN_MEMCMP)
105      # Work around http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052
106      SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/rem/rem0cmp.cc
107	PROPERTIES COMPILE_FLAGS -fno-builtin-memcmp)
108    ENDIF()
109  ENDIF()
110ENDIF()
111
112# Enable InnoDB's UNIV_DEBUG in debug builds
113SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DUNIV_DEBUG")
114
115OPTION(WITH_INNODB_AHI "Include innodb_adaptive_hash_index" ON)
116OPTION(WITH_INNODB_ROOT_GUESS "Cache index root block descriptors" ON)
117IF(WITH_INNODB_AHI)
118  ADD_DEFINITIONS(-DBTR_CUR_HASH_ADAPT -DBTR_CUR_ADAPT)
119  IF(NOT WITH_INNODB_ROOT_GUESS)
120    MESSAGE(WARNING "WITH_INNODB_AHI implies WITH_INNODB_ROOT_GUESS")
121    SET(WITH_INNODB_ROOT_GUESS ON)
122  ENDIF()
123ELSEIF(WITH_INNODB_ROOT_GUESS)
124  ADD_DEFINITIONS(-DBTR_CUR_ADAPT)
125ENDIF()
126ADD_FEATURE_INFO(INNODB_AHI WITH_INNODB_AHI "InnoDB Adaptive Hash Index")
127ADD_FEATURE_INFO(INNODB_ROOT_GUESS WITH_INNODB_ROOT_GUESS
128                 "Cache index root block descriptors in InnoDB")
129
130OPTION(WITH_INNODB_BUG_ENDIAN_CRC32 "Weaken innodb_checksum_algorithm=crc32 by supporting upgrade from big-endian systems running 5.6/10.0/10.1" ${IS_BIG_ENDIAN})
131IF(WITH_INNODB_BUG_ENDIAN_CRC32)
132  ADD_DEFINITIONS(-DINNODB_BUG_ENDIAN_CRC32)
133ENDIF()
134ADD_FEATURE_INFO(INNODB_BUG_ENDIAN_CRC32 WITH_INNODB_BUG_ENDIAN_CRC32 "Big endian weaker CRC32 to support upgrade from big-endian systems running 5.6/10.0/10.1")
135
136OPTION(WITH_INNODB_EXTRA_DEBUG "Enable extra InnoDB debug checks" OFF)
137IF(WITH_INNODB_EXTRA_DEBUG)
138  ADD_DEFINITIONS(-DUNIV_ZIP_DEBUG)
139ENDIF()
140ADD_FEATURE_INFO(INNODB_EXTRA_DEBUG WITH_INNODB_EXTRA_DEBUG "Extra InnoDB debug checks")
141
142
143CHECK_FUNCTION_EXISTS(sched_getcpu  HAVE_SCHED_GETCPU)
144IF(HAVE_SCHED_GETCPU)
145 ADD_DEFINITIONS(-DHAVE_SCHED_GETCPU=1)
146ENDIF()
147
148CHECK_FUNCTION_EXISTS(nanosleep HAVE_NANOSLEEP)
149IF(HAVE_NANOSLEEP)
150 ADD_DEFINITIONS(-DHAVE_NANOSLEEP=1)
151ENDIF()
152
153IF(HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE)
154 ADD_DEFINITIONS(-DHAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE=1)
155ENDIF()
156
157IF(NOT MSVC)
158  # Work around MDEV-18417, MDEV-18656, MDEV-18417
159  IF(WITH_ASAN AND CMAKE_COMPILER_IS_GNUCC AND
160     CMAKE_C_COMPILER_VERSION VERSION_LESS "6.0.0")
161    SET_SOURCE_FILES_PROPERTIES(trx/trx0rec.cc PROPERTIES COMPILE_FLAGS -O1)
162  ENDIF()
163
164# Only use futexes on Linux if GCC atomics are available
165IF(NOT MSVC AND NOT CMAKE_CROSSCOMPILING)
166  CHECK_C_SOURCE_RUNS(
167  "
168  #include <stdio.h>
169  #include <unistd.h>
170  #include <errno.h>
171  #include <assert.h>
172  #include <linux/futex.h>
173  #include <unistd.h>
174  #include <sys/syscall.h>
175
176   int futex_wait(int* futex, int v) {
177	return(syscall(SYS_futex, futex, FUTEX_WAIT_PRIVATE, v, NULL, NULL, 0));
178   }
179
180   int futex_signal(int* futex) {
181	return(syscall(SYS_futex, futex, FUTEX_WAKE, 1, NULL, NULL, 0));
182   }
183
184  int main() {
185	int	ret;
186	int	m = 1;
187
188	/* It is setup to fail and return EWOULDBLOCK. */
189	ret = futex_wait(&m, 0);
190	assert(ret == -1 && errno == EWOULDBLOCK);
191	/* Shouldn't wake up any threads. */
192	assert(futex_signal(&m) == 0);
193
194	return(0);
195  }"
196  HAVE_IB_LINUX_FUTEX)
197ENDIF()
198
199IF(HAVE_IB_LINUX_FUTEX)
200  ADD_DEFINITIONS(-DHAVE_IB_LINUX_FUTEX=1)
201ENDIF()
202
203ENDIF(NOT MSVC)
204
205CHECK_FUNCTION_EXISTS(vasprintf  HAVE_VASPRINTF)
206
207SET(MUTEXTYPE "event" CACHE STRING "Mutex type: event, sys or futex")
208
209IF(MUTEXTYPE MATCHES "event")
210  ADD_DEFINITIONS(-DMUTEX_EVENT)
211ELSEIF(MUTEXTYPE MATCHES "futex" AND DEFINED HAVE_IB_LINUX_FUTEX)
212  ADD_DEFINITIONS(-DMUTEX_FUTEX)
213ELSE()
214   ADD_DEFINITIONS(-DMUTEX_SYS)
215ENDIF()
216
217OPTION(WITH_INNODB_DISALLOW_WRITES "InnoDB freeze writes patch from Google" ${WITH_WSREP})
218IF (WITH_INNODB_DISALLOW_WRITES)
219  ADD_DEFINITIONS(-DWITH_INNODB_DISALLOW_WRITES)
220ENDIF()
221ADD_FEATURE_INFO(INNODB_DISALLOW_WRITES WITH_INNODB_DISALLOW_WRITES "Expose innodb_disallow_writes switch to stop innodb from writing to disk")
222
223
224# Include directories under innobase
225INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/storage/innobase/include
226		    ${CMAKE_SOURCE_DIR}/storage/innobase/handler)
227
228# Sun Studio bug with -xO2
229IF(CMAKE_CXX_COMPILER_ID MATCHES "SunPro"
230	AND CMAKE_CXX_FLAGS_RELEASE MATCHES "O2"
231	AND NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
232	# Sun Studio 12 crashes with -xO2 flag, but not with higher optimization
233	# -xO3
234	SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/rem/rem0rec.cc
235    PROPERTIES COMPILE_FLAGS -xO3)
236ENDIF()
237
238# Avoid generating Hardware Capabilities due to crc32 instructions
239IF(CMAKE_SYSTEM_NAME MATCHES "SunOS" AND CMAKE_SYSTEM_PROCESSOR MATCHES "i386")
240  MY_CHECK_CXX_COMPILER_FLAG("-Wa,-nH")
241  IF(have_CXX__Wa__nH)
242    ADD_COMPILE_FLAGS(
243      ut/ut0crc32.cc
244      COMPILE_FLAGS "-Wa,-nH"
245    )
246  ENDIF()
247ENDIF()
248
249IF(MSVC)
250  # Avoid "unreferenced label" warning in generated file
251  GET_FILENAME_COMPONENT(_SRC_DIR ${CMAKE_CURRENT_LIST_FILE} PATH)
252  SET_SOURCE_FILES_PROPERTIES(${_SRC_DIR}/pars/pars0grm.c
253          PROPERTIES COMPILE_FLAGS "/wd4102")
254  SET_SOURCE_FILES_PROPERTIES(${_SRC_DIR}/pars/lexyy.c
255          PROPERTIES COMPILE_FLAGS "/wd4003")
256ENDIF()
257
258# Include directories under innobase
259INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/storage/innobase/include
260                    ${CMAKE_SOURCE_DIR}/storage/innobase/handler
261                    ${CMAKE_SOURCE_DIR}/libbinlogevents/include )
262