1# Check if the compiler-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 6function(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() 16endfunction() 17 18function(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) 29endfunction() 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`. 35function(find_compiler_rt_library name variable) 36 cmake_parse_arguments(ARG "" "TARGET;FLAGS" "" ${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} "${ARG_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 set(cmd_prefix "") 58 if(MSVC AND ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang") 59 set(cmd_prefix "/clang:") 60 endif() 61 execute_process( 62 COMMAND ${clang_command} "${cmd_prefix}--rtlib=compiler-rt" "${cmd_prefix}-print-libgcc-file-name" 63 RESULT_VARIABLE had_error 64 OUTPUT_VARIABLE library_file 65 ) 66 string(STRIP "${library_file}" library_file) 67 file(TO_CMAKE_PATH "${library_file}" library_file) 68 get_filename_component(dirname ${library_file} DIRECTORY) 69 if(APPLE) 70 execute_process( 71 COMMAND ${clang_command} "--print-resource-dir" 72 RESULT_VARIABLE had_error 73 OUTPUT_VARIABLE resource_dir 74 ) 75 string(STRIP "${resource_dir}" resource_dir) 76 set(dirname "${resource_dir}/lib/darwin") 77 endif() 78 get_filename_component(basename ${library_file} NAME) 79 if(basename MATCHES ".*clang_rt\.([a-z0-9_\-]+)\.(a|lib)") 80 set(from_name ${CMAKE_MATCH_1}) 81 get_component_name(${CMAKE_MATCH_1} to_name) 82 string(REPLACE "${from_name}" "${to_name}" basename "${basename}") 83 set(library_file "${dirname}/${basename}") 84 cache_compiler_rt_library(${had_error} builtins "${target}" "${library_file}") 85 endif() 86 endif() 87 if(NOT COMPILER_RT_LIBRARY_builtins_${target}) 88 set(${variable} "NOTFOUND" PARENT_SCOPE) 89 return() 90 endif() 91 if(NOT DEFINED COMPILER_RT_LIBRARY_${name}_${target}) 92 # Clang gives only the builtins library path. Other library paths are 93 # obtained by substituting "builtins" with ${name} in the builtins 94 # path and then checking if the resultant path exists. The result of 95 # this check is also cached by cache_compiler_rt_library. 96 set(library_file "${COMPILER_RT_LIBRARY_builtins_${target}}") 97 if(library_file MATCHES ".*clang_rt\.([a-z0-9_\-]+)\.(a|lib)") 98 set(from_name ${CMAKE_MATCH_0}) 99 get_component_name(${name} to_name) 100 string(REPLACE "${from_name}" "${to_name}" library_file "${library_file}") 101 cache_compiler_rt_library(FALSE "${name}" "${target}" "${library_file}") 102 endif() 103 endif() 104 set(${variable} "${COMPILER_RT_LIBRARY_${name}_${target}}" PARENT_SCOPE) 105endfunction() 106