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.
44 #include "./../glslang/Public/ShaderLang.h"
46 // Default include class for normal include convention of search backward
47 // through the stack of active include paths (for nested includes).
48 // Can be overridden to customize.
49 class DirStackFileIncluder : public glslang::TShader::Includer {
51 DirStackFileIncluder() : externalLocalDirectoryCount(0) { }
53 virtual IncludeResult* includeLocal(const char* headerName,
54 const char* includerName,
55 size_t inclusionDepth) override
57 return readLocalPath(headerName, includerName, inclusionDepth);
60 virtual IncludeResult* includeSystem(const char* headerName,
61 const char* /*includerName*/,
62 size_t /*inclusionDepth*/) override
64 return readSystemPath(headerName);
67 // Externally set directories. E.g., from a command-line -I<dir>.
68 // - Most-recently pushed are checked first.
69 // - All these are checked after the parse-time stack of local directories
71 // - This only applies to the "local" form of #include.
72 // - Makes its own copy of the path.
73 virtual void pushExternalLocalDirectory(const std::string& dir)
75 directoryStack.push_back(dir);
76 externalLocalDirectoryCount = directoryStack.size();
79 virtual void releaseInclude(IncludeResult* result) override
81 if (result != nullptr) {
82 delete [] static_cast<tUserDataElement*>(result->userData);
87 virtual ~DirStackFileIncluder() override { }
90 typedef char tUserDataElement;
91 std::vector<std::string> directoryStack;
92 int externalLocalDirectoryCount;
94 // Search for a valid "local" path based on combining the stack of include
95 // directories and the nominal name of the header.
96 virtual IncludeResult* readLocalPath(const char* headerName, const char* includerName, int depth)
98 // Discard popped include directories, and
99 // initialize when at parse-time first level.
100 directoryStack.resize(depth + externalLocalDirectoryCount);
102 directoryStack.back() = getDirectory(includerName);
104 // Find a directory that works, using a reverse search of the include stack.
105 for (auto it = directoryStack.rbegin(); it != directoryStack.rend(); ++it) {
106 std::string path = *it + '/' + headerName;
107 std::replace(path.begin(), path.end(), '\\', '/');
108 std::ifstream file(path, std::ios_base::binary | std::ios_base::ate);
110 directoryStack.push_back(getDirectory(path));
111 return newIncludeResult(path, file, (int)file.tellg());
118 // Search for a valid <system> path.
119 // Not implemented yet; returning nullptr signals failure to find.
120 virtual IncludeResult* readSystemPath(const char* /*headerName*/) const
125 // Do actual reading of the file, filling in a new include result.
126 virtual IncludeResult* newIncludeResult(const std::string& path, std::ifstream& file, int length) const
128 char* content = new tUserDataElement [length];
129 file.seekg(0, file.beg);
130 file.read(content, length);
131 return new IncludeResult(path, content, length, content);
134 // If no path markers, return current working directory.
135 // Otherwise, strip file name and return path leading up to it.
136 virtual std::string getDirectory(const std::string path) const
138 size_t last = path.find_last_of("/\\");
139 return last == std::string::npos ? "." : path.substr(0, last);