@@ -111,6 +111,15 @@ impl Step for Llvm {
111111 /// Compile LLVM for `target`.
112112 fn run ( self , builder : & Builder < ' _ > ) -> PathBuf {
113113 let target = self . target ;
114+ let target_native = if self . target . starts_with ( "riscv" ) {
115+ // RISC-V target triples in Rust is not named the same as C compiler target triples.
116+ // This converts Rust RISC-V target triples to C compiler triples.
117+ let idx = target. find ( '-' ) . unwrap ( ) ;
118+
119+ format ! ( "riscv{}{}" , & target[ 5 ..7 ] , & target[ idx..] )
120+ } else {
121+ target. to_string ( )
122+ } ;
114123
115124 let Meta { stamp, build_llvm_config, out_dir, root } =
116125 match prebuilt_llvm_config ( builder, target) {
@@ -164,8 +173,8 @@ impl Step for Llvm {
164173 . define ( "LLVM_ENABLE_BINDINGS" , "OFF" )
165174 . define ( "LLVM_ENABLE_Z3_SOLVER" , "OFF" )
166175 . define ( "LLVM_PARALLEL_COMPILE_JOBS" , builder. jobs ( ) . to_string ( ) )
167- . define ( "LLVM_TARGET_ARCH" , target . split ( '-' ) . next ( ) . unwrap ( ) )
168- . define ( "LLVM_DEFAULT_TARGET_TRIPLE" , target ) ;
176+ . define ( "LLVM_TARGET_ARCH" , target_native . split ( '-' ) . next ( ) . unwrap ( ) )
177+ . define ( "LLVM_DEFAULT_TARGET_TRIPLE" , target_native ) ;
169178
170179 if !target. contains ( "netbsd" ) {
171180 cfg. define ( "LLVM_ENABLE_ZLIB" , "ON" ) ;
@@ -212,6 +221,17 @@ impl Step for Llvm {
212221 }
213222 }
214223
224+ if target. starts_with ( "riscv" ) {
225+ // In RISC-V, using C++ atomics require linking to `libatomic` but the LLVM build
226+ // system check cannot detect this. Therefore it is set manually here.
227+ if !builder. config . llvm_tools_enabled {
228+ cfg. define ( "CMAKE_EXE_LINKER_FLAGS" , "-latomic" ) ;
229+ } else {
230+ cfg. define ( "CMAKE_EXE_LINKER_FLAGS" , "-latomic -static-libstdc++" ) ;
231+ }
232+ cfg. define ( "CMAKE_SHARED_LINKER_FLAGS" , "-latomic" ) ;
233+ }
234+
215235 if target. contains ( "msvc" ) {
216236 cfg. define ( "LLVM_USE_CRT_DEBUG" , "MT" ) ;
217237 cfg. define ( "LLVM_USE_CRT_RELEASE" , "MT" ) ;
0 commit comments