Skip to content

Commit

Permalink
[compiler-rt] Move crt into builtins
Browse files Browse the repository at this point in the history
On Linux crt is typically use in combination with builtins. In the Clang
driver the use of builtins and crt is controlled by the --rtlib option.
Both builtins and crt also have similar build requirements where they
need to be built before any other runtimes and must avoid dependencies.
We also want builtins and crt these to be buildable separately from the
rest of compiler-rt for bootstrapping purposes. Given how simple crt is,
rather than maintaining a separate directory with its own separate build
setup, it's more efficient to just move crt into builtins. We still use
separate CMake option to control whether to built crt same as before.

This is an alternative to D89492 and D136664.

Differential Revision: https://reviews.llvm.org/D153989
  • Loading branch information
petrhosek committed Jul 11, 2023
1 parent f2f1e67 commit dae9d1b
Show file tree
Hide file tree
Showing 16 changed files with 129 additions and 234 deletions.
4 changes: 0 additions & 4 deletions compiler-rt/CMakeLists.txt
Expand Up @@ -39,10 +39,6 @@ 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: 17 additions & 0 deletions compiler-rt/cmake/builtin-config-ix.cmake
Expand Up @@ -13,6 +13,11 @@ 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 @@ -28,6 +33,12 @@ 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 @@ -214,4 +225,10 @@ 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: 0 additions & 4 deletions compiler-rt/lib/CMakeLists.txt
Expand Up @@ -17,10 +17,6 @@ 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: 44 additions & 8 deletions compiler-rt/lib/builtins/CMakeLists.txt
Expand Up @@ -51,12 +51,9 @@ 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 @@ -690,7 +687,7 @@ set(powerpc64_SOURCES
${GENERIC_SOURCES}
)
# These routines require __int128, which isn't supported on AIX.
if (NOT ${CMAKE_SYSTEM_NAME} MATCHES "AIX")
if (NOT OS_NAME MATCHES "AIX")
set(powerpc64_SOURCES
ppc/floattitf.c
ppc/fixtfti.c
Expand Down Expand Up @@ -855,14 +852,17 @@ 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(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
if(OS_NAME MATCHES "AIX")
include(CompilerRTAIXUtils)
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,12 +887,48 @@ 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(${CMAKE_SYSTEM_NAME} MATCHES "AIX")
if(OS_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()

add_dependencies(compiler-rt builtins)
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()
File renamed without changes.
File renamed without changes.
63 changes: 0 additions & 63 deletions compiler-rt/lib/crt/CMakeLists.txt

This file was deleted.

3 changes: 0 additions & 3 deletions compiler-rt/test/CMakeLists.txt
Expand Up @@ -103,9 +103,6 @@ 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: 4 additions & 0 deletions compiler-rt/test/builtins/CMakeLists.txt
Expand Up @@ -13,6 +13,10 @@ 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
@@ -1,3 +1,5 @@
// 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,5 +1,7 @@
// RUN: %clangxx -g -DCRT_SHARED -c %s -fPIC -o %tshared.o
// RUN: %clangxx -g -c %s -fPIC -o %t.o
// 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 -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
57 changes: 57 additions & 0 deletions compiler-rt/test/builtins/Unit/lit.cfg.py
Expand Up @@ -2,6 +2,8 @@

import os
import platform
import shlex
import subprocess

import lit.formats

Expand All @@ -28,6 +30,40 @@ 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 @@ -70,6 +106,27 @@ def get_required_attr(config, attr_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: 1 addition & 0 deletions compiler-rt/test/builtins/Unit/lit.site.cfg.py.in
Expand Up @@ -4,6 +4,7 @@ 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: 0 additions & 41 deletions compiler-rt/test/crt/CMakeLists.txt

This file was deleted.

0 comments on commit dae9d1b

Please sign in to comment.