yum-mirror/glslang

git clone https://git.yummers.dev/yum-mirror/glslang

mmmhj2Implement <system> include for the standalone wrapper (#4154)b11b0383

vulkan-sdk-1.4.357.0
6.4 KiB166 linesraw
1//
2// Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
3// Copyright (C) 2017 Google, Inc.
4//
5// All rights reserved.
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions
9// are met:
10//
11//    Redistributions of source code must retain the above copyright
12//    notice, this list of conditions and the following disclaimer.
13//
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.
18//
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.
22//
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.
35//
36
37#pragma once
38
39#include <vector>
40#include <string>
41#include <fstream>
42#include <algorithm>
43#include <set>
44
45#include "./../glslang/Public/ShaderLang.h"
46
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.
50class DirStackFileIncluder : public glslang::TShader::Includer {
51public:
52    DirStackFileIncluder() { }
53
54    virtual IncludeResult* includeLocal(const char* headerName,
55                                        const char* includerName,
56                                        size_t inclusionDepth) override
57    {
58        recordLocalPath(includerName, inclusionDepth);
59        return readLocalPath(headerName);
60    }
61
62    virtual IncludeResult* includeSystem(const char* headerName,
63                                         const char* includerName,
64                                         size_t inclusionDepth) override
65    {
66        recordLocalPath(includerName, inclusionDepth);
67        return readSystemPath(headerName);
68    }
69
70    // Externally set directories. E.g., from a command-line -I<dir>.
71    //  - Most-recently pushed are checked first.
72    //  - All these are checked after the parse-time stack of local directories
73    //    is checked.
74    //  - Makes its own copy of the path.
75    //  - Search paths are processed similar to GCC: "local" inclusions will
76    //    search in the current directory and external directories, while 
77    //    <system> inclusions will search external directories only.
78    virtual void pushExternalDirectory(const std::string& dir)
79    {
80        externalDirectoryList.push_back(dir);
81    }
82
83    virtual void releaseInclude(IncludeResult* result) override
84    {
85        if (result != nullptr) {
86            delete [] static_cast<tUserDataElement*>(result->userData);
87            delete result;
88        }
89    }
90
91    virtual std::set<std::string> getIncludedFiles()
92    {
93        return includedFiles;
94    }
95
96    virtual ~DirStackFileIncluder() override { }
97
98protected:
99    typedef char tUserDataElement;
100    std::vector<std::string> localDirectoryStack;
101    std::vector<std::string> externalDirectoryList;
102    std::set<std::string> includedFiles;
103
104    // Record local path for future local inclusion.
105    // Discard popped include directories, and
106    // initialize when at parse-time first level.
107    void recordLocalPath(const char * includerName, size_t depth)
108    {
109        localDirectoryStack.resize(depth);
110        if (depth == 1)
111            localDirectoryStack.back() = getDirectory(includerName);
112    }
113
114    // Search for a valid "local" path based on combining the stack of include
115    // directories and the nominal name of the header.
116    virtual IncludeResult* readLocalPath(const char* headerName)
117    {
118        // Find a directory that works, using a reverse search of the include stack.
119        for (auto it = localDirectoryStack.rbegin(); it != localDirectoryStack.rend(); ++it) {
120            std::string path = *it + '/' + headerName;
121            std::replace(path.begin(), path.end(), '\\', '/');
122            std::ifstream file(path, std::ios_base::binary | std::ios_base::ate);
123            if (file) {
124                localDirectoryStack.push_back(getDirectory(path));
125                includedFiles.insert(path);
126                return newIncludeResult(path, file, (int)file.tellg());
127            }
128        }
129
130        return nullptr;
131    }
132
133    // Search for a valid <system> path.
134    virtual IncludeResult* readSystemPath(const char* headerName)
135    {
136        // Search for external directories only.
137        for (auto it = externalDirectoryList.begin(); it != externalDirectoryList.end(); ++it) {
138            std::string path = *it + '/' + headerName;
139            std::replace(path.begin(), path.end(), '\\', '/');
140            std::ifstream file(path, std::ios_base::binary | std::ios_base::ate);
141            if (file) {
142                localDirectoryStack.push_back(getDirectory(path));
143                includedFiles.insert(path);
144                return newIncludeResult(path, file, (int)file.tellg());
145            }
146        }
147        return nullptr;
148    }
149
150    // Do actual reading of the file, filling in a new include result.
151    virtual IncludeResult* newIncludeResult(const std::string& path, std::ifstream& file, int length) const
152    {
153        char* content = new tUserDataElement [length];
154        file.seekg(0, file.beg);
155        file.read(content, length);
156        return new IncludeResult(path, content, length, content);
157    }
158
159    // If no path markers, return current working directory.
160    // Otherwise, strip file name and return path leading up to it.
161    virtual std::string getDirectory(const std::string path) const
162    {
163        size_t last = path.find_last_of("/\\");
164        return last == std::string::npos ? "." : path.substr(0, last);
165    }
166};