-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvscode_project.lua
More file actions
383 lines (310 loc) · 9.1 KB
/
vscode_project.lua
File metadata and controls
383 lines (310 loc) · 9.1 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
--
-- Name: vscode_project.lua
-- Purpose: Generate a vscode C/C++ project file.
-- Author: Ryan Pusztai
-- Modified by: Andrea Zanellato
-- Manu Evans
-- Tom van Dijck
-- Yehonatan Ballas
-- Jan "GamesTrap" Schürkamp
-- Created: 2013/05/06
-- Updated: 2025/11/04
-- Copyright: (c) 2008-2020 Yehonatan Ballas, Jason Perkins and the Premake project
-- (c) 2022-2025 Jan "GamesTrap" Schürkamp
--
local p = premake
local tree = p.tree
local project = p.project
local config = p.config
local vscode = p.modules.vscode
vscode.project = {}
local m = vscode.project
local function GetThreadCount()
local threads = 0
-- Check command-line arguments for threads override
for i, arg in ipairs(_ARGS) do
if (arg == "--threads" or arg == "-t") and _ARGS[i + 1] then
threads = tonumber(_ARGS[i + 1]) or 0
break
end
end
if threads > 0 then
return threads
end
-- Automatic threads detection
if os.host() == "windows" then
local result = os.outputof("wmic cpu get NumberOfLogicalProcessors")
for thread in result:gmatch("%d+") do
threads = threads + (tonumber(thread) or 0)
end
else
local result = os.outputof("nproc")
threads = tonumber(result) or 0
end
if threads <= 0 then
threads = 1
end
return threads
end
local function GetvswhereExe()
local vswhereExe = "vswhere"
local result, errorCode = os.outputof(vswhereExe)
if errorCode == 0 then
return vswhereExe
end
local vswhereExe = [["C:\Program Files (x86)\Microsoft Visual Studio\Installer\vswhere.exe"]]
result, errorCode = os.outputof(vswhereExe)
if errorCode ~= 0 then
term.setTextColor(term.errorColor)
print("Unable to find " .. vswhereExe)
term.setTextColor(nil)
return nil
end
return vswhereExe
end
local __msbuildexe = nil
local __msbuildexefirst = true
local function GetMsbuildExe()
if not __msbuildexefirst then
return __msbuildexe
end
__msbuildexefirst = false
local msbuildexeTmp = "msbuild"
local result, errorCode = os.outputof(msbuildexeTmp)
if errorCode == 0 then
__msbuildexe = msbuildexeTmp
return __msbuildexe
end
local vswhereExe = GetvswhereExe()
if vswhereExe == nil then
return nil
end
local args = "-products * -requires Microsoft.Component.MSBuild -prerelease -latest -utf8 -format json"
local result, errorCode = os.outputof(vswhereExe .. " " .. args)
if errorCode ~= 0 then
term.setTextColor(term.errorColor)
print("Failed to call vswhere...")
term.setTextColor(nil)
return nil
end
local res, ec = json.decode(result)
if res == nil or ec ~= nil then
term.setTextColor(term.errorColor)
print("Failed to decode vswhere output...")
term.setTextColor(nil)
return nil
end
local resCount = #res
if resCount <= 0 or res[1]["installationPath"] == nil then
term.setTextColor(term.errorColor)
print("Failed to retrieve installationPath from vswhere output...")
term.setTextColor(nil)
return nil
end
local vsInstallPath = res[1]["installationPath"]
__msbuildexe = vsInstallPath .. "\\MSBuild\\Current\\Bin\\MSBuild.exe"
__msbuildexe = __msbuildexe:gsub("\\", "\\\\")
return __msbuildexe
end
local function GetCompileCommand(system, action, cfgName, prjName, threadCount, all)
local threadArg = string.format("-j%s", threadCount)
local cmd = ""
if system ~= "windows" then
cmd = "clear && time "
end
if action == "make" then
return cmd .. string.format("make config=%s %s %s", string.lower(cfgName), all and "all" or prjName, threadArg)
elseif action == "ninja" then
return cmd .. string.format("ninja %s %s", all and cfgName or string.format("%s_%s", prjName, cfgName), threadArg)
elseif action == "vs" and system == "windows" then
local msbuildExe = GetMsbuildExe()
if msbuildExe == nil then
return nil
end
return cmd .. msbuildExe
end
return nil
end
local function GetVSArgs(wksName, cfgName, target, threadCount, all)
if _OPTIONS["action"] ~= "vs" then
return {}
end
if not all then
target = target:gsub("\\", "\\\\")
end
return
{
'/v:m',
string.format('/m:%s', threadCount),
string.format('${workspaceRoot}/%s.sln', wksName),
string.format('/p:Configuration=%s', cfgName),
all and '/t:Build' or string.format('/t:%s', target)
}
end
function m.vscode_c_cpp_properties(prj, isLast)
local cdialect = prj.cdialect and string.lower(prj.cdialect) or "${default}"
local cppdialect = prj.cppdialect and string.lower(prj.cppdialect) or "${default}"
local cfgIdx = 1
for cfg in project.eachconfig(prj) do
p.push('{')
p.w('"name": "%s (%s)",', prj.name, cfg.name)
p.w('"includePath":')
p.push('[')
p.w('"${workspaceFolder}/**"%s', (#cfg.includedirs > 0 or #cfg.externalincludedirs > 0) and ',' or '')
if #cfg.includedirs > 0 or #cfg.externalincludedirs > 0 then
for idx, includedir in ipairs(cfg.includedirs) do
if idx == #cfg.includedirs and #cfg.externalincludedirs == 0 then
p.w('"%s"', includedir)
else
p.w('"%s",', includedir)
end
end
for idx, includedir in ipairs(cfg.externalincludedirs) do
if idx == #cfg.externalincludedirs then
p.w('"%s"', includedir)
else
p.w('"%s",', includedir)
end
end
end
p.pop('],')
p.w('"defines":')
p.push('[')
for defineIdx, define in ipairs(cfg.defines) do
if defineIdx == #cfg.defines then
p.w('"%s"', define:gsub('"','\\"'))
else
p.w('"%s",', define:gsub('"','\\"'))
end
end
p.pop('],')
p.w('"cStandard": "%s",', cdialect)
p.w('"cppStandard": "%s",', cppdialect)
p.w('"intelliSenseMode": "${default}"', cppdialect)
if isLast and cfgIdx == #prj._cfglist then
p.pop('}')
else
p.pop('},')
end
cfgIdx = cfgIdx + 1
end
end
function m.vscode_launch(prj, isLast)
local target = path.getrelative(prj.workspace.location, prj.location)
local gdbPath = (os.host() ~= "windows") and os.outputof("which gdb") or nil
local cfgIdx = 1
for cfg in project.eachconfig(prj) do
if cfg.kind ~= "ConsoleApp" and cfg.kind ~= "WindowedApp" then --Ignore non executable configuration(s) in launch.json
cfgIdx = cfgIdx + 1
goto continue
end
local programPath = path.getrelative(prj.workspace.location, cfg.buildtarget.abspath)
local name = string.format("%s (%s)", prj.name, cfg.name)
p.push('{')
p.w('"name": "Run %s",', name)
p.w('"request": "launch",')
p.w('"type": "cppdbg",')
p.w('"program": "${workspaceRoot}/%s",', programPath)
if os.target == "linux" then
p.w('"linux":')
p.push('{')
p.w('"externalConsole": true,')
if gdbPath then
p.w('"miDebuggerPath": "%s",', gdbPath)
end
p.w('"MIMode": "gdb",')
p.w('"setupCommands":')
p.push('[')
p.push('{')
p.w('"text": "-enable-pretty-printing",')
p.w('"description": "enable pretty printing",')
p.w('"ignoreFailures": true')
p.pop('},')
p.pop('],')
p.pop('},')
elseif os.target == "windows" then
p.w('"windows":')
p.push('{')
p.w('"console": "externalTerminal')
p.pop('},')
end
p.w('"args": [],')
p.w('"stopAtEntry": false,')
p.w('"cwd": "${workspaceFolder}/%s",', target)
p.w('"environment": [],')
p.w('"preLaunchTask": "Build %s"', name)
if isLast and cfgIdx == #prj._cfglist then
p.pop('}')
else
p.pop('},')
end
cfgIdx = cfgIdx + 1
::continue::
end
end
local function GenerateTasks(cfgs, cfgsSize, nameFn, cmdFn, argsFn, isLast)
local threadCount = GetThreadCount()
local cfgIdx = 1
for cfg in cfgs do
local buildName = nameFn(cfg)
p.push('{')
p.w('"label": "%s",', buildName)
p.w('"type": "shell",')
local cmd = cmdFn(os.target(), _OPTIONS["action"], cfg.name, threadCount)
if os.target() ~= "windows" then
p.w('"linux":')
p.push('{')
p.w('"command": "%s",', cmd)
p.w('"problemMatcher": "$gcc"')
p.pop('},')
else
p.w('"windows":')
p.push('{')
p.w('"command": "%s",', cmd)
p.w('"args":')
p.push('[')
local args = argsFn(cfg, threadCount)
for argIdx, arg in ipairs(argsFn(cfg, threadCount)) do
p.w('"%s"%s', arg, argIdx == #args and '' or ',')
end
p.pop('],')
p.w('"problemMatcher": "$msCompile"')
p.pop('},')
end
p.w('"group":')
p.push('{')
p.w('"kind": "build"')
p.pop('}')
if isLast and cfgIdx == cfgsSize then
p.pop('}')
else
p.pop('},')
end
cfgIdx = cfgIdx + 1
end
end
function m.vscode_tasks(prj)
local target = path.translate(path.getrelative(prj.workspace.location, prj.location))
return GenerateTasks(project.eachconfig(prj), #prj._cfglist,
function(cfg)
return string.format("Build %s (%s)", prj.name, cfg.name)
end,
function(sys, action, cfgName, threadCount)
return GetCompileCommand(sys, action, cfgName, prj.name, threadCount, false)
end,
function(cfg, threadCount)
return GetVSArgs(prj.workspace.name, cfg.name, target, threadCount, false)
end, false)
end
function m.vscode_tasks_build_all(wks)
return GenerateTasks(p.workspace.eachconfig(wks), #p.oven.bakeWorkspace(wks).configs,
function(cfg)
return string.format("Build All (%s)", cfg.name)
end,
function(sys, action, cfgName, threadCount)
return GetCompileCommand(sys, action, cfgName, nil, threadCount, true)
end,
function(cfg, threadCount)
return GetVSArgs(wks.name, cfg.name, nil, threadCount, true)
end, true)
end