blob: 0cad01260e2923d1524cb49f96d5e78d31992882 [file] [log] [blame]
# Copyright 2020 The Pigweed Authors
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy of
# the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.
# gn-format disable
import("//build_overrides/pigweed.gni")
import("$dir_pw_build/python_script.gni")
# Updates a tokenized string database in the source tree with artifacts from one
# or more targets. Other database files may also be used.
#
# The database file must exist. A CSV or binary database can be created with the
# pw/pw_tokenizer/database.py tool. An empty CSV database file can be also
# created as a starting point.
#
# Args:
# database: source tree path to database file to update; must exist beforehand
# targets: GN targets (executables or libraries) from which to add tokens;
# these targets are added to deps
# optional_targets: GN targets from which to add tokens, if the output files
# already exist; these targets are NOT added to deps
# input_databases: paths to other database files from which to add tokens
# deps: GN targets to build prior to generating the database; artifacts from
# these targets are NOT implicitly used for database generation
#
template("pw_tokenizer_database") {
assert(defined(invoker.database),
"pw_tokenizer_database requires a 'database' variable")
if (defined(invoker.targets)) {
_targets = invoker.targets
} else {
_targets = []
}
if (defined(invoker.optional_targets)) {
_optional_targets = invoker.optional_targets
} else {
_optional_targets = []
}
if (defined(invoker.input_databases)) {
_input_databases = invoker.input_databases
} else {
_input_databases = []
}
assert(
_targets != [] || _optional_targets != [] || _input_databases != [],
"No 'targets', 'optional_targets', or 'input_databases' were set for " +
"pw_tokenizer_database! At least one target or database must be " +
"provided as an input.")
pw_python_script(target_name) {
script = "$dir_pw_tokenizer/py/pw_tokenizer/database.py"
args = [
"add",
"--database",
rebase_path(invoker.database),
]
args += rebase_path(_input_databases)
foreach(target, _targets) {
args += [ "<TARGET_FILE($target)>" ]
}
# For optional targets, the build outputs may not exist, since they aren't
# added to deps. Use TARGET_FILE_IF_EXISTS to handle this.
foreach(target, _optional_targets) {
args += [ "<TARGET_FILE_IF_EXISTS($target)>" ]
}
deps = _targets
if (defined(invoker.deps)) {
deps += invoker.deps
}
inputs = [ invoker.database ] + _input_databases
# Since the output file is in the source tree, create a corresponding stamp
# file in the output directory that is independent of the toolchain. That
# way, trying to update the database from multiple toolchains is an error.
stamp = "$root_build_dir/" + rebase_path(invoker.database, "//") + ".update"
}
}