Skip to content

Commit

Permalink
Revert "[compiler-rt] Move crt into builtins"
Browse files Browse the repository at this point in the history
This reverts commit dae9d1b since
it caused #63799.
  • Loading branch information
petrhosek committed Jul 11, 2023
1 parent e2d7d98 commit 7c4dbff
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 129 deletions.
4 changes: 4 additions & 0 deletions compiler-rt/CMakeLists.txt
Expand Up @@ -39,6 +39,10 @@ option(COMPILER_RT_BUILD_BUILTINS "Build builtins" ON)
mark_as_advanced(COMPILER_RT_BUILD_BUILTINS)
option(COMPILER_RT_DISABLE_AARCH64_FMV "Disable AArch64 Function Multi Versioning support" OFF)
mark_as_advanced(COMPILER_RT_DISABLE_AARCH64_FMV)
option(COMPILER_RT_BUILD_CRT "Build crtbegin.o/crtend.o" ON)
mark_as_advanced(COMPILER_RT_BUILD_CRT)
option(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY "Use eh_frame in crtbegin.o/crtend.o" ON)
mark_as_advanced(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY)
option(COMPILER_RT_BUILD_SANITIZERS "Build sanitizers" ON)
mark_as_advanced(COMPILER_RT_BUILD_SANITIZERS)
option(COMPILER_RT_BUILD_XRAY "Build xray" ON)
Expand Down
17 changes: 0 additions & 17 deletions compiler-rt/cmake/builtin-config-ix.cmake
Expand Up @@ -13,11 +13,6 @@ builtin_check_c_compiler_flag(-fvisibility=hidden COMPILER_RT_HAS_VISIBILITY_H
builtin_check_c_compiler_flag(-fomit-frame-pointer COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG)
builtin_check_c_compiler_flag(-ffreestanding COMPILER_RT_HAS_FFREESTANDING_FLAG)
builtin_check_c_compiler_flag(-fxray-instrument COMPILER_RT_HAS_XRAY_COMPILER_FLAG)
builtin_check_c_compiler_flag(-fno-lto COMPILER_RT_HAS_FNO_LTO_FLAG)
builtin_check_c_compiler_flag(-fno-profile-generate COMPILER_RT_HAS_FNO_PROFILE_GENERATE_FLAG)
builtin_check_c_compiler_flag(-fno-profile-instr-generate COMPILER_RT_HAS_FNO_PROFILE_INSTR_GENERATE_FLAG)
builtin_check_c_compiler_flag(-fno-profile-instr-use COMPILER_RT_HAS_FNO_PROFILE_INSTR_USE_FLAG)
builtin_check_c_compiler_flag(-Wno-pedantic COMPILER_RT_HAS_WNO_PEDANTIC)

builtin_check_c_compiler_source(COMPILER_RT_HAS_ATOMIC_KEYWORD
"
Expand All @@ -33,12 +28,6 @@ asm(\".arch armv8-a+lse\");
asm(\"cas w0, w1, [x2]\");
")

if(ANDROID)
set(OS_NAME "Android")
else()
set(OS_NAME "${CMAKE_SYSTEM_NAME}")
endif()

set(ARM64 aarch64)
set(ARM32 arm armhf armv4t armv5te armv6 armv6m armv7m armv7em armv7 armv7s armv7k armv8m.base armv8m.main armv8.1m.main)
set(AVR avr)
Expand Down Expand Up @@ -225,10 +214,4 @@ else()
${ALL_BUILTIN_SUPPORTED_ARCH})
endif()

if (OS_NAME MATCHES "Linux" AND NOT LLVM_USE_SANITIZER)
set(COMPILER_RT_HAS_CRT TRUE)
else()
set(COMPILER_RT_HAS_CRT FALSE)
endif()

message(STATUS "Builtin supported architectures: ${BUILTIN_SUPPORTED_ARCH}")
4 changes: 4 additions & 0 deletions compiler-rt/lib/CMakeLists.txt
Expand Up @@ -17,6 +17,10 @@ if(COMPILER_RT_BUILD_BUILTINS)
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})
Expand Down
52 changes: 8 additions & 44 deletions compiler-rt/lib/builtins/CMakeLists.txt
Expand Up @@ -51,9 +51,12 @@ if (COMPILER_RT_STANDALONE_BUILD)
endif()

include(builtin-config-ix)
include(CMakeDependentOption)
include(CMakePushCheckState)

if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
include(CompilerRTAIXUtils)
endif()

option(COMPILER_RT_BUILTINS_HIDE_SYMBOLS
"Do not export any symbols from the static library." ON)

Expand Down Expand Up @@ -687,7 +690,7 @@ set(powerpc64_SOURCES
${GENERIC_SOURCES}
)
# These routines require __int128, which isn't supported on AIX.
if (NOT OS_NAME MATCHES "AIX")
if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
set(powerpc64_SOURCES
ppc/floattitf.c
ppc/fixtfti.c
Expand Down Expand Up @@ -852,17 +855,14 @@ else ()
endforeach ()
endif ()

add_dependencies(compiler-rt builtins)

option(COMPILER_RT_BUILD_STANDALONE_LIBATOMIC
"Build standalone shared atomic library."
OFF)

if(COMPILER_RT_BUILD_STANDALONE_LIBATOMIC)
add_custom_target(builtins-standalone-atomic)
set(BUILTIN_TYPE SHARED)
if(OS_NAME MATCHES "AIX")
include(CompilerRTAIXUtils)
if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
if(NOT COMPILER_RT_LIBATOMIC_LINK_FLAGS)
get_aix_libatomic_default_link_flags(COMPILER_RT_LIBATOMIC_LINK_FLAGS
"${CMAKE_CURRENT_SOURCE_DIR}/ppc/atomic.exp")
Expand All @@ -887,48 +887,12 @@ if(COMPILER_RT_BUILD_STANDALONE_LIBATOMIC)
# FIXME: On AIX, we have to archive built shared libraries into a static
# archive, i.e., libatomic.a. Once cmake adds support of such usage for AIX,
# this ad-hoc part can be removed.
if(OS_NAME MATCHES "AIX")
if(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
archive_aix_libatomic(clang_rt.atomic libatomic
ARCHS ${BUILTIN_SUPPORTED_ARCH}
PARENT_TARGET builtins-standalone-atomic)
endif()
add_dependencies(compiler-rt builtins-standalone-atomic)
endif()

cmake_dependent_option(COMPILER_RT_BUILD_CRT "Build crtbegin.o/crtend.o" ON "COMPILER_RT_HAS_CRT" OFF)

if(COMPILER_RT_BUILD_CRT)
add_compiler_rt_component(crt)

option(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY "Use eh_frame in crtbegin.o/crtend.o" ON)

include(CheckSectionExists)
check_section_exists(".init_array" COMPILER_RT_HAS_INITFINI_ARRAY
SOURCE "volatile int x;\n__attribute__((constructor)) void f(void) {x = 0;}\nint main(void) { return 0; }\n")

append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 CRT_CFLAGS)
append_list_if(COMPILER_RT_HAS_INITFINI_ARRAY -DCRT_HAS_INITFINI_ARRAY CRT_CFLAGS)
append_list_if(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY -DEH_USE_FRAME_REGISTRY CRT_CFLAGS)
append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS)
append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic CRT_CFLAGS)
if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full CRT_CFLAGS)
endif()

foreach(arch ${BUILTIN_SUPPORTED_ARCH})
add_compiler_rt_runtime(clang_rt.crtbegin
OBJECT
ARCHS ${arch}
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c
CFLAGS ${CRT_CFLAGS}
PARENT_TARGET crt)
add_compiler_rt_runtime(clang_rt.crtend
OBJECT
ARCHS ${arch}
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c
CFLAGS ${CRT_CFLAGS}
PARENT_TARGET crt)
endforeach()

add_dependencies(compiler-rt crt)
endif()
add_dependencies(compiler-rt builtins)
63 changes: 63 additions & 0 deletions compiler-rt/lib/crt/CMakeLists.txt
@@ -0,0 +1,63 @@
if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
cmake_minimum_required(VERSION 3.20.0)

set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
project(CompilerRTCRT C)
set(COMPILER_RT_STANDALONE_BUILD TRUE)
set(COMPILER_RT_CRT_STANDALONE_BUILD TRUE)

set(COMPILER_RT_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../..")

set(LLVM_COMMON_CMAKE_UTILS "${COMPILER_RT_SOURCE_DIR}/../cmake")

# Add path for custom modules
list(INSERT CMAKE_MODULE_PATH 0
"${COMPILER_RT_SOURCE_DIR}/cmake"
"${COMPILER_RT_SOURCE_DIR}/cmake/Modules"
"${LLVM_COMMON_CMAKE_UTILS}"
"${LLVM_COMMON_CMAKE_UTILS}/Modules"
)

include(base-config-ix)
include(CompilerRTUtils)

load_llvm_config()
construct_compiler_rt_default_triple()

include(SetPlatformToolchainTools)
include(AddCompilerRT)
endif()

include(crt-config-ix)

if(COMPILER_RT_HAS_CRT)
add_compiler_rt_component(crt)

include(CheckSectionExists)
check_section_exists(".init_array" COMPILER_RT_HAS_INITFINI_ARRAY
SOURCE "volatile int x;\n__attribute__((constructor)) void f(void) {x = 0;}\nint main(void) { return 0; }\n")

append_list_if(COMPILER_RT_HAS_STD_C11_FLAG -std=c11 CRT_CFLAGS)
append_list_if(COMPILER_RT_HAS_INITFINI_ARRAY -DCRT_HAS_INITFINI_ARRAY CRT_CFLAGS)
append_list_if(COMPILER_RT_CRT_USE_EH_FRAME_REGISTRY -DEH_USE_FRAME_REGISTRY CRT_CFLAGS)
append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC CRT_CFLAGS)
append_list_if(COMPILER_RT_HAS_WNO_PEDANTIC -Wno-pedantic CRT_CFLAGS)
if (COMPILER_RT_HAS_FCF_PROTECTION_FLAG)
append_list_if(COMPILER_RT_ENABLE_CET -fcf-protection=full CRT_CFLAGS)
endif()

foreach(arch ${CRT_SUPPORTED_ARCH})
add_compiler_rt_runtime(clang_rt.crtbegin
OBJECT
ARCHS ${arch}
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtbegin.c
CFLAGS ${CRT_CFLAGS}
PARENT_TARGET crt)
add_compiler_rt_runtime(clang_rt.crtend
OBJECT
ARCHS ${arch}
SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/crtend.c
CFLAGS ${CRT_CFLAGS}
PARENT_TARGET crt)
endforeach()
endif()
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions compiler-rt/test/CMakeLists.txt
Expand Up @@ -103,6 +103,9 @@ if(COMPILER_RT_CAN_EXECUTE_TESTS)
if(COMPILER_RT_BUILD_ORC)
compiler_rt_Test_runtime(orc)
endif()
if(COMPILER_RT_BUILD_CRT)
add_subdirectory(crt)
endif()
# ShadowCallStack does not yet provide a runtime with compiler-rt, the tests
# include their own minimal runtime
add_subdirectory(shadowcallstack)
Expand Down
4 changes: 0 additions & 4 deletions compiler-rt/test/builtins/CMakeLists.txt
Expand Up @@ -13,10 +13,6 @@ configure_lit_site_cfg(

include(builtin-config-ix)

if (COMPILER_RT_HAS_CRT)
list(APPEND BUILTINS_TEST_DEPS crt)
endif()

# Indicate if this is an MSVC environment.
pythonize_bool(MSVC)

Expand Down
57 changes: 0 additions & 57 deletions compiler-rt/test/builtins/Unit/lit.cfg.py
Expand Up @@ -2,8 +2,6 @@

import os
import platform
import shlex
import subprocess

import lit.formats

Expand All @@ -30,40 +28,6 @@ def get_required_attr(config, attr_name):
return attr_value


def get_library_path(file):
cmd = subprocess.Popen(
[config.clang.strip(), "-print-file-name=%s" % file]
+ shlex.split(config.target_cflags),
stdout=subprocess.PIPE,
env=config.environment,
universal_newlines=True,
)
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("\\", "/")
return dir


def get_libgcc_file_name():
cmd = subprocess.Popen(
[config.clang.strip(), "-print-libgcc-file-name"]
+ shlex.split(config.target_cflags),
stdout=subprocess.PIPE,
env=config.environment,
universal_newlines=True,
)
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("\\", "/")
return dir


# Setup config name.
config.name = "Builtins" + config.name_suffix

Expand Down Expand Up @@ -106,27 +70,6 @@ def get_libgcc_file_name():
base_lib = base_lib.replace("\\", "/")
config.substitutions.append(("%librt ", base_lib + " -lc -lm "))

if config.host_os == "Linux":
base_obj = os.path.join(
config.compiler_rt_libdir, "clang_rt.%%s%s.o" % config.target_suffix
)
if sys.platform in ["win32"] and execute_external:
# Don't pass dosish path separator to msys bash.exe.
base_obj = base_obj.replace("\\", "/")

config.substitutions.append(("%crtbegin", base_obj % "crtbegin"))
config.substitutions.append(("%crtend", base_obj % "crtend"))

config.substitutions.append(("%crt1", get_library_path("crt1.o")))
config.substitutions.append(("%crti", get_library_path("crti.o")))
config.substitutions.append(("%crtn", get_library_path("crtn.o")))

config.substitutions.append(("%libgcc", get_libgcc_file_name()))

config.substitutions.append(
("%libstdcxx", "-l" + config.sanitizer_cxx_lib.lstrip("lib"))
)

builtins_source_dir = os.path.join(
get_required_attr(config, "compiler_rt_src_root"), "lib", "builtins"
)
Expand Down
1 change: 0 additions & 1 deletion compiler-rt/test/builtins/Unit/lit.site.cfg.py.in
Expand Up @@ -4,7 +4,6 @@ config.name_suffix = "@BUILTINS_TEST_CONFIG_SUFFIX@"
config.builtins_lit_source_dir = "@BUILTINS_LIT_SOURCE_DIR@/Unit"
config.target_cflags = "@BUILTINS_TEST_TARGET_CFLAGS@"
config.target_arch = "@BUILTINS_TEST_TARGET_ARCH@"
config.sanitizer_cxx_lib = "@SANITIZER_TEST_CXX_LIBNAME@"
config.is_msvc = @MSVC_PYBOOL@
config.builtins_is_msvc = @BUILTINS_IS_MSVC_PYBOOL@
config.builtins_lit_source_features = "@BUILTINS_LIT_SOURCE_FEATURES@"
Expand Down
41 changes: 41 additions & 0 deletions compiler-rt/test/crt/CMakeLists.txt
@@ -0,0 +1,41 @@
include(crt-config-ix)

if (COMPILER_RT_HAS_CRT)
set(CRT_LIT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

if(NOT COMPILER_RT_STANDALONE_BUILD)
list(APPEND CRT_TEST_DEPS crt)
endif()
if(NOT COMPILER_RT_STANDALONE_BUILD AND NOT LLVM_RUNTIMES_BUILD)
# Use LLVM utils and Clang from the same build tree.
list(APPEND CRT_TEST_DEPS
clang clang-resource-headers FileCheck not llvm-config)
endif()

set(CRT_TEST_ARCH ${CRT_SUPPORTED_ARCH})
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)

if (COMPILER_RT_ENABLE_CET)
if (${arch} MATCHES "i386|x86_64")
list(APPEND CRT_TEST_TARGET_CFLAGS -fcf-protection=full)
string(REPLACE ";" " " CRT_TEST_TARGET_CFLAGS "${CRT_TEST_TARGET_CFLAGS}")
else()
message(FATAL_ERROR "The target arch ${arch} doesn't support CET")
endif()
endif()
configure_lit_site_cfg(
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg.py)
list(APPEND CRT_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME})
endforeach()

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")
endif()
@@ -1,5 +1,3 @@
// REQUIRES: linux

// RUN: %clang -fno-use-init-array -g -c %s -o %t.o
// RUN: %clang -o %t -no-pie -nostdlib %crt1 %crti %crtbegin %t.o -lc %libgcc %crtend %crtn
// RUN: %run %t 2>&1 | FileCheck %s
Expand Down
@@ -1,7 +1,5 @@
// REQUIRES: linux

// RUN: %clangxx -g -fno-exceptions -DCRT_SHARED -c %s -fPIC -o %tshared.o
// RUN: %clangxx -g -fno-exceptions -c %s -fPIC -o %t.o
// RUN: %clangxx -g -DCRT_SHARED -c %s -fPIC -o %tshared.o
// RUN: %clangxx -g -c %s -fPIC -o %t.o
// RUN: %clangxx -g -shared -o %t.so -nostdlib %crti %crtbegin %tshared.o %libstdcxx -lc -lm %libgcc %crtend %crtn
// RUN: %clangxx -g -o %t -fno-pic -no-pie -nostdlib %crt1 %crti %crtbegin %t.o %libstdcxx -lc -lm %libgcc %t.so %crtend %crtn
// RUN: %run %t 2>&1 | FileCheck %s
Expand Down

0 comments on commit 7c4dbff

Please sign in to comment.