|
2 | 2 | # for details. All rights reserved. Use of this source code is governed by a
|
3 | 3 | # BSD-style license that can be found in the LICENSE file.
|
4 | 4 |
|
| 5 | +_dart_root = get_path_info("../..", "abspath") |
| 6 | + |
5 | 7 | template("rust_library") {
|
6 | 8 | manifest = rebase_path("Cargo.toml")
|
7 | 9 | if (defined(invoker.manifest)) {
|
8 | 10 | manifest = invoker.manifest
|
9 | 11 | }
|
10 | 12 |
|
| 13 | + debug = defined(invoker.debug) && invoker.debug |
| 14 | + shared = defined(invoker.shared) && invoker.shared |
| 15 | + |
11 | 16 | cmd = [
|
12 |
| - rebase_path("//buildtools/${current_os}-${current_cpu}/rust/bin/cargo"), |
| 17 | + rebase_path("//buildtools/${host_os}-${host_cpu}/rust/bin/cargo"), |
13 | 18 | "build",
|
14 | 19 | "--target-dir",
|
15 | 20 | rebase_path(target_out_dir),
|
16 | 21 | "--manifest-path",
|
17 | 22 | manifest,
|
18 | 23 | ]
|
19 |
| - output = "$target_out_dir/lib${invoker.lib_name}.a" |
20 |
| - debug = defined(invoker.debug) && invoker.debug |
21 | 24 |
|
22 |
| - if (!debug) { |
| 25 | + # For cross compilation, figure out the target triple. You can get a full list |
| 26 | + # of the targets that rust supports like this: rustc --print target-list |
| 27 | + cargo_out_dir = target_out_dir |
| 28 | + if (is_linux) { |
| 29 | + rust_os = "unknown-linux-gnu" |
| 30 | + } else if (is_mac) { |
| 31 | + rust_os = "apple-darwin" |
| 32 | + } else if (is_win) { |
| 33 | + rust_os = "pc-windows-gnu" |
| 34 | + } else if (is_android) { |
| 35 | + rust_os = "linux-android" |
| 36 | + } else if (is_fuchsia) { |
| 37 | + rust_os = "fuchsia" |
| 38 | + } |
| 39 | + if (defined(rust_os)) { |
| 40 | + if (current_cpu == "x86") { |
| 41 | + rust_target = "i686-${rust_os}" |
| 42 | + } else if (current_cpu == "x64") { |
| 43 | + rust_target = "x86_64-${rust_os}" |
| 44 | + } else if (current_cpu == "arm") { |
| 45 | + rust_target = "arm-${rust_os}eabi" |
| 46 | + } else if (current_cpu == "arm64") { |
| 47 | + rust_target = "aarch64-${rust_os}" |
| 48 | + } |
| 49 | + } |
| 50 | + if (defined(rust_target)) { |
| 51 | + cmd += [ |
| 52 | + "--target", |
| 53 | + rust_target, |
| 54 | + ] |
| 55 | + cargo_out_dir += "/${rust_target}" |
| 56 | + } |
| 57 | + |
| 58 | + if (debug) { |
| 59 | + cargo_out_dir += "/debug" |
| 60 | + } else { |
| 61 | + cargo_out_dir += "/release" |
23 | 62 | cmd += [ "--release" ]
|
24 | 63 | }
|
25 | 64 |
|
26 |
| - action(target_name) { |
27 |
| - script = "//build/rust/run.py" |
| 65 | + output_file = "" |
| 66 | + if (shared) { |
| 67 | + if (is_win) { |
| 68 | + output_file = "${invoker.lib_name}.dll" |
| 69 | + } else if (is_mac) { |
| 70 | + output_file = "lib${invoker.lib_name}.dylib" |
| 71 | + } else { |
| 72 | + output_file = "lib${invoker.lib_name}.so" |
| 73 | + } |
| 74 | + } else { |
| 75 | + if (is_win) { |
| 76 | + output_file = "${invoker.lib_name}.lib" |
| 77 | + }else { |
| 78 | + output_file = "lib${invoker.lib_name}.a" |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + action("${target_name}_cargo") { |
| 83 | + script = "${_dart_root}/build/rust/run.py" |
28 | 84 | args = cmd
|
29 |
| - outputs = [ output ] |
| 85 | + outputs = [ "${cargo_out_dir}/${output_file}" ] |
30 | 86 | public_configs = [ ":${target_name}_config" ]
|
31 | 87 | }
|
32 | 88 |
|
33 |
| - config("${target_name}_config") { |
34 |
| - libs = [ "wasmer" ] |
35 |
| - if (debug) { |
36 |
| - lib_dirs = [ "$target_out_dir/debug" ] |
37 |
| - } else { |
38 |
| - lib_dirs = [ "$target_out_dir/release" ] |
| 89 | + config("${target_name}_cargo_config") { |
| 90 | + if (!shared) { |
| 91 | + libs = [ invoker.lib_name ] |
| 92 | + lib_dirs = [ out_dir ] |
39 | 93 | }
|
40 | 94 | }
|
| 95 | + |
| 96 | + # Cargo leaves the library in cargo_out_dir, which varies based on the target. |
| 97 | + # So we need to copy it to target_out_dir to make it easier for dependees to |
| 98 | + # locate the library. |
| 99 | + copy(target_name) { |
| 100 | + deps = [ ":${target_name}_cargo" ] |
| 101 | + sources = [ "${cargo_out_dir}/${output_file}" ] |
| 102 | + outputs = [ "${target_out_dir}/${output_file}" ] |
| 103 | + } |
41 | 104 | }
|
0 commit comments