forked from Civitasv/cmake-tools.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkits.lua
More file actions
152 lines (137 loc) · 4.2 KB
/
kits.lua
File metadata and controls
152 lines (137 loc) · 4.2 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
local kits = {}
-- checks if there is a cmake-kits.json file and parses it to a Lua table
function kits.parse(global_kits_path, cwd)
-- helper function to find the config file
-- returns file path if found, nil otherwise
local function findcfg()
local files = vim.fn.readdir(cwd)
-- it will use local kits path first,
-- otherwise, it will use global kits path
local file = global_kits_path
for _, f in ipairs(files) do -- iterate over files in current directory
if f == "cmake-kits.json" or f == "CMakeKits.json" then -- if a kits config file is found
file = vim.fn.resolve(cwd .. "/" .. f)
break
end
end
return file
end
-- start parsing
local config = nil
local file = findcfg() -- check for config file
if file then -- if one is found ...
file = file:gsub("~", vim.fn.expand("~"))
if file:match(".*%.json") then -- .. and is a json file
config = vim.fn.json_decode(vim.fn.readfile(file))
end
end
return config
end
-- returns a list of descriptions of all kits
function kits.get(global_kits_path, cwd)
-- start parsing
local config = kits.parse(global_kits_path, cwd)
local res = {}
if config then -- if a config is found
for _, item in ipairs(config) do
local name = item.name
table.insert(res, name)
end
end
return res
end
function kits.get_by_name(kit_name, cwd, global_kits_path)
local config = kits.parse(global_kits_path, cwd)
if config then
for _, item in ipairs(config) do
local name = item.name
if name == kit_name then
return item
end
end
end
return nil
end
-- given a kit, build an argument list for CMake
function kits.build_env_and_args(kit_name, escape, cwd, global_kits_path)
local kit = kits.get_by_name(kit_name, cwd, global_kits_path)
local args = {}
local env = {}
local env_script = " "
if not kit then
return { env = env, env_script = env_script, args = args } -- silent error (empty arglist) if no config file found
end
-- local function to add an argument to `args`
local function add_args(as)
for _, a in pairs(as) do
table.insert(args, a)
end
end
local function add_env(ev)
for _, a in pairs(ev) do
table.insert(env, a)
end
end
if kit.environmentSetupScript then
env_script = kit.environmentSetupScript
-- vim.print(env_script)
end
-- if exists `compilers` option, then set variable for cmake
if kit.compilers then
for lang, compiler in pairs(kit.compilers) do
if escape then
add_args({ "-DCMAKE_" .. lang .. '_COMPILER:FILEPATH="' .. compiler .. '"' })
else
add_args({ "-DCMAKE_" .. lang .. "_COMPILER:FILEPATH=" .. compiler })
end
end
end
-- See : https://metricpanda.com/rival-fortress-update-27-compiling-with-clang-on-windows/
if kit.linker then
if escape then
table.insert(args, "-DCMAKE_LINKER=" .. '"' .. kit.linker .. '"')
else -- Quick Fix Lists
table.insert(args, "-DCMAKE_LINKER=" .. kit.linker)
end
end
if kit.generator then
if escape then
table.insert(args, "-G" .. '"' .. kit.generator .. '"')
else -- Quick Fix Lists
table.insert(args, "-G" .. kit.generator)
end
end
if kit.host_architecture then
table.insert(args, "-T host=" .. kit.host_architecture)
end
if kit.target_architecture then
table.insert(args, "-A " .. kit.target_architecture)
end
if kit.toolchainFile then
add_args({ "-DCMAKE_TOOLCHAIN_FILE:FILEPATH=" .. kit.toolchainFile })
end
if kit.cmakeSettings then
for k, v in pairs(kit.cmakeSettings) do
if type(v) == "boolean" then
if escape then
add_args({ "-D" .. k .. ":BOOL=" .. '"' .. string.upper(tostring(v)) .. '"' })
else
add_args({ "-D" .. k .. ":BOOL=" .. string.upper(tostring(v)) })
end
else
if escape then
add_args({ "-D" .. k .. ":STRING=" .. '"' .. v .. '"' })
else
add_args({ "-D" .. k .. ":STRING=" .. v })
end
end
end
end
if kit.environmentVariables then
for k, v in pairs(kit.environmentVariables) do
add_env({ k .. "=" .. v })
end
end
return { env = env, env_script = env_script, args = args }
end
return kits