yum-mirror/slang

Making it easier to work with shaders

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

Jay KwakRemove unused variable in slangc::main (#8325)288a63dea

master
5.0 KiB169 linesraw
1// main.cpp
2
3#include "slang.h"
4
5SLANG_API void spSetCommandLineCompilerMode(SlangCompileRequest* request);
6
7#include "../core/slang-io.h"
8#include "../core/slang-test-tool-util.h"
9#include "../slang/slang-internal.h"
10
11using namespace Slang;
12
13#include <assert.h>
14
15#ifdef _WIN32
16#define MAIN slangc_main
17#else
18#define MAIN main
19#endif
20
21static void _diagnosticCallback(char const* message, void* /*userData*/)
22{
23    auto stdError = StdWriters::getError();
24    stdError.put(message);
25    stdError.flush();
26}
27
28static SlangResult _compile(SlangCompileRequest* compileRequest, int argc, const char* const* argv)
29{
30    spSetDiagnosticCallback(compileRequest, &_diagnosticCallback, nullptr);
31    spSetCommandLineCompilerMode(compileRequest);
32
33    SlangResult res = spProcessCommandLineArguments(compileRequest, &argv[1], argc - 1);
34    if (SLANG_FAILED(res))
35    {
36        // TODO: print usage message
37        return res;
38    }
39
40    res = SLANG_OK;
41
42#ifndef _DEBUG
43    try
44#endif
45    {
46        // Run the compiler (this will produce any diagnostics through
47        // SLANG_WRITER_TARGET_TYPE_DIAGNOSTIC).
48        res = spCompile(compileRequest);
49        // If the compilation failed, then get out of here...
50        // Turn into an internal Result -> such that return code can be used to vary result to match
51        // previous behavior
52        res = SLANG_FAILED(res) ? SLANG_E_INTERNAL_FAIL : res;
53    }
54#ifndef _DEBUG
55    catch (const Exception& e)
56    {
57        StdWriters::getOut().print("internal compiler error: %S\n", e.Message.toWString().begin());
58        res = SLANG_FAIL;
59    }
60#endif
61
62    return res;
63}
64
65bool shouldEmbedPrelude(const char* const* argv, int argc)
66{
67    for (int i = 0; i < argc; i++)
68    {
69        if (UnownedStringSlice(argv[i]) == "-embed-prelude")
70            return true;
71    }
72    return false;
73}
74
75SLANG_TEST_TOOL_API SlangResult innerMain(
76    StdWriters* stdWriters,
77    slang::IGlobalSession* sharedSession,
78    int argc,
79    const char* const* argv)
80{
81    StdWriters::setSingleton(stdWriters);
82
83    // Assume we will used the shared session
84    ComPtr<slang::IGlobalSession> session(sharedSession);
85
86    // The sharedSession always has a pre-loaded core module, is sharedSession is not nullptr.
87    // This differed test checks if the command line has an option to setup the core module.
88    // If so we *don't* use the sharedSession, and create a new session without the core module just
89    // for this compilation.
90    if (TestToolUtil::hasDeferredCoreModule(Index(argc - 1), argv + 1))
91    {
92        SLANG_RETURN_ON_FAIL(
93            slang_createGlobalSessionWithoutCoreModule(SLANG_API_VERSION, session.writeRef()));
94    }
95    else if (!session)
96    {
97        // Just create the global session in the regular way if there isn't one set
98        SlangGlobalSessionDesc desc = {};
99        desc.enableGLSL = true;
100        Slang::GlobalSessionInternalDesc internalDesc = {};
101#ifdef SLANG_BOOTSTRAP
102        internalDesc.isBootstrap = true;
103#endif
104        SLANG_RETURN_ON_FAIL(
105            slang_createGlobalSessionImpl(&desc, &internalDesc, session.writeRef()));
106    }
107
108    if (!shouldEmbedPrelude(argv, argc))
109        TestToolUtil::setSessionDefaultPreludeFromExePath(argv[0], session);
110
111    SlangCompileRequest* compileRequest = spCreateCompileRequest(session);
112    compileRequest->addSearchPath(Path::getParentDirectory(Path::getExecutablePath()).getBuffer());
113    SlangResult res = _compile(compileRequest, argc, argv);
114    // Now that we are done, clean up after ourselves
115    spDestroyCompileRequest(compileRequest);
116
117    return res;
118}
119
120int MAIN(int argc, char** argv)
121{
122    auto stdWriters = StdWriters::initDefaultSingleton();
123    SlangResult res = innerMain(stdWriters, nullptr, argc, argv);
124    slang::shutdown();
125    return (int)TestToolUtil::getReturnCode(res);
126}
127
128#ifdef _WIN32
129int wmain(int argc, wchar_t** argv)
130{
131    int result = 0;
132
133    {
134        // Convert the wide-character Unicode arguments to UTF-8,
135        // since that is what Slang expects on the API side.
136
137        List<String> args;
138        for (int ii = 0; ii < argc; ++ii)
139        {
140            args.add(String::fromWString(argv[ii]));
141        }
142        List<char const*> argBuffers;
143        for (int ii = 0; ii < argc; ++ii)
144        {
145            argBuffers.add(args[ii].getBuffer());
146        }
147
148        result = MAIN(argc, (char**)&argBuffers[0]);
149    }
150
151#ifdef _MSC_VER
152    // _CrtXXX functions are functional only for debug build. The spec says,
153    // "When _DEBUG isn't defined, calls to _CrtSetReportMode are removed
154    // during preprocessing."
155    _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
156    _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
157    _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
158    _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
159    _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE | _CRTDBG_MODE_DEBUG);
160    _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
161
162    int memleakDetected = _CrtDumpMemoryLeaks();
163    SLANG_UNUSED(memleakDetected);
164    assert(!memleakDetected);
165#endif
166
167    return result;
168}
169#endif