rust-bitcoin / rust-secp256k1

Rust language bindings for Bitcoin secp256k1 library.
Creative Commons Zero v1.0 Universal
346 stars 265 forks source link

Cross compile from x86_64-unknown-linux-gnu to aarch64-apple-darwin fails #679

Open kcalvinalvin opened 9 months ago

kcalvinalvin commented 9 months ago

I came across this error and wanted to document it here in case it's of help. I was compiling bdk which also compiles secp256k1-sys and when trying to cross-compile to aarch64-apple-darwin from x86_64-unknown-linxu-gnu, compilation fails with gcc: error: unrecognized command-line option ‘-arch’.

This stackoverflow answer suggests that -arch is only for gcc with xcode, which means I wouldn't be able to cross-compile.

Below is the full error log I got:

[I] calvin@bitcoin ~/b/r/r/utreexod (main)> cargo build --release --target=aarch64-apple-darwin                                                                                                                                                                                                              
   Compiling secp256k1-sys v0.8.1                                                                                                                                                                                                                                                                            
   Compiling bip39 v2.0.0                                                                                                                                                                                                                                                                                    
   Compiling weedle2 v4.0.0                                                                                                                                                                                                                                                                                  
   Compiling uniffi v0.25.0                                                                                                                                                                                                                                                                                  
The following warnings were emitted during compilation:                                                                                                                                                                                                                                                      

warning: gcc: error: unrecognized command-line option ‘-arch’                                                                                                                                                                                                                                                
warning: gcc: error: unrecognized command-line option ‘-arch’                                                                                                                                                                                                                                                

error: failed to run custom build command for `secp256k1-sys v0.8.1`                                                                                                                                                                                                                                         

Caused by:                                                                                                                                                                                                                                                                                                   
  process didn't exit successfully: `/home/calvin/bitcoin-projects/rust/rustup/utreexod/target/release/build/secp256k1-sys-678d711ae927d0b8/build-script-build` (exit status: 1)                                                                                                                             
  --- stdout                                                                                                                                                                                                                                                                                                 
  TARGET = Some("aarch64-apple-darwin")                                                                                                                                                                                                                                                                      
  OPT_LEVEL = Some("3")                                                                                                                                                                                                                                                                                      
  HOST = Some("x86_64-unknown-linux-gnu")                                                                                                                                                                                                                                                                    
  cargo:rerun-if-env-changed=CC_aarch64-apple-darwin                                                                                                                                                                                                                                                         
  CC_aarch64-apple-darwin = None                                                                                                                                                                                                                                                                             
  cargo:rerun-if-env-changed=CC_aarch64_apple_darwin                                                                                                                                                                                                                                                         
  CC_aarch64_apple_darwin = None                                                                                                                                                                                                                                                                             
  cargo:rerun-if-env-changed=TARGET_CC                                                                                                                                                                                                                                                                       
  TARGET_CC = None                                                                                                                                                                                                                                                                                           
  cargo:rerun-if-env-changed=CC                                                                                                                                                                                                                                                                              
  CC = Some("gcc")                                                                                                                                                                                                                                                                                           
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS                                                                                                                                                                                                                                                            
  CRATE_CC_NO_DEFAULTS = None                                                                                                                                                                                                                                                                                
  DEBUG = Some("false")                                                                                                                                                                                                                                                                                      
  CARGO_CFG_TARGET_FEATURE = Some("aes,crc,dit,dotprod,dpb,dpb2,fcma,fhm,flagm,fp16,frintts,jsconv,lor,lse,neon,paca,pacg,pan,pmuv3,ras,rcpc,rcpc2,rdm,sb,sha2,sha3,ssbs,vh")                                                                                                                                
  cargo:rerun-if-env-changed=CFLAGS_aarch64-apple-darwin                                                                                                                                                                                                                                                     
  CFLAGS_aarch64-apple-darwin = None                                                                                                                                                                                                                                                                         
  cargo:rerun-if-env-changed=CFLAGS_aarch64_apple_darwin                                                                                                                                                                                                                                                     
  CFLAGS_aarch64_apple_darwin = None                                                                                                                                                                                                                                                                         
  cargo:rerun-if-env-changed=TARGET_CFLAGS                                                                                                                                                                                                                                                                   
  TARGET_CFLAGS = None                                                                                                                                                                                                                                                                                       
  cargo:rerun-if-env-changed=CFLAGS                                                                                                                                                                                                                                                                          
  CFLAGS = None                                                                                                                                                                                                                                                                                              
  cargo:rerun-if-env-changed=CC_aarch64-apple-darwin                                                                                                                                                                                                                                                         
  CC_aarch64-apple-darwin = None                                                                                                                                                                                                                                                                             
  cargo:rerun-if-env-changed=CC_aarch64_apple_darwin                                                                                                                                                                                                                                                         
  CC_aarch64_apple_darwin = None                                                                                                                                                                                                                                                                             
  cargo:rerun-if-env-changed=TARGET_CC                                                                                                                                                                                                                                                                       
  TARGET_CC = None                                                                                                                                                                                                                                                                                           
  cargo:rerun-if-env-changed=CC                                                                                                                                                                                                                                                                              
  CC = Some("gcc")                                                                                                                                                                                                                                                                                           
  cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS                                                                                                                                                                                                                                                            
  CRATE_CC_NO_DEFAULTS = None                                                                                                                                                                                                                                                                                
  CARGO_CFG_TARGET_FEATURE = Some("aes,crc,dit,dotprod,dpb,dpb2,fcma,fhm,flagm,fp16,frintts,jsconv,lor,lse,neon,paca,pacg,pan,pmuv3,ras,rcpc,rcpc2,rdm,sb,sha2,sha3,ssbs,vh")                                                                                                                                
  cargo:rerun-if-env-changed=CFLAGS_aarch64-apple-darwin                                                                                                                                                                                                                                                     
  CFLAGS_aarch64-apple-darwin = None                                                                                                                                                                                                                                                                         
  cargo:rerun-if-env-changed=CFLAGS_aarch64_apple_darwin                                                                                                                                                                                                                                                     
  CFLAGS_aarch64_apple_darwin = None                                                                                                                                                                                                                                                                         
  cargo:rerun-if-env-changed=TARGET_CFLAGS                                                                                                                                                                                                                                                                   
  TARGET_CFLAGS = None                                                                                                                                                                                                                                                                                       
  cargo:rerun-if-env-changed=CFLAGS                                                                                                                                                                                                                                                                          
  CFLAGS = None                                                                                                                                                                                                                                                                                              
  running: "gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-Wall" "-Wextra" "-DSECP256K1_API=" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DECM
ULT_GEN_PREC_BITS=4" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-DENABLE_MODULE_RECOVERY=1" "-o" "/home/calvin/bitcoin-projects/rust/rustup/utreexod/target/aarch64-apple-darwin/release/build/secp256k1-sys-0f08fdb2410250b6/out/depend/secp256k1/contrib/lax_der_parsing.o" "-c" "depe
nd/secp256k1/contrib/lax_der_parsing.c"                                                                                                                                                                                                                                                                      
  cargo:warning=gcc: error: unrecognized command-line option ‘-arch’                                                                                                                                                                                                                                         

  exit status: 1                                                                                                                                                                                                                                                                                             
  running: "gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm/wasm-sysroot" "-Wall" "-Wextra" "-DSECP256K1_API=" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG=1" "-DENABLE_M
ODULE_EXTRAKEYS=1" "-DECMULT_GEN_PREC_BITS=4" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-DENABLE_MODULE_RECOVERY=1" "-o" "/home/calvin/bitcoin-projects/rust/rustup/utreexod/target/aarch64-apple-darwin/release/build/secp256k1-sys-0f08fdb2410250b6/out/depend/secp256k1/contrib/lax_
der_parsing.o" "-c" "depend/secp256k1/contrib/lax_der_parsing.c"                                                                                                                                                                                                                                             
  cargo:warning=gcc: error: unrecognized command-line option ‘-arch’                                                                                                                                                                                                                                         

  exit status: 1                                                                                                                                                                                                                                                                                             

  --- stderr                                                                                                                                                                                                                                                                   

  error occurred: Command "gcc" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-arch" "arm64" "-I" "depend/secp256k1/" "-I" "depend/secp256k1/include" "-I" "depend/secp256k1/src" "-I" "wasm/wasm-sysroot" "-Wall" "-Wextra" "-DSECP256K1_API=" "-DENABLE_MODULE_ECDH=1" "-DENABLE_MODULE_SCHNORRSIG
=1" "-DENABLE_MODULE_EXTRAKEYS=1" "-DECMULT_GEN_PREC_BITS=4" "-DECMULT_WINDOW_SIZE=15" "-DUSE_EXTERNAL_DEFAULT_CALLBACKS=1" "-DENABLE_MODULE_RECOVERY=1" "-o" "/home/calvin/bitcoin-projects/rust/rustup/utreexod/target/aarch64-apple-darwin/release/build/secp256k1-sys-0f08fdb2410250b6/out/depend/secp256
k1/contrib/lax_der_parsing.o" "-c" "depend/secp256k1/contrib/lax_der_parsing.c" with args "gcc" did not execute successfully (status code exit status: 1).                       
Kixunil commented 9 months ago

Sounds like this is a problem with the cc crate, would you mind reporting to them?

elichai commented 9 months ago

I don't think you can easily cross-compile from linux to OSX, their linker tools are proprietery, so even if you plug clang instead of gcc and make it produce the right code you won't be able to produce an executable without their xcode stuff AFAIR

elichai commented 9 months ago

Maybe you can make this work: https://github.com/tpoechtrager/osxcross