@@ -111,6 +111,15 @@ impl Step for Llvm {
111
111
/// Compile LLVM for `target`.
112
112
fn run ( self , builder : & Builder < ' _ > ) -> PathBuf {
113
113
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
+ } ;
114
123
115
124
let Meta { stamp, build_llvm_config, out_dir, root } =
116
125
match prebuilt_llvm_config ( builder, target) {
@@ -164,8 +173,8 @@ impl Step for Llvm {
164
173
. define ( "LLVM_ENABLE_BINDINGS" , "OFF" )
165
174
. define ( "LLVM_ENABLE_Z3_SOLVER" , "OFF" )
166
175
. 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 ) ;
169
178
170
179
if !target. contains ( "netbsd" ) {
171
180
cfg. define ( "LLVM_ENABLE_ZLIB" , "ON" ) ;
@@ -212,6 +221,17 @@ impl Step for Llvm {
212
221
}
213
222
}
214
223
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
+
215
235
if target. contains ( "msvc" ) {
216
236
cfg. define ( "LLVM_USE_CRT_DEBUG" , "MT" ) ;
217
237
cfg. define ( "LLVM_USE_CRT_RELEASE" , "MT" ) ;
0 commit comments