Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_tokenizer / database.gni
1 # Copyright 2020 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
15 import("//build_overrides/pigweed.gni")
16
17 import("$dir_pw_build/python_action.gni")
18
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.
21 #
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.
25 #
26 # Args:
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
41 #       regular expression
42 #
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")
47
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
52   } else {
53     _create = ""
54   }
55
56   if (defined(invoker.database)) {
57     _database = invoker.database
58   } else {
59     _database = "$target_gen_dir/$target_name.${invoker.create}"
60   }
61   if (defined(invoker.targets)) {
62     _targets = invoker.targets
63   } else {
64     _targets = []
65   }
66
67   if (defined(invoker.optional_targets)) {
68     _optional_targets = invoker.optional_targets
69   } else {
70     _optional_targets = []
71   }
72
73   if (defined(invoker.input_databases)) {
74     _input_databases = invoker.input_databases
75   } else {
76     _input_databases = []
77   }
78
79   if (defined(invoker.domain)) {
80     _domain = "#" + invoker.domain
81   } else {
82     _domain = ""
83   }
84
85   if (_targets == [] && _optional_targets == []) {
86     # If no targets were specified, the domain will not be used, which is OK.
87     not_needed([ "_domain" ])
88   }
89
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") {
93     depth = 1
94   }
95
96   pw_python_action(target_name) {
97     script = "$dir_pw_tokenizer/py/pw_tokenizer/database.py"
98     pool = ":$target_name._pool"
99
100     inputs = _input_databases
101
102     if (_create == "") {
103       args = [ "add" ]
104       inputs += [ _database ]
105       stamp = true
106     } else {
107       args = [
108         "create",
109         "--force",
110         "--type",
111         _create,
112       ]
113       outputs = [ _database ]
114     }
115
116     args += [
117       "--database",
118       rebase_path(_database),
119     ]
120     args += rebase_path(_input_databases)
121
122     foreach(target, _targets) {
123       args += [ "<TARGET_FILE($target)>$_domain" ]
124     }
125
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" ]
130     }
131
132     deps = _targets
133
134     if (defined(invoker.deps)) {
135       deps += invoker.deps
136     }
137   }
138 }