Index: cmake/Modules/AddCompilerRT.cmake =================================================================== --- cmake/Modules/AddCompilerRT.cmake +++ cmake/Modules/AddCompilerRT.cmake @@ -100,8 +100,8 @@ # OBJECT_LIBS # 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") + if(NOT type MATCHES "^(OBJECT|STATIC|SHARED)$") + message(FATAL_ERROR "type argument must be OBJECT, STATIC or SHARED") return() endif() cmake_parse_arguments(LIB @@ -133,7 +133,10 @@ message(FATAL_ERROR "Architecture ${arch} can't be targeted") return() endif() - if(type STREQUAL "STATIC") + if(type STREQUAL "OBJECT") + set(libname "${name}-${arch}") + set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX}) + elseif(type STREQUAL "STATIC") set(libname "${name}-${arch}") set(output_name_${libname} ${libname}${COMPILER_RT_OS_SUFFIX}) else() @@ -186,12 +189,34 @@ set(COMPONENT_OPTION COMPONENT ${libname}) endif() - add_library(${libname} ${type} ${sources_${libname}}) - set_target_compile_flags(${libname} ${extra_cflags_${libname}}) - set_target_link_flags(${libname} ${extra_link_flags_${libname}}) - set_property(TARGET ${libname} APPEND PROPERTY - COMPILE_DEFINITIONS ${LIB_DEFS}) - set_target_output_directories(${libname} ${COMPILER_RT_LIBRARY_OUTPUT_DIR}) + if(type STREQUAL "OBJECT") + string(TOUPPER ${CMAKE_BUILD_TYPE} config) + set(cflags "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${config}}") + separate_arguments(cflags) + add_custom_command( + OUTPUT ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/${libname}.o + COMMAND ${CMAKE_C_COMPILER} ${sources_${libname}} ${cflags} ${extra_cflags_${libname}} -c -o ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/${libname}.o + DEPENDS ${sources_${libname}} + COMMENT "Building C object ${libname}.o") + add_custom_target(${libname} DEPENDS ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/${libname}.o) + install(FILES ${COMPILER_RT_LIBRARY_OUTPUT_DIR}/${libname}.o + DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR} + ${COMPONENT_OPTION}) + else() + add_library(${libname} ${type} ${sources_${libname}}) + set_target_compile_flags(${libname} ${extra_cflags_${libname}}) + set_target_link_flags(${libname} ${extra_link_flags_${libname}}) + set_property(TARGET ${libname} APPEND PROPERTY + COMPILE_DEFINITIONS ${LIB_DEFS}) + set_target_output_directories(${libname} ${COMPILER_RT_LIBRARY_OUTPUT_DIR}) + 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}) + endif() set_target_properties(${libname} PROPERTIES OUTPUT_NAME ${output_name_${libname}}) set_target_properties(${libname} PROPERTIES FOLDER "Compiler-RT Runtime") @@ -204,13 +229,6 @@ set_target_properties(${libname} PROPERTIES IMPORT_SUFFIX ".lib") endif() 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}) # We only want to generate per-library install targets if you aren't using # an IDE because the extra targets get cluttered in IDEs. Index: cmake/config-ix.cmake =================================================================== --- cmake/config-ix.cmake +++ cmake/config-ix.cmake @@ -163,6 +163,7 @@ ${ARM32} ${ARM64} ${MIPS32} ${MIPS64} ${S390X}) set(ALL_ASAN_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64} ${MIPS32} ${MIPS64} ${PPC64} ${S390X}) +set(ALL_CRT_SUPPORTED_ARCH ${X86} ${X86_64} ${ARM32} ${ARM64}) set(ALL_DFSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64}) set(ALL_LSAN_SUPPORTED_ARCH ${X86} ${X86_64} ${MIPS64} ${ARM64}) set(ALL_MSAN_SUPPORTED_ARCH ${X86_64} ${MIPS64} ${ARM64} ${PPC64}) @@ -381,6 +382,7 @@ ALL_XRAY_SUPPORTED_ARCH SANITIZER_COMMON_SUPPORTED_ARCH) else() + filter_available_targets(CRT_SUPPORTED_ARCH ${ALL_CRT_SUPPORTED_ARCH}) # Architectures supported by compiler-rt libraries. filter_available_targets(SANITIZER_COMMON_SUPPORTED_ARCH ${ALL_SANITIZER_COMMON_SUPPORTED_ARCH}) @@ -463,6 +465,12 @@ # TODO: Add builtins support. +if (CRT_SUPPORTED_ARCH AND OS_NAME MATCHES "Fuchsia") + set(COMPILER_RT_HAS_CRT TRUE) +else() + set(COMPILER_RT_HAS_CRT FALSE) +endif() + if (COMPILER_RT_HAS_SANITIZER_COMMON AND DFSAN_SUPPORTED_ARCH AND OS_NAME MATCHES "Linux") set(COMPILER_RT_HAS_DFSAN TRUE) Index: lib/CMakeLists.txt =================================================================== --- lib/CMakeLists.txt +++ lib/CMakeLists.txt @@ -17,6 +17,10 @@ add_subdirectory(builtins) endif() +if(COMPILER_RT_BUILD_CRT) + add_subdirectory(crt) +endif() + function(compiler_rt_build_runtime runtime) string(TOUPPER ${runtime} runtime_uppercase) if(COMPILER_RT_HAS_${runtime_uppercase}) Index: lib/crt/CMakeLists.txt =================================================================== --- /dev/null +++ lib/crt/CMakeLists.txt @@ -0,0 +1,30 @@ +add_compiler_rt_component(crt) + +set(SHARED_CFLAGS + -DSHARED +) + +foreach(arch ${CRT_SUPPORTED_ARCH}) + add_compiler_rt_runtime(crtbegin + OBJECT + ARCHS ${arch} + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c + PARENT_TARGET crt) + add_compiler_rt_runtime(crtbeginS + OBJECT + ARCHS ${arch} + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c + CFLAGS ${SHARED_CFLAGS} + PARENT_TARGET crt) + add_compiler_rt_runtime(crtend + OBJECT + ARCHS ${arch} + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c + PARENT_TARGET crt) + add_compiler_rt_runtime(crtendS + OBJECT + ARCHS ${arch} + SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c + CFLAGS ${SHARED_CFLAGS} + PARENT_TARGET crt) +endforeach() Index: lib/crt/crtbegin.c =================================================================== --- /dev/null +++ lib/crt/crtbegin.c @@ -0,0 +1,15 @@ +/* ===-- crtbegin.c - Provide __dso_handle ---------------------------------=== + * + * The LLVM Compiler Infrastructure + * + * This file is dual licensed under the MIT and the University of Illinois Open + * Source Licenses. See LICENSE.TXT for details. + * + * ===----------------------------------------------------------------------=== + */ + +#ifdef SHARED +__attribute__((visibility("hidden"))) void *__dso_handle = &__dso_handle; +#else +__attribute__((visibility("hidden"))) void *__dso_handle = (void *)0; +#endif Index: lib/crt/crtend.c =================================================================== --- /dev/null +++ lib/crt/crtend.c @@ -0,0 +1,12 @@ +/* ===-- crtend.c - Provide .eh_frame --------------------------------------=== + * + * The LLVM Compiler Infrastructure + * + * This file is dual licensed under the MIT and the University of Illinois Open + * Source Licenses. See LICENSE.TXT for details. + * + * ===----------------------------------------------------------------------=== + */ + +static const int __FRAME_END__[] + __attribute__((section(".eh_frame"), aligned(4), used)) = {}; Index: test/CMakeLists.txt =================================================================== --- test/CMakeLists.txt +++ test/CMakeLists.txt @@ -42,6 +42,9 @@ if(COMPILER_RT_HAS_ASAN) add_subdirectory(asan) endif() + if(COMPILER_RT_HAS_CRT) + add_subdirectory(crt) + endif() if(COMPILER_RT_HAS_DFSAN) add_subdirectory(dfsan) endif() Index: test/crt/CMakeLists.txt =================================================================== --- /dev/null +++ test/crt/CMakeLists.txt @@ -0,0 +1,31 @@ +set(CRT_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) + +set(CRT_TESTSUITES) + +set(CRT_TEST_DEPS "") + +if(NOT COMPILER_RT_STANDALONE_BUILD AND COMPILER_RT_BUILD_CRT AND + COMPILER_RT_HAS_CRT) + list(APPEND CRT_TEST_DEPS crt) +endif() + +set(CRT_TEST_ARCH ${CRT_SUPPORTED_ARCH}) +if (COMPILER_RT_BUILD_CRT AND COMPILER_RT_HAS_CRT) + foreach(arch ${CRT_TEST_ARCH}) + set(CRT_TEST_TARGET_ARCH ${arch}) + string(TOLOWER "-${arch}-${OS_NAME}" CRT_TEST_CONFIG_SUFFIX) + get_test_cc_for_arch(${arch} CRT_TEST_TARGET_CC CRT_TEST_TARGET_CFLAGS) + string(TOUPPER ${arch} ARCH_UPPER_CASE) + set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config) + + configure_lit_site_cfg( + ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in + ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg) + list(APPEND CRT_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}) + endforeach() +endif() + +add_lit_testsuite(check-crt "Running the CRT tests" + ${CRT_TESTSUITES} + DEPENDS ${CRT_TEST_DEPS}) +set_target_properties(check-crt PROPERTIES FOLDER "Compiler-RT Misc") Index: test/crt/dso_handle.cpp =================================================================== --- /dev/null +++ test/crt/dso_handle.cpp @@ -0,0 +1,19 @@ +// RUN: %clangxx -c %s -o %s.o +// RUN: %clangxx -o %t -nostartfiles -nostdlib -nodefaultlibs %crt1 %crti %crtbegin %s.o -lc -lm -lgcc %crtend %crtn +// RUN: %run %t 2>&1 | FileCheck %s + +#include + +struct A { + A() {} + ~A() { + // CHECK: ~A() + printf("~A()"); + } +}; + +A a; + +int main() { + return 0; +} Index: test/crt/lit.cfg =================================================================== --- /dev/null +++ test/crt/lit.cfg @@ -0,0 +1,55 @@ +# -*- Python -*- + +import os +import subprocess + +# Setup config name. +config.name = 'CRT' + config.name_suffix + +# Setup source root. +config.test_source_root = os.path.dirname(__file__) + + +def get_library_path(clang, file): + cmd = subprocess.Popen([clang, '-print-file-name=%s' % file], + stdout=subprocess.PIPE, + env=config.environment) + if not cmd.stdout: + lit_config.fatal("Couldn't find the library path for '%s'" % file) + dir = cmd.stdout.read().strip() + if sys.platform in ['win32'] and execute_external: + # Don't pass dosish path separator to msys bash.exe. + dir = dir.replace('\\', '/') + # Ensure the result is an ascii string, across Python2.5+ - Python3. + return str(dir.decode('ascii')) + + +def build_invocation(compile_flags): + return ' ' + ' '.join([config.clang] + compile_flags) + ' ' + + +# Setup substitutions. +config.substitutions.append( + ('%clang ', build_invocation([config.target_cflags]))) +config.substitutions.append( + ('%clangxx ', + build_invocation(config.cxx_mode_flags + [config.target_cflags]))) + +base_lib = os.path.join(config.compiler_rt_libdir, "%s.o") +config.substitutions.append(('%crtbegin', base_lib % "crtbegin")) +config.substitutions.append(('%crtbeginS', base_lib % "crtbeginS")) +config.substitutions.append(('%crtend', base_lib % "crtend")) +config.substitutions.append(('%crtendS', base_lib % "crtendS")) + +config.substitutions.append( + ('%crt1', get_library_path(config.clang, 'crt1.o'))) +config.substitutions.append( + ('%crti', get_library_path(config.clang, 'crti.o'))) +config.substitutions.append( + ('%crtn', get_library_path(config.clang, 'crtn.o'))) + +# Default test suffixes. +config.suffixes = ['.c', '.cc', '.cpp'] + +if config.host_os not in ['Linux']: + config.unsupported = True Index: test/crt/lit.site.cfg.in =================================================================== --- /dev/null +++ test/crt/lit.site.cfg.in @@ -0,0 +1,13 @@ +@LIT_SITE_CFG_IN_HEADER@ + +# Tool-specific config options. +config.name_suffix = "@CRT_TEST_CONFIG_SUFFIX@" +config.crt_lit_source_dir = "@CRT_LIT_SOURCE_DIR@" +config.target_cflags = "@CRT_TEST_TARGET_CFLAGS@" +config.target_arch = "@CRT_TEST_TARGET_ARCH@" + +# Load common config for all compiler-rt lit tests +lit_config.load_config(config, "@COMPILER_RT_BINARY_DIR@/test/lit.common.configured") + +# Load tool-specific config that would do the real work. +lit_config.load_config(config, "@CRT_LIT_SOURCE_DIR@/lit.cfg")