Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
compiler_test.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
6#include "flutter/fml/paths.h"
8
9#include <algorithm>
10#include <filesystem>
11
12namespace impeller {
13namespace compiler {
14namespace testing {
15
16static std::string GetIntermediatesPath() {
17 auto test_name = flutter::testing::GetCurrentTestName();
18 std::replace(test_name.begin(), test_name.end(), '/', '_');
19 std::replace(test_name.begin(), test_name.end(), '.', '_');
20 std::stringstream dir_name;
21 dir_name << test_name << "_" << std::to_string(fml::GetCurrentProcId());
23 {flutter::testing::GetFixturesPath(), dir_name.str()});
24}
25
27 : intermediates_path_(GetIntermediatesPath()) {
28 intermediates_directory_ =
29 fml::OpenDirectory(intermediates_path_.c_str(),
30 true, // create if necessary
32 FML_CHECK(intermediates_directory_.is_valid());
33}
34
36 intermediates_directory_.reset();
37
38 std::filesystem::remove_all(std::filesystem::path(intermediates_path_));
39}
40
41static std::string ReflectionHeaderName(const char* fixture_name) {
42 std::stringstream stream;
43 stream << fixture_name << ".h";
44 return stream.str();
45}
46
47static std::string ReflectionCCName(const char* fixture_name) {
48 std::stringstream stream;
49 stream << fixture_name << ".cc";
50 return stream.str();
51}
52
53static std::string ReflectionJSONName(const char* fixture_name) {
54 std::stringstream stream;
55 stream << fixture_name << ".json";
56 return stream.str();
57}
58
59static std::string SPIRVFileName(const char* fixture_name) {
60 std::stringstream stream;
61 stream << fixture_name << ".spv";
62 return stream.str();
63}
64
65static std::string SLFileName(const char* fixture_name,
66 TargetPlatform platform) {
67 std::stringstream stream;
68 stream << fixture_name << "." << TargetPlatformSLExtension(platform);
69 return stream.str();
70}
71
72std::unique_ptr<fml::FileMapping> CompilerTestBase::GetReflectionJson(
73 const char* fixture_name) const {
74 auto filename = ReflectionJSONName(fixture_name);
75 auto fd = fml::OpenFileReadOnly(intermediates_directory_, filename.c_str());
77}
78
79std::unique_ptr<fml::FileMapping> CompilerTestBase::GetShaderFile(
80 const char* fixture_name,
81 TargetPlatform platform) const {
82 auto filename = SLFileName(fixture_name, platform);
83 auto fd = fml::OpenFileReadOnly(intermediates_directory_, filename.c_str());
85}
86
88 const char* fixture_name,
89 SourceType source_type,
90 SourceLanguage source_language,
91 const char* entry_point_name) const {
92 std::shared_ptr<fml::Mapping> fixture =
94 if (!fixture || !fixture->GetMapping()) {
95 VALIDATION_LOG << "Could not find shader in fixtures: " << fixture_name;
96 return false;
97 }
98
99 SourceOptions source_options(fixture_name, source_type);
100 source_options.target_platform = GetParam();
101 source_options.source_language = source_language;
102 source_options.working_directory = std::make_shared<fml::UniqueFD>(
105 fixture_name, SourceTypeFromFileName(fixture_name), source_language,
106 entry_point_name);
107
108 Reflector::Options reflector_options;
109 reflector_options.header_file_name = ReflectionHeaderName(fixture_name);
110 reflector_options.shader_name = "shader_name";
111
112 Compiler compiler(fixture, source_options, reflector_options);
113 if (!compiler.IsValid()) {
114 VALIDATION_LOG << "Compilation failed: " << compiler.GetErrorMessages();
115 return false;
116 }
117
118 auto spirv_assembly = compiler.GetSPIRVAssembly();
119 if (!spirv_assembly) {
120 VALIDATION_LOG << "No spirv was generated.";
121 return false;
122 }
123
124 if (!fml::WriteAtomically(intermediates_directory_,
125 SPIRVFileName(fixture_name).c_str(),
126 *spirv_assembly)) {
127 VALIDATION_LOG << "Could not write SPIRV intermediates.";
128 return false;
129 }
130
131 auto sl_source = compiler.GetSLShaderSource();
132 if (!sl_source) {
133 VALIDATION_LOG << "No SL source was generated.";
134 return false;
135 }
136
137 if (!fml::WriteAtomically(intermediates_directory_,
138 SLFileName(fixture_name, GetParam()).c_str(),
139 *sl_source)) {
140 VALIDATION_LOG << "Could not write SL intermediates.";
141 return false;
142 }
143
144 if (TargetPlatformNeedsReflection(GetParam())) {
145 auto reflector = compiler.GetReflector();
146 if (!reflector) {
148 << "No reflector was found for target platform SL compiler.";
149 return false;
150 }
151
152 auto reflection_json = reflector->GetReflectionJSON();
153 auto reflection_header = reflector->GetReflectionHeader();
154 auto reflection_source = reflector->GetReflectionCC();
155
156 if (!reflection_json) {
157 VALIDATION_LOG << "Reflection JSON was not found.";
158 return false;
159 }
160
161 if (!reflection_header) {
162 VALIDATION_LOG << "Reflection header was not found.";
163 return false;
164 }
165
166 if (!reflection_source) {
167 VALIDATION_LOG << "Reflection source was not found.";
168 return false;
169 }
170
171 if (!fml::WriteAtomically(intermediates_directory_,
172 ReflectionHeaderName(fixture_name).c_str(),
173 *reflection_header)) {
174 VALIDATION_LOG << "Could not write reflection header intermediates.";
175 return false;
176 }
177
178 if (!fml::WriteAtomically(intermediates_directory_,
179 ReflectionCCName(fixture_name).c_str(),
180 *reflection_source)) {
181 VALIDATION_LOG << "Could not write reflection CC intermediates.";
182 return false;
183 }
184
185 if (!fml::WriteAtomically(intermediates_directory_,
186 ReflectionJSONName(fixture_name).c_str(),
187 *reflection_json)) {
188 VALIDATION_LOG << "Could not write reflection json intermediates.";
189 return false;
190 }
191 }
192 return true;
193}
194
195} // namespace testing
196} // namespace compiler
197} // namespace impeller
static std::unique_ptr< FileMapping > CreateReadOnly(const std::string &path)
Definition mapping.cc:20
void reset(const T &value=Traits::InvalidValue())
bool is_valid() const
std::shared_ptr< fml::Mapping > GetSPIRVAssembly() const
Definition compiler.cc:486
const Reflector * GetReflector() const
Definition compiler.cc:547
std::shared_ptr< fml::Mapping > GetSLShaderSource() const
Definition compiler.cc:490
std::string GetErrorMessages() const
Definition compiler.cc:504
std::shared_ptr< fml::Mapping > GetReflectionJSON() const
Definition reflector.cc:109
bool CanCompileAndReflect(const char *fixture_name, SourceType source_type=SourceType::kUnknown, SourceLanguage source_language=SourceLanguage::kGLSL, const char *entry_point_name="main") const
std::unique_ptr< fml::FileMapping > GetReflectionJson(const char *fixture_name) const
std::unique_ptr< fml::FileMapping > GetShaderFile(const char *fixture_name, TargetPlatform platform) const
#define FML_CHECK(condition)
Definition logging.h:104
std::string GetCurrentTestName()
Gets the name of the currently running test. This is useful in generating logs or assets based on tes...
Definition testing.cc:14
const char * GetFixturesPath()
Returns the directory containing the test fixture for the target if this target has fixtures configur...
fml::UniqueFD OpenFixturesDirectory()
Opens the fixtures directory for the unit-test harness.
Definition testing.cc:22
std::unique_ptr< fml::Mapping > OpenFixtureAsMapping(const std::string &fixture_name)
Opens a fixture of the given file name and returns a mapping to its contents.
Definition testing.cc:58
std::string JoinPaths(std::initializer_list< std::string > components)
Definition paths.cc:14
fml::UniqueFD OpenFileReadOnly(const fml::UniqueFD &base_directory, const char *path)
Definition file.cc:92
bool WriteAtomically(const fml::UniqueFD &base_directory, const char *file_name, const Mapping &mapping)
fml::UniqueFD OpenDirectory(const char *path, bool create_if_necessary, FilePermission permission)
Definition file_posix.cc:97
int GetCurrentProcId()
static std::string ReflectionCCName(const char *fixture_name)
static std::string ReflectionJSONName(const char *fixture_name)
static std::string GetIntermediatesPath()
static std::string ReflectionHeaderName(const char *fixture_name)
static std::string SLFileName(const char *fixture_name, TargetPlatform platform)
static std::string SPIRVFileName(const char *fixture_name)
std::string TargetPlatformSLExtension(TargetPlatform platform)
Definition types.cc:237
SourceType SourceTypeFromFileName(const std::filesystem::path &file_name)
Definition types.cc:17
std::string EntryPointFunctionNameFromSourceName(const std::filesystem::path &file_name, SourceType type, SourceLanguage source_language, const std::string &entry_point_name)
Definition types.cc:101
bool TargetPlatformNeedsReflection(TargetPlatform platform)
Definition types.cc:130
std::shared_ptr< fml::UniqueFD > working_directory
#define VALIDATION_LOG
Definition validation.h:91