gfx-rs / wgpu-native

Native WebGPU implementation based on wgpu-core
Apache License 2.0
879 stars 105 forks source link

cross compile from linux to MacOs #50

Open GopherJ opened 4 years ago

GopherJ commented 4 years ago

While cross compiling iced I came across an error on wgpu-native:

 Compiling wgpu-native v0.5.1
error: linking with `x86_64-apple-darwin14-clang` failed: exit code: 1
  |
  = note: "x86_64-apple-darwin14-clang" "-m64" "-L" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.0.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.1.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.10.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.11.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.12.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.13.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.14.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.15.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.2.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.3.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.4.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.5.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.6.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.7.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.8.rcgu.o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.wgpu_native.6kkamhy4-cgu.9.rcgu.o" "-o" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libwgpu_native-259eb0704b182ba7.dylib" "-Wl,-exported_symbols_list,/tmp/rustcJcFFXZ/list" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/wgpu_native-259eb0704b182ba7.2ls74fghvuz96pfl.rcgu.o" "-Wl,-dead_strip" "-dynamiclib" "-Wl,-dylib" "-nodefaultlibs" "-L" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps" "-L" "/home/cheng/Projects/Rust/iced/target/release/deps" "-L" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/build/objc_exception-5b64af16b264bb60/out" "-L" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/build/spirv_cross-2cda498fffa447b0/out" "-L" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libwgpu_core-7a0eb1d2ec16c3e7.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libvec_map-0cdc4d1719aef91b.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libgfx_memory-b0957f5116dad517.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libslab-a8a80358c74b7aaa.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libhibitset-51a8ed710470ef93.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libatom-4e17c0803db55e2a.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libwgpu_types-6b72d275407aeb2c.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libpeek_poke-636dcdf68cfe553c.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libgfx_descriptor-02416bbcba2108f5.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libgfx_backend_metal-fd2d5871df34aa8f.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libstorage_map-8b969022a494c6f5.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libcopyless-11263fcc7f845c81.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libgfx_auxil-40d08a5187d9196d.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libfxhash-401e829a170f5be0.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libbyteorder-ab8d11b47d17436b.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libspirv_cross-60cbb27da3f217dc.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libarrayvec-724353cd69c1d476.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libparking_lot-ddde31b9da9359bf.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libparking_lot_core-bbb77b6192a2bb78.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libsmallvec-b98c81b250f221c1.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/liblock_api-abc2b1410dc340cb.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libscopeguard-2f6bc472ecc1519b.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libmetal-f6a5dc9a853d52f9.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/liblazy_static-e63e0bd2d580c996.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libcocoa-8d9559fa2f0b20c7.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libcore_graphics-c9ef86c602b3f02d.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libforeign_types-484b6ff8f3831cb4.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libforeign_types_shared-07c0f0c2102950eb.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libcore_foundation-34c585b011cd9620.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libcore_foundation_sys-28f40862754da4ca.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libblock-590436d5f0eb89e6.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/librange_alloc-6fdfd7889cb68997.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libobjc-febcccb4db4626d9.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libmalloc_buf-58fbf4f1d52fba23.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libgfx_backend_empty-21e0815140cced69.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/liblog-032b4d7c87d5ce65.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libcfg_if-5d7f3510936b3ebf.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libgfx_hal-ea6185a4ef28cd6b.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libraw_window_handle-aad9bef18864c577.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/liblibc-3507ae351ab28dbd.rlib" "/home/cheng/Projects/Rust/iced/target/x86_64-apple-darwin/release/deps/libbitflags-53d97153ceb1e7b6.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libstd-8b17b186cd972395.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libpanic_unwind-d06c699b1a1b11b4.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libhashbrown-65f6b0602eb03a82.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_alloc-cbfad1c2e76ceaeb.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libbacktrace-6e499767504f2786.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libbacktrace_sys-767e350fd8547e78.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/librustc_demangle-9ecb8e821171ea10.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libunwind-c11544637ad3758f.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libcfg_if-2e3b92524ee7eda9.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/liblibc-628a35dcde52dad2.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/liballoc-f99de1eca1e6e970.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/librustc_std_workspace_core-ef5998790eeac756.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libcore-3fe78a4d2924ae43.rlib" "/home/cheng/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-apple-darwin/lib/libcompiler_builtins-57149daefc485a08.rlib" "-lc++" "-framework" "Metal" "-lSystem" "-framework" "AppKit" "-framework" "AppKit" "-framework" "Foundation" "-framework" "Foundation" "-framework" "QuartzCore" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreGraphics" "-framework" "CoreFoundation" "-lSystem" "-lobjc" "-lSystem" "-lresolv" "-lc" "-lm"
  = note: ld: framework not found Metal
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

error: aborting due to previous error

error: could not compile `wgpu-native`.

To learn more, run the command again with --verbose.

Any idea on this?

GopherJ commented 4 years ago

See also: https://github.com/hecrj/iced/issues/536

GopherJ commented 4 years ago

The script that I used to compile is located at:

https://github.com/GopherJ/cfg/blob/master/scripts/osxcross.sh

Environments:

System: Linux Mint 20 Cinamon Rust: rustc 1.46.0-nightly (feb3536eb 2020-06-09)

GopherJ commented 4 years ago

Am I missing something?

grovesNL commented 4 years ago

wgpu uses Metal.framework (normally available in macOS/iOS SDKs), so the argument "-framework" "Metal" will try to link to Metal.framework and fail if it can't be found.

I'm not sure how easy or possible it is to use the macOS SDK (specifically Metal.framework) from Linux for cross-compilation. I've heard it's possible at least (e.g. @kvark mentioned that Gecko cross-compiles wgpu for macOS from a Linux host). Unfortunately we haven't been using cross-compilation in this project (we use macOS hosts), so we don't have much experience with this.

kvark commented 4 years ago

@grovesNL Firefox CI already does it. The Firefox macOS builds are produced on Linux, and they include wgpu. The secret was to link with -weak-framework Metal, see https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/WeakLinking.html#//apple_ref/doc/uid/20002378-107026

grovesNL commented 4 years ago

Alright thanks! So it sounds the cross-compilation can already be done manually by replacing "-framework" "Metal" with "-weak_framework" "Metal", and longer term we might want to consider changing how this works upstream in our metal bindings (gfx-rs/metal-rs#105).

@GopherJ would you like to try manually running the "x86_64-apple-darwin14-clang" "-m64" "-L" ... command from above, but replacing "-framework" "Metal" with "-weak_framework" "Metal"?

GopherJ commented 4 years ago

@grovesNL Thanks for your reply, is there a way to do this through cargo?

To reproduce:

  1. clone iced

    git clone https://github.com/hecrj/iced
  2. change working directory

cd examples/game_of_life
  1. pull script
curl -O https://raw.githubusercontent.com/GopherJ/cfg/master/scripts/osxcross.sh
chmod u+x osxcross.sh
  1. run
./osxcross.sh
grovesNL commented 4 years ago

I'm not sure if there's a way to do it with cargo directly – we'd need to intercept the build command generated by cargo to replace the framework with weak_framework for Metal and run that. I was thinking we could just try manually running that "x86_64-apple-darwin14-clang" ... command from above with the replacement to check if it works for you.

(the eventual fix will probably be gfx-rs/metal-rs#105, but hoping we could workaround it for now)

grovesNL commented 4 years ago

I tried specifying weak_framework on the externs:

#[link(name = "Metal", kind = "weak_framework")]
extern "C" {
  /* ... */
}

but that fails with unknown kind:

error[E0458]: unknown kind: `weak_framework`
    --> src/device.rs:1352:24
     |
1352 | #[link(name = "Metal", kind = "weak_framework")]
     | -----------------------^^^^^^^^^^^^^^^^^^^^^^^--
     |                        |
     |                        unknown kind

error: aborting due to previous error

I also tried using build.rs and passing it as an linker argument there:

fn main() {
    println!("cargo:rustc-link-lib=weak_framework=Metal");
}

but that also fails:

error: unknown library kind `weak_framework`, expected one of dylib, framework, or static

So it looks like we'll have to find some other ways to pass weak_framework to the linker. I think it's possible to do it with a custom .cargo/config but we can't use that here either.

kvark commented 4 years ago

Could you file a bug to rust-lang about "weak_framework" not recognized as an option?

grovesNL commented 4 years ago

I asked on Zulip and Discourse, I guess we can track updates in https://github.com/gfx-rs/metal-rs/issues/105#issuecomment-720811909