| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "tools/gn/functions.h" | 5 #include "tools/gn/functions.h" |
| 6 | 6 |
| 7 #include "tools/gn/parse_tree.h" | 7 #include "tools/gn/parse_tree.h" |
| 8 #include "tools/gn/scope.h" | 8 #include "tools/gn/scope.h" |
| 9 #include "tools/gn/template.h" | 9 #include "tools/gn/template.h" |
| 10 #include "tools/gn/value.h" | 10 #include "tools/gn/value.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 " # Be nice and help callers debug problems by checking that the\n" | 76 " # Be nice and help callers debug problems by checking that the\n" |
| 77 " # variables the template requires are defined. This gives a nice\n" | 77 " # variables the template requires are defined. This gives a nice\n" |
| 78 " # message rather than giving the user an error about an\n" | 78 " # message rather than giving the user an error about an\n" |
| 79 " # undefined variable in the file defining the template\n" | 79 " # undefined variable in the file defining the template\n" |
| 80 " #\n" | 80 " #\n" |
| 81 " # You can also use defined() to give default values to variables\n" | 81 " # You can also use defined() to give default values to variables\n" |
| 82 " # unspecified by the invoker.\n" | 82 " # unspecified by the invoker.\n" |
| 83 " assert(defined(invoker.sources),\n" | 83 " assert(defined(invoker.sources),\n" |
| 84 " \"Need sources in $target_name listing the idl files.\")\n" | 84 " \"Need sources in $target_name listing the idl files.\")\n" |
| 85 "\n" | 85 "\n" |
| 86 " # Define a variable containing a source expansion\n" | 86 " # Name of the intermediate target that does the code gen. This must\n" |
| 87 " # (see \"gn help source_expansion\") that maps input files to\n" | 87 " # incorporate the target name so it's unique across template\n" |
| 88 " # output files. It is used in both targets below.\n" | 88 " # instantiations.\n" |
| 89 " filter = [ \"$target_gen_dir/{{source_name_part}}.cc\",\n" | 89 " code_gen_target_name = target_name + \"_code_gen\"\n" |
| 90 " \"$target_gen_dir/{{source_name_part}}.h\" ]\n" | |
| 91 "\n" | 90 "\n" |
| 92 " # Intermediate target to convert IDL to C source. Note that the name\n" | 91 " # Intermediate target to convert IDL to C source. Note that the name\n" |
| 93 " # is based on the name the invoker of the template specified. This\n" | 92 " # is based on the name the invoker of the template specified. This\n" |
| 94 " # way, each time the template is invoked we get a unique\n" | 93 " # way, each time the template is invoked we get a unique\n" |
| 95 " # intermediate action name (since all target names are in the global\n" | 94 " # intermediate action name (since all target names are in the global\n" |
| 96 " # scope).\n" | 95 " # scope).\n" |
| 97 " action_foreach(\"${target_name}_code_gen\") {\n" | 96 " action_foreach(code_gen_target_name) {\n" |
| 98 " # Access the scope defined by the invoker via the implicit\n" | 97 " # Access the scope defined by the invoker via the implicit\n" |
| 99 " # \"invoker\" variable.\n" | 98 " # \"invoker\" variable.\n" |
| 100 " sources = invoker.sources\n" | 99 " sources = invoker.sources\n" |
| 101 "\n" | 100 "\n" |
| 102 " # Note that we need an absolute path for our script file name.\n" | 101 " # Note that we need an absolute path for our script file name.\n" |
| 103 " # The current directory when executing this code will be that of\n" | 102 " # The current directory when executing this code will be that of\n" |
| 104 " # the invoker (this is why we can use the \"sources\" directly\n" | 103 " # the invoker (this is why we can use the \"sources\" directly\n" |
| 105 " # above without having to rebase all of the paths). But if we need\n" | 104 " # above without having to rebase all of the paths). But if we need\n" |
| 106 " # to reference a script relative to the template file, we'll need\n" | 105 " # to reference a script relative to the template file, we'll need\n" |
| 107 " # to use an absolute path instead.\n" | 106 " # to use an absolute path instead.\n" |
| 108 " script = \"//tools/idl/idl_code_generator.py\"\n" | 107 " script = \"//tools/idl/idl_code_generator.py\"\n" |
| 109 " outputs = filter # Variable from above.\n" | 108 "\n" |
| 109 " # Tell GN how to expand output names given the sources.\n" |
| 110 " # See \"gn help source_expansion\" for more.\n" |
| 111 " outputs = [ \"$target_gen_dir/{{source_name_part}}.cc\",\n" |
| 112 " \"$target_gen_dir/{{source_name_part}}.h\" ]\n" |
| 110 " }\n" | 113 " }\n" |
| 111 "\n" | 114 "\n" |
| 112 " # Name the source set the same as the template invocation so\n" | 115 " # Name the source set the same as the template invocation so\n" |
| 113 " # instancing this template produces something that other targets\n" | 116 " # instancing this template produces something that other targets\n" |
| 114 " # can link to in their deps.\n" | 117 " # can link to in their deps.\n" |
| 115 " source_set(target_name) {\n" | 118 " source_set(target_name) {\n" |
| 116 " # Generates the list of sources.\n" | 119 " # Generates the list of sources, we get these from the\n" |
| 117 " # See \"gn help process_file_template\"\n" | 120 " # action_foreach above.\n" |
| 118 " sources = process_file_template(invoker.sources, filter)\n" | 121 " sources = get_target_outputs(\":$code_gen_target_name\")\n" |
| 119 "\n" | 122 "\n" |
| 120 " # This target depends on the files produced by the above code gen\n" | 123 " # This target depends on the files produced by the above code gen\n" |
| 121 " # target.\n" | 124 " # target.\n" |
| 122 " deps = [ \":${target_name}_code_gen\" ]\n" | 125 " deps = [ \":$code_gen_target_name\" ]\n" |
| 123 " }\n" | 126 " }\n" |
| 124 " }\n" | 127 " }\n" |
| 125 "\n" | 128 "\n" |
| 126 "Example of invoking the resulting template:\n" | 129 "Example of invoking the resulting template:\n" |
| 127 "\n" | 130 "\n" |
| 128 " # This calls the template code above, defining target_name to be\n" | 131 " # This calls the template code above, defining target_name to be\n" |
| 129 " # \"foo_idl_files\" and \"invoker\" to be the set of stuff defined in\n" | 132 " # \"foo_idl_files\" and \"invoker\" to be the set of stuff defined in\n" |
| 130 " # the curly brackets.\n" | 133 " # the curly brackets.\n" |
| 131 " my_idl(\"foo_idl_files\") {\n" | 134 " my_idl(\"foo_idl_files\") {\n" |
| 132 " # Goes into the template as \"invoker.sources\".\n" | 135 " # Goes into the template as \"invoker.sources\".\n" |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 err->AppendSubErr(Err(existing_template->GetDefinitionRange(), | 168 err->AppendSubErr(Err(existing_template->GetDefinitionRange(), |
| 166 "Previous definition.")); | 169 "Previous definition.")); |
| 167 return Value(); | 170 return Value(); |
| 168 } | 171 } |
| 169 | 172 |
| 170 scope->AddTemplate(template_name, new Template(scope, function)); | 173 scope->AddTemplate(template_name, new Template(scope, function)); |
| 171 return Value(); | 174 return Value(); |
| 172 } | 175 } |
| 173 | 176 |
| 174 } // namespace functions | 177 } // namespace functions |
| OLD | NEW |