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# Generate C++ and JavaScript source files from mojom files. The output files
6# will go under the generated file directory tree with the same path as each
7# input file.
8#
9# Parameters:
10#
11#   sources (required)
12#       List of source .mojom files to compile.
13#
14#   deps (optional)
15#
16#   public_deps (optional)
17#
18#   testonly (optional)
19#
20#   visibility (optional)
21template("mojom") {
22  assert(defined(invoker.sources),
23         "\"sources\" must be defined for the $target_name template.")
24
25  generator_root = "//mojo/public/tools/bindings"
26  generator_script = "$generator_root/mojom_bindings_generator.py"
27  generator_sources = [
28    generator_script,
29    "$generator_root/generators/cpp_templates/enum_declaration.tmpl",
30    "$generator_root/generators/cpp_templates/interface_declaration.tmpl",
31    "$generator_root/generators/cpp_templates/interface_definition.tmpl",
32    "$generator_root/generators/cpp_templates/interface_macros.tmpl",
33    "$generator_root/generators/cpp_templates/interface_proxy_declaration.tmpl",
34    "$generator_root/generators/cpp_templates/interface_request_validator_declaration.tmpl",
35    "$generator_root/generators/cpp_templates/interface_response_validator_declaration.tmpl",
36    "$generator_root/generators/cpp_templates/interface_stub_declaration.tmpl",
37    "$generator_root/generators/cpp_templates/module.cc.tmpl",
38    "$generator_root/generators/cpp_templates/module.h.tmpl",
39    "$generator_root/generators/cpp_templates/module-internal.h.tmpl",
40    "$generator_root/generators/cpp_templates/params_definition.tmpl",
41    "$generator_root/generators/cpp_templates/struct_declaration.tmpl",
42    "$generator_root/generators/cpp_templates/struct_definition.tmpl",
43    "$generator_root/generators/cpp_templates/struct_serialization_declaration.tmpl",
44    "$generator_root/generators/cpp_templates/struct_serialization_definition.tmpl",
45    "$generator_root/generators/cpp_templates/struct_macros.tmpl",
46    "$generator_root/generators/cpp_templates/wrapper_class_declaration.tmpl",
47    "$generator_root/generators/cpp_templates/wrapper_class_definition.tmpl",
48    "$generator_root/generators/js_templates/enum_definition.tmpl",
49    "$generator_root/generators/js_templates/interface_definition.tmpl",
50    "$generator_root/generators/js_templates/module.js.tmpl",
51    "$generator_root/generators/js_templates/struct_definition.tmpl",
52    "$generator_root/generators/python_templates/module_macros.tmpl",
53    "$generator_root/generators/python_templates/module.py.tmpl",
54    "$generator_root/generators/mojom_cpp_generator.py",
55    "$generator_root/generators/mojom_js_generator.py",
56    "$generator_root/generators/mojom_python_generator.py",
57    "$generator_root/pylib/mojom/__init__.py",
58    "$generator_root/pylib/mojom/error.py",
59    "$generator_root/pylib/mojom/generate/__init__.py",
60    "$generator_root/pylib/mojom/generate/data.py",
61    "$generator_root/pylib/mojom/generate/generator.py",
62    "$generator_root/pylib/mojom/generate/module.py",
63    "$generator_root/pylib/mojom/generate/pack.py",
64    "$generator_root/pylib/mojom/generate/template_expander.py",
65    "$generator_root/pylib/mojom/parse/__init__.py",
66    "$generator_root/pylib/mojom/parse/ast.py",
67    "$generator_root/pylib/mojom/parse/lexer.py",
68    "$generator_root/pylib/mojom/parse/parser.py",
69    "$generator_root/pylib/mojom/parse/translate.py",
70  ]
71  generator_cpp_outputs = [
72    "{{source_gen_dir}}/{{source_name_part}}.mojom.cc",
73    "{{source_gen_dir}}/{{source_name_part}}.mojom.h",
74    "{{source_gen_dir}}/{{source_name_part}}.mojom-internal.h",
75  ]
76  generator_js_outputs = [
77    "{{source_gen_dir}}/{{source_name_part}}.mojom.js",
78  ]
79  generator_python_outputs = [
80    "{{source_gen_dir}}/{{source_name_part}}_mojom.py",
81  ]
82
83  if (defined(invoker.visibility)) {
84    # Need to save this because the the target_name is overwritten inside the
85    # action to be that of the action itself. Only define this in the case the
86    # var is used to avoid unused var error.
87    target_visibility = [ ":$target_name" ]
88  }
89
90  generator_target_name = target_name + "__generator"
91  action_foreach(generator_target_name) {
92    if (defined(invoker.visibility)) {
93      visibility = target_visibility + invoker.visibility
94    }
95    script = generator_script
96    inputs = generator_sources
97    sources = invoker.sources
98    outputs = generator_cpp_outputs +
99              generator_js_outputs +
100              generator_python_outputs
101    args = [
102      "{{source}}",
103      "--use_chromium_bundled_pylibs",
104      "-d", rebase_path("//", root_build_dir),
105      "-I", rebase_path("//", root_build_dir),
106      "-o", "{{source_gen_dir}}",
107    ]
108  }
109
110  source_set(target_name) {
111    if (defined(invoker.visibility)) {
112      visibility = invoker.visibility
113    }
114    if (defined(invoker.testonly)) {
115      testonly = invoker.testonly
116    }
117    sources = process_file_template(invoker.sources, generator_cpp_outputs)
118    data = process_file_template(invoker.sources, generator_js_outputs)
119    deps = [
120      ":$generator_target_name",
121      "//mojo/public/cpp/bindings",
122    ]
123    if (defined(invoker.deps)) {
124      deps += invoker.deps
125    }
126    if (defined(invoker.public_deps)) {
127      public_deps = invoker.public_deps
128    }
129  }
130}
131