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
3 // found in the LICENSE file.
4
5 #include "gn/bundle_data.h"
6
7 #include "base/logging.h"
8 #include "base/strings/string_util.h"
9 #include "gn/filesystem_utils.h"
10 #include "gn/label_pattern.h"
11 #include "gn/output_file.h"
12 #include "gn/settings.h"
13 #include "gn/substitution_writer.h"
14 #include "gn/target.h"
15
16 namespace {
17
18 // Return directory of |path| without the trailing directory separator.
FindDirNoTrailingSeparator(std::string_view path)19 std::string_view FindDirNoTrailingSeparator(std::string_view path) {
20 std::string_view::size_type pos = path.find_last_of("/\\");
21 if (pos == std::string_view::npos)
22 return std::string_view();
23 return std::string_view(path.data(), pos);
24 }
25
IsSourceFileFromAssetsCatalog(std::string_view source,SourceFile * asset_catalog)26 bool IsSourceFileFromAssetsCatalog(std::string_view source,
27 SourceFile* asset_catalog) {
28 // Check whether |source| matches one of the following pattern:
29 // .*\.xcassets/Contents.json
30 // .*\.xcassets/[^/]*\.appiconset/[^/]*
31 // .*\.xcassets/[^/]*\.colorset/[^/]*
32 // .*\.xcassets/[^/]*\.dataset/[^/]*
33 // .*\.xcassets/[^/]*\.imageset/[^/]*
34 // .*\.xcassets/[^/]*\.launchimage/[^/]*
35 // .*\.xcassets/[^/]*\.symbolset/[^/]*
36 bool is_file_from_asset_catalog = false;
37 std::string_view dir = FindDirNoTrailingSeparator(source);
38 if (base::EndsWith(source, "/Contents.json", base::CompareCase::SENSITIVE) &&
39 base::EndsWith(dir, ".xcassets", base::CompareCase::SENSITIVE)) {
40 is_file_from_asset_catalog = true;
41 } else if (base::EndsWith(dir, ".appiconset", base::CompareCase::SENSITIVE) ||
42 base::EndsWith(dir, ".colorset", base::CompareCase::SENSITIVE) ||
43 base::EndsWith(dir, ".dataset", base::CompareCase::SENSITIVE) ||
44 base::EndsWith(dir, ".imageset", base::CompareCase::SENSITIVE) ||
45 base::EndsWith(dir, ".launchimage",
46 base::CompareCase::SENSITIVE) ||
47 base::EndsWith(dir, ".symbolset", base::CompareCase::SENSITIVE)) {
48 dir = FindDirNoTrailingSeparator(dir);
49 is_file_from_asset_catalog =
50 base::EndsWith(dir, ".xcassets", base::CompareCase::SENSITIVE);
51 }
52 if (is_file_from_asset_catalog && asset_catalog) {
53 std::string asset_catalog_path(dir);
54 *asset_catalog = SourceFile(std::move(asset_catalog_path));
55 }
56 return is_file_from_asset_catalog;
57 }
58
59 } // namespace
60
61 BundleData::BundleData() = default;
62
63 BundleData::~BundleData() = default;
64
AddBundleData(const Target * target)65 void BundleData::AddBundleData(const Target* target) {
66 DCHECK_EQ(target->output_type(), Target::BUNDLE_DATA);
67 for (const auto& pattern : bundle_deps_filter_) {
68 if (pattern.Matches(target->label()))
69 return;
70 }
71 bundle_deps_.push_back(target);
72 }
73
OnTargetResolved(Target * owning_target)74 void BundleData::OnTargetResolved(Target* owning_target) {
75 // Only initialize file_rules_ and assets_catalog_sources for "create_bundle"
76 // target (properties are only used by those targets).
77 if (owning_target->output_type() != Target::CREATE_BUNDLE)
78 return;
79
80 UniqueVector<const Target*> assets_catalog_deps;
81 UniqueVector<SourceFile> assets_catalog_sources;
82
83 for (const Target* target : bundle_deps_) {
84 SourceFiles file_rule_sources;
85 for (const SourceFile& source_file : target->sources()) {
86 SourceFile assets_catalog;
87 if (IsSourceFileFromAssetsCatalog(source_file.value(), &assets_catalog)) {
88 assets_catalog_sources.push_back(assets_catalog);
89 assets_catalog_deps.push_back(target);
90 } else {
91 file_rule_sources.push_back(source_file);
92 }
93 }
94
95 if (!file_rule_sources.empty()) {
96 DCHECK_EQ(target->action_values().outputs().list().size(), 1u);
97 file_rules_.push_back(
98 BundleFileRule(target, file_rule_sources,
99 target->action_values().outputs().list()[0]));
100 }
101 }
102
103 assets_catalog_deps_.insert(assets_catalog_deps_.end(),
104 assets_catalog_deps.begin(),
105 assets_catalog_deps.end());
106 assets_catalog_sources_.insert(assets_catalog_sources_.end(),
107 assets_catalog_sources.begin(),
108 assets_catalog_sources.end());
109
110 GetSourceFiles(&owning_target->sources());
111 }
112
GetSourceFiles(SourceFiles * sources) const113 void BundleData::GetSourceFiles(SourceFiles* sources) const {
114 for (const BundleFileRule& file_rule : file_rules_) {
115 sources->insert(sources->end(), file_rule.sources().begin(),
116 file_rule.sources().end());
117 }
118 sources->insert(sources->end(), assets_catalog_sources_.begin(),
119 assets_catalog_sources_.end());
120 if (!code_signing_script_.is_null()) {
121 sources->insert(sources->end(), code_signing_sources_.begin(),
122 code_signing_sources_.end());
123 }
124 }
125
GetOutputFiles(const Settings * settings,const Target * target,OutputFiles * outputs,Err * err) const126 bool BundleData::GetOutputFiles(const Settings* settings,
127 const Target* target,
128 OutputFiles* outputs,
129 Err* err) const {
130 SourceFiles outputs_as_sources;
131 if (!GetOutputsAsSourceFiles(settings, target, &outputs_as_sources, err))
132 return false;
133 for (const SourceFile& source_file : outputs_as_sources)
134 outputs->push_back(OutputFile(settings->build_settings(), source_file));
135 return true;
136 }
137
GetOutputsAsSourceFiles(const Settings * settings,const Target * target,SourceFiles * outputs_as_source,Err * err) const138 bool BundleData::GetOutputsAsSourceFiles(const Settings* settings,
139 const Target* target,
140 SourceFiles* outputs_as_source,
141 Err* err) const {
142 for (const BundleFileRule& file_rule : file_rules_) {
143 for (const SourceFile& source : file_rule.sources()) {
144 SourceFile expanded_source_file;
145 if (!file_rule.ApplyPatternToSource(settings, target, *this, source,
146 &expanded_source_file, err))
147 return false;
148 outputs_as_source->push_back(expanded_source_file);
149 }
150 }
151
152 if (!assets_catalog_sources_.empty())
153 outputs_as_source->push_back(GetCompiledAssetCatalogPath());
154
155 if (!partial_info_plist_.is_null())
156 outputs_as_source->push_back(partial_info_plist_);
157
158 if (!code_signing_script_.is_null()) {
159 std::vector<SourceFile> code_signing_output_files;
160 SubstitutionWriter::GetListAsSourceFiles(code_signing_outputs_,
161 &code_signing_output_files);
162 outputs_as_source->insert(outputs_as_source->end(),
163 code_signing_output_files.begin(),
164 code_signing_output_files.end());
165 }
166
167 if (!root_dir_.is_null())
168 outputs_as_source->push_back(GetBundleRootDirOutput(settings));
169
170 return true;
171 }
172
GetCompiledAssetCatalogPath() const173 SourceFile BundleData::GetCompiledAssetCatalogPath() const {
174 DCHECK(!assets_catalog_sources_.empty());
175 std::string assets_car_path = resources_dir_.value() + "/Assets.car";
176 return SourceFile(std::move(assets_car_path));
177 }
178
GetBundleRootDirOutput(const Settings * settings) const179 SourceFile BundleData::GetBundleRootDirOutput(const Settings* settings) const {
180 std::string root_dir_value = root_dir().value();
181 size_t last_separator = root_dir_value.rfind('/');
182 if (last_separator != std::string::npos)
183 root_dir_value = root_dir_value.substr(0, last_separator);
184
185 return SourceFile(std::move(root_dir_value));
186 }
187
GetBundleRootDirOutputAsDir(const Settings * settings) const188 SourceDir BundleData::GetBundleRootDirOutputAsDir(
189 const Settings* settings) const {
190 return SourceDir(GetBundleRootDirOutput(settings).value());
191 }
192
GetBundleDir(const Settings * settings) const193 SourceDir BundleData::GetBundleDir(const Settings* settings) const{
194 return GetBundleRootDirOutput(settings).GetDir();
195 }
196