128 lines · plain
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 NOTFOUND6function(cache_compiler_rt_library err_flag name target library_file)7 if(err_flag OR NOT EXISTS "${library_file}")8 message(WARNING "Failed to find compiler-rt ${name} library for ${target}")9 set(COMPILER_RT_LIBRARY_${name}_${target} "NOTFOUND" CACHE INTERNAL10 "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 INTERNAL14 "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 if (CMAKE_OSX_SYSROOT MATCHES ".+MacOSX.+")24 set(component_name "${component_name}osx")25 26 elseif (CMAKE_OSX_SYSROOT MATCHES ".+iPhoneOS.+")27 set(component_name "${component_name}ios")28 elseif (CMAKE_OSX_SYSROOT MATCHES ".+iPhoneSimulator.+")29 set(component_name "${component_name}iossim")30 31 elseif (CMAKE_OSX_SYSROOT MATCHES ".+AppleTVOS.+")32 set(component_name "${component_name}tvos")33 elseif (CMAKE_OSX_SYSROOT MATCHES ".+AppleTVSimulator.+")34 set(component_name "${component_name}tvossim")35 36 elseif (CMAKE_OSX_SYSROOT MATCHES ".+WatchOS.+")37 set(component_name "${component_name}watchos")38 elseif (CMAKE_OSX_SYSROOT MATCHES ".+WatchSimulator.+")39 set(component_name "${component_name}watchossim")40 else()41 message(WARNING "Unknown Apple SDK ${CMAKE_OSX_SYSROOT}, we don't know which compiler-rt library suffix to use.")42 endif()43 else()44 set(component_name "${name}")45 endif()46 set(${variable} "${component_name}" PARENT_SCOPE)47endfunction()48 49# Find the path to compiler-rt library `name` (e.g. "builtins") for the50# specified `TARGET` (e.g. "x86_64-linux-gnu") and return it in `variable`.51# This calls cache_compiler_rt_library that caches the path to speed up52# repeated invocations with the same `name` and `target`.53function(find_compiler_rt_library name variable)54 cmake_parse_arguments(ARG "SHARED" "TARGET;FLAGS" "" ${ARGN})55 # While we can use compiler-rt runtimes with other compilers, we need to56 # query the compiler for runtime location and thus we require Clang.57 if(NOT CMAKE_CXX_COMPILER_ID MATCHES Clang)58 set(${variable} "NOTFOUND" PARENT_SCOPE)59 return()60 endif()61 set(target "${ARG_TARGET}")62 if(NOT target AND CMAKE_CXX_COMPILER_TARGET)63 set(target "${CMAKE_CXX_COMPILER_TARGET}")64 endif()65 if(NOT DEFINED COMPILER_RT_LIBRARY_builtins_${target})66 # If the cache variable is not defined, invoke Clang and then67 # set it with cache_compiler_rt_library.68 set(clang_command ${CMAKE_CXX_COMPILER} "${ARG_FLAGS}")69 if(target)70 list(APPEND clang_command "--target=${target}")71 endif()72 get_property(cxx_flags CACHE CMAKE_CXX_FLAGS PROPERTY VALUE)73 string(REPLACE " " ";" cxx_flags "${cxx_flags}")74 list(APPEND clang_command ${cxx_flags})75 set(cmd_prefix "")76 if(MSVC AND ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")77 set(cmd_prefix "/clang:")78 endif()79 execute_process(80 COMMAND ${clang_command} "${cmd_prefix}--rtlib=compiler-rt" "${cmd_prefix}-print-libgcc-file-name"81 RESULT_VARIABLE had_error82 OUTPUT_VARIABLE library_file83 )84 string(STRIP "${library_file}" library_file)85 file(TO_CMAKE_PATH "${library_file}" library_file)86 get_filename_component(dirname ${library_file} DIRECTORY)87 if(APPLE)88 execute_process(89 COMMAND ${clang_command} "--print-resource-dir"90 RESULT_VARIABLE had_error91 OUTPUT_VARIABLE resource_dir92 )93 string(STRIP "${resource_dir}" resource_dir)94 set(dirname "${resource_dir}/lib/darwin")95 endif()96 get_filename_component(basename ${library_file} NAME)97 if(basename MATCHES ".*clang_rt\.([a-z0-9_\-]+)\.(a|lib)")98 set(from_name ${CMAKE_MATCH_1})99 get_component_name(${CMAKE_MATCH_1} to_name)100 string(REPLACE "${from_name}" "${to_name}" basename "${basename}")101 set(library_file "${dirname}/${basename}")102 cache_compiler_rt_library(${had_error} builtins "${target}" "${library_file}")103 endif()104 endif()105 if(NOT COMPILER_RT_LIBRARY_builtins_${target})106 set(${variable} "NOTFOUND" PARENT_SCOPE)107 return()108 endif()109 if(NOT DEFINED COMPILER_RT_LIBRARY_${name}_${target})110 # Clang gives only the builtins library path. Other library paths are111 # obtained by substituting "builtins" with ${name} in the builtins112 # path and then checking if the resultant path exists. The result of113 # this check is also cached by cache_compiler_rt_library.114 set(library_file "${COMPILER_RT_LIBRARY_builtins_${target}}")115 get_component_name("builtins" from_name)116 get_component_name(${name} to_name)117 get_filename_component(basename ${library_file} NAME)118 string(REPLACE "${from_name}" "${to_name}" basename "${basename}")119 if (ARG_SHARED)120 string(REGEX REPLACE "\.(a|lib)$" ".so" basename "${basename}")121 endif()122 get_filename_component(dirname ${library_file} DIRECTORY)123 set(library_file "${dirname}/${basename}")124 cache_compiler_rt_library(FALSE "${name}" "${target}" "${library_file}")125 endif()126 set(${variable} "${COMPILER_RT_LIBRARY_${name}_${target}}" PARENT_SCOPE)127endfunction()128