-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathscanner.lua
More file actions
186 lines (164 loc) · 4.73 KB
/
scanner.lua
File metadata and controls
186 lines (164 loc) · 4.73 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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 _, c_name in ipairs(C_COMPILERS) do
local prefix = exe:match("^(.+%-)" .. c_name .. "$")
if prefix then
return prefix, c_name
end
if exe == c_name then
return "", c_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 path_dirs = vim.split(vim.env.PATH or "", ":", { 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 nil
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
-- Main function to scan for kits
function scanner.scan_for_kits()
vim.notify("Scanning for kits…")
local executables = get_path_executables()
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)
if has_c then
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)
if has_cxx then
kit.compilers.CXX = get_executable_path(tc.cxx)
end
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
vim.notify("Toolchain file found: " .. toolchain_file)
else
if tc.prefix ~= "" then
vim.notify("No toolchain file found for prefix: " .. tc.prefix, vim.log.levels.WARN)
end
end
table.insert(kits, kit)
end
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)
vim.notify("Kits saved to: " .. constants.cmake_kits_path)
else
vim.notify("Failed to encode kits to JSON.", vim.log.levels.ERROR)
end
vim.notify("Scanning complete.")
return kits
end
return scanner