| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "tools/gn/functions.h" | |
| 6 | |
| 7 #include "tools/gn/err.h" | |
| 8 #include "tools/gn/parse_tree.h" | |
| 9 #include "tools/gn/scope.h" | |
| 10 #include "tools/gn/target_generator.h" | |
| 11 #include "tools/gn/value.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 Value ExecuteGenericTarget(const char* target_type, | |
| 16 Scope* scope, | |
| 17 const FunctionCallNode* function, | |
| 18 const std::vector<Value>& args, | |
| 19 BlockNode* block, | |
| 20 Err* err) { | |
| 21 if (!EnsureNotProcessingImport(function, scope, err) || | |
| 22 !EnsureNotProcessingBuildConfig(function, scope, err)) | |
| 23 return Value(); | |
| 24 Scope block_scope(scope); | |
| 25 if (!FillTargetBlockScope(scope, function, target_type, block, | |
| 26 args, &block_scope, err)) | |
| 27 return Value(); | |
| 28 | |
| 29 block->ExecuteBlockInScope(&block_scope, err); | |
| 30 if (err->has_error()) | |
| 31 return Value(); | |
| 32 | |
| 33 TargetGenerator::GenerateTarget(&block_scope, function->function(), args, | |
| 34 target_type, err); | |
| 35 | |
| 36 block_scope.CheckForUnusedVars(err); | |
| 37 return Value(); | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 42 Value ExecuteComponent(Scope* scope, | |
| 43 const FunctionCallNode* function, | |
| 44 const std::vector<Value>& args, | |
| 45 BlockNode* block, | |
| 46 Err* err) { | |
| 47 // A component is either a shared or static library, depending on the value | |
| 48 // of |component_mode|. | |
| 49 const Value* component_mode_value = scope->GetValue("component_mode"); | |
| 50 | |
| 51 static const char helptext[] = | |
| 52 "You're declaring a component here but have not defined " | |
| 53 "\"component_mode\" to\neither \"shared_library\" or \"static_library\"."; | |
| 54 if (!component_mode_value) { | |
| 55 *err = Err(function->function(), "No component mode set.", helptext); | |
| 56 return Value(); | |
| 57 } | |
| 58 if (component_mode_value->type() != Value::STRING || | |
| 59 (component_mode_value->string_value() != functions::kSharedLibrary && | |
| 60 component_mode_value->string_value() != functions::kStaticLibrary)) { | |
| 61 *err = Err(function->function(), "Invalid component mode set.", helptext); | |
| 62 return Value(); | |
| 63 } | |
| 64 const std::string& component_mode = component_mode_value->string_value(); | |
| 65 | |
| 66 if (!EnsureNotProcessingImport(function, scope, err)) | |
| 67 return Value(); | |
| 68 Scope block_scope(scope); | |
| 69 if (!FillTargetBlockScope(scope, function, component_mode.c_str(), block, | |
| 70 args, &block_scope, err)) | |
| 71 return Value(); | |
| 72 | |
| 73 block->ExecuteBlockInScope(&block_scope, err); | |
| 74 if (err->has_error()) | |
| 75 return Value(); | |
| 76 | |
| 77 TargetGenerator::GenerateTarget(&block_scope, function->function(), args, | |
| 78 component_mode, err); | |
| 79 return Value(); | |
| 80 } | |
| 81 | |
| 82 Value ExecuteCopy(Scope* scope, | |
| 83 const FunctionCallNode* function, | |
| 84 const std::vector<Value>& args, | |
| 85 Err* err) { | |
| 86 if (!EnsureNotProcessingImport(function, scope, err) || | |
| 87 !EnsureNotProcessingBuildConfig(function, scope, err)) | |
| 88 return Value(); | |
| 89 TargetGenerator::GenerateTarget(scope, function->function(), args, | |
| 90 functions::kCopy, err); | |
| 91 return Value(); | |
| 92 } | |
| 93 | |
| 94 /* | |
| 95 custom: Declare a script-generated target. | |
| 96 | |
| 97 This target type allows you to run a script over a set of sources files and | |
| 98 generate a set of output files. | |
| 99 | |
| 100 The script will be executed with the given arguments with the current | |
| 101 directory being that of the current BUILD file. | |
| 102 | |
| 103 There are two modes. The first mode is the "per-file" mode where you | |
| 104 specify a list of sources and the script is run once for each one as a build | |
| 105 rule. In this case, each file specified in the |outputs| variable must be | |
| 106 unique when applied to each source file (normally you would reference | |
| 107 "{{source_name_part}}" from within each one) or the build system will get | |
| 108 confused about how to build those files. You should use the |data| variable | |
| 109 to list all additional dependencies of your script: these will be added | |
| 110 as dependencies for each build step. | |
| 111 | |
| 112 The second mode is when you just want to run a script once rather than as a | |
| 113 general rule over a set of files. In this case you don't list any sources. | |
| 114 Dependencies of your script are specified only in the |data| variable and | |
| 115 your |outputs| variable should just list all outputs. | |
| 116 | |
| 117 Variables: | |
| 118 | |
| 119 args, data, deps, outputs, script*, sources | |
| 120 * = required | |
| 121 | |
| 122 There are some special substrings that will be searched for when processing | |
| 123 some variables: | |
| 124 | |
| 125 "{{source}}" | |
| 126 Expanded in |args|, this is the name of the source file relative to the | |
| 127 current directory when running the script. This is how you specify | |
| 128 the current input file to your script. | |
| 129 | |
| 130 "{{source_name_part}}" | |
| 131 Expanded in |args| and |outputs|, this is just the filename part of the | |
| 132 current source file with no directory or extension. This is how you | |
| 133 specify a name transoformation to the output. Normally you would | |
| 134 write an output as "$target_output_dir/{{source_name_part}}.o". | |
| 135 | |
| 136 All |outputs| files must be inside the output directory of the build. You | |
| 137 would generally use "$target_output_dir" or "$target_gen_dir" to reference | |
| 138 the output or generated intermediate file directories, respectively. | |
| 139 | |
| 140 Examples: | |
| 141 | |
| 142 custom("general_rule") { | |
| 143 script = "do_processing.py" | |
| 144 sources = [ "foo.idl" ] | |
| 145 data = [ "my_configuration.txt" ] | |
| 146 outputs = [ "$target_gen_dir/{{source_name_part}}.h" ] | |
| 147 args = [ "{{source}}", | |
| 148 "-o", "$relative_target_gen_dir/{{source_name_part}}.h" ] | |
| 149 } | |
| 150 | |
| 151 custom("just_run_this_guy_once") { | |
| 152 script = "doprocessing.py" | |
| 153 data = [ "my_configuration.txt" ] | |
| 154 outputs = [ "$target_gen_dir/insightful_output.txt" ] | |
| 155 args = [ "--output_dir", $target_gen_dir ] | |
| 156 } | |
| 157 */ | |
| 158 Value ExecuteCustom(Scope* scope, | |
| 159 const FunctionCallNode* function, | |
| 160 const std::vector<Value>& args, | |
| 161 BlockNode* block, | |
| 162 Err* err) { | |
| 163 return ExecuteGenericTarget(functions::kCustom, scope, function, args, | |
| 164 block, err); | |
| 165 } | |
| 166 | |
| 167 Value ExecuteExecutable(Scope* scope, | |
| 168 const FunctionCallNode* function, | |
| 169 const std::vector<Value>& args, | |
| 170 BlockNode* block, | |
| 171 Err* err) { | |
| 172 return ExecuteGenericTarget(functions::kExecutable, scope, function, args, | |
| 173 block, err); | |
| 174 } | |
| 175 | |
| 176 Value ExecuteSharedLibrary(Scope* scope, | |
| 177 const FunctionCallNode* function, | |
| 178 const std::vector<Value>& args, | |
| 179 BlockNode* block, | |
| 180 Err* err) { | |
| 181 return ExecuteGenericTarget(functions::kSharedLibrary, scope, function, args, | |
| 182 block, err); | |
| 183 } | |
| 184 | |
| 185 Value ExecuteStaticLibrary(Scope* scope, | |
| 186 const FunctionCallNode* function, | |
| 187 const std::vector<Value>& args, | |
| 188 BlockNode* block, | |
| 189 Err* err) { | |
| 190 return ExecuteGenericTarget(functions::kStaticLibrary, scope, function, args, | |
| 191 block, err); | |
| 192 } | |
| 193 | |
| 194 /* | |
| 195 group: Declare a group of targets. | |
| 196 | |
| 197 This target type allows you to create meta-targets that just collect a set | |
| 198 of dependencies into one named target. | |
| 199 | |
| 200 Variables: | |
| 201 | |
| 202 deps | |
| 203 | |
| 204 Example: | |
| 205 | |
| 206 group("all") { | |
| 207 deps = [ | |
| 208 "//project:runner", | |
| 209 "//project:unit_tests", | |
| 210 ] | |
| 211 } | |
| 212 */ | |
| 213 Value ExecuteGroup(Scope* scope, | |
| 214 const FunctionCallNode* function, | |
| 215 const std::vector<Value>& args, | |
| 216 BlockNode* block, | |
| 217 Err* err) { | |
| 218 return ExecuteGenericTarget(functions::kGroup, scope, function, args, | |
| 219 block, err); | |
| 220 } | |
| 221 | |
| OLD | NEW |