blob: adb40f292fd6c5e7203b59b79c32868af474f1ab [file] [log] [blame]
Chandler Carruth1f5d5c02012-04-04 22:12:04 +00001# CMake build for CompilerRT.
2#
3# This build assumes that CompilerRT is checked out into the
Jonas Hahnfeldddbab7d2016-08-19 06:46:00 +00004# 'projects/compiler-rt' or 'runtimes/compiler-rt' inside of an LLVM tree.
Alexey Samsonovde4ef2a2014-02-19 07:49:16 +00005# Standalone build system for CompilerRT is not yet ready.
Chandler Carruth1f5d5c02012-04-04 22:12:04 +00006#
7# An important constraint of the build is that it only produces libraries
8# based on the ability of the host toolchain to target various platforms.
9
Pirama Arumuga Nainar31b825d2017-06-16 21:14:45 +000010cmake_minimum_required(VERSION 3.4.3)
11
Alexey Samsonovde4ef2a2014-02-19 07:49:16 +000012# Check if compiler-rt is built as a standalone project.
Jonas Hahnfeldddbab7d2016-08-19 06:46:00 +000013if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR COMPILER_RT_STANDALONE_BUILD)
Douglas Katzmanc4ffd482015-07-28 16:52:42 +000014 project(CompilerRT C CXX ASM)
Alexey Samsonovde4ef2a2014-02-19 07:49:16 +000015 set(COMPILER_RT_STANDALONE_BUILD TRUE)
Alexey Samsonovde4ef2a2014-02-19 07:49:16 +000016endif()
17
Chris Bienemanc49e5e32016-05-09 21:45:52 +000018# Add path for custom compiler-rt modules.
19list(INSERT CMAKE_MODULE_PATH 0
20 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
21 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
22 )
23
George Karpenkov50dd3fe2017-07-28 00:50:56 +000024if(CMAKE_CONFIGURATION_TYPES)
25 set(CMAKE_CFG_RESOLVED_INTDIR "${CMAKE_CFG_INTDIR}/")
26else()
27 set(CMAKE_CFG_RESOLVED_INTDIR "")
28endif()
29
Chris Bienemanc49e5e32016-05-09 21:45:52 +000030include(base-config-ix)
Jonas Hahnfeld9b2c3ab2016-08-02 05:51:05 +000031include(CompilerRTUtils)
Alexey Samsonov20abdf62013-10-01 12:52:15 +000032
Chris Bieneman679ab852015-09-14 19:59:24 +000033option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
34mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
35option(COMPILER_RT_BUILD_SANITIZERS "Build sanitizers" ON)
36mark_as_advanced(COMPILER_RT_BUILD_SANITIZERS)
Dean Michael Berris6af0a6b2016-08-08 03:58:57 +000037option(COMPILER_RT_BUILD_XRAY "Build xray" ON)
Dean Michael Berris938c5032016-07-21 07:39:55 +000038mark_as_advanced(COMPILER_RT_BUILD_XRAY)
George Karpenkov10ab2ac2017-08-21 23:25:50 +000039option(COMPILER_RT_BUILD_LIBFUZZER "Build libFuzzer" ON)
40mark_as_advanced(COMPILER_RT_BUILD_LIBFUZZER)
Michal Gorny806b8d52017-10-02 05:03:55 +000041option(COMPILER_RT_BUILD_PROFILE "Build profile runtime" ON)
42mark_as_advanced(COMPILER_RT_BUILD_PROFILE)
Dean Michael Berris449fdf82017-08-03 00:58:45 +000043option(COMPILER_RT_BUILD_XRAY_NO_PREINIT "Build xray with no preinit patching" OFF)
44mark_as_advanced(COMPILER_RT_BUILD_XRAY_NO_PREINIT)
Chris Bieneman679ab852015-09-14 19:59:24 +000045
Catherine Moore61efa172017-05-10 15:34:25 +000046set(COMPILER_RT_BAREMETAL_BUILD OFF CACHE BOOLEAN
47 "Build for a bare-metal target.")
48
Chris Bienemanc49e5e32016-05-09 21:45:52 +000049if (COMPILER_RT_STANDALONE_BUILD)
Jonas Hahnfeld9b2c3ab2016-08-02 05:51:05 +000050 load_llvm_config()
Alexey Samsonovaa980c72014-02-19 10:04:29 +000051
52 # Find Python interpreter.
53 set(Python_ADDITIONAL_VERSIONS 2.7 2.6 2.5)
54 include(FindPythonInterp)
55 if(NOT PYTHONINTERP_FOUND)
56 message(FATAL_ERROR "
57 Unable to find Python interpreter required testing. Please install Python
58 or specify the PYTHON_EXECUTABLE CMake variable.")
59 endif()
60
61 # Define default arguments to lit.
62 set(LIT_ARGS_DEFAULT "-sv")
63 if (MSVC OR XCODE)
64 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
65 endif()
66 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
Zachary Turnerce92db12017-09-15 22:10:46 +000067 set(LLVM_LIT_OUTPUT_DIR "${COMPILER_RT_EXEC_OUTPUT_DIR}")
Alexey Samsonovde4ef2a2014-02-19 07:49:16 +000068endif()
69
Jonas Hahnfeld9b2c3ab2016-08-02 05:51:05 +000070construct_compiler_rt_default_triple()
Francis Ricciba064be2017-09-19 17:54:11 +000071if ("${COMPILER_RT_DEFAULT_TARGET_ABI}" MATCHES "hf$")
72 if (${COMPILER_RT_DEFAULT_TARGET_ARCH} MATCHES "^arm")
73 set(COMPILER_RT_DEFAULT_TARGET_ARCH "armhf")
74 endif()
75endif()
Francis Riccid3272542017-09-19 17:56:27 +000076if ("${COMPILER_RT_DEFAULT_TARGET_ABI}" MATCHES "^android")
Chris Bieneman990ff382016-06-27 22:52:05 +000077 set(ANDROID 1)
78endif()
Vitaly Buka0dc3ef32017-09-16 05:13:56 +000079pythonize_bool(ANDROID)
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000080
81set(COMPILER_RT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
Alexey Samsonov6a65b182013-06-06 12:35:48 +000082set(COMPILER_RT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
Alexey Samsonovca7fcf22012-12-19 12:33:39 +000083
Timur Iskhodzhanovb9bd76b2014-05-13 14:25:49 +000084# We support running instrumented tests when we're not cross compiling
85# and target a UNIX-like system or Windows.
86# We can run tests on Android even when we are cross-compiling.
Reid Klecknerfbfed862015-07-17 16:23:05 +000087if(("${CMAKE_HOST_SYSTEM}" STREQUAL "${CMAKE_SYSTEM}" AND (UNIX OR WIN32)) OR ANDROID
Greg Fitzgerald07c88a12014-05-14 00:36:15 +000088 OR COMPILER_RT_EMULATOR)
Michael Gottesman4ddc2152013-04-01 04:13:03 +000089 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" ON)
Alexey Samsonovc20f5d22012-12-27 13:19:23 +000090else()
Michael Gottesman4ddc2152013-04-01 04:13:03 +000091 option(COMPILER_RT_CAN_EXECUTE_TESTS "Can we execute instrumented tests" OFF)
Alexey Samsonovc20f5d22012-12-27 13:19:23 +000092endif()
Michael Gottesman4ddc2152013-04-01 04:13:03 +000093
Peter Collingbournecbdea322013-10-25 23:03:34 +000094option(COMPILER_RT_DEBUG "Build runtimes with full debug info" OFF)
Chris Bieneman69a372d2015-12-03 20:08:22 +000095option(COMPILER_RT_EXTERNALIZE_DEBUGINFO
96 "Generate dSYM files and strip executables and libraries (Darwin Only)" OFF)
Peter Collingbournecbdea322013-10-25 23:03:34 +000097# COMPILER_RT_DEBUG_PYBOOL is used by lit.common.configured.in.
98pythonize_bool(COMPILER_RT_DEBUG)
99
Petr Hosek2bbb6ad2017-07-28 03:39:38 +0000100include(HandleCompilerRT)
Vedant Kumarcb7110b2016-10-06 16:45:40 +0000101include(config-ix)
102
Kuba Mracek5c2e3e22017-07-07 22:40:13 +0000103if(APPLE AND SANITIZER_MIN_OSX_VERSION AND SANITIZER_MIN_OSX_VERSION VERSION_LESS "10.9")
Francis Ricci9cf5e4f2016-08-22 18:31:37 +0000104 # Mac OS X prior to 10.9 had problems with exporting symbols from
105 # libc++/libc++abi.
Peter Collingbourned13d61f2017-09-15 20:24:12 +0000106 set(cxxabi_supported OFF)
Francis Ricci9cf5e4f2016-08-22 18:31:37 +0000107else()
Peter Collingbourned13d61f2017-09-15 20:24:12 +0000108 set(cxxabi_supported ON)
Francis Ricci9cf5e4f2016-08-22 18:31:37 +0000109endif()
110
Peter Collingbourned13d61f2017-09-15 20:24:12 +0000111option(SANITIZER_ALLOW_CXXABI "Allow use of C++ ABI details in ubsan" ON)
112
113set(SANITIZE_CAN_USE_CXXABI OFF)
114if (cxxabi_supported AND SANITIZER_ALLOW_CXXABI)
115 set(SANITIZER_CAN_USE_CXXABI ON)
116endif()
Francis Ricci9cf5e4f2016-08-22 18:31:37 +0000117pythonize_bool(SANITIZER_CAN_USE_CXXABI)
118
Petr Hoseka7a9ca42017-07-28 03:39:39 +0000119set(SANITIZER_CXX_ABI "default" CACHE STRING
120 "Specify C++ ABI library to use.")
Evgeniy Stepanovb52b1412017-08-14 20:42:43 +0000121set(CXXABIS none default libcxxabi libstdc++ libc++)
Petr Hoseka7a9ca42017-07-28 03:39:39 +0000122set_property(CACHE SANITIZER_CXX_ABI PROPERTY STRINGS ;${CXXABIS})
123
124if (SANITIZER_CXX_ABI STREQUAL "default")
125 if (HAVE_LIBCXXABI AND COMPILER_RT_DEFAULT_TARGET_ONLY)
126 set(SANITIZER_CXX_ABI_LIBNAME "libcxxabi")
127 set(SANITIZER_CXX_ABI_INTREE 1)
128 elseif (APPLE)
129 set(SANITIZER_CXX_ABI_LIBNAME "libcxxabi")
130 set(SANITIZER_CXX_ABI_SYSTEM 1)
131 else()
132 set(SANITIZER_CXX_ABI_LIBNAME "libstdc++")
133 endif()
Evgeniy Stepanovb52b1412017-08-14 20:42:43 +0000134else()
Petr Hoseka7a9ca42017-07-28 03:39:39 +0000135 set(SANITIZER_CXX_ABI_LIBNAME "${SANITIZER_CXX_ABI}")
136endif()
137
138if (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libcxxabi")
139 if (SANITIZER_CXX_ABI_INTREE)
140 if (TARGET unwind_shared OR HAVE_LIBUNWIND)
141 list(APPEND SANITIZER_CXX_ABI_LIBRARY unwind_shared)
142 endif()
143 if (TARGET cxxabi_shared OR HAVE_LIBCXXABI)
144 list(APPEND SANITIZER_CXX_ABI_LIBRARY cxxabi_shared)
145 endif()
146 else()
147 list(APPEND SANITIZER_CXX_ABI_LIBRARY "c++abi")
148 endif()
Evgeniy Stepanov3072b532017-08-11 22:28:02 +0000149elseif (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libc++")
150 list(APPEND SANITIZER_CXX_ABI_LIBRARY "c++")
Petr Hoseka7a9ca42017-07-28 03:39:39 +0000151elseif (SANITIZER_CXX_ABI_LIBNAME STREQUAL "libstdc++")
152 append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ SANITIZER_CXX_ABI_LIBRARY)
153endif()
154
Petr Hosek2bbb6ad2017-07-28 03:39:38 +0000155option(SANITIZER_USE_COMPILER_RT "Use compiler-rt builtins instead of libgcc" OFF)
156
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000157#================================
158# Setup Compiler Flags
159#================================
Saleem Abdulrasoola7452e42015-01-14 15:55:17 +0000160
Alexey Samsonovfe7e28c2014-03-13 11:31:10 +0000161if(MSVC)
Reid Klecknerd0680ad2016-06-17 17:48:52 +0000162 # Override any existing /W flags with /W4. This is what LLVM does. Failing to
163 # remove other /W[0-4] flags will result in a warning about overriding a
164 # previous flag.
165 if (COMPILER_RT_HAS_W4_FLAG)
166 string(REGEX REPLACE " /W[0-4]" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
167 string(REGEX REPLACE " /W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
168 append_string_if(COMPILER_RT_HAS_W4_FLAG /W4 CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
169 endif()
Alexey Samsonovfe7e28c2014-03-13 11:31:10 +0000170else()
171 append_string_if(COMPILER_RT_HAS_WALL_FLAG -Wall CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
172endif()
Alexey Samsonov2f27f0b2014-02-24 11:22:39 +0000173if(COMPILER_RT_ENABLE_WERROR)
Alexey Samsonovfe7e28c2014-03-13 11:31:10 +0000174 append_string_if(COMPILER_RT_HAS_WERROR_FLAG -Werror CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
175 append_string_if(COMPILER_RT_HAS_WX_FLAG /WX CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
Alexey Samsonov2f27f0b2014-02-24 11:22:39 +0000176endif()
177
Alexey Samsonovbcce1972014-03-18 12:49:22 +0000178append_string_if(COMPILER_RT_HAS_STD_CXX11_FLAG -std=c++11 CMAKE_CXX_FLAGS)
179
Reid Kleckner0140ce42014-02-26 21:54:39 +0000180# Emulate C99 and C++11's __func__ for MSVC prior to 2013 CTP.
Alexey Samsonove3e2a112014-02-27 06:52:41 +0000181if(NOT COMPILER_RT_HAS_FUNC_SYMBOL)
Alexey Samsonov8c4c0972014-02-27 07:03:32 +0000182 add_definitions(-D__func__=__FUNCTION__)
Reid Kleckner0140ce42014-02-26 21:54:39 +0000183endif()
184
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000185# Provide some common commmandline flags for Sanitizer runtimes.
Francis Ricci0b6f4872016-09-08 15:57:22 +0000186if(NOT WIN32)
187 append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC SANITIZER_COMMON_CFLAGS)
188endif()
Kuba Brecka14c0c592014-10-15 22:47:54 +0000189append_list_if(COMPILER_RT_HAS_FNO_BUILTIN_FLAG -fno-builtin SANITIZER_COMMON_CFLAGS)
190append_list_if(COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions SANITIZER_COMMON_CFLAGS)
Kuba Mracek90caf892017-03-27 17:14:48 +0000191if(NOT COMPILER_RT_DEBUG AND NOT APPLE)
Kuba Brecka83460cf2016-05-22 19:59:06 +0000192 append_list_if(COMPILER_RT_HAS_FOMIT_FRAME_POINTER_FLAG -fomit-frame-pointer SANITIZER_COMMON_CFLAGS)
193endif()
Kuba Brecka14c0c592014-10-15 22:47:54 +0000194append_list_if(COMPILER_RT_HAS_FUNWIND_TABLES_FLAG -funwind-tables SANITIZER_COMMON_CFLAGS)
195append_list_if(COMPILER_RT_HAS_FNO_STACK_PROTECTOR_FLAG -fno-stack-protector SANITIZER_COMMON_CFLAGS)
Peter Collingbourneb64d0b12015-06-15 21:08:47 +0000196append_list_if(COMPILER_RT_HAS_FNO_SANITIZE_SAFE_STACK_FLAG -fno-sanitize=safe-stack SANITIZER_COMMON_CFLAGS)
Kuba Brecka14c0c592014-10-15 22:47:54 +0000197append_list_if(COMPILER_RT_HAS_FVISIBILITY_HIDDEN_FLAG -fvisibility=hidden SANITIZER_COMMON_CFLAGS)
Chris Bieneman9586a382015-11-30 19:16:42 +0000198append_list_if(COMPILER_RT_HAS_FVISIBILITY_INLINES_HIDDEN_FLAG -fvisibility-inlines-hidden SANITIZER_COMMON_CFLAGS)
Evgeniy Stepanov8c9a0702015-01-15 16:31:22 +0000199append_list_if(COMPILER_RT_HAS_FNO_LTO_FLAG -fno-lto SANITIZER_COMMON_CFLAGS)
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000200
Kostya Kortchinsky2282ede2017-01-30 22:31:49 +0000201# The following is a workaround for powerpc64le. This is the only architecture
202# that requires -fno-function-sections to work properly. If lacking, the ASan
203# Linux test function-sections-are-bad.cc fails with the following error:
204# 'undefined symbol: __sanitizer_unaligned_load32'.
205if(DEFINED TARGET_powerpc64le_CFLAGS)
206 append_list_if(COMPILER_RT_HAS_FNO_FUNCTION_SECTIONS_FLAG -fno-function-sections TARGET_powerpc64le_CFLAGS)
207endif()
208
Alexey Samsonov6c282b42014-02-28 08:04:30 +0000209if(MSVC)
Peter Collingbourne702548d2015-07-08 22:10:34 +0000210 # Replace the /M[DT][d] flags with /MT, and strip any definitions of _DEBUG,
211 # which cause definition mismatches at link time.
Timur Iskhodzhanovde1718d2014-08-12 09:44:56 +0000212 # FIXME: In fact, sanitizers should support both /MT and /MD, see PR20214.
Alexey Samsonov6c282b42014-02-28 08:04:30 +0000213 if(COMPILER_RT_HAS_MT_FLAG)
Aaron Ballman7c7be2f2014-10-23 20:24:00 +0000214 foreach(flag_var
Andrey Turetskiybc0122b2016-02-20 12:56:04 +0000215 CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
216 CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
Aaron Ballman7c7be2f2014-10-23 20:24:00 +0000217 CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
218 CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO)
Peter Collingbourne702548d2015-07-08 22:10:34 +0000219 string(REGEX REPLACE "/M[DT]d" "/MT" ${flag_var} "${${flag_var}}")
220 string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
221 string(REGEX REPLACE "/D_DEBUG" "" ${flag_var} "${${flag_var}}")
Aaron Ballman7c7be2f2014-10-23 20:24:00 +0000222 endforeach()
Alexey Samsonov6c282b42014-02-28 08:04:30 +0000223 endif()
Kuba Brecka14c0c592014-10-15 22:47:54 +0000224 append_list_if(COMPILER_RT_HAS_Oy_FLAG /Oy- SANITIZER_COMMON_CFLAGS)
225 append_list_if(COMPILER_RT_HAS_GS_FLAG /GS- SANITIZER_COMMON_CFLAGS)
Reid Kleckner21aca482016-03-21 20:08:59 +0000226 # VS 2015 (version 1900) added support for thread safe static initialization.
227 # However, ASan interceptors run before CRT initialization, which causes the
228 # new thread safe code to crash. Disable this feature for now.
Reid Klecknerf6d54752016-07-21 20:03:37 +0000229 if (MSVC_VERSION GREATER 1899 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
Reid Kleckner21aca482016-03-21 20:08:59 +0000230 list(APPEND SANITIZER_COMMON_CFLAGS /Zc:threadSafeInit-)
231 endif()
Alexey Samsonov6c282b42014-02-28 08:04:30 +0000232endif()
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000233
Alexey Samsonovea040322015-01-06 20:25:34 +0000234append_list_if(COMPILER_RT_DEBUG -DSANITIZER_DEBUG=1 SANITIZER_COMMON_CFLAGS)
Alexey Samsonovdf3aeb82015-01-03 04:29:12 +0000235
George Karpenkove89d0a82017-07-15 00:30:46 +0000236# If we're using MSVC,
Alexey Samsonovcbc68522014-03-13 13:37:07 +0000237# always respect the optimization flags set by CMAKE_BUILD_TYPE instead.
George Karpenkove89d0a82017-07-15 00:30:46 +0000238if (NOT MSVC)
239
Zachary Turnerce92db12017-09-15 22:10:46 +0000240 # Build with optimization, unless we're in debug mode.
George Karpenkove89d0a82017-07-15 00:30:46 +0000241 if(COMPILER_RT_DEBUG)
242 list(APPEND SANITIZER_COMMON_CFLAGS -O0)
243 else()
244 list(APPEND SANITIZER_COMMON_CFLAGS -O3)
245 endif()
Hans Wennborg67c6e502013-08-27 01:24:01 +0000246endif()
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000247
Alexey Samsonovdf3aeb82015-01-03 04:29:12 +0000248# Determine if we should restrict stack frame sizes.
Dmitry Vyukov4bf08942015-04-10 09:45:22 +0000249# Stack frames on PowerPC and Mips and in debug biuld can be much larger than
Alexey Samsonovdf3aeb82015-01-03 04:29:12 +0000250# anticipated.
251# FIXME: Fix all sanitizers and add -Wframe-larger-than to
252# SANITIZER_COMMON_FLAGS
253if(COMPILER_RT_HAS_WFRAME_LARGER_THAN_FLAG AND NOT COMPILER_RT_DEBUG
Alexey Samsonov63eaeca2015-09-08 23:13:47 +0000254 AND NOT ${COMPILER_RT_DEFAULT_TARGET_ARCH} MATCHES "powerpc|mips")
Alexey Samsonovdf3aeb82015-01-03 04:29:12 +0000255 set(SANITIZER_LIMIT_FRAME_SIZE TRUE)
256else()
257 set(SANITIZER_LIMIT_FRAME_SIZE FALSE)
258endif()
259
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000260# Build sanitizer runtimes with debug info.
Kuba Brecka5238deb2014-12-23 01:52:53 +0000261if(COMPILER_RT_HAS_GLINE_TABLES_ONLY_FLAG AND NOT COMPILER_RT_DEBUG)
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000262 list(APPEND SANITIZER_COMMON_CFLAGS -gline-tables-only)
263elseif(COMPILER_RT_HAS_G_FLAG)
264 list(APPEND SANITIZER_COMMON_CFLAGS -g)
Reid Kleckner1734b972016-08-02 01:02:46 +0000265elseif(MSVC)
266 # Use /Z7 instead of /Zi for the asan runtime. This avoids the LNK4099
267 # warning from the MS linker complaining that it can't find the 'vc140.pdb'
268 # file used by our object library compilations.
269 list(APPEND SANITIZER_COMMON_CFLAGS /Z7)
270 llvm_replace_compiler_option(CMAKE_CXX_FLAGS "/Z[i7I]" "/Z7")
271 llvm_replace_compiler_option(CMAKE_CXX_FLAGS_DEBUG "/Z[i7I]" "/Z7")
272 llvm_replace_compiler_option(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/Z[i7I]" "/Z7")
Alexey Samsonov75fb6772012-11-08 14:49:28 +0000273endif()
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000274
Kuba Breckaab61c742016-10-08 09:01:27 +0000275if(LLVM_ENABLE_MODULES)
276 # Sanitizers cannot be built with -fmodules. The interceptors intentionally
277 # don't include system headers, which is incompatible with modules.
278 list(APPEND SANITIZER_COMMON_CFLAGS -fno-modules)
279endif()
280
Alexey Samsonov9a1ffce2014-02-18 07:26:58 +0000281# Turn off several warnings.
Alexey Samsonov829da452014-11-13 21:19:53 +0000282append_list_if(COMPILER_RT_HAS_WGNU_FLAG -Wno-gnu SANITIZER_COMMON_CFLAGS)
283append_list_if(COMPILER_RT_HAS_WVARIADIC_MACROS_FLAG -Wno-variadic-macros SANITIZER_COMMON_CFLAGS)
284append_list_if(COMPILER_RT_HAS_WC99_EXTENSIONS_FLAG -Wno-c99-extensions SANITIZER_COMMON_CFLAGS)
285append_list_if(COMPILER_RT_HAS_WNON_VIRTUAL_DTOR_FLAG -Wno-non-virtual-dtor SANITIZER_COMMON_CFLAGS)
Aaron Ballman1d1f2322014-10-23 20:39:58 +0000286append_list_if(COMPILER_RT_HAS_WD4146_FLAG /wd4146 SANITIZER_COMMON_CFLAGS)
287append_list_if(COMPILER_RT_HAS_WD4291_FLAG /wd4291 SANITIZER_COMMON_CFLAGS)
Kuba Brecka14c0c592014-10-15 22:47:54 +0000288append_list_if(COMPILER_RT_HAS_WD4391_FLAG /wd4391 SANITIZER_COMMON_CFLAGS)
289append_list_if(COMPILER_RT_HAS_WD4722_FLAG /wd4722 SANITIZER_COMMON_CFLAGS)
Aaron Ballman1d1f2322014-10-23 20:39:58 +0000290append_list_if(COMPILER_RT_HAS_WD4800_FLAG /wd4800 SANITIZER_COMMON_CFLAGS)
Chandler Carruthc1c9d582012-08-29 00:13:11 +0000291
Petr Hosek2bbb6ad2017-07-28 03:39:38 +0000292# Set common link flags.
293append_list_if(COMPILER_RT_HAS_NODEFAULTLIBS_FLAG -nodefaultlibs SANITIZER_COMMON_LINK_FLAGS)
294
295if (SANITIZER_USE_COMPILER_RT)
296 list(APPEND SANITIZER_COMMON_LINK_FLAGS -rtlib=compiler-rt)
297 find_compiler_rt_library(builtins COMPILER_RT_BUILTINS_LIBRARY)
298 list(APPEND SANITIZER_COMMON_LINK_LIBS ${COMPILER_RT_BUILTINS_LIBRARY})
299else()
Petr Hoseka14a2cc2017-07-31 22:46:43 +0000300 if (ANDROID)
301 append_list_if(COMPILER_RT_HAS_GCC_LIB gcc SANITIZER_COMMON_LINK_LIBS)
302 else()
303 append_list_if(COMPILER_RT_HAS_GCC_S_LIB gcc_s SANITIZER_COMMON_LINK_LIBS)
304 endif()
Petr Hosek2bbb6ad2017-07-28 03:39:38 +0000305endif()
306
307append_list_if(COMPILER_RT_HAS_LIBC c SANITIZER_COMMON_LINK_LIBS)
308
Vitaly Buka5d960ec2017-08-01 22:22:25 +0000309if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
310 list(APPEND SANITIZER_COMMON_LINK_FLAGS -Wl,-z,defs,-z,now,-z,relro)
Petr Hosekd8328f12017-09-13 01:18:15 +0000311 list(APPEND SANITIZER_COMMON_LINK_LIBS zircon)
Vitaly Buka5d960ec2017-08-01 22:22:25 +0000312endif()
313
Reid Kleckner44e6e362016-06-17 18:30:37 +0000314# Warnings to turn off for all libraries, not just sanitizers.
315append_string_if(COMPILER_RT_HAS_WUNUSED_PARAMETER_FLAG -Wno-unused-parameter CMAKE_C_FLAGS CMAKE_CXX_FLAGS)
316
Bob Haarmanf51b0d52017-01-04 21:40:00 +0000317if (CMAKE_LINKER MATCHES "link.exe$")
318 # Silence MSVC linker warnings caused by empty object files. The
319 # sanitizer libraries intentionally use ifdefs that result in empty
320 # files, rather than skipping these files in the build system.
321 # Ideally, we would pass this flag only for the libraries that need
322 # it, but CMake doesn't seem to have a way to set linker flags for
323 # individual static libraries, so we enable the suppression flag for
324 # the whole compiler-rt project.
325 append("/IGNORE:4221" CMAKE_STATIC_LINKER_FLAGS)
326endif()
327
Alexey Samsonov9f3938e2013-04-11 15:49:52 +0000328add_subdirectory(include)
329
Zachary Turner1929ffd2017-09-13 20:49:25 +0000330set(COMPILER_RT_LIBCXX_PATH ${LLVM_MAIN_SRC_DIR}/projects/libcxx)
331if(EXISTS ${COMPILER_RT_LIBCXX_PATH}/)
332 set(COMPILER_RT_HAS_LIBCXX_SOURCES TRUE)
333else()
334 set(COMPILER_RT_LIBCXX_PATH ${LLVM_MAIN_SRC_DIR}/../libcxx)
335 if(EXISTS ${COMPILER_RT_LIBCXX_PATH}/)
336 set(COMPILER_RT_HAS_LIBCXX_SOURCES TRUE)
337 else()
338 set(COMPILER_RT_HAS_LIBCXX_SOURCES FALSE)
339 endif()
Alexey Samsonov8434e602014-02-14 13:02:58 +0000340endif()
341
Zachary Turner1929ffd2017-09-13 20:49:25 +0000342set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/tools/lld)
343if(EXISTS ${COMPILER_RT_LLD_PATH}/ AND LLVM_TOOL_LLD_BUILD)
344 set(COMPILER_RT_HAS_LLD TRUE)
345else()
346 set(COMPILER_RT_LLD_PATH ${LLVM_MAIN_SRC_DIR}/../lld)
347 if(EXISTS ${COMPILER_RT_LLD_PATH}/ AND LLVM_TOOL_LLD_BUILD)
348 set(COMPILER_RT_HAS_LLD TRUE)
349 else()
350 set(COMPILER_RT_HAS_LLD FALSE)
351 endif()
Reid Kleckner45ebaf12015-08-11 00:33:07 +0000352endif()
Evgeniy Stepanov656bc5b2017-03-25 00:42:25 +0000353pythonize_bool(COMPILER_RT_HAS_LLD)
Reid Kleckner45ebaf12015-08-11 00:33:07 +0000354
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000355add_subdirectory(lib)
356
Alexey Samsonovcd8535a2014-02-19 11:18:47 +0000357if(COMPILER_RT_INCLUDE_TESTS)
Alexey Samsonov81a2b462014-02-14 11:00:07 +0000358 add_subdirectory(unittests)
Chris Bienemanfb92d9a2015-09-14 19:54:12 +0000359 add_subdirectory(test)
Zachary Turnerce92db12017-09-15 22:10:46 +0000360 if (COMPILER_RT_STANDALONE_BUILD)
361 # If we have a valid source tree, generate llvm-lit into the bin directory.
362 # The user can still choose to have the check targets *use* a different lit
363 # by specifying -DLLVM_EXTERNAL_LIT, but we generate it regardless.
364 if (EXISTS ${LLVM_MAIN_SRC_DIR}/utils/llvm-lit)
365 add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
366 elseif(NOT EXISTS ${LLVM_EXTERNAL_LIT})
367 message(WARNING "Could not find LLVM source directory and LLVM_EXTERNAL_LIT does not"
368 "point to a valid file. You will not be able to run tests.")
369 endif()
370 endif()
Chandler Carruth1f5d5c02012-04-04 22:12:04 +0000371endif()