summaryrefslogtreecommitdiffstats
path: root/tools/slang-test/test-context.cpp
blob: 5581ab6db68626c2c4d6d7b47a43ad4a071a9185 (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
// test-context.cpp
#include "test-context.h"

#include "../../source/core/slang-io.h"
#include "../../source/core/slang-string-util.h"

#include <stdio.h>
#include <stdlib.h>

using namespace Slang;

TestContext::TestContext() 
{
    m_session = nullptr;
}

Result TestContext::init()
{
    m_session = spCreateSession(nullptr);
    if (!m_session)
    {
        return SLANG_FAIL;
    }
    return SLANG_OK;
}

TestContext::~TestContext()
{
    for (auto& pair : m_sharedLibTools)
    {
        const auto& tool = pair.Value;
        if (tool.m_sharedLibrary)
        {
            SharedLibrary::unload(tool.m_sharedLibrary);
        }
    }

    if (m_session)
    {
        spDestroySession(m_session);
    }
}

TestContext::InnerMainFunc TestContext::getInnerMainFunc(const String& dirPath, const String& name)
{
    {
        SharedLibraryTool* tool = m_sharedLibTools.TryGetValue(name);
        if (tool)
        {
            return tool->m_func;
        }
    }

    StringBuilder sharedLibToolBuilder;
    sharedLibToolBuilder.append(name);
    sharedLibToolBuilder.append("-tool");

    StringBuilder builder;
    SharedLibrary::appendPlatformFileName(sharedLibToolBuilder.getUnownedSlice(), builder);
    String path = Path::combine(dirPath, builder);

    SharedLibraryTool tool = {};

    if (SLANG_SUCCEEDED(SharedLibrary::loadWithPlatformPath(path.begin(), tool.m_sharedLibrary)))
    {
        tool.m_func = (InnerMainFunc)SharedLibrary::findFuncByName(tool.m_sharedLibrary, "innerMain");
    }

    m_sharedLibTools.Add(name, tool);
    return tool.m_func;
}

void TestContext::setInnerMainFunc(const String& name, InnerMainFunc func)
{
    SharedLibraryTool* tool = m_sharedLibTools.TryGetValue(name);
    if (tool)
    {
        if (tool->m_sharedLibrary)
        {
            SharedLibrary::unload(tool->m_sharedLibrary);
            tool->m_sharedLibrary = nullptr;
        }

        tool->m_func = func;
    }
    else
    {
        SharedLibraryTool tool = {};
        tool.m_func = func;
        m_sharedLibTools.Add(name, tool);
    }
}

CPPCompilerSet* TestContext::getCPPCompilerSet()
{
    if (!cppCompilerSet)
    {
        cppCompilerSet = new CPPCompilerSet;

        CPPCompilerUtil::InitializeSetDesc desc;
        CPPCompilerUtil::initializeSet(desc, cppCompilerSet);
    }
    return cppCompilerSet;
}