xref: /aosp_15_r20/external/pigweed/pw_build/rust_library.gni (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1# Copyright 2023 The Pigweed Authors
2#
3# Licensed under the Apache License, Version 2.0 (the "License"); you may not
4# use this file except in compliance with the License. You may obtain a copy of
5# the License at
6#
7#     https://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12# License for the specific language governing permissions and limitations under
13# the License.
14
15import("//build_overrides/pigweed.gni")
16
17import("$dir_pw_build/gn_internal/build_target.gni")
18import("$dir_pw_rust/rust.gni")
19
20# Note: In general, prefer to import target_types.gni rather than this file.
21#
22# This template wraps a configurable target type specified by the current
23# toolchain to be used for all pw_rust_library targets. A wrapper for GN's
24# built-in rust_library target, it is analogous to pw_static_library with
25# additions to support rust specific parameters such as rust edition and cargo
26# config features.
27#
28# For more information on the features provided by this template, see the full
29# docs at https://pigweed.dev/pw_build/?highlight=pw_rust_library
30template("pw_rust_library") {
31  pw_internal_build_target(target_name) {
32    forward_variables_from(invoker, "*")
33
34    if (!defined(crate_name)) {
35      crate_name = target_name
36    }
37
38    _edition = "2021"
39    if (defined(edition)) {
40      _edition = edition
41    }
42    assert(_edition == "2015" || _edition == "2018" || _edition == "2021",
43           "edition ${_edition} is not supported")
44
45    if (!defined(configs)) {
46      configs = []
47    }
48    configs += [ "$dir_pw_build:rust_edition_${_edition}" ]
49
50    if (!defined(rustflags)) {
51      rustflags = []
52    }
53    if (defined(features)) {
54      foreach(i, features) {
55        rustflags += [ "--cfg=feature=\"${i}\"" ]
56      }
57    }
58
59    if (!defined(underlying_target_type)) {
60      underlying_target_type = "rust_library"
61    }
62
63    crate_name = string_replace(crate_name, "-", "_")
64    output_dir = "${target_out_dir}/lib"
65    add_global_link_deps = false
66  }
67}
68