• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/[^/]*\.imageset/[^/]*
32   //    .*\.xcassets/[^/]*\.launchimage/[^/]*
33   //    .*\.xcassets/[^/]*\.colorset/[^/]*
34   //    .*\.xcassets/[^/]*\.dataset/[^/]*
35   bool is_file_from_asset_catalog = false;
36   std::string_view dir = FindDirNoTrailingSeparator(source);
37   if (base::EndsWith(source, "/Contents.json", base::CompareCase::SENSITIVE) &&
38       base::EndsWith(dir, ".xcassets", base::CompareCase::SENSITIVE)) {
39     is_file_from_asset_catalog = true;
40   } else if (base::EndsWith(dir, ".appiconset", base::CompareCase::SENSITIVE) ||
41              base::EndsWith(dir, ".imageset", base::CompareCase::SENSITIVE) ||
42              base::EndsWith(dir, ".launchimage",
43                             base::CompareCase::SENSITIVE) ||
44              base::EndsWith(dir, ".colorset", base::CompareCase::SENSITIVE) ||
45              base::EndsWith(dir, ".dataset", base::CompareCase::SENSITIVE)) {
46     dir = FindDirNoTrailingSeparator(dir);
47     is_file_from_asset_catalog =
48         base::EndsWith(dir, ".xcassets", base::CompareCase::SENSITIVE);
49   }
50   if (is_file_from_asset_catalog && asset_catalog) {
51     std::string asset_catalog_path(dir);
52     *asset_catalog = SourceFile(std::move(asset_catalog_path));
53   }
54   return is_file_from_asset_catalog;
55 }
56 
57 }  // namespace
58 
59 BundleData::BundleData() = default;
60 
61 BundleData::~BundleData() = default;
62 
AddBundleData(const Target * target)63 void BundleData::AddBundleData(const Target* target) {
64   DCHECK_EQ(target->output_type(), Target::BUNDLE_DATA);
65   for (const auto& pattern : bundle_deps_filter_) {
66     if (pattern.Matches(target->label()))
67       return;
68   }
69   bundle_deps_.push_back(target);
70 }
71 
OnTargetResolved(Target * owning_target)72 void BundleData::OnTargetResolved(Target* owning_target) {
73   // Only initialize file_rules_ and assets_catalog_sources for "create_bundle"
74   // target (properties are only used by those targets).
75   if (owning_target->output_type() != Target::CREATE_BUNDLE)
76     return;
77 
78   UniqueVector<const Target*> assets_catalog_deps;
79   UniqueVector<SourceFile> assets_catalog_sources;
80 
81   for (const Target* target : bundle_deps_) {
82     SourceFiles file_rule_sources;
83     for (const SourceFile& source_file : target->sources()) {
84       SourceFile assets_catalog;
85       if (IsSourceFileFromAssetsCatalog(source_file.value(), &assets_catalog)) {
86         assets_catalog_sources.push_back(assets_catalog);
87         assets_catalog_deps.push_back(target);
88       } else {
89         file_rule_sources.push_back(source_file);
90       }
91     }
92 
93     if (!file_rule_sources.empty()) {
94       DCHECK_EQ(target->action_values().outputs().list().size(), 1u);
95       file_rules_.push_back(
96           BundleFileRule(target, file_rule_sources,
97                          target->action_values().outputs().list()[0]));
98     }
99   }
100 
101   assets_catalog_deps_.insert(assets_catalog_deps_.end(),
102                               assets_catalog_deps.begin(),
103                               assets_catalog_deps.end());
104   assets_catalog_sources_.insert(assets_catalog_sources_.end(),
105                                  assets_catalog_sources.begin(),
106                                  assets_catalog_sources.end());
107 
108   GetSourceFiles(&owning_target->sources());
109 }
110 
GetSourceFiles(SourceFiles * sources) const111 void BundleData::GetSourceFiles(SourceFiles* sources) const {
112   for (const BundleFileRule& file_rule : file_rules_) {
113     sources->insert(sources->end(), file_rule.sources().begin(),
114                     file_rule.sources().end());
115   }
116   sources->insert(sources->end(), assets_catalog_sources_.begin(),
117                   assets_catalog_sources_.end());
118   if (!code_signing_script_.is_null()) {
119     sources->insert(sources->end(), code_signing_sources_.begin(),
120                     code_signing_sources_.end());
121   }
122 }
123 
GetOutputFiles(const Settings * settings,const Target * target,OutputFiles * outputs,Err * err) const124 bool BundleData::GetOutputFiles(const Settings* settings,
125                                 const Target* target,
126                                 OutputFiles* outputs,
127                                 Err* err) const {
128   SourceFiles outputs_as_sources;
129   if (!GetOutputsAsSourceFiles(settings, target, &outputs_as_sources, err))
130     return false;
131   for (const SourceFile& source_file : outputs_as_sources)
132     outputs->push_back(OutputFile(settings->build_settings(), source_file));
133   return true;
134 }
135 
GetOutputsAsSourceFiles(const Settings * settings,const Target * target,SourceFiles * outputs_as_source,Err * err) const136 bool BundleData::GetOutputsAsSourceFiles(const Settings* settings,
137                                          const Target* target,
138                                          SourceFiles* outputs_as_source,
139                                          Err* err) const {
140   for (const BundleFileRule& file_rule : file_rules_) {
141     for (const SourceFile& source : file_rule.sources()) {
142       SourceFile expanded_source_file;
143       if (!file_rule.ApplyPatternToSource(settings, target, *this, source,
144                                           &expanded_source_file, err))
145         return false;
146       outputs_as_source->push_back(expanded_source_file);
147     }
148   }
149 
150   if (!assets_catalog_sources_.empty())
151     outputs_as_source->push_back(GetCompiledAssetCatalogPath());
152 
153   if (!partial_info_plist_.is_null())
154     outputs_as_source->push_back(partial_info_plist_);
155 
156   if (!code_signing_script_.is_null()) {
157     std::vector<SourceFile> code_signing_output_files;
158     SubstitutionWriter::GetListAsSourceFiles(code_signing_outputs_,
159                                              &code_signing_output_files);
160     outputs_as_source->insert(outputs_as_source->end(),
161                               code_signing_output_files.begin(),
162                               code_signing_output_files.end());
163   }
164 
165   if (!root_dir_.is_null())
166     outputs_as_source->push_back(GetBundleRootDirOutput(settings));
167 
168   return true;
169 }
170 
GetCompiledAssetCatalogPath() const171 SourceFile BundleData::GetCompiledAssetCatalogPath() const {
172   DCHECK(!assets_catalog_sources_.empty());
173   std::string assets_car_path = resources_dir_.value() + "/Assets.car";
174   return SourceFile(std::move(assets_car_path));
175 }
176 
GetBundleRootDirOutput(const Settings * settings) const177 SourceFile BundleData::GetBundleRootDirOutput(const Settings* settings) const {
178   std::string root_dir_value = root_dir().value();
179   size_t last_separator = root_dir_value.rfind('/');
180   if (last_separator != std::string::npos)
181     root_dir_value = root_dir_value.substr(0, last_separator);
182 
183   return SourceFile(std::move(root_dir_value));
184 }
185 
GetBundleRootDirOutputAsDir(const Settings * settings) const186 SourceDir BundleData::GetBundleRootDirOutputAsDir(
187     const Settings* settings) const {
188   return SourceDir(GetBundleRootDirOutput(settings).value());
189 }
190