// Copyright 2015 The Shaderc Authors. All rights reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314#ifndef GLSLC_DEPENDENCY_INFO_H15#define GLSLC_DEPENDENCY_INFO_H1617#include <unordered_set>18#include <string>19#include <string>2021namespace glslc {2223// An object to handle everything about dumping dependency info. Internally it24// has two valid dumping mode: 1) Dump to extra dependency info files, such as25// *.d files. This mode is used when we want to generate dependency info and26// also compile. 2) Overwrite the original compilation output and dump27// dependency info as compilation output. This mode is used when we do not want28// to compile the source code and want the dependency info only.29class DependencyInfoDumpingHandler {30public:31DependencyInfoDumpingHandler();3233// Sets the dependency target explicitly. It's the same as the argument to34// -MT.35void SetTarget(const std::string& target_label) {36user_specified_dep_target_label_ = target_label;37}3839// Sets the name of the file where dependency info will be written.40void SetDependencyFileName(const std::string& dep_file_name) {41user_specified_dep_file_name_ = dep_file_name;42}4344// Dump depdendency info to a) an extra dependency info file, b) an string45// which holds the compilation output. The choice depends on the dump46// mode of the handler. Returns true if dumping is succeeded, false otherwise.47//48// The dependency file name and target are deduced based on 1) user49// specified dependency file name and target name, 2) the output filename when50// the compiler is in 'does not need linking' and 'not preprocessing-only'51// mode. It is passed through compilation_output_file_name.52//53// When the handler is set to dump dependency info as extra dependency info54// files, this method will open a file with the dependency file name and write55// the dependency info to it. Error messages caused by writing to the file are56// emitted to stderr.57//58// When the handler is set to dump dependency info as compilation output, the59// compilation output string, which is passed through compilation_output_ptr,60// will be cleared and this method will write dependency info to it. Then the61// dependency info should be emitted as normal compilation output.62//63// If the dump mode is not set when this method is called, return false.64bool DumpDependencyInfo(std::string compilation_output_file_name,65std::string source_file_name,66std::string* compilation_output_ptr,67const std::unordered_set<std::string>& dependent_files);6869// Sets to always dump dependency info as an extra file, instead of the normal70// compilation output. This means the output name specified by -o options71// won't be used for the dependency info file.72void SetDumpToExtraDependencyInfoFiles() { mode_ = dump_as_extra_file; }7374// Sets to dump dependency info as normal compilation output. The dependency75// info will be either saved in a file with -o option specified file, or, if76// no output file name specified, to stdout.77void SetDumpAsNormalCompilationOutput() {78mode_ = dump_as_compilation_output;79}8081// Returns true if the handler's dumping mode is set to dump dependency info82// as extra dependency info files.83bool DumpingToExtraDependencyInfoFiles() {84return mode_ == dump_as_extra_file;85}8687// Returns true if the handler's dumping mode is set to dump dependency info88// as normal compilation output.89bool DumpingAsCompilationOutput() {90return mode_ == dump_as_compilation_output;91}9293// Returns true if the handler's dumping mode is not set.94bool DumpingModeNotSet() { return mode_ == not_set; }9596// Returns true if the handler is at valid state for dumping dependency info.97bool IsValid(std::string* error_msg_ptr, size_t num_files);9899private:100typedef enum {101// not_set mode tells that the dumping mode is not set yet, so the handler102// is not ready for dumping dependency info. Calling DumpDependencyInfo when103// the handler is in this mode will cause failure.104not_set = 0,105// Dumping dependency info as normal compilation output mode. In this mode,106// the dependency info will be dumped as compilation output by overwriting107// the string which holds the compilation output.108dump_as_compilation_output,109// Dumping dependency info as extra dependency info files mode. In this110// mode, dependency info will be dumped to a user specified dependency info111// file or a *.d file. Compilation output will still be generated along with112// the dependency info.113dump_as_extra_file,114} dump_mode;115116// Returns the target file label to be used in depdendency info file. If -MT117// defined a label, use that string as the label. Otherwise returns the118// compilation output filename deduced in 'doesn't need linking' and 'not119// preprocessing-only' mode.120std::string GetTarget(const std::string& compilation_output_file_name);121122// Returns the dependency file name to be used. If -MF defined a file name123// before, use it. Othwise, returns a filename formed by appending .d to the124// output filename deduced in 'doesn't need linking' and 'no125// preprocessing-only' mode.126std::string GetDependencyFileName(127const std::string& compilation_output_file_name);128129std::string user_specified_dep_file_name_;130std::string user_specified_dep_target_label_;131dump_mode mode_;132};133}134135#endif // GLSLC_DEPENDENCY_INFO_H136137138