summaryrefslogtreecommitdiffstats
path: root/extensions/generated_extensions_api.gni
blob: 5360711eecec1559ddca9136518ccbd5beed649b (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# Defines a static library corresponding to the output of schema compiler tools
# over a set of extensions API schemas (IDL or JSON format.) The library target
# has implicit hard dependencies on all schema files listed by the invoker and
# is itself a hard dependency.
#
# Invocations of this template may use the following variables:
#
# sources [required] A list of schema files to be compiled.
#
# root_namespace [required]
#     A Python string substituion pattern used to generate the C++
#     namespace for each API. Use %(namespace)s to replace with the API
#     namespace, like "toplevel::%(namespace)s_api".
#
# bundle [optional, default = false]
#   Boolean indicating if the schema files should be bundled or not.
#
# impl_dir [required if bundle = true, otherwise unused]
#   The path containing C++ implementations of API functions. This path is
#   used as the root path when looking for {schema}/{schema}_api.h headers
#   during the API bundle generation phase. Such headers, if found, are
#   automatically included by the generated code.
#
# uncompiled_sources [optional, only used when bundle = true]
#   A list of schema files which should not be compiled, but which should still
#   be processed for API bundle generation.
#
# deps [optional]
#   If any deps are specified they will be inherited by the static library
#   target.
#
# The static libarary target also inherits the visibility and output_name
# of its invoker.

template("generated_extensions_api") {
  assert(defined(invoker.sources),
         "\"sources\" must be defined for the $target_name template.")
  assert(defined(invoker.root_namespace),
         "\"root_namespace\" must be defined for the $target_name template.")

  bundle = defined(invoker.bundle) && invoker.bundle

  # Keep a copy of the target_name here since it will be trampled
  # in nested targets.
  target_visibility = ":$target_name"

  generated_config_name = target_name + "_generated_config"
  config(generated_config_name) {
    include_dirs = [ target_gen_dir ]
    visibility = target_visibility
  }

  schemas = invoker.sources
  root_namespace = invoker.root_namespace

  compiler_root = "//tools/json_schema_compiler"
  compiler_script = "$compiler_root/compiler.py"
  compiler_sources = [
    "$compiler_root/cc_generator.py",
    "$compiler_root/code.py",
    "$compiler_root/compiler.py",
    "$compiler_root/cpp_generator.py",
    "$compiler_root/cpp_type_generator.py",
    "$compiler_root/cpp_util.py",
    "$compiler_root/h_generator.py",
    "$compiler_root/idl_schema.py",
    "$compiler_root/model.py",
    "$compiler_root/util_cc_helper.py",
  ]

  schema_generator_name = target_name + "_schema_generator"
  action_foreach(schema_generator_name) {
    script = compiler_script
    inputs = compiler_sources
    sources = schemas
    outputs = [
      "$target_gen_dir/{{source_name_part}}.cc",
      "$target_gen_dir/{{source_name_part}}.h",
    ]
    args = [
      "{{source}}",
      "--root=" + rebase_path("//", root_build_dir),
      "--destdir=" + rebase_path(root_gen_dir, root_build_dir),
      "--namespace=$root_namespace",
      "--generator=cpp" ]
    visibility = target_visibility
  }

  if (bundle) {
    assert(defined(invoker.impl_dir),
           "\"impl_dir\" must be defined for the $target_name template.")
    impl_dir = invoker.impl_dir

    uncompiled_schemas = []
    if (defined(invoker.uncompiled_sources)) {
      uncompiled_schemas = invoker.uncompiled_sources
    }

    bundle_generator_name = target_name + "_bundle_generator"
    action(bundle_generator_name) {
      script = compiler_script
      inputs = compiler_sources + schemas + uncompiled_schemas
      outputs = [
        "$target_gen_dir/generated_api.cc",
        "$target_gen_dir/generated_api.h",
        "$target_gen_dir/generated_schemas.cc",
        "$target_gen_dir/generated_schemas.h",
      ]
      args = [
        "--root=" + rebase_path("//", root_build_dir),
        "--destdir=" + rebase_path(root_gen_dir, root_build_dir),
        "--namespace=$root_namespace",
        "--generator=cpp-bundle",
        "--impl-dir=" + rebase_path(impl_dir, "//"),
      ] +
        rebase_path(schemas, root_build_dir) +
        rebase_path(uncompiled_schemas, root_build_dir)
    }
  }

  source_set(target_name) {
    sources = get_target_outputs(":$schema_generator_name")

    deps = [
      ":$schema_generator_name",
      "//tools/json_schema_compiler:generated_api_util",
    ]

    if (bundle) {
      sources += get_target_outputs(":$bundle_generator_name")
      deps += [ ":$bundle_generator_name" ]
    }

    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    direct_dependent_configs = [ ":$generated_config_name" ]

    if (defined(invoker.visibility)) {
      visibility = invoker.visibility
    }
    if (defined(invoker.output_name)) {
      output_name = invoker.output_name
    }
  }
}