1 include(CMakePushCheckState)
2 include(CheckSymbolExists)
4 # Because compiler-rt spends a lot of time setting up custom compile flags,
5 # define a handy helper function for it. The compile flags setting in CMake
6 # has serious issues that make its syntax challenging at best.
7 function(set_target_compile_flags target)
8 set_property(TARGET ${target} PROPERTY COMPILE_OPTIONS ${ARGN})
11 function(set_target_link_flags target)
12 set_property(TARGET ${target} PROPERTY LINK_OPTIONS ${ARGN})
15 # Set the variable var_PYBOOL to True if var holds a true-ish string,
16 # otherwise set it to False.
17 macro(pythonize_bool var)
19 set(${var}_PYBOOL True)
21 set(${var}_PYBOOL False)
25 # Appends value to all lists in ARGN, if the condition is true.
26 macro(append_list_if condition value)
29 list(APPEND ${list} ${value})
34 # Appends value to all strings in ARGN, if the condition is true.
35 macro(append_string_if condition value)
38 set(${str} "${${str}} ${value}")
43 macro(append_rtti_flag polarity list)
45 append_list_if(COMPILER_RT_HAS_FRTTI_FLAG -frtti ${list})
46 append_list_if(COMPILER_RT_HAS_GR_FLAG /GR ${list})
48 append_list_if(COMPILER_RT_HAS_FNO_RTTI_FLAG -fno-rtti ${list})
49 append_list_if(COMPILER_RT_HAS_GR_FLAG /GR- ${list})
53 macro(list_intersect output input1 input2)
55 foreach(it ${${input1}})
56 list(FIND ${input2} ${it} index)
57 if( NOT (index EQUAL -1))
58 list(APPEND ${output} ${it})
63 function(list_replace input_list old new)
65 foreach(item ${${input_list}})
66 if(${item} STREQUAL ${old})
67 list(APPEND replaced_list ${new})
69 list(APPEND replaced_list ${item})
72 set(${input_list} "${replaced_list}" PARENT_SCOPE)
75 # Takes ${ARGN} and puts only supported architectures in @out_var list.
76 function(filter_available_targets out_var)
77 set(archs ${${out_var}})
79 list(FIND COMPILER_RT_SUPPORTED_ARCH ${arch} ARCH_INDEX)
80 if(NOT (ARCH_INDEX EQUAL -1) AND CAN_TARGET_${arch})
81 list(APPEND archs ${arch})
84 set(${out_var} ${archs} PARENT_SCOPE)
87 # Add $arch as supported with no additional flags.
88 macro(add_default_target_arch arch)
89 set(TARGET_${arch}_CFLAGS "")
90 set(CAN_TARGET_${arch} 1)
91 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
94 function(check_compile_definition def argstring out_var)
95 if("${def}" STREQUAL "")
96 set(${out_var} TRUE PARENT_SCOPE)
99 cmake_push_check_state()
100 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} ${argstring}")
101 check_symbol_exists(${def} "" ${out_var})
102 cmake_pop_check_state()
105 # test_target_arch(<arch> <def> <target flags...>)
106 # Checks if architecture is supported: runs host compiler with provided
107 # flags to verify that:
108 # 1) <def> is defined (if non-empty)
109 # 2) simple file can be successfully built.
110 # If successful, saves target flags for this architecture.
111 macro(test_target_arch arch def)
112 set(TARGET_${arch}_CFLAGS ${ARGN})
113 set(TARGET_${arch}_LINK_FLAGS ${ARGN})
116 set(argstring "${argstring} ${arg}")
118 check_compile_definition("${def}" "${argstring}" HAS_${arch}_DEF)
119 if(NOT DEFINED CAN_TARGET_${arch})
120 if(NOT HAS_${arch}_DEF)
121 set(CAN_TARGET_${arch} FALSE)
122 elseif(TEST_COMPILE_ONLY)
123 try_compile_only(CAN_TARGET_${arch}
124 SOURCE "#include <limits.h>\nint foo(int x, int y) { return x + y; }\n"
125 FLAGS ${TARGET_${arch}_CFLAGS})
127 set(FLAG_NO_EXCEPTIONS "")
128 if(COMPILER_RT_HAS_FNO_EXCEPTIONS_FLAG)
129 set(FLAG_NO_EXCEPTIONS " -fno-exceptions ")
131 set(SAVED_CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS})
132 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${argstring}")
133 try_compile(CAN_TARGET_${arch} ${CMAKE_BINARY_DIR} ${SIMPLE_SOURCE}
134 COMPILE_DEFINITIONS "${TARGET_${arch}_CFLAGS} ${FLAG_NO_EXCEPTIONS}"
135 OUTPUT_VARIABLE TARGET_${arch}_OUTPUT)
136 set(CMAKE_EXE_LINKER_FLAGS ${SAVED_CMAKE_EXE_LINKER_FLAGS})
139 if(${CAN_TARGET_${arch}})
140 list(APPEND COMPILER_RT_SUPPORTED_ARCH ${arch})
141 elseif("${COMPILER_RT_DEFAULT_TARGET_ARCH}" STREQUAL "${arch}" AND
142 COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE)
143 # Bail out if we cannot target the architecture we plan to test.
144 message(FATAL_ERROR "Cannot compile for ${arch}:\n${TARGET_${arch}_OUTPUT}")
148 macro(detect_target_arch)
149 check_symbol_exists(__arm__ "" __ARM)
150 check_symbol_exists(__AVR__ "" __AVR)
151 check_symbol_exists(__aarch64__ "" __AARCH64)
152 check_symbol_exists(__x86_64__ "" __X86_64)
153 check_symbol_exists(__i386__ "" __I386)
154 check_symbol_exists(__loongarch__ "" __LOONGARCH)
155 check_symbol_exists(__mips__ "" __MIPS)
156 check_symbol_exists(__mips64__ "" __MIPS64)
157 check_symbol_exists(__powerpc__ "" __PPC)
158 check_symbol_exists(__powerpc64__ "" __PPC64)
159 check_symbol_exists(__powerpc64le__ "" __PPC64LE)
160 check_symbol_exists(__riscv "" __RISCV)
161 check_symbol_exists(__s390x__ "" __S390X)
162 check_symbol_exists(__sparc "" __SPARC)
163 check_symbol_exists(__sparcv9 "" __SPARCV9)
164 check_symbol_exists(__wasm32__ "" __WEBASSEMBLY32)
165 check_symbol_exists(__wasm64__ "" __WEBASSEMBLY64)
166 check_symbol_exists(__ve__ "" __VE)
168 add_default_target_arch(arm)
170 add_default_target_arch(avr)
172 add_default_target_arch(aarch64)
174 if(CMAKE_SIZEOF_VOID_P EQUAL "4")
175 add_default_target_arch(x32)
176 elseif(CMAKE_SIZEOF_VOID_P EQUAL "8")
177 add_default_target_arch(x86_64)
179 message(FATAL_ERROR "Unsupported pointer size for X86_64")
182 add_default_target_arch(i386)
184 if(CMAKE_SIZEOF_VOID_P EQUAL "4")
185 add_default_target_arch(loongarch32)
186 elseif(CMAKE_SIZEOF_VOID_P EQUAL "8")
187 add_default_target_arch(loongarch64)
189 message(FATAL_ERROR "Unsupported pointer size for LoongArch")
191 elseif(__MIPS64) # must be checked before __MIPS
192 add_default_target_arch(mips64)
194 add_default_target_arch(mips)
195 elseif(__PPC64) # must be checked before __PPC
196 add_default_target_arch(powerpc64)
198 add_default_target_arch(powerpc64le)
200 add_default_target_arch(powerpc)
202 if(CMAKE_SIZEOF_VOID_P EQUAL "4")
203 add_default_target_arch(riscv32)
204 elseif(CMAKE_SIZEOF_VOID_P EQUAL "8")
205 add_default_target_arch(riscv64)
207 message(FATAL_ERROR "Unsupport XLEN for RISC-V")
210 add_default_target_arch(s390x)
212 add_default_target_arch(sparcv9)
214 add_default_target_arch(sparc)
215 elseif(__WEBASSEMBLY32)
216 add_default_target_arch(wasm32)
217 elseif(__WEBASSEMBLY64)
218 add_default_target_arch(wasm64)
220 add_default_target_arch(ve)
224 function(get_compiler_rt_root_source_dir ROOT_DIR_VAR)
225 # Compute the path to the root of the Compiler-RT source tree
226 # regardless of how the project was configured.
228 # This function is useful because using `${CMAKE_SOURCE_DIR}`
229 # is error prone due to the numerous ways Compiler-RT can be
232 # `ROOT_DIR_VAR` - the name of the variable to write the result to.
234 # TODO(dliew): When CMake min version is 3.17 or newer use
235 # `CMAKE_CURRENT_FUNCTION_LIST_DIR` instead.
236 if ("${ROOT_DIR_VAR}" STREQUAL "")
237 message(FATAL_ERROR "ROOT_DIR_VAR cannot be empty")
240 # Compiler-rt supports different source root paths.
241 # Handle each case here.
242 set(PATH_TO_COMPILER_RT_SOURCE_ROOT "")
243 if (DEFINED CompilerRTBuiltins_SOURCE_DIR)
244 # Compiler-RT Builtins standalone build.
245 # `llvm-project/compiler-rt/lib/builtins`
246 set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CompilerRTBuiltins_SOURCE_DIR}/../../")
247 elseif (DEFINED CompilerRTCRT_SOURCE_DIR)
248 # Compiler-RT CRT standalone build.
249 # `llvm-project/compiler-rt/lib/crt`
250 set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CompilerRTCRT_SOURCE_DIR}/../../")
251 elseif(DEFINED CompilerRT_SOURCE_DIR)
252 # Compiler-RT standalone build.
253 # `llvm-project/compiler-rt`
254 set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CompilerRT_SOURCE_DIR}")
255 elseif (EXISTS "${CMAKE_SOURCE_DIR}/../compiler-rt")
256 # In tree build with LLVM as the root project.
257 # See `llvm-project/projects/`.
258 # Assumes monorepo layout.
259 set(PATH_TO_COMPILER_RT_SOURCE_ROOT "${CMAKE_SOURCE_DIR}/../compiler-rt")
261 message(FATAL_ERROR "Unhandled Compiler-RT source root configuration.")
264 get_filename_component(ROOT_DIR "${PATH_TO_COMPILER_RT_SOURCE_ROOT}" ABSOLUTE)
265 if (NOT EXISTS "${ROOT_DIR}")
266 message(FATAL_ERROR "Path \"${ROOT_DIR}\" doesn't exist")
269 # Sanity check: Make sure we can locate the current source file via the
271 set(PATH_TO_CURRENT_FILE "${ROOT_DIR}/cmake/Modules/CompilerRTUtils.cmake")
272 if (NOT EXISTS "${PATH_TO_CURRENT_FILE}")
273 message(FATAL_ERROR "Could not find \"${PATH_TO_CURRENT_FILE}\"")
276 set("${ROOT_DIR_VAR}" "${ROOT_DIR}" PARENT_SCOPE)
279 macro(load_llvm_config)
280 if (LLVM_CONFIG_PATH AND NOT LLVM_CMAKE_DIR)
282 "LLVM_CONFIG_PATH is deprecated, please use LLVM_CMAKE_DIR instead")
283 # Compute the path to the LLVM install prefix and pass it as LLVM_CMAKE_DIR,
284 # CMake will locate the appropriate lib*/cmake subdirectory from there.
285 # For example. for -DLLVM_CONFIG_PATH=/usr/lib/llvm/16/bin/llvm-config
286 # this will yield LLVM_CMAKE_DIR=/usr/lib/llvm/16.
287 get_filename_component(LLVM_CMAKE_DIR "${LLVM_CONFIG_PATH}" DIRECTORY)
288 get_filename_component(LLVM_CMAKE_DIR "${LLVM_CMAKE_DIR}" DIRECTORY)
291 # Compute path to LLVM sources assuming the monorepo layout.
292 # We don't set `LLVM_MAIN_SRC_DIR` directly to avoid overriding a user provided
294 get_compiler_rt_root_source_dir(COMPILER_RT_ROOT_SRC_PATH)
295 get_filename_component(LLVM_MAIN_SRC_DIR_DEFAULT "${COMPILER_RT_ROOT_SRC_PATH}/../llvm" ABSOLUTE)
296 if (NOT EXISTS "${LLVM_MAIN_SRC_DIR_DEFAULT}")
297 # TODO(dliew): Remove this legacy fallback path.
299 "LLVM source tree not found at \"${LLVM_MAIN_SRC_DIR_DEFAULT}\". "
300 "You are not using the monorepo layout. This configuration is DEPRECATED.")
303 find_package(LLVM HINTS "${LLVM_CMAKE_DIR}")
305 message(WARNING "UNSUPPORTED COMPILER-RT CONFIGURATION DETECTED: "
306 "LLVM cmake package not found.\n"
307 "Reconfigure with -DLLVM_CMAKE_DIR=/path/to/llvm.")
309 list(APPEND CMAKE_MODULE_PATH "${LLVM_DIR}")
310 # Turn into CACHE PATHs for overwritting
311 set(LLVM_BINARY_DIR "${LLVM_BINARY_DIR}" CACHE PATH "Path to LLVM build tree")
312 set(LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE PATH "Path to llvm/lib")
313 set(LLVM_TOOLS_BINARY_DIR "${LLVM_TOOLS_BINARY_DIR}" CACHE PATH "Path to llvm/bin")
314 set(LLVM_INCLUDE_DIR ${LLVM_INCLUDE_DIRS} CACHE PATH "Path to llvm/include and any other header dirs needed")
316 list(FIND LLVM_AVAILABLE_LIBS LLVMXRay XRAY_INDEX)
317 set(COMPILER_RT_HAS_LLVMXRAY TRUE)
318 if (XRAY_INDEX EQUAL -1)
319 message(WARNING "LLVMXRay not found in LLVM_AVAILABLE_LIBS")
320 set(COMPILER_RT_HAS_LLVMXRAY FALSE)
323 list(FIND LLVM_AVAILABLE_LIBS LLVMTestingSupport TESTINGSUPPORT_INDEX)
324 set(COMPILER_RT_HAS_LLVMTESTINGSUPPORT TRUE)
325 if (TESTINGSUPPORT_INDEX EQUAL -1)
326 message(WARNING "LLVMTestingSupport not found in LLVM_AVAILABLE_LIBS")
327 set(COMPILER_RT_HAS_LLVMTESTINGSUPPORT FALSE)
331 set(LLVM_LIBRARY_OUTPUT_INTDIR
332 ${LLVM_BINARY_DIR}/${CMAKE_CFG_INTDIR}/lib${LLVM_LIBDIR_SUFFIX})
334 set(LLVM_MAIN_SRC_DIR "${LLVM_MAIN_SRC_DIR_DEFAULT}" CACHE PATH "Path to LLVM source tree")
335 message(STATUS "LLVM_MAIN_SRC_DIR: \"${LLVM_MAIN_SRC_DIR}\"")
336 if (NOT EXISTS "${LLVM_MAIN_SRC_DIR}")
337 # TODO(dliew): Make this a hard error
338 message(WARNING "LLVM_MAIN_SRC_DIR (${LLVM_MAIN_SRC_DIR}) does not exist. "
339 "You can override the inferred path by adding "
340 "`-DLLVM_MAIN_SRC_DIR=<path_to_llvm_src>` to your CMake invocation "
341 "where `<path_to_llvm_src>` is the path to the `llvm` directory in "
342 "the `llvm-project` repo. "
343 "This will be treated as error in the future.")
347 # This configuration tries to configure without the prescence of `LLVMConfig.cmake`. It is
348 # intended for testing purposes (generating the lit test suites) and will likely not support
349 # a build of the runtimes in compiler-rt.
350 include(CompilerRTMockLLVMCMakeConfig)
351 compiler_rt_mock_llvm_cmake_config()
356 macro(construct_compiler_rt_default_triple)
357 if(COMPILER_RT_DEFAULT_TARGET_ONLY)
358 if(DEFINED COMPILER_RT_DEFAULT_TARGET_TRIPLE)
359 message(FATAL_ERROR "COMPILER_RT_DEFAULT_TARGET_TRIPLE isn't supported when building for default target only")
361 if ("${CMAKE_C_COMPILER_TARGET}" STREQUAL "")
362 message(FATAL_ERROR "CMAKE_C_COMPILER_TARGET must also be set when COMPILER_RT_DEFAULT_TARGET_ONLY is ON")
364 message(STATUS "cmake c compiler target: ${CMAKE_C_COMPILER_TARGET}")
365 set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${CMAKE_C_COMPILER_TARGET})
367 set(COMPILER_RT_DEFAULT_TARGET_TRIPLE ${LLVM_TARGET_TRIPLE} CACHE STRING
368 "Default triple for which compiler-rt runtimes will be built.")
371 string(REPLACE "-" ";" LLVM_TARGET_TRIPLE_LIST ${COMPILER_RT_DEFAULT_TARGET_TRIPLE})
372 list(GET LLVM_TARGET_TRIPLE_LIST 0 COMPILER_RT_DEFAULT_TARGET_ARCH)
374 # Map various forms of the architecture names to the canonical forms
375 # (as they are used by clang, see getArchNameForCompilerRTLib).
376 if("${COMPILER_RT_DEFAULT_TARGET_ARCH}" MATCHES "^i.86$")
377 # Android uses i686, but that's remapped at a later stage.
378 set(COMPILER_RT_DEFAULT_TARGET_ARCH "i386")
381 # Determine if test target triple is specified explicitly, and doesn't match the
383 if(NOT COMPILER_RT_DEFAULT_TARGET_TRIPLE STREQUAL LLVM_TARGET_TRIPLE)
384 set(COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE TRUE)
386 set(COMPILER_RT_HAS_EXPLICIT_DEFAULT_TARGET_TRIPLE FALSE)
390 # Filter out generic versions of routines that are re-implemented in an
391 # architecture specific manner. This prevents multiple definitions of the same
392 # symbols, making the symbol selection non-deterministic.
394 # We follow the convention that a source file that exists in a sub-directory
395 # (e.g. `ppc/divtc3.c`) is architecture-specific and that if a generic
396 # implementation exists it will be a top-level source file with the same name
397 # modulo the file extension (e.g. `divtc3.c`).
398 function(filter_builtin_sources inout_var name)
399 set(intermediate ${${inout_var}})
400 foreach(_file ${intermediate})
401 get_filename_component(_file_dir ${_file} DIRECTORY)
402 if (NOT "${_file_dir}" STREQUAL "")
403 # Architecture specific file. If a generic version exists, print a notice
404 # and ensure that it is removed from the file list.
405 get_filename_component(_name ${_file} NAME)
406 string(REGEX REPLACE "\\.S$" ".c" _cname "${_name}")
407 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${_cname}")
408 message(STATUS "For ${name} builtins preferring ${_file} to ${_cname}")
409 list(REMOVE_ITEM intermediate ${_cname})
413 set(${inout_var} ${intermediate} PARENT_SCOPE)
416 function(get_compiler_rt_target arch variable)
417 string(FIND ${COMPILER_RT_DEFAULT_TARGET_TRIPLE} "-" dash_index)
418 string(SUBSTRING ${COMPILER_RT_DEFAULT_TARGET_TRIPLE} ${dash_index} -1 triple_suffix)
419 string(SUBSTRING ${COMPILER_RT_DEFAULT_TARGET_TRIPLE} 0 ${dash_index} triple_cpu)
420 if(COMPILER_RT_DEFAULT_TARGET_ONLY)
421 # Use exact spelling when building only for the target specified to CMake.
422 set(target "${COMPILER_RT_DEFAULT_TARGET_TRIPLE}")
423 elseif(ANDROID AND ${arch} STREQUAL "i386")
424 set(target "i686${triple_suffix}")
425 elseif(${arch} STREQUAL "amd64")
426 set(target "x86_64${triple_suffix}")
427 elseif(${arch} STREQUAL "sparc64")
428 set(target "sparcv9${triple_suffix}")
429 elseif("${arch}" MATCHES "mips64|mips64el")
430 string(REGEX REPLACE "-gnu.*" "-gnuabi64" triple_suffix_gnu "${triple_suffix}")
431 string(REGEX REPLACE "mipsisa32" "mipsisa64" triple_cpu_mips "${triple_cpu}")
432 string(REGEX REPLACE "^mips$" "mips64" triple_cpu_mips "${triple_cpu_mips}")
433 string(REGEX REPLACE "^mipsel$" "mips64el" triple_cpu_mips "${triple_cpu_mips}")
434 set(target "${triple_cpu_mips}${triple_suffix_gnu}")
435 elseif("${arch}" MATCHES "mips|mipsel")
436 string(REGEX REPLACE "-gnuabi.*" "-gnu" triple_suffix_gnu "${triple_suffix}")
437 string(REGEX REPLACE "mipsisa64" "mipsisa32" triple_cpu_mips "${triple_cpu}")
438 string(REGEX REPLACE "mips64" "mips" triple_cpu_mips "${triple_cpu_mips}")
439 set(target "${triple_cpu_mips}${triple_suffix_gnu}")
440 elseif("${arch}" MATCHES "^arm")
441 # Arch is arm, armhf, armv6m (anything else would come from using
442 # COMPILER_RT_DEFAULT_TARGET_ONLY, which is checked above).
443 if (${arch} STREQUAL "armhf")
444 # If we are building for hard float but our ABI is soft float.
445 if ("${triple_suffix}" MATCHES ".*eabi$")
446 # Change "eabi" -> "eabihf"
447 set(triple_suffix "${triple_suffix}hf")
449 # ABI is already set in the triple, don't repeat it in the architecture.
452 # If we are building for soft float, but the triple's ABI is hard float.
453 if ("${triple_suffix}" MATCHES ".*eabihf$")
454 # Change "eabihf" -> "eabi"
455 string(REGEX REPLACE "hf$" "" triple_suffix "${triple_suffix}")
458 set(target "${arch}${triple_suffix}")
460 set(target "${arch}${triple_suffix}")
462 set(${variable} ${target} PARENT_SCOPE)
465 function(get_compiler_rt_install_dir arch install_dir)
466 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
467 get_compiler_rt_target(${arch} target)
468 set(${install_dir} ${COMPILER_RT_INSTALL_LIBRARY_DIR}/${target} PARENT_SCOPE)
470 set(${install_dir} ${COMPILER_RT_INSTALL_LIBRARY_DIR} PARENT_SCOPE)
474 function(get_compiler_rt_output_dir arch output_dir)
475 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
476 get_compiler_rt_target(${arch} target)
477 set(${output_dir} ${COMPILER_RT_OUTPUT_LIBRARY_DIR}/${target} PARENT_SCOPE)
479 set(${output_dir} ${COMPILER_RT_OUTPUT_LIBRARY_DIR} PARENT_SCOPE)
483 # compiler_rt_process_sources(
486 # [ADDITIONAL_HEADERS <header> ...]
489 # Process the provided sources and write the list of new sources
490 # into `<OUTPUT_VAR>`.
492 # ADDITIONAL_HEADERS - Adds the supplied header to list of sources for IDEs.
494 # This function is very similar to `llvm_process_sources()` but exists here
495 # because we need to support standalone builds of compiler-rt.
496 function(compiler_rt_process_sources OUTPUT_VAR)
497 cmake_parse_arguments(
504 set(sources ${ARG_UNPARSED_ARGUMENTS})
506 if (XCODE OR MSVC_IDE OR CMAKE_EXTRA_GENERATOR)
507 # For IDEs we need to tell CMake about header files.
508 # Otherwise they won't show up in UI.
509 set(headers ${ARG_ADDITIONAL_HEADERS})
510 list(LENGTH headers headers_length)
511 if (${headers_length} GREATER 0)
512 set_source_files_properties(${headers}
513 PROPERTIES HEADER_FILE_ONLY ON)
516 set("${OUTPUT_VAR}" ${sources} ${headers} PARENT_SCOPE)
519 # Create install targets for a library and its parent component (if specified).
520 function(add_compiler_rt_install_targets name)
521 cmake_parse_arguments(ARG "" "PARENT_TARGET" "" ${ARGN})
523 if(ARG_PARENT_TARGET AND NOT TARGET install-${ARG_PARENT_TARGET})
524 # The parent install target specifies the parent component to scrape up
525 # anything not installed by the individual install targets, and to handle
526 # installation when running the multi-configuration generators.
527 add_custom_target(install-${ARG_PARENT_TARGET}
528 DEPENDS ${ARG_PARENT_TARGET}
529 COMMAND "${CMAKE_COMMAND}"
530 -DCMAKE_INSTALL_COMPONENT=${ARG_PARENT_TARGET}
531 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
532 add_custom_target(install-${ARG_PARENT_TARGET}-stripped
533 DEPENDS ${ARG_PARENT_TARGET}
534 COMMAND "${CMAKE_COMMAND}"
535 -DCMAKE_INSTALL_COMPONENT=${ARG_PARENT_TARGET}
536 -DCMAKE_INSTALL_DO_STRIP=1
537 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
538 set_target_properties(install-${ARG_PARENT_TARGET} PROPERTIES
539 FOLDER "Compiler-RT Misc")
540 set_target_properties(install-${ARG_PARENT_TARGET}-stripped PROPERTIES
541 FOLDER "Compiler-RT Misc")
542 add_dependencies(install-compiler-rt install-${ARG_PARENT_TARGET})
543 add_dependencies(install-compiler-rt-stripped install-${ARG_PARENT_TARGET}-stripped)
546 # We only want to generate per-library install targets if you aren't using
547 # an IDE because the extra targets get cluttered in IDEs.
548 if(NOT CMAKE_CONFIGURATION_TYPES)
549 add_custom_target(install-${name}
551 COMMAND "${CMAKE_COMMAND}"
552 -DCMAKE_INSTALL_COMPONENT=${name}
553 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
554 add_custom_target(install-${name}-stripped
556 COMMAND "${CMAKE_COMMAND}"
557 -DCMAKE_INSTALL_COMPONENT=${name}
558 -DCMAKE_INSTALL_DO_STRIP=1
559 -P "${CMAKE_BINARY_DIR}/cmake_install.cmake")
560 # If you have a parent target specified, we bind the new install target
561 # to the parent install target.
562 if(LIB_PARENT_TARGET)
563 add_dependencies(install-${LIB_PARENT_TARGET} install-${name})
564 add_dependencies(install-${LIB_PARENT_TARGET}-stripped install-${name}-stripped)
569 # Add warnings to catch potential errors that can lead to security
571 function(add_security_warnings out_flags macosx_sdk_version)
572 set(flags "${${out_flags}}")
574 append_list_if(COMPILER_RT_HAS_ARRAY_BOUNDS_FLAG -Werror=array-bounds flags)
575 append_list_if(COMPILER_RT_HAS_UNINITIALIZED_FLAG -Werror=uninitialized flags)
576 append_list_if(COMPILER_RT_HAS_SHADOW_FLAG -Werror=shadow flags)
577 append_list_if(COMPILER_RT_HAS_EMPTY_BODY_FLAG -Werror=empty-body flags)
578 append_list_if(COMPILER_RT_HAS_SIZEOF_POINTER_MEMACCESS_FLAG -Werror=sizeof-pointer-memaccess flags)
579 append_list_if(COMPILER_RT_HAS_SIZEOF_ARRAY_ARGUMENT_FLAG -Werror=sizeof-array-argument flags)
580 append_list_if(COMPILER_RT_HAS_SUSPICIOUS_MEMACCESS_FLAG -Werror=suspicious-memaccess flags)
581 append_list_if(COMPILER_RT_HAS_BUILTIN_MEMCPY_CHK_SIZE_FLAG -Werror=builtin-memcpy-chk-size flags)
582 append_list_if(COMPILER_RT_HAS_ARRAY_BOUNDS_POINTER_ARITHMETIC_FLAG -Werror=array-bounds-pointer-arithmetic flags)
583 append_list_if(COMPILER_RT_HAS_RETURN_STACK_ADDRESS_FLAG -Werror=return-stack-address flags)
584 append_list_if(COMPILER_RT_HAS_SIZEOF_ARRAY_DECAY_FLAG -Werror=sizeof-array-decay flags)
585 append_list_if(COMPILER_RT_HAS_FORMAT_INSUFFICIENT_ARGS_FLAG -Werror=format-insufficient-args flags)
586 append_list_if(COMPILER_RT_HAS_BUILTIN_FORMAL_SECURITY_FLAG -Werror=format-security flags)
587 append_list_if(COMPILER_RT_HAS_SIZEOF_ARRAY_DIV_FLAG -Werror=sizeof-array-div)
588 append_list_if(COMPILER_RT_HAS_SIZEOF_POINTER_DIV_FLAG -Werror=sizeof-pointer-div)
590 # Add -Wformat-nonliteral only if we can avoid adding the definition of
591 # eprintf. On Apple platforms, eprintf is needed only on macosx and only if
592 # its version is older than 10.7.
593 if ("${macosx_sdk_version}" VERSION_GREATER_EQUAL 10.7)
594 list(APPEND flags -Werror=format-nonliteral -DDONT_DEFINE_EPRINTF)
597 set(${out_flags} "${flags}" PARENT_SCOPE)