File: os_compiler_cache_helper.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (121 lines) | stat: -rw-r--r-- 3,193 bytes parent folder | download
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
/*
 * Copyright (C) 2023-2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "shared/source/compiler_interface/os_compiler_cache_helper.h"

#include "shared/source/helpers/path.h"
#include "shared/source/os_interface/windows/sys_calls.h"
#include "shared/source/utilities/debug_settings_reader.h"

#include <ShlObj.h>
#include <algorithm>
#include <wchar.h>

namespace NEO {
std::string getKnownFolderPath(REFKNOWNFOLDERID rfid) {
    PWSTR path = nullptr;
    auto result = SysCalls::shGetKnownFolderPath(rfid, 0, nullptr, &path);
    if (result != S_OK) {
        SysCalls::coTaskMemFree(path);
        return std::string();
    }
    std::wstring temp(path);
    SysCalls::coTaskMemFree(path);
    std::string ret(temp.length(), 0);
    std::transform(temp.begin(), temp.end(), ret.begin(), [](wchar_t c) {
        return static_cast<char>(c);
    });
    return ret;
}

bool createCompilerCachePath(std::string &cacheDir) {
    if (pathExists(cacheDir)) {
        cacheDir = joinPath(cacheDir, "neo_compiler_cache");
        if (pathExists(cacheDir)) {
            return true;
        }

        auto result = SysCalls::createDirectoryA(cacheDir.c_str(), NULL);
        if (result) {
            return true;
        }

        if (SysCalls::getLastError() == ERROR_ALREADY_EXISTS) {
            return true;
        }
    }

    cacheDir = "";
    return false;
}

bool checkDefaultCacheDirSettings(std::string &cacheDir, NEO::EnvironmentVariableReader &reader) {
    cacheDir = getKnownFolderPath(FOLDERID_LocalAppData);

    if (cacheDir.empty()) {
        return false;
    }

    cacheDir = joinPath(cacheDir, "NEO\\");
    if (!pathExists(cacheDir)) {
        SysCalls::createDirectoryA(cacheDir.c_str(), NULL);
    }

    if (pathExists(cacheDir)) {
        return createCompilerCachePath(cacheDir);
    }

    cacheDir = "";
    return false;
}

time_t getFileModificationTime(const std::string &path) {
    WIN32_FIND_DATAA ffd;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    hFind = SysCalls::findFirstFileA(path.c_str(), &ffd);
    if (hFind == INVALID_HANDLE_VALUE) {
        return 0;
    }
    ULARGE_INTEGER uli;
    uli.LowPart = ffd.ftLastWriteTime.dwLowDateTime;
    uli.HighPart = ffd.ftLastWriteTime.dwHighDateTime;
    SysCalls::findClose(hFind);
    return uli.QuadPart;
}

size_t getFileSize(const std::string &path) {
    WIN32_FIND_DATAA ffd;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    hFind = SysCalls::findFirstFileA(path.c_str(), &ffd);
    if (hFind == INVALID_HANDLE_VALUE) {
        return 0u;
    }
    SysCalls::findClose(hFind);
    return static_cast<size_t>((ffd.nFileSizeHigh * (MAXDWORD + 1)) + ffd.nFileSizeLow);
}
bool isAnyIgcEnvVarSet() {
    LPWCH envStrings = SysCalls::getEnvironmentStringsW();
    if (envStrings == nullptr) {
        return false;
    }

    for (LPWCH var = envStrings; *var != L'\0';) {
        if (wcsncmp(var, L"IGC_", 4) == 0) {
            SysCalls::freeEnvironmentStringsW(envStrings);
            return true;
        }
        while (*var != L'\0') {
            ++var;
        }
        ++var;
    }

    SysCalls::freeEnvironmentStringsW(envStrings);
    return false;
}

} // namespace NEO