1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
# Copyright 2023 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# There are two distinct variants of tflite:
# . One that depends upon //base.
# . One that does not depend upon //base.
# Dealing with this necessitates two distinct targets, one containing the
# dependency on base and the other containing no dependency on base. The
# templates in this file help ease this pain by generating the two targets.
# The templates are designed as drop in replacements for existing gn targets.
# The primary difference is any dependency on a target that needs to be
# treated specially should be moved to either conditional_deps, or
# conditional_public_deps. For example, consider the following:
#
# source_set("tflite_internal") {
# sources = ["foo"]
# deps = [ "//base" ]
# }
# source_set("tflite_internal_standalone") {
# sources = ["foo"]
# }
#
# source_set("tflite_internal2") {
# sources = ["foo2"]
# deps = [ ":tflite_internal" ]
# }
# source_set("tflite_internal2_standalone") {
# sources = ["foo2"]
# deps = [ ":tflite_internal_standalone" ]
# }
#
# tflite_internal2 and tflite_internal2_standalone can be replaced by:
# tflite_source_set_("tflite_internal2") {
# sources = ["foo2"]
# conditional_deps = [ ":tflite_internal" ]
# }
# Set of variables that are forwarded to the actual target.
minimal_vars_to_forward_to_target = [
"cflags",
"configs",
"defines",
"deps",
"include_dirs",
"public_configs",
"public_deps",
"sources",
"visibility",
]
# Shared template used by all other templates.
template("tflite_target_gen") {
assert(defined(invoker.target_type),
"Must declare a target_type of either static_library or source_set")
# Loop through twice, once for the target depending on base, and again for the
# target that does not depend on base.
foreach(type,
[
"",
"_standalone",
]) {
target(invoker.target_type, target_name + type) {
forward_variables_from(invoker, minimal_vars_to_forward_to_target)
if (defined(invoker.conditional_deps)) {
_adjusted_deps = []
foreach(dep, invoker.conditional_deps) {
_adjusted_name = dep + type
_adjusted_deps += [ _adjusted_name ]
}
if (defined(deps)) {
deps += _adjusted_deps
} else {
deps = _adjusted_deps
}
}
if (defined(invoker.conditional_public_deps)) {
_adjusted_deps = []
foreach(dep, invoker.conditional_public_deps) {
_adjusted_name = dep + type
_adjusted_deps += [ _adjusted_name ]
}
if (defined(public_deps)) {
public_deps += _adjusted_deps
} else {
public_deps = _adjusted_deps
}
}
if (defined(invoker.configs_to_remove)) {
configs -= invoker.configs_to_remove
}
if (defined(invoker.configs_to_add)) {
configs += invoker.configs_to_add
}
if (type == "_standalone") {
# Standalone targets should not depend upon base.
assert_no_deps = [ "//base" ]
if (defined(invoker.public_deps_for_standalone)) {
public_deps += invoker.public_deps_for_standalone
}
} else {
not_needed(invoker, [ "public_deps_for_standalone" ])
}
}
}
}
# Forwards variables used by the nested template.
vars_to_forward = minimal_vars_to_forward_to_target + [
"conditional_deps",
"conditional_public_deps",
"configs_to_add",
"configs_to_remove",
"public_deps_for_standalone",
]
template("tflite_static_library") {
tflite_target_gen(target_name) {
target_type = "static_library"
forward_variables_from(invoker, vars_to_forward)
}
}
template("tflite_source_set") {
tflite_target_gen(target_name) {
target_type = "source_set"
forward_variables_from(invoker, vars_to_forward)
}
}
template("tflite_group") {
tflite_target_gen(target_name) {
target_type = "group"
forward_variables_from(invoker, vars_to_forward)
}
}
|