summaryrefslogtreecommitdiffstats
path: root/source/core/slang-test-tool-util.cpp
blob: 9bf404e5eed7ee46c2369a4b65d20ab72f3b813c (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
#include "slang-test-tool-util.h"

#include "../../slang-com-helper.h"

#include "slang-io.h"
#include "slang-string-util.h"

namespace Slang
{

/* static */ToolReturnCode TestToolUtil::getReturnCode(SlangResult res)
{
    switch (res)
    {
        case SLANG_OK:              return ToolReturnCode::Success;
        case SLANG_E_INTERNAL_FAIL: return ToolReturnCode::CompilationFailed;
        case SLANG_FAIL:            return ToolReturnCode::Failed;
        case SLANG_E_NOT_AVAILABLE: return ToolReturnCode::Ignored;
        default:
        {
            return (SLANG_SUCCEEDED(res)) ? ToolReturnCode::Success : ToolReturnCode::Failed;
        }
    }
}

/* static */ToolReturnCode TestToolUtil::getReturnCodeFromInt(int code)
{
    if (code >= int(ToolReturnCodeSpan::First) && code <= int(ToolReturnCodeSpan::Last))
    {
        return ToolReturnCode(code);
    }
    else
    {
        SLANG_ASSERT(!"Invalid integral code");
        return ToolReturnCode::Failed;
    }
}

/* static */SlangResult TestToolUtil::setSessionDefaultPrelude(const char* exePath, slang::IGlobalSession* session)
{
    // Set the prelude to a path
    String canonicalPath;
    if (SLANG_SUCCEEDED(Path::getCanonical(exePath, canonicalPath)))
    {
        // Get the directory
        canonicalPath = Path::getParentDirectory(canonicalPath);

        String path = Path::combine(canonicalPath, "../../../prelude/slang-cpp-prelude.h");
        if (SLANG_SUCCEEDED(Path::getCanonical(path, canonicalPath)))
        {
            // Use forward slashes, to avoid escaping the path
            canonicalPath = StringUtil::calcCharReplaced(canonicalPath, '\\', '/');

            // It must exist!
            if (!File::exists(canonicalPath))
            {
                SLANG_ASSERT(!"Couldn't find the prelude relative to the executable");
                return SLANG_FAIL;
            }

            StringBuilder prelude;
            prelude << "#include \"" << canonicalPath << "\"\n\n";
            const SlangPassThrough downstreamCompilers[] = {
                SLANG_PASS_THROUGH_CLANG,                   ///< Clang C/C++ compiler 
                SLANG_PASS_THROUGH_VISUAL_STUDIO,           ///< Visual studio C/C++ compiler
                SLANG_PASS_THROUGH_GCC,                     ///< GCC C/C++ compiler
                SLANG_PASS_THROUGH_GENERIC_C_CPP,
            };
            for (auto downstreamCompiler : downstreamCompilers)
            {
                session->setDownstreamCompilerPrelude(downstreamCompiler, prelude.getBuffer());
            }
        }
    }

    return SLANG_OK;
}


}