@@ -112,6 +112,15 @@ impl Step for Llvm {
112
112
/// Compile LLVM for `target`.
113
113
fn run ( self , builder : & Builder < ' _ > ) -> PathBuf {
114
114
let target = self . target ;
115
+ let target_native = if self . target . starts_with ( "riscv" ) {
116
+ // RISC-V target triples in Rust is not named the same as C compiler target triples.
117
+ // This converts Rust RISC-V target triples to C compiler triples.
118
+ let idx = target. find ( '-' ) . unwrap ( ) ;
119
+
120
+ format ! ( "riscv{}{}" , & target[ 5 ..7 ] , & target[ idx..] )
121
+ } else {
122
+ target. to_string ( )
123
+ } ;
115
124
116
125
let Meta { stamp, build_llvm_config, out_dir, root } =
117
126
match prebuilt_llvm_config ( builder, target) {
@@ -165,8 +174,8 @@ impl Step for Llvm {
165
174
. define ( "LLVM_ENABLE_BINDINGS" , "OFF" )
166
175
. define ( "LLVM_ENABLE_Z3_SOLVER" , "OFF" )
167
176
. define ( "LLVM_PARALLEL_COMPILE_JOBS" , builder. jobs ( ) . to_string ( ) )
168
- . define ( "LLVM_TARGET_ARCH" , target . split ( '-' ) . next ( ) . unwrap ( ) )
169
- . define ( "LLVM_DEFAULT_TARGET_TRIPLE" , target ) ;
177
+ . define ( "LLVM_TARGET_ARCH" , target_native . split ( '-' ) . next ( ) . unwrap ( ) )
178
+ . define ( "LLVM_DEFAULT_TARGET_TRIPLE" , target_native ) ;
170
179
171
180
if !target. contains ( "netbsd" ) {
172
181
cfg. define ( "LLVM_ENABLE_ZLIB" , "ON" ) ;
@@ -213,6 +222,17 @@ impl Step for Llvm {
213
222
}
214
223
}
215
224
225
+ if target. starts_with ( "riscv" ) {
226
+ // In RISC-V, using C++ atomics require linking to `libatomic` but the LLVM build
227
+ // system check cannot detect this. Therefore it is set manually here.
228
+ if !builder. config . llvm_tools_enabled {
229
+ cfg. define ( "CMAKE_EXE_LINKER_FLAGS" , "-latomic" ) ;
230
+ } else {
231
+ cfg. define ( "CMAKE_EXE_LINKER_FLAGS" , "-latomic -static-libstdc++" ) ;
232
+ }
233
+ cfg. define ( "CMAKE_SHARED_LINKER_FLAGS" , "-latomic" ) ;
234
+ }
235
+
216
236
if target. contains ( "msvc" ) {
217
237
cfg. define ( "LLVM_USE_CRT_DEBUG" , "MT" ) ;
218
238
cfg. define ( "LLVM_USE_CRT_RELEASE" , "MT" ) ;
0 commit comments