File: compiler.cmake

package info (click to toggle)
tarantool 2.6.0-1.4
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 85,412 kB
  • sloc: ansic: 513,775; cpp: 69,493; sh: 25,650; python: 19,190; perl: 14,973; makefile: 4,178; yacc: 1,329; sql: 1,074; pascal: 620; ruby: 190; awk: 18; lisp: 7
file content (420 lines) | stat: -rw-r--r-- 15,953 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#
# Check if the same compile family is used for both C and CXX
#
if (NOT (CMAKE_C_COMPILER_ID STREQUAL CMAKE_CXX_COMPILER_ID))
    message(WARNING "CMAKE_C_COMPILER_ID (${CMAKE_C_COMPILER_ID}) is different "
                    "from CMAKE_CXX_COMPILER_ID (${CMAKE_CXX_COMPILER_ID})."
                    "The final binary may be unusable.")
endif()

# We support building with Clang and gcc. First check 
# what we're using for build.
#
if (CMAKE_C_COMPILER_ID STREQUAL Clang)
    set(CMAKE_COMPILER_IS_CLANG  ON)
    set(CMAKE_COMPILER_IS_GNUCC  OFF)
    set(CMAKE_COMPILER_IS_GNUCXX OFF)
endif()

#
# Hard coding the compiler version is ugly from cmake POV, but 
# at least gives user a friendly error message. The most critical
# demand for C++ compiler is support of C++11 lambdas, added
# only in version 4.5 https://gcc.gnu.org/projects/cxx0x.html
#
if (CMAKE_COMPILER_IS_GNUCC)
# cmake 2.8.9 and earlier doesn't support CMAKE_CXX_COMPILER_VERSION
       if (NOT CMAKE_CXX_COMPILER_VERSION)
           execute_process(COMMAND ${CMAKE_C_COMPILER} -dumpversion
                           OUTPUT_VARIABLE CMAKE_CXX_COMPILER_VERSION)
       endif()
       if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.5)
           message(FATAL_ERROR "
           Your GCC version is ${CMAKE_CXX_COMPILER_VERSION}, please update
                   ")
       endif()
else()
     if (BUILD_STATIC)
           message(FATAL_ERROR "Static build is supported for GCC only")
     endif()
endif()

#
# Check supported standards
#
if((NOT HAVE_STD_C11 AND NOT HAVE_STD_GNU99) OR
   (NOT HAVE_STD_CXX11 AND NOT HAVE_STD_GNUXX0X))
    set(CMAKE_REQUIRED_FLAGS "-std=c11")
    check_c_source_compiles("
    /*
     * FreeBSD 10 ctype.h header fail to compile on gcc4.8 in c11 mode.
     * Make sure we aren't affected.
     */
    #include <ctype.h>
    int main(void) { return 0; }
    " HAVE_STD_C11)
    set(CMAKE_REQUIRED_FLAGS "-std=gnu99")
    check_c_source_compiles("int main(void) { return 0; }" HAVE_STD_GNU99)
    set(CMAKE_REQUIRED_FLAGS "-std=c++11")
    check_cxx_source_compiles("int main(void) { return 0; }" HAVE_STD_CXX11)
    set(CMAKE_REQUIRED_FLAGS "-std=gnu++0x")
    check_cxx_source_compiles("int main(void) { return 0; }" HAVE_STD_GNUXX0X)
    set(CMAKE_REQUIRED_FLAGS "")
endif()
if((NOT HAVE_STD_C11 AND NOT HAVE_STD_GNU99) OR
   (NOT HAVE_STD_CXX11 AND NOT HAVE_STD_GNUXX0X))
    message (FATAL_ERROR
        "${CMAKE_C_COMPILER} should support -std=c11 or -std=gnu99. "
        "${CMAKE_CXX_COMPILER} should support -std=c++11 or -std=gnu++0x. "
        "Please consider upgrade to gcc 4.5+ or clang 3.2+.")
endif()

#
# Check for an omp support
#
set(CMAKE_REQUIRED_FLAGS "-fopenmp -Werror")
check_cxx_source_compiles("int main(void) {
#pragma omp parallel
    {
    }
    return 0;
}" HAVE_OPENMP)
set(CMAKE_REQUIRED_FLAGS "")

if (NOT HAVE_OPENMP)
    add_compile_flags("C;CXX" -Wno-unknown-pragmas)
endif()

#
# GCC started to warn for unused result starting from 4.2, and
# this is when it introduced -Wno-unused-result
# GCC can also be built on top of llvm runtime (on mac).
#

check_c_compiler_flag("-Wno-parentheses" CC_HAS_WNO_PARENTHESES)
check_c_compiler_flag("-Wno-parentheses-equality" CC_HAS_WNO_PARENTHESES_EQUALITY)
check_c_compiler_flag("-Wno-tautological-compare" CC_HAS_WNO_TAUTOLOGICAL_COMPARE)
check_c_compiler_flag("-Wno-misleading-indentation" CC_HAS_WNO_MISLEADING_INDENTATION)
check_c_compiler_flag("-Wno-varargs" CC_HAS_WNO_VARARGS)
check_c_compiler_flag("-Wno-char-subscripts" CC_HAS_WNO_CHAR_SUBSCRIPTS)
check_c_compiler_flag("-Wno-format-truncation" CC_HAS_WNO_FORMAT_TRUNCATION)
check_c_compiler_flag("-Wno-implicit-fallthrough" CC_HAS_WNO_IMPLICIT_FALLTHROUGH)
check_c_compiler_flag("-Wno-cast-function-type" CC_HAS_WNO_CAST_FUNCTION_TYPE)

#
# Perform build type specific configuration.
#
check_c_compiler_flag("-ggdb" CC_HAS_GGDB)
if (CC_HAS_GGDB)
    set (CC_DEBUG_OPT "-ggdb")
endif()

set (CMAKE_C_FLAGS_DEBUG
    "${CMAKE_C_FLAGS_DEBUG} ${CC_DEBUG_OPT} -O0")
set (CMAKE_C_FLAGS_RELWITHDEBINFO
    "${CMAKE_C_FLAGS_RELWITHDEBINFO} ${CC_DEBUG_OPT} -O2")
set (CMAKE_CXX_FLAGS_DEBUG
    "${CMAKE_CXX_FLAGS_DEBUG} ${CC_DEBUG_OPT} -O0")
set (CMAKE_CXX_FLAGS_RELWITHDEBINFO
    "${CMAKE_CXX_FLAGS_RELWITHDEBINFO} ${CC_DEBUG_OPT} -O2")

unset(CC_DEBUG_OPT)

check_include_file(libunwind.h HAVE_LIBUNWIND_H)
if(BUILD_STATIC)
    set(UNWIND_LIB_NAME libunwind.a)
else()
    set(UNWIND_LIB_NAME unwind)
endif()
find_library(UNWIND_LIBRARY PATH_SUFFIXES system NAMES ${UNWIND_LIB_NAME})

# Disabled backtraces support on FreeBSD by default, because of
# gh-4278.
set(ENABLE_BACKTRACE_DEFAULT OFF)
if (NOT TARGET_OS_FREEBSD AND UNWIND_LIBRARY AND HAVE_LIBUNWIND_H)
    set(ENABLE_BACKTRACE_DEFAULT ON)
endif()

option(ENABLE_BACKTRACE "Enable output of fiber backtrace information in
'fiber.info()' command." ${ENABLE_BACKTRACE_DEFAULT})

if (ENABLE_BACKTRACE)
    # unwind is required
    if (NOT (UNWIND_LIBRARY AND HAVE_LIBUNWIND_H))
        message (FATAL_ERROR "ENABLE_BACKTRACE option is set but unwind "
                             "library is not found")
    endif()
    if (TARGET_OS_DARWIN)
        set (UNWIND_LIBRARIES ${UNWIND_LIBRARY})
    else()
        if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR
            CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64")
            if(BUILD_STATIC)
                set(UNWIND_PLATFORM_LIB_NAME "libunwind-${CMAKE_SYSTEM_PROCESSOR}.a")
            else()
                set(UNWIND_PLATFORM_LIB_NAME "unwind-${CMAKE_SYSTEM_PROCESSOR}")
            endif()
        elseif (CMAKE_SYSTEM_PROCESSOR STREQUAL "i686")
            if(BUILD_STATIC)
                set(UNWIND_PLATFORM_LIB_NAME "libunwind-x86.a")
            else()
                set(UNWIND_PLATFORM_LIB_NAME "unwind-x86")
            endif()
        elseif (CMAKE_SYSTEM_PROCESSOR MATCHES "arm*")
            if(BUILD_STATIC)
                set(UNWIND_PLATFORM_LIB_NAME "libunwind-arm.a")
            else()
                set(UNWIND_PLATFORM_LIB_NAME "unwind-arm")
            endif()
        endif()
        find_library(UNWIND_PLATFORM_LIBRARY PATH_SUFFIXES system
            NAMES ${UNWIND_PLATFORM_LIB_NAME})
        set(UNWIND_LIBRARIES ${UNWIND_PLATFORM_LIBRARY} ${UNWIND_LIBRARY})
    endif()
    if (BUILD_STATIC)
        # some versions of libunwind need liblzma, and we don't use pkg-config
        # so we just look whether liblzma is installed, and add it if it is.
        # It might not be actually needed, but doesn't hurt if it is not.
        # We don't need any headers, just the lib, as it's privately needed.
        find_library(LZMA_LIBRARY PATH_SUFFIXES system NAMES liblzma.a)
        if (NOT LZMA_LIBRARY STREQUAL "LZMA_LIBRARY-NOTFOUND")
            message(STATUS "liblzma found")
            set(UNWIND_LIBRARIES ${UNWIND_LIBRARIES} ${LZMA_LIBRARY})
        endif()
    endif()
    find_package_message(UNWIND_LIBRARIES "Found unwind" "${UNWIND_LIBRARIES}")
endif()

if(BUILD_STATIC)
    # Static linking for c++ routines
    add_compile_flags("C;CXX" "-static-libstdc++")
endif()

#
# Set flags for all include files: those maintained by us and
# coming from third parties.
# Since we began using luajit, which uses gcc stack unwind
# internally, we also need to make sure all code is compiled
# with unwind info.
#

add_compile_flags("C;CXX" "-fexceptions" "-funwind-tables")

# We must set -fno-omit-frame-pointer here, since we rely
# on frame pointer when getting a backtrace, and it must
# be used consistently across all object files.
# The same reasoning applies to -fno-stack-protector switch.

if (ENABLE_BACKTRACE)
    add_compile_flags("C;CXX"
        "-fno-omit-frame-pointer"
        "-fno-stack-protector")
endif()

# In C a global variable without a storage specifier (static/extern) and
# without an initialiser is called a ’tentative definition’. The
# language permits multiple tentative definitions in the single
# translation unit; i.e. int foo; int foo; is perfectly ok. GNU
# toolchain goes even further, allowing multiple tentative definitions
# in *different* translation units. Internally, variables introduced via
# tentative definitions are implemented as ‘common’ symbols. Linker
# permits multiple definitions if they are common symbols, and it picks
# one arbitrarily for inclusion in the binary being linked.
#
# -fno-common forces GNU toolchain to behave in a more
# standard-conformant way in respect to tentative definitions and it
# prevents common symbols generation. Since we are a cross-platform
# project it really makes sense. There are toolchains that don’t
# implement GNU style handling of the tentative definitions and there
# are platforms lacking proper support for common symbols (osx).
#

add_compile_flags("C;CXX" "-fno-common")

if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
# Remove VALGRIND code and assertions in *any* type of release build.
    add_definitions("-DNDEBUG" "-DNVALGRIND")
endif()

option(ENABLE_WERROR "Make all compiler warnings into errors" OFF)

option(ENABLE_UB_SANITIZER "Make the compiler generate runtime code to perform undefined behaviour checks" OFF)

macro(enable_tnt_compile_flags)
    # Tarantool code is written in GNU C dialect.
    # Additionally, compile it with more strict flags than the rest
    # of the code.

    # Set standard
    if (HAVE_STD_C11)
        add_compile_flags("C" "-std=c11")
    else()
        add_compile_flags("C" "-std=gnu99")
    endif()

    if (HAVE_STD_CXX11)
        add_compile_flags("CXX" "-std=c++11")
    else()
        add_compile_flags("CXX" "-std=gnu++0x")
        add_definitions("-Doverride=")
    endif()

    add_compile_flags("C;CXX"
        "-Wall"
        "-Wextra"
        "-Wno-strict-aliasing"
    )

    if (ENABLE_UB_SANITIZER)
        if (NOT CMAKE_COMPILER_IS_CLANG)
            message(FATAL_ERROR "Undefined behaviour sanitizer only available for clang")
        else()
            string(JOIN "," SANITIZE_FLAGS
                alignment bool bounds builtin enum float-cast-overflow
                float-divide-by-zero function integer-divide-by-zero return
                shift unreachable vla-bound
            )

            # Exclude "object-size".
            # Gives compilation warnings when -O0 is used, which is always,
            # because some tests build with -O0.

            # Exclude "pointer-overflow".
            # Stailq data structure subtracts a positive value from NULL.

            # Exclude "vptr".
            # Intrusive data structures may abuse '&obj->member' on pointer
            # 'obj' which is not really a pointer at an object of its type.
            # For example, rlist uses '&item->member' expression in macro cycles
            # to check end of cycle, but on the last iteration 'item' points at
            # the list metadata head, not at an object of type stored in this
            # list.

            # Exclude "implicit-signed-integer-truncation",
            # "implicit-integer-sign-change", "signed-integer-overflow".
            # Integer overflow and truncation are disabled due to extensive
            # usage of this UB in SQL code to 'implement' some kind of int65_t.

            # Exclude "null", "nonnull-attribute", "nullability-arg",
            # "returns-nonnull-attribute", "nullability-assign",
            # "nullability-return".
            # NULL checking is disabled, because this is not a UB and raises
            # lots of false-positive fails such as typeof(*obj) with
            # obj == NULL, or memcpy() with NULL argument and 0 size. All
            # nullability sanitations are disabled, because from the tests it
            # seems they implicitly turn each other on, when one is used. For
            # example, having "returns-nonnull-attribute" may lead to fail in
            # the typeof(*obj) when obj is NULL, even though there is nothing
            # related to return.

            set(SANITIZE_FLAGS "-fsanitize=${SANITIZE_FLAGS} -fno-sanitize-recover=${SANITIZE_FLAGS}")

            add_compile_flags("C;CXX" "${SANITIZE_FLAGS}")
        endif()
    endif()

    if (CMAKE_COMPILER_IS_CLANG AND CC_HAS_WNO_UNUSED_VALUE)
        # False-positive warnings for ({ xx = ...; x; }) macroses
        add_compile_flags("C;CXX" "-Wno-unused-value")
    endif()

    if (CC_HAS_WNO_CHAR_SUBSCRIPTS)
        add_compile_flags("C;CXX" "-Wno-char-subscripts")
    endif()

    if (CC_HAS_WNO_FORMAT_TRUNCATION)
        add_compile_flags("C;CXX" "-Wno-format-truncation")
    endif()

    if (CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNUCXX)
        # G++ bug. http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31488
        # Also offsetof() is widely used in Tarantool source code
        # for classes and structs to implement intrusive lists and
        # some other data structures. G++ and clang++ both
        # complain about classes, having a virtual table. They
        # complain fair, but this can't be fixed for now.
        add_compile_flags("CXX"
            "-Wno-invalid-offsetof"
        )
        add_compile_flags("C;CXX" "-Wno-gnu-alignof-expression")
    endif()

    if (CMAKE_COMPILER_IS_GNUCC)
        # A workaround for Redhat Developer Toolset 2.x on RHEL/CentOS 5.x
        add_compile_flags("C" "-fno-gnu89-inline")
    endif()

    # Suppress noise GCC 8 warnings.
    #
    # reflection.h casts a pointer to a member function to an another pointer
    # to a member function to store it in a structure, but cast it back before
    # a call. It is legal and does not lead to an undefined behaviour.
    if (CC_HAS_WNO_CAST_FUNCTION_TYPE)
        add_compile_flags("C;CXX" "-Wno-cast-function-type")
    endif()

    add_definitions("-D__STDC_FORMAT_MACROS=1")
    add_definitions("-D__STDC_LIMIT_MACROS=1")
    add_definitions("-D__STDC_CONSTANT_MACROS=1")

    # Only add -Werror if it's a debug build, done by developers.
    # Release builds should not cause extra trouble.
    if ((${CMAKE_BUILD_TYPE} STREQUAL "Debug")
        AND HAVE_STD_C11 AND HAVE_STD_CXX11)
        add_compile_flags("C;CXX" "-Werror")
    endif()

    # Add -Werror if it is requested explicitly.
    if (ENABLE_WERROR)
        add_compile_flags("C;CXX" "-Werror")
    endif()
endmacro(enable_tnt_compile_flags)

if (HAVE_OPENMP)
    add_compile_flags("C;CXX" "-fopenmp")
endif()

if (CMAKE_COMPILER_IS_CLANG OR CMAKE_COMPILER_IS_GNUCC)
    set(HAVE_BUILTIN_CTZ 1)
    set(HAVE_BUILTIN_CTZLL 1)
    set(HAVE_BUILTIN_CLZ 1)
    set(HAVE_BUILTIN_CLZLL 1)
    set(HAVE_BUILTIN_POPCOUNT 1)
    set(HAVE_BUILTIN_POPCOUNTLL 1)
    set(HAVE_BUILTIN_BSWAP32 1)
    set(HAVE_BUILTIN_BSWAP64 1)
else()
    set(HAVE_BUILTIN_CTZ 0)
    set(HAVE_BUILTIN_CTZLL 0)
    set(HAVE_BUILTIN_CLZ 0)
    set(HAVE_BUILTIN_CLZLL 0)
    set(HAVE_BUILTIN_POPCOUNT 0)
    set(HAVE_BUILTIN_POPCOUNTLL 0)
    set(HAVE_BUILTIN_BSWAP32 0)
    set(HAVE_BUILTIN_BSWAP64 0)
    find_package_message(CC_BIT "Using slow implementation of bit operations"
        "${CMAKE_COMPILER_IS_CLANG}:${CMAKE_COMPILER_IS_GNUCC}")
endif()

if (NOT HAVE_BUILTIN_CTZ OR NOT HAVE_BUILTIN_CTZLL)
    # Check if -D_GNU_SOURCE has been defined and add this flag to
    # CMAKE_REQUIRED_DEFINITIONS in order to get check_prototype_definition work
    get_property(var DIRECTORY PROPERTY COMPILE_DEFINITIONS)
    list(FIND var "_GNU_SOURCE" var)
    if (NOT var EQUAL -1)
        set(CMAKE_REQUIRED_FLAGS "-Wno-error")
        set(CMAKE_REQUIRED_DEFINITIONS "-D_GNU_SOURCE")
        check_c_source_compiles("#include <string.h>\n#include <strings.h>\nint main(void) { return ffsl(0L); }"
            HAVE_FFSL)
        check_c_source_compiles("#include <string.h>\n#include <strings.h>\nint main(void) { return ffsll(0UL); }"
            HAVE_FFSLL)
    endif()
endif()

if (CMAKE_CROSSCOMPILING)
    set(CMAKE_HOST_C_COMPILER cc)
    set(CMAKE_HOST_CXX_COMPILER c++)
else()
    set(CMAKE_HOST_C_COMPILER ${CMAKE_C_COMPILER})
    set(CMAKE_HOST_CXX_COMPILER ${CMAKE_CXX_COMPILER})
endif()