[CMake] Add OBJECT_LIBS option to add_compiler_rt_runtime, and refactored asan call site to use it.

Summary: This is one more step to allow us to eliminate platform-specific code from the library CMakeLists files. Subsequent patches will refactor all call sites to use this and other new features.

Reviewers: filcab, bogner, kubabrecka, zaks.anna, glider, samsonov

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D12339

llvm-svn: 246047
This commit is contained in:
Chris Bieneman 2015-08-26 18:33:51 +00:00
parent 3354fe473f
commit 7173f07d70
3 changed files with 59 additions and 50 deletions

View File

@ -46,6 +46,15 @@ function(add_compiler_rt_object_libraries name)
endforeach() endforeach()
endfunction() endfunction()
# Takes a list of object library targets, and a suffix and appends the proper
# TARGET_OBJECTS string to the output variable.
# format_object_libs(<output> <suffix> ...)
macro(format_object_libs output suffix)
foreach(lib ${ARGN})
list(APPEND ${output} $<TARGET_OBJECTS:${lib}.${suffix}>)
endforeach()
endmacro()
# Adds static or shared runtime for a list of architectures and operating # Adds static or shared runtime for a list of architectures and operating
# systems and puts it in the proper directory in the build and install trees. # systems and puts it in the proper directory in the build and install trees.
# add_compiler_rt_runtime(<name> # add_compiler_rt_runtime(<name>
@ -57,6 +66,7 @@ endfunction()
# LINKFLAGS <linker flags> # LINKFLAGS <linker flags>
# DEFS <compile definitions> # DEFS <compile definitions>
# LINK_LIBS <linked libraries> (only for shared library) # LINK_LIBS <linked libraries> (only for shared library)
# OBJECT_LIBS <object libraries to use as sources>
# PARENT_TARGET <convenience parent target>) # PARENT_TARGET <convenience parent target>)
function(add_compiler_rt_runtime name type) function(add_compiler_rt_runtime name type)
if(NOT type MATCHES "^(STATIC|SHARED)$") if(NOT type MATCHES "^(STATIC|SHARED)$")
@ -66,7 +76,7 @@ function(add_compiler_rt_runtime name type)
cmake_parse_arguments(LIB cmake_parse_arguments(LIB
"" ""
"PARENT_TARGET" "PARENT_TARGET"
"OS;ARCHS;SOURCES;CFLAGS;LINKFLAGS;DEFS;LINK_LIBS" "OS;ARCHS;SOURCES;CFLAGS;LINKFLAGS;DEFS;LINK_LIBS;OBJECT_LIBS"
${ARGN}) ${ARGN})
set(libnames) set(libnames)
if(APPLE) if(APPLE)
@ -82,6 +92,8 @@ function(add_compiler_rt_runtime name type)
list(APPEND libnames ${libname}) list(APPEND libnames ${libname})
set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${LIB_CFLAGS}) set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${LIB_CFLAGS})
set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX}) set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
set(sources_${libname} ${LIB_SOURCES})
format_object_libs(sources_${libname} ${os} ${LIB_OBJECT_LIBS})
endif() endif()
endforeach() endforeach()
else() else()
@ -102,6 +114,8 @@ function(add_compiler_rt_runtime name type)
set(output_name_${libname} ${name}-${arch}${COMPILER_RT_OS_SUFFIX}) set(output_name_${libname} ${name}-${arch}${COMPILER_RT_OS_SUFFIX})
endif() endif()
endif() endif()
set(sources_${libname} ${LIB_SOURCES})
format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS})
set(libnames ${libnames} ${libname}) set(libnames ${libnames} ${libname})
set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS}) set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
endforeach() endforeach()
@ -116,7 +130,7 @@ function(add_compiler_rt_runtime name type)
endif() endif()
foreach(libname ${libnames}) foreach(libname ${libnames})
add_library(${libname} ${type} ${LIB_SOURCES}) add_library(${libname} ${type} ${sources_${libname}})
set_target_compile_flags(${libname} ${extra_cflags_${libname}}) set_target_compile_flags(${libname} ${extra_cflags_${libname}})
set_target_link_flags(${libname} ${extra_linkflags_${libname}}) set_target_link_flags(${libname} ${extra_linkflags_${libname}})
set_property(TARGET ${libname} APPEND PROPERTY set_property(TARGET ${libname} APPEND PROPERTY

View File

@ -73,14 +73,15 @@ append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ ASAN_DYNAMIC_LIBS)
append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS) append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
# Compile ASan sources into an object library. # Compile ASan sources into an object library.
if(APPLE)
add_compiler_rt_object_libraries(RTAsan add_compiler_rt_object_libraries(RTAsan_dynamic
OS ${SANITIZER_COMMON_SUPPORTED_OS} OS ${SANITIZER_COMMON_SUPPORTED_OS}
ARCHS ${ASAN_SUPPORTED_ARCH} ARCHS ${ASAN_SUPPORTED_ARCH}
SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES} SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
CFLAGS ${ASAN_DYNAMIC_CFLAGS} CFLAGS ${ASAN_DYNAMIC_CFLAGS}
DEFS ${ASAN_DYNAMIC_DEFINITIONS}) DEFS ${ASAN_DYNAMIC_DEFINITIONS})
else()
if(NOT APPLE)
add_compiler_rt_object_libraries(RTAsan add_compiler_rt_object_libraries(RTAsan
ARCHS ${ASAN_SUPPORTED_ARCH} ARCHS ${ASAN_SUPPORTED_ARCH}
SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS} SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
@ -93,11 +94,6 @@ else()
ARCHS ${ASAN_SUPPORTED_ARCH} ARCHS ${ASAN_SUPPORTED_ARCH}
SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS} SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
DEFS ${ASAN_COMMON_DEFINITIONS}) DEFS ${ASAN_COMMON_DEFINITIONS})
add_compiler_rt_object_libraries(RTAsan_dynamic
ARCHS ${ASAN_SUPPORTED_ARCH}
SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
CFLAGS ${ASAN_DYNAMIC_CFLAGS}
DEFS ${ASAN_DYNAMIC_DEFINITIONS})
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc "") file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc "")
add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
@ -110,57 +106,56 @@ endif()
# Build ASan runtimes shipped with Clang. # Build ASan runtimes shipped with Clang.
add_custom_target(asan) add_custom_target(asan)
if(APPLE) if(APPLE)
foreach (os ${SANITIZER_COMMON_SUPPORTED_OS}) add_compiler_rt_runtime(clang_rt.asan
add_compiler_rt_runtime(clang_rt.asan SHARED
SHARED OS ${SANITIZER_COMMON_SUPPORTED_OS}
OS ${os} ARCHS ${ASAN_SUPPORTED_ARCH}
ARCHS ${ASAN_SUPPORTED_ARCH} OBJECT_LIBS RTAsan_dynamic
SOURCES $<TARGET_OBJECTS:RTAsan.${os}> RTInterception
$<TARGET_OBJECTS:RTInterception.${os}> RTSanitizerCommon
$<TARGET_OBJECTS:RTSanitizerCommon.${os}> RTLSanCommon
$<TARGET_OBJECTS:RTLSanCommon.${os}> RTUbsan
$<TARGET_OBJECTS:RTUbsan.${os}> CFLAGS ${ASAN_DYNAMIC_CFLAGS}
CFLAGS ${ASAN_DYNAMIC_CFLAGS} DEFS ${ASAN_DYNAMIC_DEFINITIONS}
DEFS ${ASAN_DYNAMIC_DEFINITIONS} PARENT_TARGET asan)
PARENT_TARGET asan)
endforeach()
else() else()
# Build separate libraries for each target. # Build separate libraries for each target.
foreach(arch ${ASAN_SUPPORTED_ARCH})
set(ASAN_COMMON_RUNTIME_OBJECTS set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
$<TARGET_OBJECTS:RTInterception.${arch}> RTInterception
$<TARGET_OBJECTS:RTSanitizerCommon.${arch}> RTSanitizerCommon
$<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}> RTSanitizerCommonLibc
$<TARGET_OBJECTS:RTLSanCommon.${arch}> RTLSanCommon
$<TARGET_OBJECTS:RTUbsan.${arch}>) RTUbsan)
add_compiler_rt_runtime(clang_rt.asan add_compiler_rt_runtime(clang_rt.asan
STATIC STATIC
ARCHS ${arch} ARCHS ${ASAN_SUPPORTED_ARCH}
SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}> OBJECT_LIBS RTAsan_preinit
$<TARGET_OBJECTS:RTAsan.${arch}> RTAsan
${ASAN_COMMON_RUNTIME_OBJECTS} ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
CFLAGS ${ASAN_CFLAGS} CFLAGS ${ASAN_CFLAGS}
DEFS ${ASAN_COMMON_DEFINITIONS} DEFS ${ASAN_COMMON_DEFINITIONS}
PARENT_TARGET asan) PARENT_TARGET asan)
add_compiler_rt_runtime(clang_rt.asan_cxx add_compiler_rt_runtime(clang_rt.asan_cxx
STATIC STATIC
ARCHS ${arch} ARCHS ${ASAN_SUPPORTED_ARCH}
SOURCES $<TARGET_OBJECTS:RTAsan_cxx.${arch}> OBJECT_LIBS RTAsan_cxx
$<TARGET_OBJECTS:RTUbsan_cxx.${arch}> RTUbsan_cxx
CFLAGS ${ASAN_CFLAGS} CFLAGS ${ASAN_CFLAGS}
DEFS ${ASAN_COMMON_DEFINITIONS} DEFS ${ASAN_COMMON_DEFINITIONS}
PARENT_TARGET asan) PARENT_TARGET asan)
add_compiler_rt_runtime(clang_rt.asan-preinit add_compiler_rt_runtime(clang_rt.asan-preinit
STATIC STATIC
ARCHS ${arch} ARCHS ${ASAN_SUPPORTED_ARCH}
SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}> OBJECT_LIBS RTAsan_preinit
CFLAGS ${ASAN_CFLAGS} CFLAGS ${ASAN_CFLAGS}
DEFS ${ASAN_COMMON_DEFINITIONS} DEFS ${ASAN_COMMON_DEFINITIONS}
PARENT_TARGET asan) PARENT_TARGET asan)
foreach(arch ${ASAN_SUPPORTED_ARCH})
if (UNIX AND NOT ${arch} MATCHES "i386|i686") if (UNIX AND NOT ${arch} MATCHES "i386|i686")
add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch} add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch} LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
@ -178,14 +173,14 @@ else()
add_compiler_rt_runtime(clang_rt.asan add_compiler_rt_runtime(clang_rt.asan
SHARED SHARED
ARCHS ${arch} ARCHS ${arch}
SOURCES $<TARGET_OBJECTS:RTAsan_dynamic.${arch}> OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
RTAsan_dynamic
# The only purpose of RTAsan_dynamic_version_script_dummy is to carry # The only purpose of RTAsan_dynamic_version_script_dummy is to carry
# a dependency of the shared runtime on the version script. With CMake # a dependency of the shared runtime on the version script. With CMake
# 3.1 or later it can be replaced with a straightforward # 3.1 or later it can be replaced with a straightforward
# add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list) # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
$<TARGET_OBJECTS:RTAsan_dynamic_version_script_dummy.${arch}> RTAsan_dynamic_version_script_dummy
$<TARGET_OBJECTS:RTUbsan_cxx.${arch}> RTUbsan_cxx
${ASAN_COMMON_RUNTIME_OBJECTS}
CFLAGS ${ASAN_DYNAMIC_CFLAGS} CFLAGS ${ASAN_DYNAMIC_CFLAGS}
LINKFLAGS ${ASAN_DYNAMIC_LINK_FLAGS} LINKFLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
${VERSION_SCRIPT_FLAG} ${VERSION_SCRIPT_FLAG}

View File

@ -217,7 +217,7 @@ macro(add_asan_tests_for_arch_and_kind arch kind)
set(ASAN_TEST_RUNTIME RTAsanTest.${arch}${kind}) set(ASAN_TEST_RUNTIME RTAsanTest.${arch}${kind})
if(APPLE) if(APPLE)
set(ASAN_TEST_RUNTIME_OBJECTS set(ASAN_TEST_RUNTIME_OBJECTS
$<TARGET_OBJECTS:RTAsan.osx> $<TARGET_OBJECTS:RTAsan_dynamic.osx>
$<TARGET_OBJECTS:RTInterception.osx> $<TARGET_OBJECTS:RTInterception.osx>
$<TARGET_OBJECTS:RTSanitizerCommon.osx> $<TARGET_OBJECTS:RTSanitizerCommon.osx>
$<TARGET_OBJECTS:RTLSanCommon.osx> $<TARGET_OBJECTS:RTLSanCommon.osx>