aboutsummaryrefslogtreecommitdiff
path: root/src/Linker.cpp
blob: 7d90d469a100da7c1f6bbadf1d863e8499ad5c5d (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
#include "../include/Linker.hpp"
#include "../include/FileUtil.hpp"

static void split_string(const std::string &str, char delimiter, std::function<bool(const char*,size_t)> callback) {
    size_t index = 0;
    while(index < str.size()) {
        const size_t end_index = str.find(delimiter, index);
        if(end_index == std::string::npos)
            break;

        if(!callback(&str[index], end_index - index))
            break;

        index = end_index + 1;
    }
}

namespace sibs
{
    static bool is_linker_installed(const char *linker_binary_name) {
        const char *path = getenv("PATH");
        if(!path)
            return false;

        bool linker_found = false;
        split_string(path, ':', [&](const char *str, size_t size) {
            std::string fullpath(str, size);
            fullpath += "/";
            fullpath += linker_binary_name;

            if(getFileType(fullpath.c_str()) == FileType::REGULAR) {
                linker_found = true;
                return false;
            }

            return true;
        });

        return linker_found;
    }

    bool is_gold_linker_installed() {
        return is_linker_installed("ld.gold");
    }

    bool is_lld_linker_installed() {
        return is_linker_installed("ld.lld");
    }
}