forked from maeday-modus/premake-vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvscode_project.lua
More file actions
179 lines (142 loc) · 4.93 KB
/
vscode_project.lua
File metadata and controls
179 lines (142 loc) · 4.93 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
-- PROJECT GENERATION
-- Aliases
local p = premake
local project = p.project
local config = p.config
local tree = p.tree
local vscode = p.modules.vscode
-- Initialize project object
vscode.project = {}
vscode.project.cCppProperties = {}
-- Supported C/C++ Properties
local cCppProperties = vscode.project.cCppProperties
cCppProperties.cppStandards = {
["C++98"] = "c++98",
["C++11"] = "c++11",
["C++14"] = "c++14",
["C++17"] = "c++17",
["C++20"] = "c++20",
["C++2a"] = "c++20",
["gnu++98"] = "gnu++98",
["gnu++11"] = "gnu++11",
["gnu++14"] = "gnu++14",
["gnu++17"] = "gnu++17",
["gnu++20"] = "gnu++20"
}
-- NOTE(Peter): This is trash, but I can't think of a better way of doing this right now other than asking people to put the relavant directories in some env var (which is also trash)
cCppProperties.toolsetPaths = {
["windows"] = {
["msc"] = "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.35.32215/bin/Hostx64/x64/cl.exe",
["clang"] = "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/Llvm/x64/bin/clang-cl.exe"
},
["linux"] = {
["gcc"] = "/usr/bin/g++",
["clang"] = "/usr/bin/clang++"
}
}
-- C/C++ Property Generation reflection
cCppProperties.configProps = function(prj, cfg)
return {
cCppProperties.intelliSenseMode,
cCppProperties.includeDirs,
cCppProperties.defines,
cCppProperties.forceIncludes,
cCppProperties.cppStandard,
cCppProperties.compilerPath
}
end
-- GENERATION METHODS --
-- Intellisense
function cCppProperties.intelliSenseMode(prj, cfg)
-- Supported intellisense modes
-- NOTE(minifalafel): Maybe this should be stored somewhere else? In case we ever need to access it anywhere else.
local supportedModes = {
["msc"] = "msvc-x64",
["clang"] = "clang-x64",
["gcc"] = "gcc-x64"
}
-- Select the mode based on the toolset (if it's supported)
local toolset = vscode.getToolsetName(cfg)
local mode = supportedModes[toolset]
if mode == nil then
error("Unsupported toolset '" .. toolset "'")
end
-- Finally write the option
p.w('"intelliSenseMode": "%s",', mode)
end
function cCppProperties.includeDirs(prj, cfg)
-- TODO: Maybe these could be consolidated into a single array and then checked?
local hasIncludeDirs = #cfg.sysincludedirs > 0 or #cfg.externalincludedirs > 0 or #cfg.includedirs > 0
if hasIncludeDirs then
p.push('"includePath": [')
-- NOTE(Peter): VS Code currently doesn't have a property for external include dirs or system include dirs
for _, includedir in ipairs(cfg.sysincludedirs) do
p.w('"%s",', includedir:gsub([[\]], "/"))
end
for _, includedir in ipairs(cfg.externalincludedirs) do
p.w('"%s",', includedir:gsub([[\]], "/"))
end
for _, includedir in ipairs(cfg.includedirs) do
p.w('"%s",', includedir:gsub([[\]], "/"))
end
p.pop('],')
end
end
function cCppProperties.defines(prj, cfg)
if #cfg.defines > 0 then
p.push('"defines": [')
for _, define in ipairs(cfg.defines) do
p.w('"%s",', p.esc(define):gsub(" ", "\\ "))
end
p.pop('],')
end
end
function cCppProperties.forceIncludes(prj, cfg)
local toolset = vscode.getCompiler(cfg)
local forceIncludes = {}
table.foreachi(cfg.forceincludes, function(file)
tree.traverse(project.getsourcetree(prj), {
onleaf = function(node, depth)
if node.name == file then
table.insert(forceIncludes, p.quoted(node.abspath))
end
end
})
end)
if #forceIncludes > 0 then
p.push('"forcedInclude": [')
for _, include in ipairs(forceIncludes) do
p.w('"%s",', include)
end
p.pop('],')
end
end
function cCppProperties.cppStandard(prj, cfg)
if (cfg.cppdialect and cfg.cppdialect:len() > 0) or cfg.cppdialect == "Default" then
p.w('"cppStandard": "%s",', cCppProperties.cppStandards[cfg.cppdialect])
end
end
function cCppProperties.compilerPath(prj, cfg)
local toolset = vscode.getToolsetName(cfg)
local toolsetPath = cCppProperties.toolsetPaths[cfg.system][toolset]
p.w('"compilerPath": "%s",', toolsetPath)
end
-- C/C++ COMBINED GENERATION
function cCppProperties.generate(prj)
-- ".json" formatted opening bracket and property name
p.push('{')
p.push('"configurations": [')
-- For each project configuration
for cfg in project.eachconfig(prj) do
p.push('{')
-- Set the name
local configName = vscode.configName(cfg, #prj.workspace.platforms > 1)
p.w('"name": "%s",', configName)
-- Generate the C/C++ Properties
p.callArray(cCppProperties.configProps, prj, cfg)
p.pop('},')
end
p.pop('],')
p.w('"version": 4')
p.pop('}')
end