Skip to content

Commit

Permalink
[CMake] merge add_compiler_rt_runtime and add_compiler_rt_darwin_runt…
Browse files Browse the repository at this point in the history
…ime into a single function

Summary: This refactoring moves much of the Apple-specific behavior into a function in AddCompilerRT. The next cleanup patch will remove more of the if(APPLE) checks in the outlying CMakeLists.

This patch adds a bunch of new functionality to add_compiler_rt_runtime so that the target names don't need to be reconstructed outside the call. It also updates some of the call sites to exercise the new functionality, but does not update all uses fully. Subsequent patches will further update call sites and move to using the new features.

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

Subscribers: beanz, rengolin, llvm-commits

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

llvm-svn: 245970
  • Loading branch information
Chris Bieneman committed Aug 25, 2015
1 parent 47aa53e commit d160260
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 159 deletions.
161 changes: 91 additions & 70 deletions compiler-rt/cmake/Modules/AddCompilerRT.cmake
Expand Up @@ -46,83 +46,104 @@ function(add_compiler_rt_object_libraries name)
endforeach()
endfunction()

# Adds static or shared runtime for a given architecture and puts it in the
# proper directory in the build and install trees.
# add_compiler_rt_runtime(<name> <arch> {STATIC,SHARED}
# 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.
# add_compiler_rt_runtime(<name>
# {STATIC|SHARED}
# ARCHS <architectures>
# OS <os list>
# SOURCES <source files>
# CFLAGS <compile flags>
# LINKFLAGS <linker flags>
# DEFS <compile definitions>
# OUTPUT_NAME <output library name>)
macro(add_compiler_rt_runtime name arch type)
if(CAN_TARGET_${arch})
cmake_parse_arguments(LIB "" "OUTPUT_NAME" "SOURCES;CFLAGS;LINKFLAGS;DEFS" ${ARGN})
add_library(${name} ${type} ${LIB_SOURCES})
# Setup compile flags and definitions.
set_target_compile_flags(${name}
${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
set_target_link_flags(${name}
${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS} ${LIB_LINKFLAGS})
set_property(TARGET ${name} APPEND PROPERTY
COMPILE_DEFINITIONS ${LIB_DEFS})
# Setup correct output directory in the build tree.
set_target_properties(${name} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
RUNTIME_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
if ("${LIB_OUTPUT_NAME}" STREQUAL "")
set_target_properties(${name} PROPERTIES
OUTPUT_NAME ${name}${COMPILER_RT_OS_SUFFIX})
else()
set_target_properties(${name} PROPERTIES
OUTPUT_NAME ${LIB_OUTPUT_NAME})
endif()
# Add installation command.
install(TARGETS ${name}
ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
# LINK_LIBS <linked libraries> (only for shared library)
# PARENT_TARGET <convenience parent target>)
function(add_compiler_rt_runtime name type)
if(NOT type MATCHES "^(STATIC|SHARED)$")
message(FATAL_ERROR "type argument must be STATIC or SHARED")
return()
endif()
cmake_parse_arguments(LIB
""
"PARENT_TARGET"
"OS;ARCHS;SOURCES;CFLAGS;LINKFLAGS;DEFS;LINK_LIBS"
${ARGN})
set(libnames)
if(APPLE)
foreach(os ${LIB_OS})
if(type STREQUAL "STATIC")
set(libname "${name}_${os}")
else()
set(libname "${name}_${os}_dynamic")
set(extra_linkflags_${libname} ${DARWIN_${os}_LINKFLAGS} ${LIB_LINKFLAGS})
endif()
list_union(LIB_ARCHS_${libname} DARWIN_${os}_ARCHS LIB_ARCHS)
if(LIB_ARCHS_${libname})
list(APPEND libnames ${libname})
set(extra_cflags_${libname} ${DARWIN_${os}_CFLAGS} ${LIB_CFLAGS})
set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
endif()
endforeach()
else()
message(FATAL_ERROR "Architecture ${arch} can't be targeted")
foreach(arch ${LIB_ARCHS})
if(NOT CAN_TARGET_${arch})
message(FATAL_ERROR "Architecture ${arch} can't be targeted")
return()
endif()
if(type STREQUAL "STATIC")
set(libname "${name}-${arch}")
set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX})
else()
set(libname "${name}-dynamic-${arch}")
set(extra_linkflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS} ${LIB_LINKFLAGS})
if(WIN32)
set(output_name_${libname} ${name}_dynamic-${arch}${COMPILER_RT_OS_SUFFIX})
else()
set(output_name_${libname} ${name}-${arch}${COMPILER_RT_OS_SUFFIX})
endif()
endif()
set(libnames ${libnames} ${libname})
set(extra_cflags_${libname} ${TARGET_${arch}_CFLAGS} ${LIB_CFLAGS})
endforeach()
endif()
endmacro()

# Adds runtime library for darwin platforms, which supports multiple
# architectures.
# add_compiler_rt_darwin_runtime(<name> <os>
# STATIC|SHARED
# ARCHS <architectures>
# SOURCES <source files>
# CFLAGS <compile flags>
# DEFS <compile definitions>
# LINKFLAGS <link flags>)
function(add_compiler_rt_darwin_runtime name os)
cmake_parse_arguments(LIB "SHARED;STATIC" "" "ARCHS;SOURCES;CFLAGS;DEFS;LINKFLAGS" ${ARGN})
list_union(filtered_arches DARWIN_${os}_ARCHS LIB_ARCHS)
# if there are no supported architectures, don't create the library
if(filtered_arches)
if(LIB_SHARED)
add_library(${name} SHARED ${LIB_SOURCES})
set_target_properties(${name} PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
install(TARGETS ${name}
LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
elseif(LIB_STATIC)
add_library(${name} STATIC ${LIB_SOURCES})
set_target_properties(${name} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
install(TARGETS ${name}
ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
else()
message(FATAL_ERROR "Must specified SHARED|STATIC to add_compiler_rt_darwin_runtime")
endif()

set_target_compile_flags(${name} ${LIB_CFLAGS} ${DARWIN_${os}_CFLAGS})
set_target_link_flags(${name} ${LIB_LINKFLAGS} ${DARWIN_${os}_LINKFLAGS})
set_property(TARGET ${name} APPEND PROPERTY
COMPILE_DEFINITIONS ${LIB_DEFS})
if(NOT libnames)
return()
endif()

if(LIB_PARENT_TARGET)
set(COMPONENT_OPTION COMPONENT ${LIB_PARENT_TARGET})
endif()

set_target_properties(${name} PROPERTIES
OSX_ARCHITECTURES "${filtered_arches}")
foreach(libname ${libnames})
add_library(${libname} ${type} ${LIB_SOURCES})
set_target_compile_flags(${libname} ${extra_cflags_${libname}})
set_target_link_flags(${libname} ${extra_linkflags_${libname}})
set_property(TARGET ${libname} APPEND PROPERTY
COMPILE_DEFINITIONS ${LIB_DEFS})
set_target_properties(${libname} PROPERTIES
ARCHIVE_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
LIBRARY_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR}
RUNTIME_OUTPUT_DIRECTORY ${COMPILER_RT_LIBRARY_OUTPUT_DIR})
set_target_properties(${libname} PROPERTIES
OUTPUT_NAME ${output_name_${libname}})
if(LIB_LINK_LIBS AND ${type} STREQUAL "SHARED")
target_link_libraries(${libname} ${LIB_LINK_LIBS})
endif()
install(TARGETS ${libname}
ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
${COMPONENT_OPTION}
LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
${COMPONENT_OPTION}
RUNTIME DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
${COMPONENT_OPTION})
if(APPLE)
set_target_properties(${libname} PROPERTIES
OSX_ARCHITECTURES "${LIB_ARCHS_${libname}}")
endif()
endforeach()
if(LIB_PARENT_TARGET)
add_dependencies(${LIB_PARENT_TARGET} ${libnames})
endif()
endfunction()

Expand Down
62 changes: 34 additions & 28 deletions compiler-rt/lib/asan/CMakeLists.txt
Expand Up @@ -111,17 +111,18 @@ endif()
add_custom_target(asan)
if(APPLE)
foreach (os ${SANITIZER_COMMON_SUPPORTED_OS})
add_compiler_rt_darwin_runtime(clang_rt.asan_${os}_dynamic ${os}
add_compiler_rt_runtime(clang_rt.asan
SHARED
OS ${os}
ARCHS ${ASAN_SUPPORTED_ARCH}
SOURCES $<TARGET_OBJECTS:RTAsan.${os}>
$<TARGET_OBJECTS:RTInterception.${os}>
$<TARGET_OBJECTS:RTSanitizerCommon.${os}>
$<TARGET_OBJECTS:RTLSanCommon.${os}>
$<TARGET_OBJECTS:RTUbsan.${os}>
CFLAGS ${ASAN_DYNAMIC_CFLAGS}
DEFS ${ASAN_DYNAMIC_DEFINITIONS})
add_dependencies(asan clang_rt.asan_${os}_dynamic)
DEFS ${ASAN_DYNAMIC_DEFINITIONS}
PARENT_TARGET asan)
endforeach()
else()
# Build separate libraries for each target.
Expand All @@ -133,26 +134,32 @@ else()
$<TARGET_OBJECTS:RTLSanCommon.${arch}>
$<TARGET_OBJECTS:RTUbsan.${arch}>)

add_compiler_rt_runtime(clang_rt.asan-${arch} ${arch} STATIC
add_compiler_rt_runtime(clang_rt.asan
STATIC
ARCHS ${arch}
SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
$<TARGET_OBJECTS:RTAsan.${arch}>
${ASAN_COMMON_RUNTIME_OBJECTS}
CFLAGS ${ASAN_CFLAGS}
DEFS ${ASAN_COMMON_DEFINITIONS})
add_dependencies(asan clang_rt.asan-${arch})
DEFS ${ASAN_COMMON_DEFINITIONS}
PARENT_TARGET asan)

add_compiler_rt_runtime(clang_rt.asan_cxx-${arch} ${arch} STATIC
add_compiler_rt_runtime(clang_rt.asan_cxx
STATIC
ARCHS ${arch}
SOURCES $<TARGET_OBJECTS:RTAsan_cxx.${arch}>
$<TARGET_OBJECTS:RTUbsan_cxx.${arch}>
CFLAGS ${ASAN_CFLAGS}
DEFS ${ASAN_COMMON_DEFINITIONS})
add_dependencies(asan clang_rt.asan_cxx-${arch})
DEFS ${ASAN_COMMON_DEFINITIONS}
PARENT_TARGET asan)

add_compiler_rt_runtime(clang_rt.asan-preinit-${arch} ${arch} STATIC
add_compiler_rt_runtime(clang_rt.asan-preinit
STATIC
ARCHS ${arch}
SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
CFLAGS ${ASAN_CFLAGS}
DEFS ${ASAN_COMMON_DEFINITIONS})
add_dependencies(asan clang_rt.asan-preinit-${arch})
DEFS ${ASAN_COMMON_DEFINITIONS}
PARENT_TARGET asan)

if (UNIX AND NOT ${arch} MATCHES "i386|i686")
add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
Expand All @@ -168,13 +175,9 @@ else()
set(VERSION_SCRIPT_FLAG)
endif()

if (WIN32)
set(SHARED_ASAN_NAME clang_rt.asan_dynamic-${arch}${COMPILER_RT_OS_SUFFIX})
else()
set(SHARED_ASAN_NAME clang_rt.asan-${arch}${COMPILER_RT_OS_SUFFIX})
endif()
add_compiler_rt_runtime(clang_rt.asan-dynamic-${arch} ${arch} SHARED
OUTPUT_NAME ${SHARED_ASAN_NAME}
add_compiler_rt_runtime(clang_rt.asan
SHARED
ARCHS ${arch}
SOURCES $<TARGET_OBJECTS:RTAsan_dynamic.${arch}>
# The only purpose of RTAsan_dynamic_version_script_dummy is to carry
# a dependency of the shared runtime on the version script. With CMake
Expand All @@ -186,9 +189,9 @@ else()
CFLAGS ${ASAN_DYNAMIC_CFLAGS}
LINKFLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
${VERSION_SCRIPT_FLAG}
DEFS ${ASAN_DYNAMIC_DEFINITIONS})
target_link_libraries(clang_rt.asan-dynamic-${arch} ${ASAN_DYNAMIC_LIBS})
add_dependencies(asan clang_rt.asan-dynamic-${arch})
LINKLIBS ${ASAN_DYNAMIC_LIBS}
DEFS ${ASAN_DYNAMIC_DEFINITIONS}
PARENT_TARGET asan)

if (UNIX AND NOT ${arch} MATCHES "i386|i686")
add_sanitizer_rt_symbols(clang_rt.asan_cxx-${arch})
Expand All @@ -198,18 +201,21 @@ else()
endif()

if (WIN32)
add_compiler_rt_runtime(clang_rt.asan_dll_thunk-${arch} ${arch} STATIC
add_compiler_rt_runtime(clang_rt.asan_dll_thunk
STATIC
ARCHS ${arch}
SOURCES asan_win_dll_thunk.cc
$<TARGET_OBJECTS:RTInterception.${arch}>
CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
DEFS ${ASAN_COMMON_DEFINITIONS})
add_dependencies(asan clang_rt.asan_dll_thunk-${arch})
add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk-${arch} ${arch}
DEFS ${ASAN_COMMON_DEFINITIONS}
PARENT_TARGET asan)
add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
STATIC
ARCHS ${arch}
SOURCES asan_win_dynamic_runtime_thunk.cc
CFLAGS ${ASAN_CFLAGS} -DASAN_DYNAMIC_RUNTIME_THUNK -Zl
DEFS ${ASAN_COMMON_DEFINITIONS})
add_dependencies(asan clang_rt.asan_dynamic_runtime_thunk-${arch})
DEFS ${ASAN_COMMON_DEFINITIONS}
PARENT_TARGET asan)
endif()
endforeach()
endif()
Expand Down
13 changes: 10 additions & 3 deletions compiler-rt/lib/builtins/CMakeLists.txt
Expand Up @@ -297,6 +297,10 @@ set(aarch64_SOURCES

add_custom_target(builtins)

if(APPLE)
set(OS_OPTION OS osx)
endif()

if (NOT WIN32 OR MINGW)
foreach (arch x86_64 i386 i686 arm aarch64)
if (CAN_TARGET_${arch})
Expand All @@ -311,10 +315,13 @@ if (NOT WIN32 OR MINGW)
endif ()
endforeach ()

add_compiler_rt_runtime(clang_rt.builtins-${arch} ${arch} STATIC
add_compiler_rt_runtime(clang_rt.builtins
STATIC
ARCH ${arch}
${OS_OPTION}
SOURCES ${${arch}_SOURCES}
CFLAGS "-std=c99")
add_dependencies(builtins clang_rt.builtins-${arch})
CFLAGS "-std=c99"
PARENT_TARGET builtins)
endif ()
endforeach ()
endif ()
Expand Down
8 changes: 5 additions & 3 deletions compiler-rt/lib/dfsan/CMakeLists.txt
Expand Up @@ -15,15 +15,17 @@ add_custom_target(dfsan)
foreach(arch ${DFSAN_SUPPORTED_ARCH})
set(DFSAN_CFLAGS ${DFSAN_COMMON_CFLAGS})
append_list_if(COMPILER_RT_HAS_FPIE_FLAG -fPIE DFSAN_CFLAGS)
add_compiler_rt_runtime(clang_rt.dfsan-${arch} ${arch} STATIC
add_compiler_rt_runtime(clang_rt.dfsan
STATIC
ARCHS ${arch}
SOURCES ${DFSAN_RTL_SOURCES}
$<TARGET_OBJECTS:RTInterception.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
CFLAGS ${DFSAN_CFLAGS})
CFLAGS ${DFSAN_CFLAGS}
PARENT_TARGET dfsan)
add_sanitizer_rt_symbols(clang_rt.dfsan-${arch} dfsan.syms.extra)
add_dependencies(dfsan
clang_rt.dfsan-${arch}
clang_rt.dfsan-${arch}-symbols)
endforeach()

Expand Down
8 changes: 5 additions & 3 deletions compiler-rt/lib/lsan/CMakeLists.txt
Expand Up @@ -26,14 +26,16 @@ add_compiler_rt_object_libraries(RTLSanCommon

if(COMPILER_RT_HAS_LSAN)
foreach(arch ${LSAN_SUPPORTED_ARCH})
add_compiler_rt_runtime(clang_rt.lsan-${arch} ${arch} STATIC
add_compiler_rt_runtime(clang_rt.lsan
STATIC
ARCHS ${arch}
SOURCES ${LSAN_SOURCES}
$<TARGET_OBJECTS:RTInterception.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
$<TARGET_OBJECTS:RTLSanCommon.${arch}>
CFLAGS ${LSAN_CFLAGS})
add_dependencies(lsan clang_rt.lsan-${arch})
CFLAGS ${LSAN_CFLAGS}
PARENT_TARGET lsan)
endforeach()
endif()

Expand Down
16 changes: 10 additions & 6 deletions compiler-rt/lib/msan/CMakeLists.txt
Expand Up @@ -27,19 +27,23 @@ set(MSAN_RUNTIME_LIBRARIES)
# Static runtime library.
add_custom_target(msan)
foreach(arch ${MSAN_SUPPORTED_ARCH})
add_compiler_rt_runtime(clang_rt.msan-${arch} ${arch} STATIC
add_compiler_rt_runtime(clang_rt.msan
STATIC
ARCHS ${arch}
SOURCES ${MSAN_RTL_SOURCES}
$<TARGET_OBJECTS:RTInterception.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
$<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
$<TARGET_OBJECTS:RTUbsan.${arch}>
CFLAGS ${MSAN_RTL_CFLAGS})
add_compiler_rt_runtime(clang_rt.msan_cxx-${arch} ${arch} STATIC
CFLAGS ${MSAN_RTL_CFLAGS}
PARENT_TARGET msan)
add_compiler_rt_runtime(clang_rt.msan_cxx
STATIC
ARCHS ${arch}
SOURCES ${MSAN_RTL_CXX_SOURCES}
$<TARGET_OBJECTS:RTUbsan_cxx.${arch}>
CFLAGS ${MSAN_RTL_CFLAGS})
add_dependencies(msan clang_rt.msan-${arch}
clang_rt.msan_cxx-${arch})
CFLAGS ${MSAN_RTL_CFLAGS}
PARENT_TARGET msan)
list(APPEND MSAN_RUNTIME_LIBRARIES clang_rt.msan-${arch}
clang_rt.msan_cxx-${arch})
if(UNIX)
Expand Down

0 comments on commit d160260

Please sign in to comment.