2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2017 Google, Inc.
5 // All rights reserved.
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
14 // Redistributions in binary form must reproduce the above
15 // copyright notice, this list of conditions and the following
16 // disclaimer in the documentation and/or other materials provided
17 // with the distribution.
19 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
20 // contributors may be used to endorse or promote products derived
21 // from this software without specific prior written permission.
23 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 // POSSIBILITY OF SUCH DAMAGE.
45 #include "./../glslang/Public/ShaderLang.h"
47 // Default include class for normal include convention of search backward
48 // through the stack of active include paths (for nested includes).
49 // Can be overridden to customize.
50 class DirStackFileIncluder : public glslang::TShader::Includer {
52 DirStackFileIncluder() : externalLocalDirectoryCount(0) { }
54 virtual IncludeResult* includeLocal(const char* headerName,
55 const char* includerName,
56 size_t inclusionDepth) override
58 return readLocalPath(headerName, includerName, (int)inclusionDepth);
61 virtual IncludeResult* includeSystem(const char* headerName,
62 const char* /*includerName*/,
63 size_t /*inclusionDepth*/) override
65 return readSystemPath(headerName);
68 // Externally set directories. E.g., from a command-line -I<dir>.
69 // - Most-recently pushed are checked first.
70 // - All these are checked after the parse-time stack of local directories
72 // - This only applies to the "local" form of #include.
73 // - Makes its own copy of the path.
74 virtual void pushExternalLocalDirectory(const std::string& dir)
76 directoryStack.push_back(dir);
77 externalLocalDirectoryCount = (int)directoryStack.size();
80 virtual void releaseInclude(IncludeResult* result) override
82 if (result != nullptr) {
83 delete [] static_cast<tUserDataElement*>(result->userData);
88 virtual std::set<std::string> getIncludedFiles()
93 virtual ~DirStackFileIncluder() override { }
96 typedef char tUserDataElement;
97 std::vector<std::string> directoryStack;
98 int externalLocalDirectoryCount;
99 std::set<std::string> includedFiles;
101 // Search for a valid "local" path based on combining the stack of include
102 // directories and the nominal name of the header.
103 virtual IncludeResult* readLocalPath(const char* headerName, const char* includerName, int depth)
105 // Discard popped include directories, and
106 // initialize when at parse-time first level.
107 directoryStack.resize(depth + externalLocalDirectoryCount);
109 directoryStack.back() = getDirectory(includerName);
111 // Find a directory that works, using a reverse search of the include stack.
112 for (auto it = directoryStack.rbegin(); it != directoryStack.rend(); ++it) {
113 std::string path = *it + '/' + headerName;
114 std::replace(path.begin(), path.end(), '\\', '/');
115 std::ifstream file(path, std::ios_base::binary | std::ios_base::ate);
117 directoryStack.push_back(getDirectory(path));
118 includedFiles.insert(path);
119 return newIncludeResult(path, file, (int)file.tellg());
126 // Search for a valid <system> path.
127 // Not implemented yet; returning nullptr signals failure to find.
128 virtual IncludeResult* readSystemPath(const char* /*headerName*/) const
133 // Do actual reading of the file, filling in a new include result.
134 virtual IncludeResult* newIncludeResult(const std::string& path, std::ifstream& file, int length) const
136 char* content = new tUserDataElement [length];
137 file.seekg(0, file.beg);
138 file.read(content, length);
139 return new IncludeResult(path, content, length, content);
142 // If no path markers, return current working directory.
143 // Otherwise, strip file name and return path leading up to it.
144 virtual std::string getDirectory(const std::string path) const
146 size_t last = path.find_last_of("/\\");
147 return last == std::string::npos ? "." : path.substr(0, last);