xref: /oneTBB/cmake/compilers/GNU.cmake (revision 2a6bd1e1)
1# Copyright (c) 2020-2023 Intel Corporation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15if (MINGW)
16    set(TBB_LINK_DEF_FILE_FLAG "")
17    set(TBB_DEF_FILE_PREFIX "")
18elseif (APPLE)
19    set(TBB_LINK_DEF_FILE_FLAG -Wl,-exported_symbols_list,)
20    set(TBB_DEF_FILE_PREFIX mac${TBB_ARCH})
21
22    # For correct ucontext.h structures layout
23    set(TBB_COMMON_COMPILE_FLAGS ${TBB_COMMON_COMPILE_FLAGS} -D_XOPEN_SOURCE)
24else()
25    set(TBB_LINK_DEF_FILE_FLAG -Wl,--version-script=)
26    set(TBB_DEF_FILE_PREFIX lin${TBB_ARCH})
27endif()
28
29set(TBB_WARNING_LEVEL -Wall -Wextra $<$<BOOL:${TBB_STRICT}>:-Werror> -Wfatal-errors)
30set(TBB_TEST_WARNING_FLAGS -Wshadow -Wcast-qual -Woverloaded-virtual -Wnon-virtual-dtor)
31
32# Depfile options (e.g. -MD) are inserted automatically in some cases.
33# Don't add -MMD to avoid conflicts in such cases.
34if (NOT CMAKE_GENERATOR MATCHES "Ninja" AND NOT CMAKE_CXX_DEPENDS_USE_COMPILER)
35    set(TBB_MMD_FLAG -MMD)
36endif()
37
38# Enable Intel(R) Transactional Synchronization Extensions (-mrtm) and WAITPKG instructions support (-mwaitpkg) on relevant processors
39if (CMAKE_SYSTEM_PROCESSOR MATCHES "(AMD64|amd64|i.86|x86)")
40    set(TBB_COMMON_COMPILE_FLAGS ${TBB_COMMON_COMPILE_FLAGS} -mrtm $<$<AND:$<NOT:$<CXX_COMPILER_ID:Intel>>,$<NOT:$<VERSION_LESS:${CMAKE_CXX_COMPILER_VERSION},11.0>>>:-mwaitpkg>)
41endif()
42
43set(TBB_COMMON_LINK_LIBS ${CMAKE_DL_LIBS})
44
45# Ignore -Werror set through add_compile_options() or added to CMAKE_CXX_FLAGS if TBB_STRICT is disabled.
46if (NOT TBB_STRICT AND COMMAND tbb_remove_compile_flag)
47    tbb_remove_compile_flag(-Werror)
48endif()
49
50if (NOT ${CMAKE_CXX_COMPILER_ID} STREQUAL Intel)
51    # gcc 6.0 and later have -flifetime-dse option that controls elimination of stores done outside the object lifetime
52    set(TBB_DSE_FLAG $<$<NOT:$<VERSION_LESS:${CMAKE_CXX_COMPILER_VERSION},6.0>>:-flifetime-dse=1>)
53    set(TBB_COMMON_COMPILE_FLAGS ${TBB_COMMON_COMPILE_FLAGS} $<$<NOT:$<VERSION_LESS:${CMAKE_CXX_COMPILER_VERSION},8.0>>:-fstack-clash-protection>)
54endif()
55
56# Workaround for heavy tests and too many symbols in debug (rellocation truncated to fit: R_MIPS_CALL16)
57if ("${CMAKE_SYSTEM_PROCESSOR}" MATCHES "mips")
58    set(TBB_TEST_COMPILE_FLAGS ${TBB_TEST_COMPILE_FLAGS} -DTBB_TEST_LOW_WORKLOAD $<$<CONFIG:DEBUG>:-fPIE -mxgot>)
59    set(TBB_TEST_LINK_FLAGS ${TBB_TEST_LINK_FLAGS} $<$<CONFIG:DEBUG>:-pie>)
60endif()
61
62set(TBB_IPO_COMPILE_FLAGS $<$<NOT:$<CONFIG:Debug>>:-flto>)
63set(TBB_IPO_LINK_FLAGS $<$<NOT:$<CONFIG:Debug>>:-flto>)
64
65
66if (MINGW AND CMAKE_SYSTEM_PROCESSOR MATCHES "i.86")
67    list (APPEND TBB_COMMON_COMPILE_FLAGS -msse2)
68endif ()
69
70# Gnu flags to prevent compiler from optimizing out security checks
71set(TBB_COMMON_COMPILE_FLAGS ${TBB_COMMON_COMPILE_FLAGS} -fno-strict-overflow -fno-delete-null-pointer-checks -fwrapv)
72set(TBB_COMMON_COMPILE_FLAGS ${TBB_COMMON_COMPILE_FLAGS} -Wformat -Wformat-security -Werror=format-security
73    -fstack-protector-strong )
74# -z switch is not supported on MacOS and MinGW
75if (NOT APPLE AND NOT MINGW)
76    set(TBB_LIB_LINK_FLAGS ${TBB_LIB_LINK_FLAGS} -Wl,-z,relro,-z,now,-z,noexecstack)
77endif()
78set(TBB_COMMON_COMPILE_FLAGS ${TBB_COMMON_COMPILE_FLAGS} $<$<NOT:$<CONFIG:Debug>>:-D_FORTIFY_SOURCE=2> )
79
80
81# TBB malloc settings
82set(TBBMALLOC_LIB_COMPILE_FLAGS -fno-rtti -fno-exceptions)
83set(TBB_OPENMP_FLAG -fopenmp)
84