|
| 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 | +function(get_component_name name variable) |
| 19 | + if(APPLE) |
| 20 | + if(NOT name MATCHES "builtins.*") |
| 21 | + set(component_name "${name}_") |
| 22 | + endif() |
| 23 | + # TODO: Support ios, tvos and watchos as well. |
| 24 | + set(component_name "${component_name}osx") |
| 25 | + else() |
| 26 | + set(component_name "${name}") |
| 27 | + endif() |
| 28 | + set(${variable} "${component_name}" PARENT_SCOPE) |
| 29 | +endfunction() |
| 30 | + |
| 31 | +# Find the path to compiler-rt library `name` (e.g. "builtins") for the |
| 32 | +# specified `TARGET` (e.g. "x86_64-linux-gnu") and return it in `variable`. |
| 33 | +# This calls cache_compiler_rt_library that caches the path to speed up |
| 34 | +# repeated invocations with the same `name` and `target`. |
| 35 | +function(find_compiler_rt_library name variable) |
| 36 | + cmake_parse_arguments(ARG "" "TARGET" "" ${ARGN}) |
| 37 | + # While we can use compiler-rt runtimes with other compilers, we need to |
| 38 | + # query the compiler for runtime location and thus we require Clang. |
| 39 | + if(NOT CMAKE_CXX_COMPILER_ID MATCHES Clang) |
| 40 | + set(${variable} "NOTFOUND" PARENT_SCOPE) |
| 41 | + return() |
| 42 | + endif() |
| 43 | + set(target "${ARG_TARGET}") |
| 44 | + if(NOT target AND CMAKE_CXX_COMPILER_TARGET) |
| 45 | + set(target "${CMAKE_CXX_COMPILER_TARGET}") |
| 46 | + endif() |
| 47 | + if(NOT DEFINED COMPILER_RT_LIBRARY_builtins_${target}) |
| 48 | + # If the cache variable is not defined, invoke Clang and then |
| 49 | + # set it with cache_compiler_rt_library. |
| 50 | + set(clang_command ${CMAKE_CXX_COMPILER} ${CMAKE_REQUIRED_FLAGS}) |
| 51 | + if(target) |
| 52 | + list(APPEND clang_command "--target=${target}") |
| 53 | + endif() |
| 54 | + get_property(cxx_flags CACHE CMAKE_CXX_FLAGS PROPERTY VALUE) |
| 55 | + string(REPLACE " " ";" cxx_flags "${cxx_flags}") |
| 56 | + list(APPEND clang_command ${cxx_flags}) |
| 57 | + execute_process( |
| 58 | + COMMAND ${clang_command} "--rtlib=compiler-rt" "-print-libgcc-file-name" |
| 59 | + RESULT_VARIABLE had_error |
| 60 | + OUTPUT_VARIABLE library_file |
| 61 | + ) |
| 62 | + string(STRIP "${library_file}" library_file) |
| 63 | + file(TO_CMAKE_PATH "${library_file}" library_file) |
| 64 | + get_filename_component(dirname ${library_file} DIRECTORY) |
| 65 | + if(APPLE) |
| 66 | + execute_process( |
| 67 | + COMMAND ${clang_command} "--print-resource-dir" |
| 68 | + RESULT_VARIABLE had_error |
| 69 | + OUTPUT_VARIABLE resource_dir |
| 70 | + ) |
| 71 | + string(STRIP "${resource_dir}" resource_dir) |
| 72 | + set(dirname "${resource_dir}/lib/darwin") |
| 73 | + endif() |
| 74 | + get_filename_component(basename ${library_file} NAME) |
| 75 | + if(basename MATCHES "libclang_rt\.([a-z0-9_\-]+)\.a") |
| 76 | + set(from_name ${CMAKE_MATCH_1}) |
| 77 | + get_component_name(${CMAKE_MATCH_1} to_name) |
| 78 | + string(REPLACE "${from_name}" "${to_name}" basename "${basename}") |
| 79 | + set(library_file "${dirname}/${basename}") |
| 80 | + cache_compiler_rt_library(${had_error} builtins "${target}" "${library_file}") |
| 81 | + endif() |
| 82 | + endif() |
| 83 | + if(NOT COMPILER_RT_LIBRARY_builtins_${target}) |
| 84 | + set(${variable} "NOTFOUND" PARENT_SCOPE) |
| 85 | + return() |
| 86 | + endif() |
| 87 | + if(NOT DEFINED COMPILER_RT_LIBRARY_${name}_${target}) |
| 88 | + # Clang gives only the builtins library path. Other library paths are |
| 89 | + # obtained by substituting "builtins" with ${name} in the builtins |
| 90 | + # path and then checking if the resultant path exists. The result of |
| 91 | + # this check is also cached by cache_compiler_rt_library. |
| 92 | + set(library_file "${COMPILER_RT_LIBRARY_builtins_${target}}") |
| 93 | + if(library_file MATCHES ".*libclang_rt\.([a-z0-9_\-]+)\.a") |
| 94 | + set(from_name ${CMAKE_MATCH_0}) |
| 95 | + get_component_name(${name} to_name) |
| 96 | + string(REPLACE "${from_name}" "${to_name}" library_file "${library_file}") |
| 97 | + cache_compiler_rt_library(FALSE "${name}" "${target}" "${library_file}") |
| 98 | + endif() |
| 99 | + endif() |
| 100 | + set(${variable} "${COMPILER_RT_LIBRARY_${name}_${target}}" PARENT_SCOPE) |
| 101 | +endfunction() |
0 commit comments