OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/create_bundle_target_generator.h" | 5 #include "tools/gn/create_bundle_target_generator.h" |
6 | 6 |
| 7 #include "base/logging.h" |
7 #include "tools/gn/filesystem_utils.h" | 8 #include "tools/gn/filesystem_utils.h" |
8 #include "tools/gn/parse_tree.h" | 9 #include "tools/gn/parse_tree.h" |
9 #include "tools/gn/scope.h" | 10 #include "tools/gn/scope.h" |
10 #include "tools/gn/substitution_type.h" | 11 #include "tools/gn/substitution_type.h" |
11 #include "tools/gn/target.h" | 12 #include "tools/gn/target.h" |
12 #include "tools/gn/value.h" | 13 #include "tools/gn/value.h" |
| 14 #include "tools/gn/value_extractors.h" |
13 #include "tools/gn/variables.h" | 15 #include "tools/gn/variables.h" |
14 | 16 |
15 CreateBundleTargetGenerator::CreateBundleTargetGenerator( | 17 CreateBundleTargetGenerator::CreateBundleTargetGenerator( |
16 Target* target, | 18 Target* target, |
17 Scope* scope, | 19 Scope* scope, |
18 const FunctionCallNode* function_call, | 20 const FunctionCallNode* function_call, |
19 Err* err) | 21 Err* err) |
20 : TargetGenerator(target, scope, function_call, err) {} | 22 : TargetGenerator(target, scope, function_call, err) {} |
21 | 23 |
22 CreateBundleTargetGenerator::~CreateBundleTargetGenerator() {} | 24 CreateBundleTargetGenerator::~CreateBundleTargetGenerator() {} |
23 | 25 |
24 void CreateBundleTargetGenerator::DoRun() { | 26 void CreateBundleTargetGenerator::DoRun() { |
25 target_->set_output_type(Target::CREATE_BUNDLE); | 27 target_->set_output_type(Target::CREATE_BUNDLE); |
26 | 28 |
27 BundleData& bundle_data = target_->bundle_data(); | 29 BundleData& bundle_data = target_->bundle_data(); |
28 if (!GetBundleDir(SourceDir(), | 30 if (!FillBundleDir(SourceDir(), variables::kBundleRootDir, |
29 variables::kBundleRootDir, | 31 &bundle_data.root_dir())) |
30 &bundle_data.root_dir())) | |
31 return; | 32 return; |
32 if (!GetBundleDir(bundle_data.root_dir(), | 33 if (!FillBundleDir(bundle_data.root_dir(), variables::kBundleResourcesDir, |
33 variables::kBundleResourcesDir, | 34 &bundle_data.resources_dir())) |
34 &bundle_data.resources_dir())) | |
35 return; | 35 return; |
36 if (!GetBundleDir(bundle_data.root_dir(), | 36 if (!FillBundleDir(bundle_data.root_dir(), variables::kBundleExecutableDir, |
37 variables::kBundleExecutableDir, | 37 &bundle_data.executable_dir())) |
38 &bundle_data.executable_dir())) | |
39 return; | 38 return; |
40 if (!GetBundleDir(bundle_data.root_dir(), | 39 if (!FillBundleDir(bundle_data.root_dir(), variables::kBundlePlugInsDir, |
41 variables::kBundlePlugInsDir, | 40 &bundle_data.plugins_dir())) |
42 &bundle_data.plugins_dir())) | |
43 return; | 41 return; |
44 | 42 |
45 const Value* value = scope_->GetValue(variables::kProductType, true); | 43 if (!FillProductType()) |
46 if (value) { | 44 return; |
47 if (!value->VerifyTypeIs(Value::STRING, err_)) | |
48 return; | |
49 | 45 |
50 bundle_data.product_type().assign(value->string_value()); | 46 if (!FillCodeSigningScript()) |
51 } | 47 return; |
| 48 |
| 49 if (!FillCodeSigningSources()) |
| 50 return; |
| 51 |
| 52 if (!FillCodeSigningOutputs()) |
| 53 return; |
| 54 |
| 55 if (!FillCodeSigningArgs()) |
| 56 return; |
52 } | 57 } |
53 | 58 |
54 bool CreateBundleTargetGenerator::GetBundleDir( | 59 bool CreateBundleTargetGenerator::FillBundleDir( |
55 const SourceDir& bundle_root_dir, | 60 const SourceDir& bundle_root_dir, |
56 const base::StringPiece& name, | 61 const base::StringPiece& name, |
57 SourceDir* bundle_dir) { | 62 SourceDir* bundle_dir) { |
58 const Value* value = scope_->GetValue(name, true); | 63 const Value* value = scope_->GetValue(name, true); |
59 if (!value) | 64 if (!value) |
60 return true; | 65 return true; |
61 if (!value->VerifyTypeIs(Value::STRING, err_)) | 66 if (!value->VerifyTypeIs(Value::STRING, err_)) |
62 return false; | 67 return false; |
63 std::string str = value->string_value(); | 68 std::string str = value->string_value(); |
64 if (!str.empty() && str[str.size() - 1] != '/') | 69 if (!str.empty() && str[str.size() - 1] != '/') |
65 str.push_back('/'); | 70 str.push_back('/'); |
66 if (!EnsureStringIsInOutputDir(GetBuildSettings()->build_dir(), str, | 71 if (!EnsureStringIsInOutputDir(GetBuildSettings()->build_dir(), str, |
67 value->origin(), err_)) | 72 value->origin(), err_)) |
68 return false; | 73 return false; |
69 if (str != bundle_root_dir.value() && | 74 if (str != bundle_root_dir.value() && |
70 !IsStringInOutputDir(bundle_root_dir, str)) { | 75 !IsStringInOutputDir(bundle_root_dir, str)) { |
71 *err_ = Err(value->origin(), "Path is not in bundle root dir.", | 76 *err_ = Err(value->origin(), "Path is not in bundle root dir.", |
72 "The given file should be in the bundle root directory or below.\n" | 77 "The given file should be in the bundle root directory or below.\n" |
73 "Normally you would do \"$bundle_root_dir/foo\". I interpreted this\n" | 78 "Normally you would do \"$bundle_root_dir/foo\". I interpreted this\n" |
74 "as \"" + str + "\"."); | 79 "as \"" + str + "\"."); |
75 return false; | 80 return false; |
76 } | 81 } |
77 bundle_dir->SwapValue(&str); | 82 bundle_dir->SwapValue(&str); |
78 return true; | 83 return true; |
79 } | 84 } |
| 85 |
| 86 bool CreateBundleTargetGenerator::FillProductType() { |
| 87 const Value* value = scope_->GetValue(variables::kProductType, true); |
| 88 if (!value) |
| 89 return true; |
| 90 |
| 91 if (!value->VerifyTypeIs(Value::STRING, err_)) |
| 92 return false; |
| 93 |
| 94 target_->bundle_data().product_type().assign(value->string_value()); |
| 95 return true; |
| 96 } |
| 97 |
| 98 bool CreateBundleTargetGenerator::FillCodeSigningScript() { |
| 99 const Value* value = scope_->GetValue(variables::kCodeSigningScript, true); |
| 100 if (!value) |
| 101 return true; |
| 102 |
| 103 if (!value->VerifyTypeIs(Value::STRING, err_)) |
| 104 return false; |
| 105 |
| 106 SourceFile script_file = scope_->GetSourceDir().ResolveRelativeFile( |
| 107 *value, err_, scope_->settings()->build_settings()->root_path_utf8()); |
| 108 if (err_->has_error()) |
| 109 return false; |
| 110 |
| 111 target_->bundle_data().set_code_signing_script(script_file); |
| 112 return true; |
| 113 } |
| 114 |
| 115 bool CreateBundleTargetGenerator::FillCodeSigningSources() { |
| 116 const Value* value = scope_->GetValue(variables::kCodeSigningSources, true); |
| 117 if (!value) |
| 118 return true; |
| 119 |
| 120 if (target_->bundle_data().code_signing_script().is_null()) { |
| 121 *err_ = Err( |
| 122 function_call_, |
| 123 "No code signing script." |
| 124 "You must define code_signing_script if you use code_signing_sources."); |
| 125 return false; |
| 126 } |
| 127 |
| 128 Target::FileList script_sources; |
| 129 if (!ExtractListOfRelativeFiles(scope_->settings()->build_settings(), *value, |
| 130 scope_->GetSourceDir(), &script_sources, |
| 131 err_)) |
| 132 return false; |
| 133 |
| 134 target_->bundle_data().code_signing_sources().swap(script_sources); |
| 135 return true; |
| 136 } |
| 137 |
| 138 bool CreateBundleTargetGenerator::FillCodeSigningOutputs() { |
| 139 const Value* value = scope_->GetValue(variables::kCodeSigningOutputs, true); |
| 140 if (!value) |
| 141 return true; |
| 142 |
| 143 if (target_->bundle_data().code_signing_script().is_null()) { |
| 144 *err_ = Err( |
| 145 function_call_, |
| 146 "No code signing script." |
| 147 "You must define code_signing_script if you use code_signing_outputs."); |
| 148 return false; |
| 149 } |
| 150 |
| 151 if (!value->VerifyTypeIs(Value::LIST, err_)) |
| 152 return false; |
| 153 |
| 154 SubstitutionList& outputs = target_->bundle_data().code_signing_outputs(); |
| 155 if (!outputs.Parse(*value, err_)) |
| 156 return false; |
| 157 |
| 158 if (outputs.list().empty()) { |
| 159 *err_ = |
| 160 Err(function_call_, |
| 161 "Code signing script has no output." |
| 162 "If you have no outputs, the build system can not tell when your\n" |
| 163 "code signing script needs to be run."); |
| 164 return false; |
| 165 } |
| 166 |
| 167 // Validate that outputs are in the output dir. |
| 168 CHECK_EQ(value->list_value().size(), outputs.list().size()); |
| 169 for (size_t i = 0; i < value->list_value().size(); ++i) { |
| 170 if (!EnsureSubstitutionIsInOutputDir(outputs.list()[i], |
| 171 value->list_value()[i])) |
| 172 return false; |
| 173 } |
| 174 |
| 175 return true; |
| 176 } |
| 177 |
| 178 bool CreateBundleTargetGenerator::FillCodeSigningArgs() { |
| 179 const Value* value = scope_->GetValue(variables::kCodeSigningArgs, true); |
| 180 if (!value) |
| 181 return true; |
| 182 |
| 183 if (target_->bundle_data().code_signing_script().is_null()) { |
| 184 *err_ = Err( |
| 185 function_call_, |
| 186 "No code signing script." |
| 187 "You must define code_signing_script if you use code_signing_args."); |
| 188 return false; |
| 189 } |
| 190 |
| 191 if (!value->VerifyTypeIs(Value::LIST, err_)) |
| 192 return false; |
| 193 |
| 194 return target_->bundle_data().code_signing_args().Parse(*value, err_); |
| 195 } |
OLD | NEW |