James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 1 | # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | # Defines a static library corresponding to the output of schema compiler tools |
| 6 | # over a set of extensions API schemas (IDL or JSON format.) The library target |
| 7 | # has implicit hard dependencies on all schema files listed by the invoker and |
| 8 | # is itself a hard dependency. |
| 9 | # |
| 10 | # Invocations of this template may use the following variables: |
| 11 | # |
| 12 | # sources [required] A list of schema files to be compiled. |
| 13 | # |
| 14 | # root_namespace [required] |
| 15 | # A Python string substituion pattern used to generate the C++ |
| 16 | # namespace for each API. Use %(namespace)s to replace with the API |
| 17 | # namespace, like "toplevel::%(namespace)s_api". |
| 18 | # |
| 19 | # schema_include_rules [optional] |
| 20 | # A list of paths to include when searching for referenced objects, |
| 21 | # with the namespace separated by a :. |
| 22 | # Example: |
| 23 | # [ '/foo/bar:Foo::Bar::%(namespace)s' ] |
| 24 | # |
| 25 | # schemas [optional, default = false] |
| 26 | # Boolean indicating if the schema files should be generated. |
| 27 | # |
| 28 | # bundle [optional, default = false] |
| 29 | # Boolean indicating if the schema bundle files should be generated. |
| 30 | # |
| 31 | # bundle_registration [optional, default = false] |
| 32 | # Boolean indicating if the API registration bundle files should be generated. |
| 33 | # |
| 34 | # impl_dir [required if bundle_registration = true, otherwise unused] |
| 35 | # The path containing C++ implementations of API functions. This path is |
| 36 | # used as the root path when looking for {schema}/{schema}_api.h headers |
| 37 | # when generating API registration bundles. Such headers, if found, are |
| 38 | # automatically included by the generated code. |
| 39 | # |
| 40 | # uncompiled_sources [optional, only used when bundle = true or |
| 41 | # bundle_registration = true] |
| 42 | # A list of schema files which should not be compiled, but which should still |
| 43 | # be processed for API bundle generation. |
| 44 | # |
| 45 | # deps [optional] |
| 46 | # If any deps are specified they will be inherited by the static library |
| 47 | # target. |
| 48 | # |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 49 | # generate_static_library [optional, defaults to false] |
| 50 | # Produces a static library instead of a source_set. |
| 51 | # |
| 52 | # The generated library target also inherits the visibility and output_name |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 53 | # of its invoker. |
| 54 | |
| 55 | template("json_schema_api") { |
| 56 | assert(defined(invoker.sources), |
| 57 | "\"sources\" must be defined for the $target_name template.") |
| 58 | assert(defined(invoker.root_namespace), |
| 59 | "\"root_namespace\" must be defined for the $target_name template.") |
| 60 | |
| 61 | schemas = defined(invoker.schemas) && invoker.schemas |
| 62 | bundle = defined(invoker.bundle) && invoker.bundle |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 63 | bundle_registration = |
| 64 | defined(invoker.bundle_registration) && invoker.bundle_registration |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 65 | |
| 66 | schema_include_rules = "" |
| 67 | if (defined(invoker.schema_include_rules)) { |
| 68 | schema_include_rules = invoker.schema_include_rules |
| 69 | } |
| 70 | |
| 71 | # Keep a copy of the target_name here since it will be trampled |
| 72 | # in nested targets. |
| 73 | target_visibility = [ ":$target_name" ] |
| 74 | |
| 75 | generated_config_name = target_name + "_generated_config" |
| 76 | config(generated_config_name) { |
Etienne Membrives | 18cca09 | 2015-01-14 00:46:18 +0100 | [diff] [blame] | 77 | include_dirs = [ root_gen_dir ] |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 78 | visibility = target_visibility |
| 79 | } |
| 80 | |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 81 | root_namespace = invoker.root_namespace |
| 82 | |
| 83 | compiler_root = "//tools/json_schema_compiler" |
| 84 | compiler_script = "$compiler_root/compiler.py" |
| 85 | compiler_sources = [ |
| 86 | "$compiler_root/cc_generator.py", |
| 87 | "$compiler_root/code.py", |
| 88 | "$compiler_root/compiler.py", |
| 89 | "$compiler_root/cpp_generator.py", |
| 90 | "$compiler_root/cpp_type_generator.py", |
| 91 | "$compiler_root/cpp_util.py", |
| 92 | "$compiler_root/h_generator.py", |
| 93 | "$compiler_root/idl_schema.py", |
| 94 | "$compiler_root/model.py", |
| 95 | "$compiler_root/util_cc_helper.py", |
| 96 | ] |
| 97 | |
| 98 | if (schemas) { |
| 99 | schema_generator_name = target_name + "_schema_generator" |
| 100 | action_foreach(schema_generator_name) { |
| 101 | script = compiler_script |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 102 | sources = invoker.sources |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 103 | inputs = compiler_sources |
| 104 | outputs = [ |
| 105 | "$target_gen_dir/{{source_name_part}}.cc", |
| 106 | "$target_gen_dir/{{source_name_part}}.h", |
| 107 | ] |
| 108 | args = [ |
| 109 | "{{source}}", |
| 110 | "--root=" + rebase_path("//", root_build_dir), |
| 111 | "--destdir=" + rebase_path(root_gen_dir, root_build_dir), |
| 112 | "--namespace=$root_namespace", |
| 113 | "--generator=cpp", |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 114 | "--include-rules=$schema_include_rules", |
| 115 | ] |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 116 | |
| 117 | if (defined(invoker.visibility)) { |
| 118 | # If visibility is restricted, add our own target to it. |
Benjamin Lerman | cdfc88d | 2015-02-03 14:35:12 +0100 | [diff] [blame] | 119 | visibility = invoker.visibility + target_visibility |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (bundle) { |
| 125 | uncompiled_sources = [] |
| 126 | if (defined(invoker.uncompiled_sources)) { |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 127 | uncompiled_sources = invoker.uncompiled_sources |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | bundle_generator_schema_name = target_name + "_bundle_generator_schema" |
| 131 | action(bundle_generator_schema_name) { |
| 132 | script = compiler_script |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 133 | inputs = compiler_sources + invoker.sources + uncompiled_sources |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 134 | outputs = [ |
| 135 | "$target_gen_dir/generated_schemas.cc", |
| 136 | "$target_gen_dir/generated_schemas.h", |
| 137 | ] |
| 138 | args = [ |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 139 | "--root=" + rebase_path("//", root_build_dir), |
| 140 | "--destdir=" + rebase_path(root_gen_dir, root_build_dir), |
| 141 | "--namespace=$root_namespace", |
| 142 | "--generator=cpp-bundle-schema", |
| 143 | "--include-rules=$schema_include_rules", |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 144 | ] + rebase_path(invoker.sources, root_build_dir) + |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 145 | rebase_path(uncompiled_sources, root_build_dir) |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | if (bundle_registration) { |
| 150 | uncompiled_sources = [] |
| 151 | if (defined(invoker.uncompiled_sources)) { |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 152 | uncompiled_sources = invoker.uncompiled_sources |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | assert(defined(invoker.impl_dir), |
| 156 | "\"impl_dir\" must be defined for the $target_name template.") |
Viet-Trung Luu | 235cf3d | 2015-06-11 10:01:25 -0700 | [diff] [blame] | 157 | |
| 158 | # Child directory inside the generated file tree. |
| 159 | gen_child_dir = rebase_path(invoker.impl_dir, "//") |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 160 | |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 161 | bundle_generator_registration_name = |
| 162 | target_name + "_bundle_generator_registration" |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 163 | action(bundle_generator_registration_name) { |
| 164 | script = compiler_script |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 165 | inputs = compiler_sources + invoker.sources + uncompiled_sources |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 166 | outputs = [ |
Viet-Trung Luu | 235cf3d | 2015-06-11 10:01:25 -0700 | [diff] [blame] | 167 | "$root_gen_dir/$gen_child_dir/generated_api_registration.cc", |
| 168 | "$root_gen_dir/$gen_child_dir/generated_api_registration.h", |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 169 | ] |
| 170 | args = [ |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 171 | "--root=" + rebase_path("//", root_build_dir), |
| 172 | "--destdir=" + rebase_path(root_gen_dir, root_build_dir), |
| 173 | "--namespace=$root_namespace", |
| 174 | "--generator=cpp-bundle-registration", |
Viet-Trung Luu | 235cf3d | 2015-06-11 10:01:25 -0700 | [diff] [blame] | 175 | "--impl-dir=$gen_child_dir", |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 176 | "--include-rules=$schema_include_rules", |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 177 | ] + rebase_path(invoker.sources, root_build_dir) + |
James Robinson | 6a64b81 | 2014-12-03 13:38:42 -0800 | [diff] [blame] | 178 | rebase_path(uncompiled_sources, root_build_dir) |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 182 | # Compute the contents of the library/source set. |
| 183 | lib_sources = invoker.sources |
| 184 | lib_deps = [] |
| 185 | lib_public_deps = [] |
| 186 | lib_extra_configs = [] |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 187 | |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 188 | if (schemas) { |
| 189 | lib_sources += get_target_outputs(":$schema_generator_name") |
| 190 | lib_public_deps += [ ":$schema_generator_name" ] |
| 191 | lib_deps += [ "//tools/json_schema_compiler:generated_api_util" ] |
| 192 | lib_extra_configs += [ "//build/config/compiler:no_size_t_to_int_warning" ] |
| 193 | } |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 194 | |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 195 | if (bundle) { |
| 196 | lib_sources += get_target_outputs(":$bundle_generator_schema_name") |
| 197 | lib_deps += [ ":$bundle_generator_schema_name" ] |
| 198 | } |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 199 | |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 200 | if (bundle_registration) { |
| 201 | lib_sources += get_target_outputs(":$bundle_generator_registration_name") |
| 202 | lib_deps += [ ":$bundle_generator_registration_name" ] |
| 203 | } |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 204 | |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 205 | if (defined(invoker.deps)) { |
| 206 | lib_deps += invoker.deps |
| 207 | } |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 208 | |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 209 | # Generate either a static library or a source set. |
| 210 | if (defined(invoker.generate_static_library) && |
| 211 | invoker.generate_static_library) { |
| 212 | static_library(target_name) { |
| 213 | sources = lib_sources |
| 214 | deps = lib_deps |
| 215 | public_deps = lib_public_deps |
| 216 | configs += lib_extra_configs |
| 217 | public_configs = [ ":$generated_config_name" ] |
| 218 | |
| 219 | if (defined(invoker.visibility)) { |
| 220 | visibility = invoker.visibility |
| 221 | } |
| 222 | if (defined(invoker.output_name)) { |
| 223 | output_name = invoker.output_name |
| 224 | } |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 225 | } |
James Robinson | f19b102 | 2015-05-05 18:12:15 -0700 | [diff] [blame] | 226 | } else { |
| 227 | source_set(target_name) { |
| 228 | sources = lib_sources |
| 229 | deps = lib_deps |
| 230 | public_deps = lib_public_deps |
| 231 | configs += lib_extra_configs |
| 232 | public_configs = [ ":$generated_config_name" ] |
| 233 | |
| 234 | if (defined(invoker.visibility)) { |
| 235 | visibility = invoker.visibility |
| 236 | } |
| 237 | if (defined(invoker.output_name)) { |
| 238 | output_name = invoker.output_name |
| 239 | } |
James Robinson | 6165015 | 2014-10-26 23:24:55 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | } |