2015-03-14 05:39:29 +08:00
|
|
|
# On Windows, CMAKE_*_FLAGS are built for MSVC but we use the GCC clang.exe,
|
|
|
|
# which uses completely different flags. Translate some common flag types, and
|
|
|
|
# drop the rest.
|
|
|
|
function(translate_msvc_cflags out_flags msvc_flags)
|
|
|
|
# Insert an empty string in the list to simplify processing.
|
|
|
|
set(msvc_flags ";${msvc_flags}")
|
|
|
|
|
|
|
|
# Canonicalize /flag to -flag.
|
|
|
|
string(REPLACE ";/" ";-" msvc_flags "${msvc_flags}")
|
|
|
|
|
|
|
|
# Make space separated -D and -U flags into joined flags.
|
|
|
|
string(REGEX REPLACE ";-\([DU]\);" ";-\\1" msvc_flags "${msvc_flags}")
|
|
|
|
|
|
|
|
set(clang_flags "")
|
|
|
|
foreach(flag ${msvc_flags})
|
|
|
|
if ("${flag}" MATCHES "^-[DU]")
|
|
|
|
# Pass through basic command line macro definitions (-DNDEBUG).
|
|
|
|
list(APPEND clang_flags "${flag}")
|
|
|
|
elseif ("${flag}" MATCHES "^-O[2x]")
|
|
|
|
# Canonicalize normal optimization flags to -O2.
|
|
|
|
list(APPEND clang_flags "-O2")
|
|
|
|
endif()
|
|
|
|
endforeach()
|
|
|
|
set(${out_flags} "${clang_flags}" PARENT_SCOPE)
|
|
|
|
endfunction()
|
|
|
|
|
2017-07-29 01:32:37 +08:00
|
|
|
# Compile a sanitizer test with a freshly built clang
|
|
|
|
# for a given architecture, adding the result to the object list.
|
|
|
|
# - obj_list: output list of objects, populated by path
|
2017-08-16 02:35:02 +08:00
|
|
|
# of a generated object file.
|
2017-07-29 01:32:37 +08:00
|
|
|
# - source: source file of a test.
|
|
|
|
# - arch: architecture to compile for.
|
|
|
|
# sanitizer_test_compile(<obj_list> <source> <arch>
|
|
|
|
# KIND <custom namespace>
|
|
|
|
# COMPILE_DEPS <list of compile-time dependencies>
|
|
|
|
# DEPS <list of dependencies>
|
|
|
|
# CFLAGS <list of flags>
|
|
|
|
# )
|
2017-08-16 02:35:02 +08:00
|
|
|
function(sanitizer_test_compile obj_list source arch)
|
2017-07-29 01:32:37 +08:00
|
|
|
cmake_parse_arguments(TEST
|
|
|
|
"" "" "KIND;COMPILE_DEPS;DEPS;CFLAGS" ${ARGN})
|
|
|
|
get_filename_component(basename ${source} NAME)
|
2017-08-16 02:35:02 +08:00
|
|
|
set(output_obj
|
|
|
|
"${CMAKE_CFG_RESOLVED_INTDIR}${obj_list}.${basename}.${arch}${TEST_KIND}.o")
|
2017-07-29 01:32:37 +08:00
|
|
|
|
|
|
|
# Write out architecture-specific flags into TARGET_CFLAGS variable.
|
|
|
|
get_target_flags_for_arch(${arch} TARGET_CFLAGS)
|
|
|
|
set(COMPILE_DEPS ${TEST_COMPILE_DEPS})
|
|
|
|
if(NOT COMPILER_RT_STANDALONE_BUILD)
|
|
|
|
list(APPEND COMPILE_DEPS ${TEST_DEPS})
|
|
|
|
endif()
|
|
|
|
clang_compile(${output_obj} ${source}
|
|
|
|
CFLAGS ${TEST_CFLAGS} ${TARGET_CFLAGS}
|
2017-10-13 02:51:37 +08:00
|
|
|
DEPS ${COMPILE_DEPS})
|
2017-07-29 01:32:37 +08:00
|
|
|
list(APPEND ${obj_list} ${output_obj})
|
2017-08-16 02:35:02 +08:00
|
|
|
set("${obj_list}" "${${obj_list}}" PARENT_SCOPE)
|
|
|
|
endfunction()
|
2017-07-29 01:32:37 +08:00
|
|
|
|
2014-02-19 21:01:03 +08:00
|
|
|
# Compile a source into an object file with COMPILER_RT_TEST_COMPILER using
|
2012-12-19 20:33:39 +08:00
|
|
|
# a provided compile flags and dependenices.
|
|
|
|
# clang_compile(<object> <source>
|
|
|
|
# CFLAGS <list of compile flags>
|
|
|
|
# DEPS <list of dependencies>)
|
2017-08-16 02:38:14 +08:00
|
|
|
function(clang_compile object_file source)
|
2015-06-19 11:39:24 +08:00
|
|
|
cmake_parse_arguments(SOURCE "" "" "CFLAGS;DEPS" ${ARGN})
|
2012-12-19 20:33:39 +08:00
|
|
|
get_filename_component(source_rpath ${source} REALPATH)
|
2014-02-19 21:01:03 +08:00
|
|
|
if(NOT COMPILER_RT_STANDALONE_BUILD)
|
2015-01-07 04:58:40 +08:00
|
|
|
list(APPEND SOURCE_DEPS clang compiler-rt-headers)
|
2014-02-19 21:01:03 +08:00
|
|
|
endif()
|
2017-08-25 10:36:36 +08:00
|
|
|
if (TARGET CompilerRTUnitTestCheckCxx)
|
|
|
|
list(APPEND SOURCE_DEPS CompilerRTUnitTestCheckCxx)
|
2014-09-11 01:23:58 +08:00
|
|
|
endif()
|
2020-07-24 05:17:37 +08:00
|
|
|
if(COMPILER_RT_STANDALONE_BUILD)
|
|
|
|
# Only add global flags in standalone build.
|
|
|
|
string(REGEX MATCH "[.](cc|cpp)$" is_cxx ${source_rpath})
|
|
|
|
if(is_cxx)
|
|
|
|
string(REPLACE " " ";" global_flags "${CMAKE_CXX_FLAGS}")
|
|
|
|
else()
|
|
|
|
string(REPLACE " " ";" global_flags "${CMAKE_C_FLAGS}")
|
|
|
|
endif()
|
2015-03-14 05:39:29 +08:00
|
|
|
|
2020-07-24 05:17:37 +08:00
|
|
|
if (MSVC)
|
|
|
|
translate_msvc_cflags(global_flags "${global_flags}")
|
|
|
|
endif()
|
2015-03-14 05:39:29 +08:00
|
|
|
|
2020-07-24 05:17:37 +08:00
|
|
|
if (APPLE)
|
|
|
|
set(global_flags ${OSX_SYSROOT_FLAG} ${global_flags})
|
|
|
|
endif()
|
2015-08-21 01:35:56 +08:00
|
|
|
|
2020-07-24 05:17:37 +08:00
|
|
|
# Ignore unknown warnings. CMAKE_CXX_FLAGS may contain GCC-specific options
|
|
|
|
# which are not supported by Clang.
|
|
|
|
list(APPEND global_flags -Wno-unknown-warning-option)
|
|
|
|
set(compile_flags ${global_flags} ${SOURCE_CFLAGS})
|
|
|
|
else()
|
|
|
|
set(compile_flags ${SOURCE_CFLAGS})
|
|
|
|
endif()
|
2020-08-01 07:09:13 +08:00
|
|
|
|
|
|
|
string(REGEX MATCH "[.](m|mm)$" is_objc ${source_rpath})
|
|
|
|
if (is_objc)
|
|
|
|
list(APPEND compile_flags "-ObjC")
|
|
|
|
endif()
|
|
|
|
|
2012-12-19 20:33:39 +08:00
|
|
|
add_custom_command(
|
|
|
|
OUTPUT ${object_file}
|
2014-05-12 16:55:20 +08:00
|
|
|
COMMAND ${COMPILER_RT_TEST_COMPILER} ${compile_flags} -c
|
2014-05-28 16:38:13 +08:00
|
|
|
-o "${object_file}"
|
2014-02-19 21:01:03 +08:00
|
|
|
${source_rpath}
|
2012-12-19 20:33:39 +08:00
|
|
|
MAIN_DEPENDENCY ${source}
|
2014-02-19 21:01:03 +08:00
|
|
|
DEPENDS ${SOURCE_DEPS})
|
2017-08-16 02:38:14 +08:00
|
|
|
endfunction()
|
2014-09-11 01:23:58 +08:00
|
|
|
|
|
|
|
# On Darwin, there are no system-wide C++ headers and the just-built clang is
|
|
|
|
# therefore not able to compile C++ files unless they are copied/symlinked into
|
|
|
|
# ${LLVM_BINARY_DIR}/include/c++
|
|
|
|
# The just-built clang is used to build compiler-rt unit tests. Let's detect
|
|
|
|
# this before we try to build the tests and print out a suggestion how to fix
|
|
|
|
# it.
|
|
|
|
# On other platforms, this is currently not an issue.
|
|
|
|
macro(clang_compiler_add_cxx_check)
|
|
|
|
if (APPLE)
|
|
|
|
set(CMD
|
2015-08-21 01:35:56 +08:00
|
|
|
"echo '#include <iostream>' | ${COMPILER_RT_TEST_COMPILER} ${OSX_SYSROOT_FLAG} -E -x c++ - > /dev/null"
|
2014-09-11 01:23:58 +08:00
|
|
|
"if [ $? != 0 ] "
|
|
|
|
" then echo"
|
|
|
|
" echo 'Your just-built clang cannot find C++ headers, which are needed to build and run compiler-rt tests.'"
|
|
|
|
" echo 'You should copy or symlink your system C++ headers into ${LLVM_BINARY_DIR}/include/c++'"
|
|
|
|
" if [ -d $(dirname $(dirname $(xcrun -f clang)))/include/c++ ]"
|
|
|
|
" then echo 'e.g. with:'"
|
|
|
|
" echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/include/c++ '${LLVM_BINARY_DIR}/include/'"
|
|
|
|
" elif [ -d $(dirname $(dirname $(xcrun -f clang)))/lib/c++ ]"
|
|
|
|
" then echo 'e.g. with:'"
|
|
|
|
" echo ' cp -r' $(dirname $(dirname $(xcrun -f clang)))/lib/c++ '${LLVM_BINARY_DIR}/include/'"
|
|
|
|
" fi"
|
|
|
|
" echo 'This can also be fixed by checking out the libcxx project from llvm.org and installing the headers'"
|
|
|
|
" echo 'into your build directory:'"
|
2016-02-02 05:08:16 +08:00
|
|
|
" echo ' cd ${LLVM_MAIN_SRC_DIR}/projects && svn co http://llvm.org/svn/llvm-project/libcxx/trunk libcxx'"
|
|
|
|
" echo ' cd ${LLVM_BINARY_DIR} && make -C ${LLVM_MAIN_SRC_DIR}/projects/libcxx installheaders HEADER_DIR=${LLVM_BINARY_DIR}/include'"
|
2014-09-11 01:23:58 +08:00
|
|
|
" echo"
|
|
|
|
" false"
|
|
|
|
"fi"
|
|
|
|
)
|
2017-08-25 10:36:36 +08:00
|
|
|
add_custom_target(CompilerRTUnitTestCheckCxx
|
2014-09-11 01:23:58 +08:00
|
|
|
COMMAND bash -c "${CMD}"
|
|
|
|
COMMENT "Checking that just-built clang can find C++ headers..."
|
|
|
|
VERBATIM)
|
2019-02-13 14:49:47 +08:00
|
|
|
if (NOT COMPILER_RT_STANDALONE_BUILD AND NOT RUNTIMES_BUILD)
|
2017-08-25 10:36:36 +08:00
|
|
|
ADD_DEPENDENCIES(CompilerRTUnitTestCheckCxx clang)
|
|
|
|
endif()
|
2014-09-11 01:23:58 +08:00
|
|
|
endif()
|
|
|
|
endmacro()
|