blob: 1febf27b4d40065abef398f2d7b067f97bcb51b3 [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)) {
foreach(dep, invoker.deps) {
rust_deps += [ get_label_info(dep, "label_no_toolchain") ]
}
}
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"
}
}
rust_base = "//buildtools/${host_os}-${host_cpu}/rust"
if (is_debug) {
build_type = "debug"
} else {
build_type = "release"
}
group_deps = []
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}_lib"
group_deps += [ ":$build_target_name" ]
}
main_target_name = target_name
if (type == "bin") {
copy("${target_name}_bin") {
sources = [
output_file,
]
outputs = [
"$root_out_dir/$name",
]
deps = [
":$build_target_name",
]
}
group_deps += [ ":${target_name}_bin" ]
}
if (defined(invoker.with_tests) && invoker.with_tests) {
copy("${target_name}_test") {
base_out_dir = "$target_out_dir/$main_target_name.rust"
sources = [
"$base_out_dir/$name-$type-test",
]
outputs = [
"$root_out_dir/$name-$type-test",
]
deps = [
":$build_target_name",
]
}
group_deps += [ ":${target_name}_test" ]
}
action(build_target_name) {
script = "//build/rust/build_target.py"
depfile = output_depfile
base_out_dir = "$target_out_dir/$main_target_name.rust"
base_gen_dir = "$target_gen_dir/$main_target_name.rust"
inputs = [
"${source_dir}/Cargo.toml",
"//build/rust/local_crates.py",
# This will trigger rebuilds whenever third-party crates are updated.
"//third_party/rust-crates/.vendor-update.stamp",
]
outputs = [
output_file,
]
if (type == "lib") {
outputs += [ "$base_gen_dir/$main_target_name.info.toml" ]
}
deps = rust_deps
if (defined(invoker.non_rust_deps)) {
deps += invoker.non_rust_deps
}
if (is_fuchsia) {
clang_wrapper_target =
"//garnet/public/rust/crates/fuchsia-zircon/tools:clang_wrapper($host_toolchain)"
deps += [ clang_wrapper_target ]
}
args = [
"--type",
type,
"--name",
name,
"--out-dir",
rebase_path(base_out_dir),
"--gen-dir",
rebase_path(base_gen_dir),
"--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
args += [
"--shared-libs-root",
rebase_path(
get_label_info("//default($shlib_toolchain)", "root_out_dir")),
]
if (defined(invoker.with_tests) && invoker.with_tests) {
args += [ "--with-tests" ]
outputs += [ "$base_out_dir/$name-$type-test" ]
}
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"),
]
}
}
group(target_name) {
public_deps = group_deps
}
}