| Flutter Impeller |

includer.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter 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 "impeller/compiler/includer.h"
6
7 #include <cstring>
8
9 #include "flutter/fml/paths.h"
10
11 namespace impeller {
12 namespace compiler {
13
14 Includer::Includer(std::shared_ptr<fml::UniqueFD> working_directory,
15 std::vector<IncludeDir> include_dirs,
16 std::function<void(std::string)> on_file_included)
17 : working_directory_(std::move(working_directory)),
18 include_dirs_(std::move(include_dirs)),
19 on_file_included_(std::move(on_file_included)) {}
20
21 // |shaderc::CompileOptions::IncluderInterface|
22 Includer::~Includer() = default;
23
24 std::unique_ptr<fml::FileMapping> Includer::TryOpenMapping(
25 const IncludeDir& dir,
26 const char* requested_source) {
27 if (!dir.dir || !dir.dir->is_valid()) {
28 return nullptr;
29 }
30
31 if (requested_source == nullptr) {
32 return nullptr;
33 }
34
35 std::string source(requested_source);
36 if (source.empty()) {
37 return nullptr;
38 }
39
40 auto mapping = fml::FileMapping::CreateReadOnly(*dir.dir, requested_source);
41 if (!mapping || !mapping->IsValid()) {
42 return nullptr;
43 }
44
45 on_file_included_(fml::paths::JoinPaths({dir.name, requested_source}));
46
47 return mapping;
48 }
49
50 std::unique_ptr<fml::FileMapping> Includer::FindFirstMapping(
51 const char* requested_source) {
52 // Always try the working directory first no matter what the include
53 // directories are.
54 {
55 IncludeDir dir;
56 dir.name = ".";
57 dir.dir = working_directory_;
58 if (auto mapping = TryOpenMapping(dir, requested_source)) {
59 return mapping;
60 }
61 }
62
63 for (const auto& include_dir : include_dirs_) {
64 if (auto mapping = TryOpenMapping(include_dir, requested_source)) {
65 return mapping;
66 }
67 }
68 return nullptr;
69 }
70
71 shaderc_include_result* Includer::GetInclude(const char* requested_source,
72 shaderc_include_type type,
73 const char* requesting_source,
74 size_t include_depth) {
75 auto result = std::make_unique<shaderc_include_result>();
76
77 // Default initialize to failed inclusion.
78 result->source_name = "";
79 result->source_name_length = 0;
80
81 constexpr const char* kFileNotFoundMessage = "Included file not found.";
82 result->content = kFileNotFoundMessage;
83 result->content_length = ::strlen(kFileNotFoundMessage);
84 result->user_data = nullptr;
85
86 if (!working_directory_ || !working_directory_->is_valid()) {
87 return result.release();
88 }
89
90 if (requested_source == nullptr) {
91 return result.release();
92 }
93
94 auto file = FindFirstMapping(requested_source);
95
96 if (!file || file->GetMapping() == nullptr) {
97 return result.release();
98 }
99
100 auto includer_data =
101 std::make_unique<IncluderData>(requested_source, std::move(file));
102
103 result->source_name = includer_data->file_name.c_str();
104 result->source_name_length = includer_data->file_name.length();
105 result->content = reinterpret_cast<decltype(result->content)>(
106 includer_data->mapping->GetMapping());
107 result->content_length = includer_data->mapping->GetSize();
108 result->user_data = includer_data.release();
109
110 return result.release();
111 }
112
113 void Includer::ReleaseInclude(shaderc_include_result* data) {
114 delete reinterpret_cast<IncluderData*>(data->user_data);
115 delete data;
116 }
117
118 } // namespace compiler
119 } // namespace impeller
impeller::compiler::Includer::~Includer
~Includer() override
includer.h
impeller::compiler::IncludeDir::name
std::string name
Definition: include_dir.h:18
impeller::compiler::IncludeDir
Definition: include_dir.h:16
impeller::compiler::Includer::GetInclude
shaderc_include_result * GetInclude(const char *requested_source, shaderc_include_type type, const char *requesting_source, size_t include_depth) override
Definition: includer.cc:71
std
Definition: comparable.h:95
impeller::compiler::Includer::ReleaseInclude
void ReleaseInclude(shaderc_include_result *data) override
Definition: includer.cc:113
impeller::compiler::IncluderData
Definition: includer.h:17
impeller::compiler::Includer::Includer
Includer(std::shared_ptr< fml::UniqueFD > working_directory, std::vector< IncludeDir > include_dirs, std::function< void(std::string)> on_file_included)
Definition: includer.cc:14
impeller::compiler::IncludeDir::dir
std::shared_ptr< fml::UniqueFD > dir
Definition: include_dir.h:17
impeller
Definition: aiks_blur_unittests.cc:20