blob: d8c6496e5859fef9206e1b35395450daf2026ed6 [file] [log] [blame]
# Copyright 2017 The Fuchsia Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Defines a Rust artifact
#
# Only for internal use, supporting rust_library and rust_binary.
template("rust_artifact") {
if (defined(invoker.type)) {
type = invoker.type
assert(type == "bin" || type == "lib")
} else {
assert(false, "Must specify an artifact type")
}
if (defined(invoker.name)) {
name = invoker.name
} else {
if (type == "lib") {
name = exec_script("//build/rust/sanitize_target_name.py",
[ target_name ],
"trim string")
} else {
name = target_name
}
}
rust_deps = []
if (defined(invoker.deps)) {
rust_deps = invoker.deps
}
source_dir = "."
if (defined(invoker.source_dir)) {
source_dir = invoker.source_dir
}
target_label = get_label_info(":$target_name", "label_no_toolchain")
assert(current_os == "mac" || current_os == "linux" || current_os == "fuchsia")
if (current_os == "mac") {
target_triple = "x86_64-apple-darwin"
assert(current_cpu == "x64")
} else if (current_os == "linux") {
assert(current_cpu == "x64")
target_triple = "x86_64-unknown-linux-gnu"
} else if (current_os == "fuchsia") {
assert(current_cpu == "x64" || current_cpu == "arm64")
if (current_cpu == "x64") {
target_triple = "x86_64-unknown-fuchsia"
} else if (current_cpu == "arm64") {
target_triple = "aarch64-unknown-fuchsia"
}
}
assert(host_cpu == "x64")
assert(host_os == "mac" || host_os == "linux")
if (host_os == "mac") {
host_triple = "x86_64-apple-darwin"
} else if (host_os == "linux") {
host_triple = "x86_64-unknown-linux-gnu"
}
rust_base = "//buildtools/rust/rust-$host_triple"
if (is_debug) {
build_type = "debug"
} else {
build_type = "release"
}
if (type == "bin") {
output_file = "$target_out_dir/$target_name.rust/$target_triple/$build_type/$name"
output_depfile = "$output_file.d"
build_target_name = "${target_name}_build"
} else if (type == "lib") {
output_base = "$target_out_dir/$target_name.rust/$target_triple/$build_type/lib$name"
output_file = "$output_base.rmeta"
output_depfile = "$output_base.d"
build_target_name = target_name
}
main_target_name = target_name
if (type == "bin") {
copy(target_name) {
sources = [
output_file
]
outputs = [
"$root_out_dir/$name",
]
deps = [
":$build_target_name",
]
}
}
action(build_target_name) {
script = "//build/rust/build_target.py"
depfile = output_depfile
inputs = [
"${source_dir}/Cargo.toml",
]
outputs = [
output_file,
]
if (type == "lib") {
outputs += [
"$target_gen_dir/$main_target_name.rust/$main_target_name.rust_name",
]
}
deps = rust_deps
if (defined(invoker.non_rust_deps)) {
deps += invoker.non_rust_deps
}
if (is_fuchsia) {
clang_wrapper_target = "//rust/magenta-rs/tools:clang_wrapper($host_toolchain)"
deps += [
clang_wrapper_target,
]
}
args = [
"--type",
type,
"--name",
name,
"--out-dir",
rebase_path("$target_out_dir/$main_target_name.rust"),
"--gen-dir",
rebase_path("$target_gen_dir/$main_target_name.rust"),
"--root-out-dir",
rebase_path(root_build_dir),
"--root-gen-dir",
rebase_path(root_gen_dir),
"--crate-root",
rebase_path(source_dir),
"--cargo",
rebase_path("$rust_base/bin/cargo"),
"--rustc",
rebase_path("$rust_base/bin/rustc"),
"--target-triple",
target_triple,
"--label",
target_label,
"--cmake-dir",
rebase_path("//buildtools/cmake/bin"),
"--vendor-directory",
rebase_path("//third_party/rust-crates/vendor"),
"--deps",
] + rust_deps
if (!is_debug) {
args += [
"--release",
]
}
if (is_fuchsia) {
wrapper_dir = get_label_info(clang_wrapper_target, "root_out_dir")
args += [
"--linker",
rebase_path("$wrapper_dir/$target_triple-cc"),
]
}
}
}