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
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}")
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}")
18 function(get_component_name name variable)
20 if(NOT name MATCHES "builtins.*")
21 set(component_name "${name}_")
23 if (CMAKE_OSX_SYSROOT MATCHES ".+MacOSX.+")
24 set(component_name "${component_name}osx")
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")
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")
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")
41 message(WARNING "Unknown Apple SDK ${CMAKE_OSX_SYSROOT}, we don't know which compiler-rt library suffix to use.")
44 set(component_name "${name}")
46 set(${variable} "${component_name}" PARENT_SCOPE)
49 # Find the path to compiler-rt library `name` (e.g. "builtins") for the
50 # 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 up
52 # repeated invocations with the same `name` and `target`.
53 function(find_compiler_rt_library name variable)
54 cmake_parse_arguments(ARG "" "TARGET;FLAGS" "" ${ARGN})
55 # While we can use compiler-rt runtimes with other compilers, we need to
56 # 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)
61 set(target "${ARG_TARGET}")
62 if(NOT target AND CMAKE_CXX_COMPILER_TARGET)
63 set(target "${CMAKE_CXX_COMPILER_TARGET}")
65 if(NOT DEFINED COMPILER_RT_LIBRARY_builtins_${target})
66 # If the cache variable is not defined, invoke Clang and then
67 # set it with cache_compiler_rt_library.
68 set(clang_command ${CMAKE_CXX_COMPILER} "${ARG_FLAGS}")
70 list(APPEND clang_command "--target=${target}")
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})
76 if(MSVC AND ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
77 set(cmd_prefix "/clang:")
80 COMMAND ${clang_command} "${cmd_prefix}--rtlib=compiler-rt" "${cmd_prefix}-print-libgcc-file-name"
81 RESULT_VARIABLE had_error
82 OUTPUT_VARIABLE library_file
84 string(STRIP "${library_file}" library_file)
85 file(TO_CMAKE_PATH "${library_file}" library_file)
86 get_filename_component(dirname ${library_file} DIRECTORY)
89 COMMAND ${clang_command} "--print-resource-dir"
90 RESULT_VARIABLE had_error
91 OUTPUT_VARIABLE resource_dir
93 string(STRIP "${resource_dir}" resource_dir)
94 set(dirname "${resource_dir}/lib/darwin")
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}")
105 if(NOT COMPILER_RT_LIBRARY_builtins_${target})
106 set(${variable} "NOTFOUND" PARENT_SCOPE)
109 if(NOT DEFINED COMPILER_RT_LIBRARY_${name}_${target})
110 # Clang gives only the builtins library path. Other library paths are
111 # obtained by substituting "builtins" with ${name} in the builtins
112 # path and then checking if the resultant path exists. The result of
113 # this check is also cached by cache_compiler_rt_library.
114 set(library_file "${COMPILER_RT_LIBRARY_builtins_${target}}")
115 if(library_file MATCHES ".*clang_rt\.([a-z0-9_\-]+)\.(a|lib)")
116 set(from_name ${CMAKE_MATCH_0})
117 get_component_name(${name} to_name)
118 string(REPLACE "${from_name}" "${to_name}" library_file "${library_file}")
119 cache_compiler_rt_library(FALSE "${name}" "${target}" "${library_file}")
122 set(${variable} "${COMPILER_RT_LIBRARY_${name}_${target}}" PARENT_SCOPE)