Skip to content

Commit 9657446

Browse files
snps-riyazsmeenai
authored andcommitted
[compiler-rt] Build with correct ABI (PR38025)
Summary: This patch fixes [[ https://bugs.llvm.org/show_bug.cgi?id=38025 | PR38025 ]]: Wrong ABI used when building compiler-rt Differential Revision: https://reviews.llvm.org/D74133
1 parent ea397a7 commit 9657446

File tree

4 files changed

+68
-20
lines changed

4 files changed

+68
-20
lines changed

compiler-rt/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ append_list_if(COMPILER_RT_HAS_NODEFAULTLIBS_FLAG -nodefaultlibs SANITIZER_COMMO
413413
append_list_if(COMPILER_RT_HAS_Z_TEXT -Wl,-z,text SANITIZER_COMMON_LINK_FLAGS)
414414

415415
if (COMPILER_RT_USE_BUILTINS_LIBRARY)
416-
list(APPEND SANITIZER_COMMON_LINK_LIBS ${COMPILER_RT_BUILTINS_LIBRARY})
417416
string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
418417
else()
419418
if (ANDROID)

compiler-rt/cmake/Modules/AddCompilerRT.cmake

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
include(ExternalProject)
22
include(CompilerRTUtils)
3+
include(HandleCompilerRT)
34

45
function(set_target_output_directories target output_dir)
56
# For RUNTIME_OUTPUT_DIRECTORY variable, Multi-configuration generators
@@ -233,6 +234,10 @@ function(add_compiler_rt_runtime name type)
233234
set_output_name(output_name_${libname} ${name} ${arch})
234235
endif()
235236
endif()
237+
if(COMPILER_RT_USE_BUILTINS_LIBRARY AND NOT type STREQUAL "OBJECT")
238+
get_compiler_rt_target(${arch} target)
239+
find_compiler_rt_library(builtins ${target} builtins_${libname})
240+
endif()
236241
set(sources_${libname} ${LIB_SOURCES})
237242
format_object_libs(sources_${libname} ${arch} ${LIB_OBJECT_LIBS})
238243
set(libnames ${libnames} ${libname})
@@ -326,6 +331,9 @@ function(add_compiler_rt_runtime name type)
326331
if(LIB_LINK_LIBS)
327332
target_link_libraries(${libname} PRIVATE ${LIB_LINK_LIBS})
328333
endif()
334+
if(builtins_${libname})
335+
target_link_libraries(${libname} PRIVATE ${builtins_${libname}})
336+
endif()
329337
if(${type} STREQUAL "SHARED")
330338
if(COMMAND llvm_setup_rpath)
331339
llvm_setup_rpath(${libname})
Lines changed: 59 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,65 @@
1-
function(find_compiler_rt_library name variable)
2-
set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${SANITIZER_COMMON_CFLAGS}
3-
"--rtlib=compiler-rt" "--print-libgcc-file-name")
4-
if (CMAKE_CXX_COMPILER_ID MATCHES Clang AND CMAKE_CXX_COMPILER_TARGET)
5-
list(APPEND CLANG_COMMAND "--target=${CMAKE_CXX_COMPILER_TARGET}")
1+
# Check if compile-rt library file path exists.
2+
# If found, cache the path in:
3+
# COMPILER_RT_LIBRARY-<name>-<target>
4+
# If err_flag is true OR path not found, emit a message and set:
5+
# COMPILER_RT_LIBRARY-<name>-<target> to NOTFOUND
6+
function(cache_compiler_rt_library err_flag name target library_file)
7+
if(err_flag OR NOT EXISTS "${library_file}")
8+
message(STATUS "Failed to find compiler-rt ${name} library for ${target}")
9+
set(COMPILER_RT_LIBRARY-${name}-${target} "NOTFOUND" CACHE INTERNAL
10+
"compiler-rt ${name} library for ${target}")
11+
else()
12+
message(STATUS "Found compiler-rt ${name} library: ${library_file}")
13+
set(COMPILER_RT_LIBRARY-${name}-${target} "${library_file}" CACHE INTERNAL
14+
"compiler-rt ${name} library for ${target}")
15+
endif()
16+
endfunction()
17+
18+
# Find the path to compiler-rt library `name` (e.g. "builtins") for
19+
# the specified `target` (e.g. "x86_64-linux") and return it in `variable`.
20+
# This calls cache_compiler_rt_library that caches the path to speed up
21+
# repeated invocations with the same `name` and `target`.
22+
function(find_compiler_rt_library name target variable)
23+
if(NOT CMAKE_CXX_COMPILER_ID MATCHES Clang)
24+
set(${variable} "NOTFOUND" PARENT_SCOPE)
25+
return()
26+
endif()
27+
if (NOT target AND CMAKE_CXX_COMPILER_TARGET)
28+
set(target "${CMAKE_CXX_COMPILER_TARGET}")
629
endif()
7-
get_property(SANITIZER_CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
8-
string(REPLACE " " ";" SANITIZER_CXX_FLAGS "${SANITIZER_CXX_FLAGS}")
9-
list(APPEND CLANG_COMMAND ${SANITIZER_CXX_FLAGS})
10-
execute_process(
30+
if(NOT DEFINED COMPILER_RT_LIBRARY-builtins-${target})
31+
# If the cache variable is not defined, invoke clang and then
32+
# set it with cache_compiler_rt_library.
33+
set(CLANG_COMMAND ${CMAKE_CXX_COMPILER} ${SANITIZER_COMMON_FLAGS}
34+
"--rtlib=compiler-rt" "-print-libgcc-file-name")
35+
if(target)
36+
list(APPEND CLANG_COMMAND "--target=${target}")
37+
endif()
38+
get_property(SANITIZER_CXX_FLAGS CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)
39+
string(REPLACE " " ";" SANITIZER_CXX_FLAGS "${SANITIZER_CXX_FLAGS}")
40+
list(APPEND CLANG_COMMAND ${SANITIZER_CXX_FLAGS})
41+
execute_process(
1142
COMMAND ${CLANG_COMMAND}
1243
RESULT_VARIABLE HAD_ERROR
1344
OUTPUT_VARIABLE LIBRARY_FILE
14-
)
15-
string(STRIP "${LIBRARY_FILE}" LIBRARY_FILE)
16-
file(TO_CMAKE_PATH "${LIBRARY_FILE}" LIBRARY_FILE)
17-
string(REPLACE "builtins" "${name}" LIBRARY_FILE "${LIBRARY_FILE}")
18-
if (NOT HAD_ERROR AND EXISTS "${LIBRARY_FILE}")
19-
message(STATUS "Found compiler-rt ${name} library: ${LIBRARY_FILE}")
20-
set(${variable} "${LIBRARY_FILE}" PARENT_SCOPE)
21-
else()
22-
message(STATUS "Failed to find compiler-rt ${name} library")
45+
)
46+
string(STRIP "${LIBRARY_FILE}" LIBRARY_FILE)
47+
file(TO_CMAKE_PATH "${LIBRARY_FILE}" LIBRARY_FILE)
48+
cache_compiler_rt_library(${HAD_ERROR}
49+
builtins "${target}" "${LIBRARY_FILE}")
50+
endif()
51+
if(NOT COMPILER_RT_LIBRARY-builtins-${target})
52+
set(${variable} "NOTFOUND" PARENT_SCOPE)
53+
return()
54+
endif()
55+
if(NOT DEFINED COMPILER_RT_LIBRARY-${name}-${target})
56+
# clang gives only the builtins library path. Other library paths are
57+
# obtained by substituting "builtins" with ${name} in the builtins
58+
# path and then checking if the resultant path exists. The result of
59+
# this check is also cached by cache_compiler_rt_library.
60+
set(LIBRARY_FILE "${COMPILER_RT_LIBRARY-builtins-${target}}")
61+
string(REPLACE "builtins" "${name}" LIBRARY_FILE "${LIBRARY_FILE}")
62+
cache_compiler_rt_library(FALSE "${name}" "${target}" "${LIBRARY_FILE}")
2363
endif()
64+
set(${variable} "${COMPILER_RT_LIBRARY-${name}-${target}}" PARENT_SCOPE)
2465
endfunction()

compiler-rt/cmake/config-ix.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ endfunction()
1616
check_library_exists(c fopen "" COMPILER_RT_HAS_LIBC)
1717
if (COMPILER_RT_USE_BUILTINS_LIBRARY)
1818
include(HandleCompilerRT)
19-
find_compiler_rt_library(builtins COMPILER_RT_BUILTINS_LIBRARY)
19+
find_compiler_rt_library(builtins "" COMPILER_RT_BUILTINS_LIBRARY)
2020
else()
2121
if (ANDROID)
2222
check_library_exists(gcc __gcc_personality_v0 "" COMPILER_RT_HAS_GCC_LIB)

0 commit comments

Comments
 (0)