[LoongArch] Implement MCTargetExpr::fixELFSymbolsInTLSFixups hook
[llvm-project.git] / cmake / Modules / HandleCompilerRT.cmake
blob6865f45175ed79754f8cab45d8f095f642e529b8
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}")
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()
18 function(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")
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")
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)
47 endfunction()
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)
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 then
67     # 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_error
82       OUTPUT_VARIABLE library_file
83     )
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_error
91         OUTPUT_VARIABLE resource_dir
92       )
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 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}")
120     endif()
121   endif()
122   set(${variable} "${COMPILER_RT_LIBRARY_${name}_${target}}" PARENT_SCOPE)
123 endfunction()