summaryrefslogtreecommitdiffstats
path: root/tools/slang-test/test-context.h
blob: b42ac1ec94fa407b327c29e49b06eec9bceaaf8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// test-context.h

#ifndef TEST_CONTEXT_H_INCLUDED
#define TEST_CONTEXT_H_INCLUDED

#include "../../source/core/slang-string-util.h"
#include "../../source/core/slang-platform.h"
#include "../../source/core/slang-std-writers.h"
#include "../../source/core/slang-dictionary.h"
#include "../../source/core/slang-test-tool-util.h"
#include "../../source/core/slang-render-api-util.h"

#include "../../source/compiler-core/slang-downstream-compiler.h"
#include "../../source/compiler-core/slang-downstream-compiler-util.h"

#include "../../source/compiler-core/slang-json-rpc-connection.h"

#include "slang-com-ptr.h"

#include "filecheck.h"

#include "options.h"

#include <mutex>

typedef uint32_t PassThroughFlags;
struct PassThroughFlag
{
    enum Enum : PassThroughFlags
    {
        Dxc = 1 << int(SLANG_PASS_THROUGH_DXC),
        Fxc = 1 << int(SLANG_PASS_THROUGH_FXC),
        Glslang = 1 << int(SLANG_PASS_THROUGH_GLSLANG),
        VisualStudio = 1 << int(SLANG_PASS_THROUGH_VISUAL_STUDIO),
        GCC = 1 << int(SLANG_PASS_THROUGH_GCC),
        Clang = 1 << int(SLANG_PASS_THROUGH_CLANG),
        Generic_C_CPP = 1 << int(SLANG_PASS_THROUGH_GENERIC_C_CPP),
        NVRTC = 1 << int(SLANG_PASS_THROUGH_NVRTC),
        LLVM = 1 << int(SLANG_PASS_THROUGH_LLVM),
        Metal = 1 << int(SLANG_PASS_THROUGH_METAL)
    };
};

/// Structure that describes requirements needs to run - such as rendering APIs or
/// back-end availability 
struct TestRequirements
{
    
    TestRequirements& addUsedRenderApi(Slang::RenderApiType type)
    {
        using namespace Slang;
        if (type != RenderApiType::Unknown)
        {
            usedRenderApiFlags |=RenderApiFlags(1) << int(type);
        }
        return *this;
    }
    TestRequirements& addUsedBackEnd(SlangPassThrough type)
    {
        if (type != SLANG_PASS_THROUGH_NONE)
        {
            usedBackendFlags |= PassThroughFlags(1) << int(type);
        }
        return *this;
    }
    TestRequirements& addUsedBackends(PassThroughFlags flags)
    {
        usedBackendFlags |= flags;
        return *this;
    }
    TestRequirements& addUsedRenderApis(Slang::RenderApiFlags flags)
    {
        usedRenderApiFlags |= flags;
        return *this;
    }
        /// True if has this render api as used
    bool isUsed(Slang::RenderApiType apiType) const
    {
        return (apiType != Slang::RenderApiType::Unknown) && ((usedRenderApiFlags & (Slang::RenderApiFlags(1) << int(apiType))) != 0);
    }

    Slang::RenderApiType explicitRenderApi = Slang::RenderApiType::Unknown;     ///< The render api explicitly specified 
    PassThroughFlags usedBackendFlags = 0;                                          ///< Used backends
    Slang::RenderApiFlags usedRenderApiFlags = 0;                               ///< Used render api flags (some might be implied)
};

class TestContext
{
    public:

    typedef Slang::TestToolUtil::InnerMainFunc InnerMainFunc;

        /// Get the slang session
    SlangSession* getSession() const { return m_session;  }

    SlangResult init(const char* exePath);

        /// Get the inner main function (from shared library)
    InnerMainFunc getInnerMainFunc(const Slang::String& dirPath, const Slang::String& name);
        /// Set the function for the shared library
    void setInnerMainFunc(const Slang::String& name, InnerMainFunc func);

    void setTestRequirements(TestRequirements* req);

    TestRequirements* getTestRequirements() const;

        /// If true tests aren't being run just the information on testing is being accumulated
    bool isCollectingRequirements() const { return getTestRequirements() != nullptr; }
        /// If set, then tests are executed
    bool isExecuting() const { return getTestRequirements() == nullptr; }

        /// True if a render API filter is enabled
    bool isRenderApiFilterEnabled() const { return options.enabledApis != Slang::RenderApiFlag::AllOf && options.enabledApis != 0; }

        /// True if a test with the requiredFlags can in principal run (it may not be possible if the API is not available though)
    bool canRunTestWithRenderApiFlags(Slang::RenderApiFlags requiredFlags);

        /// True if can run unit tests
    bool canRunUnitTests() const { return options.apiOnly == false; }

        /// Given a spawn type, return the final spawn type.
        /// In particular we want 'Default' spawn type to vary by the environment (for example running on test server on CI)
    SpawnType getFinalSpawnType(SpawnType spawnType);

    SpawnType getFinalSpawnType();

        /// Get compiler set
    Slang::DownstreamCompilerSet* getCompilerSet();
    Slang::IDownstreamCompiler* getDefaultCompiler(SlangSourceLanguage sourceLanguage);

    Slang::JSONRPCConnection* getOrCreateJSONRPCConnection();
    void destroyRPCConnection();

        /// Ctor
    TestContext();
        /// Dtor
    ~TestContext();

    Options options;
    TestCategorySet categorySet;

        /// If set then tests are not run, but their requirements are set 

    PassThroughFlags availableBackendFlags = 0;
    Slang::RenderApiFlags availableRenderApiFlags = 0;
    bool isAvailableRenderApiFlagsValid = false;

    Slang::RefPtr<Slang::DownstreamCompilerSet> compilerSet;

    Slang::String exeDirectoryPath;
    Slang::String exePath;

        /// Timeout time for communication over connection.
        /// NOTE! If the timeout is hit, the connection will be destroyed, and then recreated.
        /// For tests that compile the stdlib, if that takes this time, the stdlib will be
        /// repeatedly compiled and each time fail.
        /// NOTE! This timeout may be altered in the ctor for a specific target, the initializatoin
        /// value is just the default.
        ///
        /// TODO(JS): We could split the stdlib compilation from other actions, and have timeout specific for
        /// that. To do this we could have a 'compileStdLib' RPC method.
        ///
        /// Current default is 60 seconds.
    Slang::Int connectionTimeOutInMs = 60 * 1000;

    void setThreadIndex(int index);
    void setMaxTestRunnerThreadCount(int count);

    void setTestReporter(TestReporter* reporter);
    TestReporter* getTestReporter();
    SlangResult createLanguageServerJSONRPCConnection(Slang::RefPtr<Slang::JSONRPCConnection>& out);

    std::mutex mutex;
    Slang::RefPtr<Slang::JSONRPCConnection> m_languageServerConnection;

    Slang::IFileCheck* getFileCheck() { return m_fileCheck; };

protected:
    SlangResult _createJSONRPCConnection(Slang::RefPtr<Slang::JSONRPCConnection>& out);

    SlangResult locateFileCheck();

    struct SharedLibraryTool
    {
        Slang::ComPtr<ISlangSharedLibrary> m_sharedLibrary;
        InnerMainFunc m_func;
    };

    Slang::List<Slang::RefPtr<Slang::JSONRPCConnection>> m_jsonRpcConnections;
    Slang::List<TestReporter*> m_reporters;
    Slang::List<TestRequirements*> m_testRequirements = nullptr;
    
    SlangSession* m_session;

    Slang::Dictionary<Slang::String, SharedLibraryTool> m_sharedLibTools;

    Slang::ComPtr<ISlangSharedLibrary> m_fileCheckLibrary;
    Slang::ComPtr<Slang::IFileCheck> m_fileCheck;
};

#endif // TEST_CONTEXT_H_INCLUDED