38 std::vector<std::string> &visited_files,
39 const std::string &file_buffer,
40 const std::string &file_name)
42 Language language = language_from_filename(file_name);
51 for (
const auto &
filename : file_list) {
52 if (
filename.find(dep) != std::string::npos) {
58 std::cout <<
"Error: Included file not found " << dep << std::endl;
61 else if (std::find(visited_files.begin(), visited_files.end(), file) == visited_files.end()) {
62 visited_files.emplace_back(file);
64 std::ifstream input_file(file);
66 std::cerr <<
"Error: Could not open file " << file << std::endl;
70 std::stringstream buffer;
71 buffer << input_file.rdbuf();
74 file_list, visited_files, buffer.str(), file);
93int main(
int argc,
char **argv)
97 std::cerr <<
"Usage: shader_tool <data_file_from> <data_file_to> <metadata_file_to> "
98 "<infos_file_to> <dep_file_to> <include_dir1> <include_dir2> ..."
103 const char *input_file_name = argv[1];
104 const char *output_file_name = argv[2];
105 const char *metadata_file_name = argv[3];
106 const char *infos_file_name = argv[4];
107 const char *dep_file_name = argv[5];
110 std::ifstream input_file(input_file_name);
112 std::cerr <<
"Error: Could not open input file " << input_file_name << std::endl;
120 std::filesystem::path parent_dir = std::filesystem::path(output_file_name).parent_path();
122 if (!std::filesystem::create_directories(parent_dir, ec)) {
124 std::cerr <<
"Unable to create " << parent_dir <<
" : " << ec.message() << std::endl;
130 std::ofstream output_file(output_file_name, std::ofstream::out | std::ofstream::binary);
132 std::cerr <<
"Error: Could not open output file " << output_file_name << std::endl;
138 std::ofstream metadata_file(metadata_file_name, std::ofstream::out | std::ofstream::binary);
139 if (!metadata_file) {
140 std::cerr <<
"Error: Could not open output file " << metadata_file_name << std::endl;
146 std::ofstream infos_file(infos_file_name, std::ofstream::out | std::ofstream::binary);
148 std::cerr <<
"Error: Could not open output file " << infos_file_name << std::endl;
154 std::ofstream dep_file(dep_file_name, std::ofstream::out | std::ofstream::binary);
156 std::cerr <<
"Error: Could not open output file " << dep_file_name << std::endl;
162 std::vector<std::string> file_list;
163 for (
int i = 6; i < argc; i++) {
166 file_list.insert(file_list.end(),
list.begin(),
list.end());
169 std::stringstream buffer;
170 buffer << input_file.rdbuf();
172 std::string
filename(input_file_name);
173 const bool is_info =
filename.ends_with(
"infos.hh") ||
filename.ends_with(
".bsl.hh");
187 filename.ends_with(
"gpu_shader_material_radial_tiling.bsl.hh") ||
188 filename.ends_with(
"gpu_shader_material_tex_voronoi.bsl.hh") ||
190 (
filename.find(
"gpu/shaders/") == std::string::npos &&
191 filename.find(
"draw/intern/shaders/") == std::string::npos &&
192 filename.find(
"workbench/shaders/") == std::string::npos &&
193 filename.find(
"overlay/shaders/") == std::string::npos &&
194 filename.find(
"select/shaders/") == std::string::npos &&
195 filename.find(
"eevee/shaders/") == std::string::npos &&
196 filename.find(
"gpencil/shaders/") == std::string::npos))
203 std::vector<std::string> visited_files{input_file_name};
208 SourceProcessor processor(buffer.str(), input_file_name, language, file_list);
214 size_t last_slash =
filename.find_last_of(
'/');
215 std::string name = (last_slash == std::string::npos) ?
filename :
217 std::string metadata_function_name =
"metadata_" + name +
"_tmp";
218 std::ranges::replace(metadata_function_name,
'.',
'_');
220 metadata_file <<
metadata.serialize(metadata_function_name);
222 infos_file <<
metadata.serialize_infos();
224 if (!
metadata.visited_files.empty()) {
225 visited_files =
metadata.visited_files;
229 auto escape_path = [](std::string filepath) {
231 while ((pos = filepath.find(
' ', pos)) != std::string::npos) {
232 filepath.replace(pos, 1,
"\\ ");
238 dep_file << output_file_name <<
" : ";
239 for (
const auto &file : visited_files) {
240 dep_file << escape_path(file) <<
" ";
246 metadata_file.close();
251 std::cerr << error.value().full_report << std::endl;
254 return error.has_value() ? 1 : 0;