1 # Copyright 2020 The Pigweed Authors
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
7 # https://www.apache.org/licenses/LICENSE-2.0
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
15 import("//build_overrides/pigweed.gni")
17 import("$dir_pw_build/python_action.gni")
19 # Updates a tokenized string database in the source tree with artifacts from one
20 # or more targets. Other database files may also be used.
22 # The database file must exist. A CSV or binary database can be created with the
23 # pw/pw_tokenizer/database.py tool. An empty CSV database file can be also
24 # created as a starting point.
27 # database: if updating a database, path to an existing database in the source
28 # tree; optional if creating a database, but may provide an output
29 # directory path to override the default of
30 # "$target_gen_dir/$target_name.[csv/binary]"
31 # create: if specified, create a database instead of updating one; 'create'
32 # must be set to one of the supported database types: "csv" or "binary"
33 # targets: GN targets (executables or libraries) from which to add tokens;
34 # these targets are added to deps
35 # optional_targets: GN targets from which to add tokens, if the output files
36 # already exist; these targets are NOT added to deps
37 # input_databases: paths to other database files from which to add tokens
38 # deps: GN targets to build prior to generating the database; artifacts from
39 # these targets are NOT implicitly used for database generation
40 # domain: if provided, extract strings from tokenization domains matching this
43 template("pw_tokenizer_database") {
44 assert(defined(invoker.database) || defined(invoker.create),
45 "pw_tokenizer_database requires a 'database' variable, unless " +
46 "'create' is specified")
48 if (defined(invoker.create)) {
49 assert(invoker.create == "csv" || invoker.create == "binary",
50 "If provided, 'create' must be \"csv\" or \"binary\"")
51 _create = invoker.create
56 if (defined(invoker.database)) {
57 _database = invoker.database
59 _database = "$target_gen_dir/$target_name.${invoker.create}"
61 if (defined(invoker.targets)) {
62 _targets = invoker.targets
67 if (defined(invoker.optional_targets)) {
68 _optional_targets = invoker.optional_targets
70 _optional_targets = []
73 if (defined(invoker.input_databases)) {
74 _input_databases = invoker.input_databases
79 if (defined(invoker.domain)) {
80 _domain = "#" + invoker.domain
85 if (_targets == [] && _optional_targets == []) {
86 # If no targets were specified, the domain will not be used, which is OK.
87 not_needed([ "_domain" ])
90 # Restrict parallelism for updating this database file to one thread. This
91 # makes it safe to update it from multiple toolchains.
92 pool("$target_name._pool") {
96 pw_python_action(target_name) {
97 script = "$dir_pw_tokenizer/py/pw_tokenizer/database.py"
98 pool = ":$target_name._pool"
100 inputs = _input_databases
104 inputs += [ _database ]
113 outputs = [ _database ]
118 rebase_path(_database),
120 args += rebase_path(_input_databases)
122 foreach(target, _targets) {
123 args += [ "<TARGET_FILE($target)>$_domain" ]
126 # For optional targets, the build outputs may not exist, since they aren't
127 # added to deps. Use TARGET_FILE_IF_EXISTS to handle this.
128 foreach(target, _optional_targets) {
129 args += [ "<TARGET_FILE_IF_EXISTS($target)>$_domain" ]
134 if (defined(invoker.deps)) {