summaryrefslogtreecommitdiffstats
path: root/third_party/mojo/src/mojo/public/tools/bindings/mojom.gni
blob: 3e0ca4fe4b2432e03892ea4a1d8fb27b9aa6ac82 (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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# 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.

import("../../mojo_sdk.gni")

# Generate C++ and JavaScript source files from mojom files. The output files
# will go under the generated file directory tree with the same path as each
# input file.
#
# If a mojom target is intended for use in a client repo where the location of
# the Mojo SDK will be different than its location in the Mojo repo,
# dependencies on the SDK should be specified relative to the parent directory
# of the Mojo public SDK in |mojo_sdk_deps| rather than via absolute paths in
# |deps|.
#
# Parameters:
#
#   sources (required)
#       List of source .mojom files to compile.
#
#   deps (optional)
#       Note: this can contain only other mojom targets.
#
#   mojo_sdk_deps (optional)
#       List of deps specified relative to the parent directory of the Mojo
#       public SDK. These deps will be added as ordinary deps rebased to the
#       current directory.
#
#   public_deps (optional)
#       Note: this can contain only other mojom targets.
#
#   mojo_sdk_public_deps (optional)
#       List of public deps specified relative to the parent directory of the
#       Mojo public SDK. These deps will be added as ordinary public deps
#       rebased to the current directory.
#
#   import_dirs (optional)
#       List of import directories that will get added when processing sources.
#
#   testonly (optional)
#
#   visibility (optional)
template("mojom") {
  assert(defined(invoker.sources),
         "\"sources\" must be defined for the $target_name template.")

  generator_root = rebase_path("mojo/public/tools/bindings", ".", mojo_root)
  generator_script = "$generator_root/mojom_bindings_generator.py"
  generator_sources = [
    generator_script,
    "$generator_root/generators/cpp_templates/enum_declaration.tmpl",
    "$generator_root/generators/cpp_templates/interface_declaration.tmpl",
    "$generator_root/generators/cpp_templates/interface_definition.tmpl",
    "$generator_root/generators/cpp_templates/interface_macros.tmpl",
    "$generator_root/generators/cpp_templates/interface_proxy_declaration.tmpl",
    "$generator_root/generators/cpp_templates/interface_request_validator_declaration.tmpl",
    "$generator_root/generators/cpp_templates/interface_response_validator_declaration.tmpl",
    "$generator_root/generators/cpp_templates/interface_stub_declaration.tmpl",
    "$generator_root/generators/cpp_templates/module.cc.tmpl",
    "$generator_root/generators/cpp_templates/module.h.tmpl",
    "$generator_root/generators/cpp_templates/module-internal.h.tmpl",
    "$generator_root/generators/cpp_templates/params_definition.tmpl",
    "$generator_root/generators/cpp_templates/struct_declaration.tmpl",
    "$generator_root/generators/cpp_templates/struct_definition.tmpl",
    "$generator_root/generators/cpp_templates/struct_serialization_declaration.tmpl",
    "$generator_root/generators/cpp_templates/struct_serialization_definition.tmpl",
    "$generator_root/generators/cpp_templates/struct_macros.tmpl",
    "$generator_root/generators/cpp_templates/wrapper_class_declaration.tmpl",
    "$generator_root/generators/cpp_templates/wrapper_class_definition.tmpl",
    "$generator_root/generators/dart_templates/enum_definition.tmpl",
    "$generator_root/generators/dart_templates/interface_definition.tmpl",
    "$generator_root/generators/dart_templates/module.lib.tmpl",
    "$generator_root/generators/dart_templates/module_definition.tmpl",
    "$generator_root/generators/dart_templates/struct_definition.tmpl",
    "$generator_root/generators/go_templates/source.tmpl",
    "$generator_root/generators/java_templates/constant_definition.tmpl",
    "$generator_root/generators/java_templates/constants.java.tmpl",
    "$generator_root/generators/java_templates/enum.java.tmpl",
    "$generator_root/generators/java_templates/enum_definition.tmpl",
    "$generator_root/generators/java_templates/header.java.tmpl",
    "$generator_root/generators/java_templates/interface.java.tmpl",
    "$generator_root/generators/java_templates/interface_definition.tmpl",
    "$generator_root/generators/java_templates/interface_internal.java.tmpl",
    "$generator_root/generators/java_templates/struct.java.tmpl",
    "$generator_root/generators/java_templates/struct_definition.tmpl",
    "$generator_root/generators/js_templates/enum_definition.tmpl",
    "$generator_root/generators/js_templates/interface_definition.tmpl",
    "$generator_root/generators/js_templates/module.amd.tmpl",
    "$generator_root/generators/js_templates/module.sky.tmpl",
    "$generator_root/generators/js_templates/module_definition.tmpl",
    "$generator_root/generators/js_templates/struct_definition.tmpl",
    "$generator_root/generators/python_templates/module_macros.tmpl",
    "$generator_root/generators/python_templates/module.py.tmpl",
    "$generator_root/generators/mojom_cpp_generator.py",
    "$generator_root/generators/mojom_dart_generator.py",
    "$generator_root/generators/mojom_go_generator.py",
    "$generator_root/generators/mojom_js_generator.py",
    "$generator_root/generators/mojom_java_generator.py",
    "$generator_root/generators/mojom_python_generator.py",
    "$generator_root/pylib/mojom/__init__.py",
    "$generator_root/pylib/mojom/error.py",
    "$generator_root/pylib/mojom/generate/__init__.py",
    "$generator_root/pylib/mojom/generate/data.py",
    "$generator_root/pylib/mojom/generate/generator.py",
    "$generator_root/pylib/mojom/generate/module.py",
    "$generator_root/pylib/mojom/generate/pack.py",
    "$generator_root/pylib/mojom/generate/template_expander.py",
    "$generator_root/pylib/mojom/parse/__init__.py",
    "$generator_root/pylib/mojom/parse/ast.py",
    "$generator_root/pylib/mojom/parse/lexer.py",
    "$generator_root/pylib/mojom/parse/parser.py",
    "$generator_root/pylib/mojom/parse/translate.py",
  ]
  generator_cpp_outputs = [
    "{{source_gen_dir}}/{{source_name_part}}.mojom.cc",
    "{{source_gen_dir}}/{{source_name_part}}.mojom.h",
    "{{source_gen_dir}}/{{source_name_part}}.mojom-internal.h",
  ]
  generator_dart_outputs =
      [ "{{source_gen_dir}}/{{source_name_part}}.mojom.dart" ]
  generator_dart_zip_output = "$target_out_dir/$target_name.dartzip"
  generator_java_outputs =
      [ "{{source_gen_dir}}/{{source_name_part}}.mojom.srcjar" ]
  generator_js_outputs = [ "{{source_gen_dir}}/{{source_name_part}}.mojom.js" ]
  generator_python_outputs =
      [ "{{source_gen_dir}}/{{source_name_part}}_mojom.py" ]
  generator_python_zip_output = "$target_out_dir/$target_name.pyzip"

  rebased_mojo_sdk_public_deps = []
  if (defined(invoker.mojo_sdk_public_deps)) {
    foreach(sdk_dep, invoker.mojo_sdk_public_deps) {
      # Check that the SDK dep was not mistakenly given as an absolute path.
      assert(get_path_info(sdk_dep, "abspath") != sdk_dep)
      rebased_mojo_sdk_public_deps += [ rebase_path(sdk_dep, ".", mojo_root) ]
    }
  }

  rebased_mojo_sdk_deps = []
  if (defined(invoker.mojo_sdk_deps)) {
    foreach(sdk_dep, invoker.mojo_sdk_deps) {
      # Check that the SDK dep was not mistakenly given as an absolute path.
      assert(get_path_info(sdk_dep, "abspath") != sdk_dep)
      rebased_mojo_sdk_deps += [ rebase_path(sdk_dep, ".", mojo_root) ]
    }
  }

  if (defined(invoker.visibility)) {
    # Need to save this because the the target_name is overwritten inside the
    # action to be that of the action itself. Only define this in the case the
    # var is used to avoid unused var error.
    target_visibility = [ ":$target_name" ]
  }

  generator_target_name = target_name + "__generator"
  action_foreach(generator_target_name) {
    if (defined(invoker.visibility)) {
      visibility = target_visibility + invoker.visibility
    }
    script = generator_script
    inputs = generator_sources
    sources = invoker.sources
    outputs =
        generator_cpp_outputs + generator_dart_outputs +
        generator_java_outputs + generator_js_outputs + generator_python_outputs
    args = [
      "{{source}}",
      "--use_bundled_pylibs",
      "-d",
      rebase_path("//", root_build_dir),
      "-I",
      rebase_path("//", root_build_dir),
      "-I",
      rebase_path(mojo_root, root_build_dir),
      "-o",
      rebase_path(root_gen_dir),
    ]

    if (defined(invoker.import_dirs)) {
      foreach(import_dir, invoker.import_dirs) {
        args += [
          "-I",
          rebase_path(import_dir, root_build_dir),
        ]
      }
    }
  }

  source_set(target_name) {
    if (defined(invoker.visibility)) {
      visibility = invoker.visibility
    }
    if (defined(invoker.testonly)) {
      testonly = invoker.testonly
    }
    sources = process_file_template(invoker.sources, generator_cpp_outputs)
    data = process_file_template(invoker.sources, generator_js_outputs)

    public_configs =
        rebase_path([ "mojo/public/build/config:mojo_sdk" ], ".", mojo_root)

    public_deps = rebase_path([ "mojo/public/cpp/bindings" ], ".", mojo_root)
    public_deps += rebased_mojo_sdk_public_deps
    if (defined(invoker.public_deps)) {
      public_deps += invoker.public_deps
    }

    deps = [
      ":$generator_target_name",
    ]
    deps += rebased_mojo_sdk_deps
    if (defined(invoker.deps)) {
      deps += invoker.deps
    }
    data_deps = [
      ":${target_name}_python",
      ":${target_name}_dart",
    ]
    if (defined(invoker.mojo_sdk_deps)) {
      foreach(sdk_dep, invoker.mojo_sdk_deps) {
        # Check that the SDK dep was not mistakenly given as an absolute path.
        assert(get_path_info(sdk_dep, "abspath") != sdk_dep)
        deps += [ rebase_path(sdk_dep, ".", mojo_root) ]
      }
    }
  }

  all_deps = rebased_mojo_sdk_deps + rebased_mojo_sdk_public_deps
  if (defined(invoker.deps)) {
    all_deps += invoker.deps
  }
  if (defined(invoker.public_deps)) {
    all_deps += invoker.public_deps
  }

  group("${target_name}__is_mojom") {
  }

  # Explicitly ensure that all dependencies (invoker.deps and
  # invoker.public_deps) are mojom targets themselves.
  group("${target_name}__check_deps_are_all_mojom") {
    deps = []
    foreach(d, all_deps) {
      name = get_label_info(d, "label_no_toolchain")
      toolchain = get_label_info(d, "toolchain")
      deps += [ "${name}__is_mojom(${toolchain})" ]
    }
  }

  action("${target_name}_python") {
    script = rebase_path("mojo/public/tools/gn/zip.py", ".", mojo_root)

    inputs = process_file_template(invoker.sources, generator_python_outputs)

    deps = []
    zip_inputs = []

    foreach(d, all_deps) {
      # Resolve the name, so that a target //mojo/something becomes
      # //mojo/something:something and we can append "_python" to get the python
      # dependency name.
      full_name = get_label_info(d, "label_no_toolchain")
      dep_name = get_label_info(d, "name")
      dep_target_out_dir = get_label_info(d, "target_out_dir")
      deps += [ "${full_name}_python" ]
      zip_inputs += [ "$dep_target_out_dir/$dep_name.pyzip" ]
    }

    output = generator_python_zip_output
    outputs = [
      output,
    ]

    rebase_base_dir = rebase_path(target_gen_dir, root_build_dir)
    rebase_inputs = rebase_path(inputs, root_build_dir)
    rebase_zip_inputs = rebase_path(zip_inputs, root_build_dir)
    rebase_output = rebase_path(output, root_build_dir)
    args = [
      "--base-dir=$rebase_base_dir",
      "--inputs=$rebase_inputs",
      "--zip-inputs=$rebase_zip_inputs",
      "--output=$rebase_output",
    ]
  }

  action("${target_name}_dart") {
    script = rebase_path("mojo/public/tools/gn/zip.py", ".", mojo_root)

    inputs = process_file_template(invoker.sources, generator_dart_outputs)

    deps = []
    zip_inputs = []

    foreach(d, all_deps) {
      # Resolve the name, so that a target //mojo/something becomes
      # //mojo/something:something and we can append "_dart" to get the dart
      # dependency name.
      full_name = get_label_info(d, "label_no_toolchain")
      dep_name = get_label_info(d, "name")
      dep_target_out_dir = get_label_info(d, "target_out_dir")
      deps += [ "${full_name}_dart" ]
      zip_inputs += [ "$dep_target_out_dir/$dep_name.dartzip" ]
    }

    output = generator_dart_zip_output
    outputs = [
      output,
    ]

    rebase_base_dir = rebase_path("$root_build_dir/gen/", root_build_dir)
    rebase_inputs = rebase_path(inputs, root_build_dir)
    rebase_zip_inputs = rebase_path(zip_inputs, root_build_dir)
    rebase_output = rebase_path(output, root_build_dir)
    args = [
      "--base-dir=$rebase_base_dir",
      "--inputs=$rebase_inputs",
      "--zip-inputs=$rebase_zip_inputs",
      "--output=$rebase_output",
    ]
  }

  if (is_android) {
    import("//build/config/android/rules.gni")

    java_target_name = target_name + "_java"
    android_library(java_target_name) {
      deps = rebase_path([
                           "mojo/public/java:bindings",
                           "mojo/public/java:system",
                         ],
                         ".",
                         mojo_root)

      foreach(d, all_deps) {
        # Resolve the name, so that a target //mojo/something becomes
        # //mojo/something:something and we can append "_java" to get the java
        # dependency name.
        full_name = get_label_info(d, "label_no_toolchain")
        deps += [ "${full_name}_java" ]
      }

      srcjars = process_file_template(invoker.sources, generator_java_outputs)
    }
  }
}