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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
|
--!A cross-platform build utility based on Lua
--
-- Licensed under the Apache License, Version 2.0 (the "License");
-- you may not use this file except in compliance with the License.
-- You may obtain a copy of the License at
--
-- http://www.apache.org/licenses/LICENSE-2.0
--
-- Unless required by applicable law or agreed to in writing, software
-- distributed under the License is distributed on an "AS IS" BASIS,
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-- See the License for the specific language governing permissions and
-- limitations under the License.
--
-- Copyright (C) 2015-present, TBOOX Open Source Group.
--
-- @author OpportunityLiu
-- @file getinfo.lua
--
-- imports
import("core.base.option")
import("core.base.semver")
import("core.base.hashset")
import("core.project.config")
import("core.project.project")
import("core.platform.platform")
import("core.tool.compiler")
import("core.tool.linker")
import("core.cache.memcache")
import("core.cache.localcache")
import("lib.detect.find_tool")
import("private.action.run.make_runenvs")
import("actions.require.install", {alias = "install_requires", rootdir = os.programdir()})
import("actions.config.configheader", {alias = "generate_configheader", rootdir = os.programdir()})
import("actions.config.configfiles", {alias = "generate_configfiles", rootdir = os.programdir()})
-- escape special chars in msbuild file
function _escape(str)
if not str then
return nil
end
local map =
{
["%"] = "%25" -- Referencing metadata
, ["$"] = "%24" -- Referencing properties
, ["@"] = "%40" -- Referencing item lists
, ["'"] = "%27" -- Conditions and other expressions
, [";"] = "%3B" -- List separator
, ["?"] = "%3F" -- Wildcard character for file names in Include and Exclude attributes
, ["*"] = "%2A" -- Wildcard character for use in file names in Include and Exclude attributes
-- html entities
, ["\""] = """
, ["<"] = "<"
, [">"] = ">"
, ["&"] = "&"
}
return (string.gsub(str, "[%%%$@';%?%*\"<>&]", function (c) return assert(map[c]) end))
end
function _vs_arch(arch)
if arch == 'x86' or arch == 'i386' then return "Win32" end
if arch == 'x86_64' then return "x64" end
if arch:startswith('arm64') then return "ARM64" end
if arch:startswith('arm') then return "ARM" end
return arch
end
function _make_dirs(dir)
if dir == nil then
return ""
end
if type(dir) == "string" then
dir = path.translate(dir)
if dir == "" then
return ""
end
if path.is_absolute(dir) then
if dir:startswith(project.directory()) then
return path.join("$(XmakeProjectDir)", _escape(path.relative(dir, project.directory())))
end
return _escape(dir)
end
return path.join("$(XmakeProjectDir)", _escape(dir))
end
local r = {}
for k, v in ipairs(dir) do
r[k] = _make_dirs(v)
end
r = table.unique(r)
return path.joinenv(r)
end
function _make_arrs(arr)
if arr == nil then
return ""
end
if type(arr) == "string" then
return _escape(arr)
end
local r = {}
for k, v in ipairs(arr) do
r[k] = _make_arrs(v)
end
r = table.unique(r)
return table.concat(r, ";")
end
-- get values from target
function _get_values_from_target(target, name)
local values = table.wrap(target:get(name))
table.join2(values, target:get_from_opts(name))
table.join2(values, target:get_from_pkgs(name))
table.join2(values, target:get_from_deps(name, {interface = true}))
return table.unique(values)
end
-- make target info
function _make_targetinfo(mode, arch, target)
-- init target info
local targetinfo =
{
mode = mode
, arch = arch
, plat = config.get("plat")
, vsarch = _vs_arch(arch)
, sdkver = config.get("vs_sdkver")
}
-- write only if not default
-- use target:get("xxx") rather than target:xxx()
-- save target kind
targetinfo.kind = target:get("kind")
-- save target file
targetinfo.basename = _escape(target:get("basename"))
targetinfo.filename = _escape(target:get("filename"))
-- save dirs
targetinfo.targetdir = _make_dirs(target:get("targetdir"))
targetinfo.buildir = _make_dirs(config.get("buildir"))
targetinfo.rundir = _make_dirs(target:get("rundir"))
targetinfo.configdir = _make_dirs(os.getenv("XMAKE_CONFIGDIR"))
targetinfo.configfiledir = _make_dirs(target:get("configdir"))
targetinfo.includedirs = _make_dirs(table.join(_get_values_from_target(target, "includedirs") or {}, _get_values_from_target(target, "sysincludedirs")))
targetinfo.linkdirs = _make_dirs(_get_values_from_target(target, "linkdirs"))
targetinfo.sourcedirs = _make_dirs(_get_values_from_target(target, "values.project.vsxmake.sourcedirs"))
-- save defines
targetinfo.defines = _make_arrs(_get_values_from_target(target, "defines"))
-- save languages
targetinfo.languages = _make_arrs(_get_values_from_target(target, "languages"))
if targetinfo.languages then
-- fix c++17 to cxx17 for Xmake.props
targetinfo.languages = targetinfo.languages:replace("c++", "cxx", {plain = true})
end
-- save subsystem
local linkflags = linker.linkflags(target:targetkind(), target:sourcekinds(), {target = target})
for _, linkflag in ipairs(linkflags) do
if linkflag:lower():find("[%-/]subsystem:windows") then
targetinfo.subsystem = "windows"
end
end
if not targetinfo.subsystem then
targetinfo.subsystem = "console"
end
-- save config flags
local flags = {}
for k, v in pairs(localcache.get("config", "options_" .. target:name())) do
if k ~= "plat" and k ~= "mode" and k ~= "arch" and k ~= "clean" and k ~= "buildir" then
table.insert(flags, "--" .. k .. "=" .. tostring(v));
end
end
targetinfo.configflags = os.args(flags)
-- save runenvs
local runenvs = {}
local addrunenvs, setrunenvs = make_runenvs(target)
for k, v in pairs(addrunenvs) do
if k:upper() == "PATH" then
runenvs[k] = format("%s;$([System.Environment]::GetEnvironmentVariable('%s'))", _make_dirs(v), k)
else
runenvs[k] = format("%s;$([System.Environment]::GetEnvironmentVariable('%s'))", path.joinenv(v), k)
end
end
for k, v in pairs(setrunenvs) do
if #v == 1 then
v = v[1]
if path.is_absolute(v) and v:startswith(project.directory()) then
runenvs[k] = _make_dirs(v)
else
runenvs[k] = v[1]
end
else
runenvs[k] = path.joinenv(v)
end
end
local runenvstr = {}
for k, v in pairs(runenvs) do
table.insert(runenvstr, k .. "=" .. v)
end
targetinfo.runenvs = table.concat(runenvstr, "\n")
-- use mfc? save the mfc runtime kind
if target:rule("win.sdk.mfc.shared_app") or target:rule("win.sdk.mfc.shared") then
targetinfo.mfckind = "Dynamic"
elseif target:rule("win.sdk.mfc.static_app") or target:rule("win.sdk.mfc.static") then
targetinfo.mfckind = "Static"
end
-- use cuda? save the cuda runtime version
if target:rule("cuda") then
local nvcc = find_tool("nvcc", { version = true })
local ver = semver.new(nvcc.version)
targetinfo.cudaver = ver:major() .. "." .. ver:minor()
end
-- ok
return targetinfo
end
function _make_vsinfo_modes()
local vsinfo_modes = {}
local modes = option.get("modes")
if modes then
if not modes:find("\"") then
modes = modes:gsub(",", path.envsep())
end
for _, mode in ipairs(path.splitenv(modes)) do
table.insert(vsinfo_modes, mode:trim())
end
else
vsinfo_modes = project.modes()
end
if not vsinfo_modes or #vsinfo_modes == 0 then
vsinfo_modes = { config.mode() }
end
return vsinfo_modes
end
function _make_vsinfo_archs()
local vsinfo_archs = {}
local archs = option.get("archs")
if archs then
if not archs:find("\"") then
archs = archs:gsub(",", path.envsep())
end
for _, arch in ipairs(path.splitenv(archs)) do
table.insert(vsinfo_archs, arch:trim())
end
else
-- we use it first if global set_arch("xx") is setted in xmake.lua
vsinfo_archs = project.get("target.arch") or platform.archs()
end
if not vsinfo_archs or #vsinfo_archs == 0 then
vsinfo_archs = { config.arch() }
end
return vsinfo_archs
end
function _make_vsinfo_groups()
local groups = {}
local group_deps = {}
for targetname, target in pairs(project.targets()) do
if not target:isphony() then
local group_path = target:get("group")
if group_path then
local group_name = path.filename(group_path)
local group_names = path.split(group_path)
for idx, name in ipairs(group_names) do
local group = groups["group." .. name] or {}
group.group = name
group.group_id = hash.uuid4(name)
if idx > 1 then
group_deps["group_dep." .. name] = {current_id = group.group_id, parent_id = hash.uuid4(group_names[idx - 1])}
end
groups["group." .. name] = group
end
group_deps["group_dep.target." .. targetname] = {current_id = hash.uuid4(targetname), parent_id = groups["group." .. group_name].group_id}
end
end
end
return groups, group_deps
end
-- make vstudio project
function main(outputdir, vsinfo)
-- enter project directory
local oldir = os.cd(project.directory())
-- init solution directory
vsinfo.solution_dir = path.absolute(path.join(outputdir, "vsxmake" .. vsinfo.vstudio_version))
vsinfo.programdir = _make_dirs(xmake.programdir())
vsinfo.projectdir = project.directory()
vsinfo.sln_projectfile = path.relative(project.rootfile(), vsinfo.solution_dir)
local projectfile = path.filename(project.rootfile())
vsinfo.slnfile = path.filename(project.directory())
-- write only if not default
if projectfile ~= "xmake.lua" then
vsinfo.projectfile = projectfile
vsinfo.slnfile = path.basename(projectfile)
end
vsinfo.xmake_info = format("xmake version %s", xmake.version())
vsinfo.solution_id = hash.uuid4(project.directory() .. vsinfo.solution_dir)
vsinfo.vs_version = vsinfo.project_version .. ".0"
-- init modes
vsinfo.modes = _make_vsinfo_modes()
-- init archs
vsinfo.archs = _make_vsinfo_archs()
-- init groups
local groups, group_deps = _make_vsinfo_groups()
vsinfo.groups = table.keys(groups)
vsinfo.group_deps = table.keys(group_deps)
vsinfo._groups = groups
vsinfo._group_deps = group_deps
-- load targets
local targets = {}
vsinfo._arch_modes = {}
for _, mode in ipairs(vsinfo.modes) do
vsinfo._arch_modes[mode] = {}
for _, arch in ipairs(vsinfo.archs) do
vsinfo._arch_modes[mode][arch] = { mode = mode, arch = arch }
-- trace
print("checking for %s.%s ...", mode, arch)
-- reload config, project and platform
-- modify config
config.set("as", nil, {force = true}) -- force to re-check as for ml/ml64
config.set("mode", mode, {readonly = true, force = true})
config.set("arch", arch, {readonly = true, force = true})
-- clear all options
for _, opt in ipairs(project.options()) do
opt:clear()
end
-- clear cache
memcache.clear()
localcache.clear("config")
localcache.clear("detect")
localcache.clear("option")
localcache.clear("package")
localcache.clear("toolchain")
-- check platform
platform.load(config.plat(), arch):check()
-- check project options
project.check()
-- install and update requires
install_requires()
-- update config files
generate_configfiles()
generate_configheader()
-- ensure to enter project directory
os.cd(project.directory())
-- save targets
for targetname, target in pairs(project.targets()) do
if not target:isphony() then
-- make target with the given mode and arch
targets[targetname] = targets[targetname] or {}
local _target = targets[targetname]
-- init target info
_target.target = targetname
_target.vcxprojdir = path.join(vsinfo.solution_dir, targetname)
_target.target_id = hash.uuid4(targetname)
_target.kind = target:targetkind()
_target.scriptdir = path.relative(target:scriptdir(), _target.vcxprojdir)
_target.projectdir = path.relative(project.directory(), _target.vcxprojdir)
local targetdir = target:get("targetdir")
if targetdir then _target.targetdir = path.relative(targetdir, _target.vcxprojdir) end
_target._targets = _target._targets or {}
_target._targets[mode] = _target._targets[mode] or {}
local targetinfo = _make_targetinfo(mode, arch, target)
_target._targets[mode][arch] = targetinfo
_target.sdkver = targetinfo.sdkver
-- save all sourcefiles and headerfiles
_target.sourcefiles = table.unique(table.join(_target.sourcefiles or {}, (target:sourcefiles())))
_target.headerfiles = table.unique(table.join(_target.headerfiles or {}, (target:headerfiles())))
-- save deps
_target.deps = table.unique(table.join(_target.deps or {}, table.keys(target:deps()), nil))
end
end
end
end
os.cd(oldir)
for _, target in pairs(targets) do
target._paths = {}
local dirs = {}
local root = project.directory()
target.sourcefiles = table.imap(target.sourcefiles, function(_, v) return path.relative(v, root) end)
target.headerfiles = table.imap(target.headerfiles, function(_, v) return path.relative(v, root) end)
for _, f in ipairs(table.join(target.sourcefiles, target.headerfiles)) do
local dir = path.directory(f)
target._paths[f] =
{
path = _escape(f),
dir = _escape(dir)
}
while dir ~= "." do
if not dirs[dir] then
dirs[dir] =
{
dir = _escape(dir),
dir_id = hash.uuid4(dir)
}
end
dir = path.directory(dir)
end
end
target._dirs = dirs
target.dirs = table.keys(dirs)
target._deps = {}
for _, v in ipairs(target.deps) do
target._deps[v] = targets[v]
end
end
vsinfo.targets = table.keys(targets)
vsinfo._targets = targets
return vsinfo
end
|