-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathscanner.lua
More file actions
172 lines (150 loc) · 4.31 KB
/
scanner.lua
File metadata and controls
172 lines (150 loc) · 4.31 KB
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
local constants = require("cmake-tools.const")
local scanner = {}
local C_COMPILERS = { "gcc", "clang" }
local TOOLCHAIN_SEARCH_PATHS = {
vim.fn.expand("~/.cmake/toolchains"),
"/usr/share/cmake/toolchains",
"/usr/local/share/cmake/toolchains",
"/etc/cmake/toolchains",
}
local function toolchain_candidates(prefix)
if prefix == "" then
return {}
end
local triplet = prefix:gsub("%-$", "")
return {
triplet .. ".cmake",
triplet .. "-toolchain.cmake",
"toolchain-" .. triplet .. ".cmake",
}
end
local function find_toolchain_file(prefix)
local candidates = toolchain_candidates(prefix)
if #candidates == 0 then
return nil
end
for _, search_dir in ipairs(TOOLCHAIN_SEARCH_PATHS) do
for _, filename in ipairs(candidates) do
local full_path = search_dir .. "/" .. filename
if vim.fn.filereadable(full_path) == 1 then
return full_path
end
end
end
return nil
end
local function match_c_compiler(exe)
for _, compiler_name in ipairs(C_COMPILERS) do
local prefix = exe:match("^(.+%-)" .. compiler_name .. "$")
if prefix then
return prefix, compiler_name
end
if exe == compiler_name then
return "", compiler_name
end
end
return nil, nil
end
local function derive_toolchain(prefix, c_name)
local map = {
gcc = { cxx = "g++", linker = "ld" },
clang = { cxx = "clang++", linker = "lld" },
}
local companions = map[c_name]
if not companions then
return nil
end
return {
c = (prefix or "") .. c_name,
cxx = (prefix or "") .. companions.cxx,
linker = (prefix or "") .. companions.linker,
prefix = prefix or "",
}
end
local function get_path_executables()
local separator = vim.uv.os_uname().sysname == "Windows_NT" and ";" or ":"
local path_dirs = vim.split(vim.env.PATH or "", separator, { plain = true })
local executables = {}
for _, dir in ipairs(path_dirs) do
local entries = vim.fn.readdir(dir)
for _, entry in ipairs(entries) do
local full = dir .. "/" .. entry
if vim.fn.executable(full) == 1 then
executables[entry] = true
end
end
end
return executables
end
local function discover_toolchains(executables)
local seen = {}
local chains = {}
for exe in pairs(executables) do
local prefix, c_name = match_c_compiler(exe)
if c_name then
local key = prefix .. c_name
if not seen[key] then
seen[key] = true
local chain = derive_toolchain(prefix, c_name)
if chain then
table.insert(chains, chain)
end
end
end
end
return chains
end
local function check_executable_exists(compiler)
if not compiler or compiler == "" then
return false
end
local exists = vim.fn.executable(compiler) == 1
return exists
end
local function get_executable_path(compiler)
if not compiler or compiler == "" then
return nil
end
local path = vim.fn.exepath(compiler)
return path
end
local function get_compiler_version(compiler)
if not compiler or compiler == "" then
return nil
end
local version_output = vim.fn.system({ compiler, "--version" })
local version = version_output:match("%d+%.%d+%.%d+")
return version
end
local executables = get_path_executables()
function scanner.scan_for_kits()
local toolchains = discover_toolchains(executables)
local kits = {}
for _, tc in ipairs(toolchains) do
local has_c = check_executable_exists(tc.c)
local has_cxx = check_executable_exists(tc.cxx)
local kit = { compilers = {} }
local version = get_compiler_version(tc.c)
local prefix_label = tc.prefix ~= "" and (tc.prefix:gsub("%-$", "") .. " ") or ""
kit.name = prefix_label .. tc.c .. " " .. (version or "Unknown")
kit.compilers.C = get_executable_path(tc.c)
kit.compilers.CXX = get_executable_path(tc.cxx)
if check_executable_exists(tc.linker) then
kit.linker = get_executable_path(tc.linker)
end
local toolchain_file = find_toolchain_file(tc.prefix)
if toolchain_file then
kit.toolchainFile = toolchain_file
end
table.insert(kits, kit)
end
if vim.fn.isdirectory(constants.cmake_config_path) == 0 then
vim.fn.mkdir(constants.cmake_config_path, "p")
end
local json_kits = vim.fn.json_encode(kits)
if json_kits then
vim.fn.writefile({ json_kits }, constants.cmake_kits_path)
end
return kits
end
return scanner