1# Copyright (c) 2006, 2020, Oracle and/or its affiliates. All rights reserved.
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License, version 2.0,
5# as published by the Free Software Foundation.
6#
7# This program is also distributed with certain software (including
8# but not limited to OpenSSL) that is licensed under separate terms,
9# as designated in a particular file or component or in included license
10# documentation.  The authors of MySQL hereby grant you an additional
11# permission to link the program and your derivative works with the
12# separately licensed software that they have included with MySQL.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License, version 2.0, for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
22
23# This is the CMakeLists for InnoDB
24
25INCLUDE(CheckFunctionExists)
26INCLUDE(CheckCSourceCompiles)
27INCLUDE(CheckCSourceRuns)
28
29IF(LZ4_INCLUDE_DIR AND LZ4_LIBRARY)
30  ADD_DEFINITIONS(-DHAVE_LZ4=1)
31  INCLUDE_DIRECTORIES(${LZ4_INCLUDE_DIR})
32ENDIF()
33
34# OS tests
35IF(UNIX AND NOT IGNORE_AIO_CHECK)
36  IF(LINUX)
37
38    ADD_DEFINITIONS("-DUNIV_LINUX")
39
40    CHECK_INCLUDE_FILES (libaio.h HAVE_LIBAIO_H)
41    CHECK_LIBRARY_EXISTS(aio io_queue_init "" HAVE_LIBAIO)
42
43    IF(HAVE_LIBAIO_H AND HAVE_LIBAIO)
44      ADD_DEFINITIONS(-DLINUX_NATIVE_AIO=1)
45      LINK_LIBRARIES(aio)
46    ENDIF()
47
48  ELSEIF(SOLARIS)
49    ADD_DEFINITIONS("-DUNIV_SOLARIS")
50  ENDIF()
51ENDIF()
52
53OPTION(INNODB_COMPILER_HINTS "Compile InnoDB with compiler hints" ON)
54MARK_AS_ADVANCED(INNODB_COMPILER_HINTS)
55
56IF(INNODB_COMPILER_HINTS)
57   ADD_DEFINITIONS("-DCOMPILER_HINTS")
58ENDIF()
59
60SET(MUTEXTYPE "event" CACHE STRING "Mutex type: event, sys or futex")
61
62IF(MY_COMPILER_IS_GNU_OR_CLANG)
63  # Turn off unused parameter warnings.
64  STRING_APPEND(CMAKE_CXX_FLAGS " -Wno-unused-parameter")
65  # Turn off warnings about implicit casting away const.
66  STRING_APPEND(CMAKE_CXX_FLAGS " -Wno-cast-qual")
67ENDIF()
68
69IF(MY_COMPILER_IS_GNU)
70  # Add -Wconversion if compiling with GCC
71  ## As of Mar 15 2011 this flag causes 3573+ warnings. If you are reading this
72  ## please fix them and enable the following code:
73  #SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wconversion")
74
75  IF (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64" OR
76      CMAKE_SYSTEM_PROCESSOR MATCHES "i386")
77    INCLUDE(CheckCXXCompilerFlag)
78    CHECK_CXX_COMPILER_FLAG("-fno-builtin-memcmp" HAVE_NO_BUILTIN_MEMCMP)
79    IF (HAVE_NO_BUILTIN_MEMCMP)
80      # Work around http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43052
81      SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/rem/rem0cmp.cc
82	PROPERTIES COMPILE_FLAGS -fno-builtin-memcmp)
83    ENDIF()
84  ENDIF()
85ENDIF()
86
87# Enable InnoDB's UNIV_DEBUG in debug builds
88SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DUNIV_DEBUG")
89
90OPTION(WITH_INNODB_EXTRA_DEBUG "Enable extra InnoDB debug checks" OFF)
91IF(WITH_INNODB_EXTRA_DEBUG)
92  IF(NOT WITH_DEBUG)
93    MESSAGE(FATAL_ERROR "WITH_INNODB_EXTRA_DEBUG can be enabled only when WITH_DEBUG is enabled")
94  ENDIF()
95
96  SET(EXTRA_DEBUG_FLAGS "")
97  SET(EXTRA_DEBUG_FLAGS "${EXTRA_DEBUG_FLAGS} -DUNIV_AHI_DEBUG")
98  SET(EXTRA_DEBUG_FLAGS "${EXTRA_DEBUG_FLAGS} -DUNIV_DDL_DEBUG")
99  SET(EXTRA_DEBUG_FLAGS "${EXTRA_DEBUG_FLAGS} -DUNIV_DEBUG_FILE_ACCESSES")
100  SET(EXTRA_DEBUG_FLAGS "${EXTRA_DEBUG_FLAGS} -DUNIV_ZIP_DEBUG")
101
102  SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${EXTRA_DEBUG_FLAGS}")
103  SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${EXTRA_DEBUG_FLAGS}")
104ENDIF()
105
106CHECK_FUNCTION_EXISTS(sched_getcpu  HAVE_SCHED_GETCPU)
107IF(HAVE_SCHED_GETCPU)
108 ADD_DEFINITIONS(-DHAVE_SCHED_GETCPU=1)
109ENDIF()
110
111CHECK_FUNCTION_EXISTS(nanosleep HAVE_NANOSLEEP)
112IF(HAVE_NANOSLEEP)
113 ADD_DEFINITIONS(-DHAVE_NANOSLEEP=1)
114ENDIF()
115
116IF(NOT MSVC)
117  CHECK_C_SOURCE_RUNS(
118  "
119  #ifndef _GNU_SOURCE
120  #define _GNU_SOURCE
121  #endif
122  #include <fcntl.h>
123  #include <linux/falloc.h>
124  int main()
125  {
126    /* Ignore the return value for now. Check if the flags exist.
127    The return value is checked  at runtime. */
128    fallocate(0, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, 0, 0);
129
130    return(0);
131  }"
132  HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE
133  )
134  CHECK_C_SOURCE_COMPILES(
135  "
136  #ifndef _GNU_SOURCE
137  #define _GNU_SOURCE
138  #endif
139  #include <fcntl.h>
140  #include <linux/falloc.h>
141  int main()
142  {
143    /* Ignore the return value for now. Check if the flags exist.
144    The return value is checked  at runtime. */
145    fallocate(0, FALLOC_FL_ZERO_RANGE, 0, 0);
146
147    return(0);
148  }"
149  HAVE_FALLOC_FL_ZERO_RANGE
150  )
151ENDIF()
152
153IF(HAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE)
154 ADD_DEFINITIONS(-DHAVE_FALLOC_PUNCH_HOLE_AND_KEEP_SIZE=1)
155ENDIF()
156
157IF(HAVE_FALLOC_FL_ZERO_RANGE)
158 ADD_DEFINITIONS(-DHAVE_FALLOC_FL_ZERO_RANGE=1)
159ENDIF()
160
161IF(NOT MSVC)
162IF(NOT CMAKE_CROSSCOMPILING)
163  CHECK_C_SOURCE_RUNS(
164  "#include<stdint.h>
165  int main()
166  {
167    __sync_synchronize();
168    return(0);
169  }"
170  HAVE_IB_GCC_SYNC_SYNCHRONISE
171  )
172  CHECK_C_SOURCE_RUNS(
173  "#include<stdint.h>
174  int main()
175  {
176    __atomic_thread_fence(__ATOMIC_ACQUIRE);
177    __atomic_thread_fence(__ATOMIC_RELEASE);
178    return(0);
179  }"
180  HAVE_IB_GCC_ATOMIC_THREAD_FENCE
181  )
182  CHECK_C_SOURCE_RUNS(
183  "#include<stdint.h>
184  int main()
185  {
186    unsigned char	a = 0;
187    unsigned char	b = 0;
188    unsigned char	c = 1;
189
190    __atomic_exchange(&a, &b,  &c, __ATOMIC_RELEASE);
191    __atomic_compare_exchange(&a, &b, &c, 0,
192			      __ATOMIC_RELEASE, __ATOMIC_ACQUIRE);
193    return(0);
194  }"
195  HAVE_IB_GCC_ATOMIC_COMPARE_EXCHANGE
196  )
197ENDIF()
198
199IF(HAVE_IB_GCC_SYNC_SYNCHRONISE)
200 ADD_DEFINITIONS(-DHAVE_IB_GCC_SYNC_SYNCHRONISE=1)
201ENDIF()
202
203IF(HAVE_IB_GCC_ATOMIC_THREAD_FENCE)
204 ADD_DEFINITIONS(-DHAVE_IB_GCC_ATOMIC_THREAD_FENCE=1)
205ENDIF()
206
207IF(HAVE_IB_GCC_ATOMIC_COMPARE_EXCHANGE)
208 ADD_DEFINITIONS(-DHAVE_IB_GCC_ATOMIC_COMPARE_EXCHANGE=1)
209ENDIF()
210
211 # either define HAVE_IB_ATOMIC_PTHREAD_T_GCC or not
212IF(NOT CMAKE_CROSSCOMPILING)
213  CHECK_C_SOURCE_RUNS(
214  "
215  #include <pthread.h>
216  #include <string.h>
217
218  int main() {
219    pthread_t       x1;
220    pthread_t       x2;
221    pthread_t       x3;
222
223    memset(&x1, 0x0, sizeof(x1));
224    memset(&x2, 0x0, sizeof(x2));
225    memset(&x3, 0x0, sizeof(x3));
226
227    __sync_bool_compare_and_swap(&x1, x2, x3);
228
229    return(0);
230  }"
231  HAVE_IB_ATOMIC_PTHREAD_T_GCC)
232ENDIF()
233IF(HAVE_IB_ATOMIC_PTHREAD_T_GCC)
234  ADD_DEFINITIONS(-DHAVE_IB_ATOMIC_PTHREAD_T_GCC=1)
235ENDIF()
236
237# Only use futexes on Linux if GCC atomics are available
238IF(NOT MSVC AND NOT CMAKE_CROSSCOMPILING)
239  CHECK_C_SOURCE_RUNS(
240  "
241  #include <stdio.h>
242  #include <unistd.h>
243  #include <errno.h>
244  #include <assert.h>
245  #include <linux/futex.h>
246  #include <unistd.h>
247  #include <sys/syscall.h>
248
249   int futex_wait(int* futex, int v) {
250	return(syscall(SYS_futex, futex, FUTEX_WAIT_PRIVATE, v, NULL, NULL, 0));
251   }
252
253   int futex_signal(int* futex) {
254	return(syscall(SYS_futex, futex, FUTEX_WAKE, 1, NULL, NULL, 0));
255   }
256
257  int main() {
258	int	ret;
259	int	m = 1;
260
261	/* It is setup to fail and return EWOULDBLOCK. */
262	ret = futex_wait(&m, 0);
263	assert(ret == -1 && errno == EWOULDBLOCK);
264	/* Shouldn't wake up any threads. */
265	assert(futex_signal(&m) == 0);
266
267	return(0);
268  }"
269  HAVE_IB_LINUX_FUTEX)
270ENDIF()
271IF(HAVE_IB_LINUX_FUTEX)
272  ADD_DEFINITIONS(-DHAVE_IB_LINUX_FUTEX=1)
273ENDIF()
274
275ENDIF(NOT MSVC)
276
277# Solaris atomics
278IF(SOLARIS)
279  IF(NOT CMAKE_CROSSCOMPILING)
280  CHECK_C_SOURCE_COMPILES(
281  "#include <mbarrier.h>
282  int main() {
283    __machine_r_barrier();
284    __machine_w_barrier();
285    return(0);
286  }"
287  HAVE_IB_MACHINE_BARRIER_SOLARIS)
288  ENDIF()
289  IF(HAVE_IB_MACHINE_BARRIER_SOLARIS)
290    ADD_DEFINITIONS(-DHAVE_IB_MACHINE_BARRIER_SOLARIS=1)
291  ENDIF()
292ENDIF()
293
294IF(MSVC)
295  ADD_DEFINITIONS(-DHAVE_WINDOWS_MM_FENCE)
296ENDIF()
297
298IF(MUTEXTYPE MATCHES "event")
299  ADD_DEFINITIONS(-DMUTEX_EVENT)
300ELSEIF(MUTEXTYPE MATCHES "futex" AND DEFINED HAVE_IB_LINUX_FUTEX)
301  ADD_DEFINITIONS(-DMUTEX_FUTEX)
302ELSE()
303   ADD_DEFINITIONS(-DMUTEX_SYS)
304ENDIF()
305
306# Include directories under innobase
307INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/storage/innobase/
308		    ${CMAKE_SOURCE_DIR}/storage/innobase/include
309		    ${CMAKE_SOURCE_DIR}/storage/innobase/handler
310                    )
311