yum-mirror/slang

Making it easier to work with shaders

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

Ellie HermaszewskaMove switch statement bodies to their own lines (#5493)b118451e3

master
5.8 KiB200 linesraw
1
2#include "slang-test-tool-util.h"
3
4#include "slang-com-helper.h"
5#include "slang-io.h"
6#include "slang-string-util.h"
7
8namespace Slang
9{
10
11/* static */ ToolReturnCode TestToolUtil::getReturnCode(SlangResult res)
12{
13    switch (res)
14    {
15    case SLANG_OK:
16        return ToolReturnCode::Success;
17    case SLANG_E_INTERNAL_FAIL:
18        return ToolReturnCode::CompilationFailed;
19    case SLANG_FAIL:
20        return ToolReturnCode::Failed;
21    case SLANG_E_NOT_AVAILABLE:
22        return ToolReturnCode::Ignored;
23    default:
24        {
25            return (SLANG_SUCCEEDED(res)) ? ToolReturnCode::Success : ToolReturnCode::Failed;
26        }
27    }
28}
29
30/* static */ ToolReturnCode TestToolUtil::getReturnCodeFromInt(int code)
31{
32    if (code >= int(ToolReturnCodeSpan::First) && code <= int(ToolReturnCodeSpan::Last))
33    {
34        return ToolReturnCode(code);
35    }
36    else
37    {
38        SLANG_ASSERT(!"Invalid integral code");
39        return ToolReturnCode::Failed;
40    }
41}
42
43/* static */ bool TestToolUtil::hasDeferredCoreModule(Index argc, const char* const* argv)
44{
45    for (Index i = 0; i < argc; ++i)
46    {
47        UnownedStringSlice option(argv[i]);
48        if (option == "-load-core-module" || option == "-compile-core-module")
49        {
50            return true;
51        }
52    }
53    return false;
54}
55
56/* static */ SlangResult TestToolUtil::getIncludePath(
57    const String& parentPath,
58    const char* path,
59    String& outIncludePath)
60{
61    String includePath;
62    SLANG_RETURN_ON_FAIL(Path::getCanonical(Path::combine(parentPath, path), includePath));
63
64    // Use forward slashes, to avoid escaping the path
65    includePath = StringUtil::calcCharReplaced(includePath, '\\', '/');
66
67    // It must exist!
68    if (!File::exists(includePath))
69    {
70        return SLANG_FAIL;
71    }
72
73    outIncludePath = includePath;
74    return SLANG_OK;
75}
76
77static SlangResult _addCPPPrelude(const String& rootPath, slang::IGlobalSession* session)
78{
79    String includePath;
80    SlangResult res = SLANG_FAIL;
81    if (SLANG_FAILED(res))
82        res = TestToolUtil::getIncludePath(
83            Path::combine(rootPath, "include"),
84            "slang-cpp-prelude.h",
85            includePath);
86    if (SLANG_FAILED(res))
87        res = TestToolUtil::getIncludePath(rootPath, "prelude/slang-cpp-prelude.h", includePath);
88    SLANG_RETURN_ON_FAIL(res);
89    StringBuilder prelude;
90    prelude << "#include \"" << includePath << "\"\n\n";
91    session->setLanguagePrelude(SLANG_SOURCE_LANGUAGE_CPP, prelude.getBuffer());
92    return SLANG_OK;
93}
94
95static SlangResult _addCUDAPrelude(const String& rootPath, slang::IGlobalSession* session)
96{
97    String includePath;
98    SlangResult res = SLANG_FAIL;
99    if (SLANG_FAILED(res))
100        res = TestToolUtil::getIncludePath(
101            Path::combine(rootPath, "include"),
102            "slang-cuda-prelude.h",
103            includePath);
104    if (SLANG_FAILED(res))
105        res = TestToolUtil::getIncludePath(rootPath, "prelude/slang-cuda-prelude.h", includePath);
106    SLANG_RETURN_ON_FAIL(res);
107    StringBuilder prelude;
108    prelude << "#include \"" << includePath << "\"\n\n";
109    session->setLanguagePrelude(SLANG_SOURCE_LANGUAGE_CUDA, prelude.getBuffer());
110    return SLANG_OK;
111}
112
113/* static */ SlangResult TestToolUtil::getExeDirectoryPath(
114    const char* exePath,
115    String& outExeDirectoryPath)
116{
117    String canonicalPath;
118    SLANG_RETURN_ON_FAIL(Path::getCanonical(exePath, canonicalPath));
119    // Get the directory
120    outExeDirectoryPath = Path::getParentDirectory(canonicalPath);
121    return SLANG_OK;
122}
123
124/* static */ SlangResult TestToolUtil::getDllDirectoryPath(
125    const char* exePath,
126    String& outDllDirectoryPath)
127{
128    String canonicalPath;
129    SLANG_RETURN_ON_FAIL(Path::getCanonical(exePath, canonicalPath));
130
131    // Get the directory
132    String binPath = Path::getParentDirectory(canonicalPath);
133
134    // Windows puts the dlls in the same directory as the exe, while on other platforms they are in
135    // a 'lib' directory
136#ifdef _WIN32
137    outDllDirectoryPath = binPath;
138#else
139    String binaryRootPath = Path::getParentDirectory(binPath);
140    outDllDirectoryPath = Path::combine(binaryRootPath, "lib");
141#endif
142    return SLANG_OK;
143}
144
145/* static */ SlangResult TestToolUtil::getRootPath(const char* inExePath, String& outExePath)
146{
147    // Get the directory holding the exe
148    String parentPath;
149    SLANG_RETURN_ON_FAIL(getExeDirectoryPath(inExePath, parentPath));
150
151    // Work out the relative path to the root, we will search upwards until we
152    // find a directory containing 'prelude/slang-cpp-prelude.h'
153    String rootRelPath;
154    SLANG_RETURN_ON_FAIL(Path::getCanonical(parentPath, rootRelPath));
155    do
156    {
157        if (File::exists(Path::combine(rootRelPath, "include/slang-cpp-prelude.h")))
158            break;
159        if (File::exists(Path::combine(rootRelPath, "prelude/slang-cpp-prelude.h")))
160            break;
161
162        rootRelPath = Path::getParentDirectory(rootRelPath);
163        if (rootRelPath == "")
164            return SLANG_E_NOT_AVAILABLE;
165    } while (1);
166
167    outExePath = std::move(rootRelPath);
168    return SLANG_OK;
169}
170
171/* static */ SlangResult TestToolUtil::setSessionDefaultPreludeFromExePath(
172    const char* inExePath,
173    slang::IGlobalSession* session)
174{
175    String rootPath;
176    SLANG_RETURN_ON_FAIL(getRootPath(inExePath, rootPath));
177    SLANG_RETURN_ON_FAIL(setSessionDefaultPreludeFromRootPath(rootPath, session));
178    return SLANG_OK;
179}
180
181/* static */ SlangResult TestToolUtil::setSessionDefaultPreludeFromRootPath(
182    const String& rootPath,
183    slang::IGlobalSession* session)
184{
185    // Set the prelude to a path
186
187    if (SLANG_FAILED(_addCPPPrelude(rootPath, session)))
188    {
189        SLANG_ASSERT(!"Couldn't find the C++ prelude relative to the executable");
190    }
191
192    if (SLANG_FAILED(_addCUDAPrelude(rootPath, session)))
193    {
194        SLANG_ASSERT(!"Couldn't find the CUDA prelude relative to the executable");
195    }
196
197    return SLANG_OK;
198}
199
200} // namespace Slang