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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
|
--!A cross-platform build utility based on Lua
--
-- Licensed to the Apache Software Foundation (ASF) under one
-- or more contributor license agreements. See the NOTICE file
-- distributed with this work for additional information
-- regarding copyright ownership. The ASF licenses this file
-- to you 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 - 2018, TBOOX Open Source Group.
--
-- @author ruki
-- @file vs200x_vcproj.lua
--
-- imports
import("core.tool.linker")
import("core.tool.compiler")
import("core.project.config")
import("vsfile")
-- make compiling flags
function _make_compflags(sourcefile, target, vcprojdir)
-- make the compiling flags
local compflags = compiler.compflags(sourcefile, {target = target})
-- replace -Idir or /Idir, -Fdsymbol.pdb or /Fdsymbol.pdb
local flags = {}
for _, flag in ipairs(compflags) do
-- replace -Idir or /Idir
flag = flag:gsub("[%-|/]I(.*)", function (dir)
dir = dir:trim()
if not path.is_absolute(dir) then
dir = path.relative(path.absolute(dir), vcprojdir)
end
return "/I" .. dir
end)
-- replace -Fdsymbol.pdb or /Fdsymbol.pdb
flag = flag:gsub("[%-|/]Fd(.*)", function (dir)
dir = dir:trim()
if not path.is_absolute(dir) then
dir = path.relative(path.absolute(dir), vcprojdir)
end
return "/Fd" .. dir
end)
-- save flag
table.insert(flags, flag)
end
-- make flags string
flags = os.args(flags)
-- replace " => "
flags = flags:gsub("\"", """)
-- ok?
return flags
end
-- make linking flags
function _make_linkflags(target, vcprojdir)
-- make the linking flags
local linkflags = linker.linkflags(target:get("kind"), target:sourcekinds(), {target = target})
-- replace -libpath:dir or /libpath:dir, -pdb:symbol.pdb or /pdb:symbol.pdb
local flags = {}
for _, flag in ipairs(linkflags) do
-- replace -libpath:dir or /libpath:dir
flag = flag:gsub("[%-|/]libpath:(.*)", function (dir)
dir = dir:trim()
if not path.is_absolute(dir) then
dir = path.relative(path.absolute(dir), vcprojdir)
end
return "/libpath:" .. dir
end)
-- replace -pdb:symbol.pdb or /pdb:symbol.pdb
flag = flag:gsub("[%-|/]pdb:(.*)", function (dir)
dir = dir:trim()
if not path.is_absolute(dir) then
dir = path.relative(path.absolute(dir), vcprojdir)
end
return "/pdb:" .. dir
end)
-- save flag
table.insert(flags, flag)
end
-- make flags string
flags = os.args(flags)
-- replace " => "
flags = flags:gsub("\"", """)
-- ok?
return flags
end
-- make header
function _make_header(vcprojfile, vsinfo, target)
-- the target name
local targetname = target:name()
-- the versions
local versions =
{
vs2002 = '7.0'
, vs2003 = '7.1'
, vs2005 = '8.0'
, vs2008 = '9.0'
}
-- make header
vcprojfile:print("<?xml version=\"1.0\" encoding=\"gb2312\"?>")
vcprojfile:enter("<VisualStudioProject")
vcprojfile:print("ProjectType=\"Visual C++\"")
vcprojfile:print("Version=\"%s0\"", assert(versions["vs" .. vsinfo.vstudio_version]))
vcprojfile:print("Name=\"%s\"", targetname)
vcprojfile:print("ProjectGUID=\"{%s}\"", hash.uuid(targetname))
vcprojfile:print("RootNamespace=\"%s\"", targetname)
vcprojfile:print("TargetFrameworkVersion=\"196613\"")
vcprojfile:print(">")
end
-- make tailer
function _make_tailer(vcprojfile, vsinfo, target)
vcprojfile:leave("</VisualStudioProject>")
end
-- make platforms
function _make_platforms(vcprojfile, vsinfo, target)
-- add Win32 platform
vcprojfile:enter("<Platforms>")
vcprojfile:enter("<Platform")
vcprojfile:print("Name=\"Win32\"")
vcprojfile:leave("/>")
vcprojfile:leave("</Platforms>")
end
-- make toolfiles
function _make_toolfiles(vcprojfile, vsinfo, target)
-- empty toolfiles
vcprojfile:enter("<ToolFiles>")
vcprojfile:leave("</ToolFiles>")
end
-- make VCCLCompilerTool
--
-- .e.g
--
-- <Tool
-- Name="VCCLCompilerTool"
-- AdditionalOptions="/TP"
-- Optimization="0"
-- AdditionalIncludeDirectories=""$(SolutionDir)\..\..\src";""
-- PreprocessorDefinitions=""
-- MinimalRebuild="true"
-- BasicRuntimeChecks="3"
-- RuntimeLibrary="3"
-- UsePrecompiledHeader="0"
-- WarningLevel="3"
-- DebugInformationFormat="4"
-- />
function _make_VCCLCompilerTool(vcprojfile, vsinfo, target)
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCCLCompilerTool\"")
vcprojfile:print("ProgramDataBaseFileName=\"\"") -- disable pdb file default
vcprojfile:leave("/>")
end
-- make VCLinkerTool
--
-- .e.g
-- <Tool
-- Name="VCLinkerTool"
-- AdditionalDependencies="xxx.lib"
-- LinkIncremental="2"
-- AdditionalLibraryDirectories=""$(SolutionDir)\..\..\lib""
-- GenerateDebugInformation="true"
-- SubSystem="1"
-- TargetMachine="1"
-- />
function _make_VCLinkerTool(vcprojfile, vsinfo, target, vcprojdir)
-- need not linker?
local kind = target:get("kind")
if kind ~= "binary" and kind ~= "shared" then
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCLinkerTool\"")
vcprojfile:leave("/>")
return
end
-- generate debug infomation?
local debug = false
for _, symbol in ipairs(target:get("symbols")) do
if symbol == "debug" then
debug = true
break
end
end
-- make it
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCLinkerTool\"")
vcprojfile:print("AdditionalOptions=\"%s\"", _make_linkflags(target, vcprojdir))
vcprojfile:print("AdditionalDependencies=\"\"")
vcprojfile:print("AdditionalLibraryDirectories=\"\"")
vcprojfile:print("LinkIncremental=\"2\"") -- enable: 2, disable: 1
vcprojfile:print("GenerateDebugInformation=\"%s\"", tostring(debug))
vcprojfile:print("SubSystem=\"1\"") -- console: 1, windows: 2
vcprojfile:print("TargetMachine=\"%d\"", ifelse(config.arch() == "x64", 17, 1))
vcprojfile:leave("/>")
end
-- make configurations
function _make_configurations(vcprojfile, vsinfo, target, vcprojdir)
-- init configuration type
local configuration_types =
{
binary = 1
, shared = 2
, static = 4
}
-- enter configurations
vcprojfile:enter("<Configurations>")
-- make configuration for the current mode
vcprojfile:enter("<Configuration")
vcprojfile:print("Name=\"$(mode)|Win32\"")
vcprojfile:print("OutputDirectory=\"%s\"", path.relative(path.absolute(config.get("buildir")), vcprojdir))
vcprojfile:print("IntermediateDirectory=\"%$(ConfigurationName)\"")
vcprojfile:print("ConfigurationType=\"%d\"", assert(configuration_types[target:get("kind")]))
vcprojfile:print("CharacterSet=\"2\"") -- mbc: 2, wcs: 1
vcprojfile:print(">")
-- make VCPreBuildEventTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCPreBuildEventTool\"")
vcprojfile:leave("/>")
-- make VCCustomBuildTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCCustomBuildTool\"")
vcprojfile:leave("/>")
-- make VCXMLDataGeneratorTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCXMLDataGeneratorTool\"")
vcprojfile:leave("/>")
-- make VCWebServiceProxyGeneratorTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCWebServiceProxyGeneratorTool\"")
vcprojfile:leave("/>")
-- make VCMIDLTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCMIDLTool\"")
vcprojfile:leave("/>")
-- make VCCLCompilerTool
_make_VCCLCompilerTool(vcprojfile, vsinfo, target)
-- make VCManagedResourceCompilerTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCManagedResourceCompilerTool\"")
vcprojfile:leave("/>")
-- make VCResourceCompilerTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCResourceCompilerTool\"")
vcprojfile:leave("/>")
-- make VCPreLinkEventTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCPreLinkEventTool\"")
vcprojfile:leave("/>")
-- make VCLinkerTool
_make_VCLinkerTool(vcprojfile, vsinfo, target, vcprojdir)
-- make VCALinkTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCALinkTool\"")
vcprojfile:leave("/>")
-- make VCManifestTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCManifestTool\"")
vcprojfile:leave("/>")
-- make VCXDCMakeTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCXDCMakeTool\"")
vcprojfile:leave("/>")
-- make VCBscMakeTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCBscMakeTool\"")
vcprojfile:leave("/>")
-- make VCFxCopTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCFxCopTool\"")
vcprojfile:leave("/>")
-- make VCAppVerifierTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCAppVerifierTool\"")
vcprojfile:leave("/>")
-- make VCPostBuildEventTool
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCPostBuildEventTool\"")
vcprojfile:leave("/>")
-- leave configuration
vcprojfile:leave("</Configuration>")
-- leave configurations
vcprojfile:leave("</Configurations>")
end
-- make references
function _make_references(vcprojfile, vsinfo, target)
vcprojfile:enter("<References>")
vcprojfile:leave("</References>")
end
-- make file
--
-- .e.g
-- <File
-- RelativePath="..\..\..\src\file3.c"
-- >
-- <FileConfiguration
-- Name="Debug|Win32"
-- >
-- <Tool
-- Name="VCCLCompilerTool"
-- AdditionalOptions="-Dtest"
-- />
-- </FileConfiguration>
-- </File>
function _make_file(vcprojfile, vsinfo, target, sourcefile, objectfile, vcprojdir)
-- get the target key
local key = tostring(target)
-- make flags cache
_g.flags = _g.flags or {}
-- make flags
local flags = _g.flags[key] or _make_compflags(sourcefile, target, vcprojdir)
_g.flags[key] = flags
-- enter file
vcprojfile:enter("<File")
-- add file path
vcprojfile:print("RelativePath=\"%s\"", path.relative(path.absolute(sourcefile), vcprojdir))
vcprojfile:print(">")
-- add file configuration
vcprojfile:enter("<FileConfiguration")
vcprojfile:print("Name=\"$(mode)|Win32\"")
vcprojfile:print(">")
-- add compiling options
vcprojfile:enter("<Tool")
vcprojfile:print("Name=\"VCCLCompilerTool\"")
vcprojfile:print("AdditionalOptions=\"%s\"", flags)
vcprojfile:print("ObjectFile=\"%s\"", path.relative(path.absolute(objectfile), vcprojdir))
-- complie as c++ if exists flag: /TP
if flags:find("[%-|/]TP") then
vcprojfile:print("CompileAs=\"2\"")
end
vcprojfile:leave("/>")
vcprojfile:leave("</FileConfiguration>")
-- leave file
vcprojfile:leave("</File>")
end
-- make files
--
-- .e.g
-- <Filter
-- Name="Source Files"
-- >
-- <File
-- RelativePath="..\..\..\src\file1.c"
-- >
-- </File>
-- <File
-- RelativePath="..\..\..\src\file2.c"
-- >
-- </File>
-- <File
-- RelativePath="..\..\..\src\file3.c"
-- >
-- <FileConfiguration
-- Name="Debug|Win32"
-- >
-- <Tool
-- Name="VCCLCompilerTool"
-- AdditionalOptions="-Dtest"
-- />
-- </FileConfiguration>
-- </File>
-- <File
-- RelativePath="..\..\..\src\file4.c"
-- >
-- </File>
-- </Filter>
function _make_files(vcprojfile, vsinfo, target, vcprojdir)
-- enter files
vcprojfile:enter("<Files>")
vcprojfile:enter("<Filter")
vcprojfile:print("Name=\"Source Files\"")
vcprojfile:print(">")
-- add files
local objectfiles = target:objectfiles()
for idx, sourcefile in ipairs(target:sourcefiles()) do
_make_file(vcprojfile, vsinfo, target, sourcefile, objectfiles[idx], vcprojdir)
end
-- leave files
vcprojfile:leave("</Filter>")
vcprojfile:leave("</Files>")
end
-- make globals
function _make_globals(vcprojfile, vsinfo, target)
vcprojfile:enter("<Globals>")
vcprojfile:leave("</Globals>")
end
-- make vcproj
function make(vsinfo, target)
-- the target name
local targetname = target:name()
-- the vcproj directory
local vcprojdir = path.join(vsinfo.solution_dir, targetname)
-- open vcproj file
local vcprojfile = vsfile.open(path.join(vcprojdir, targetname .. ".vcproj"), "w")
-- init indent character
vsfile.indentchar('\t')
-- make header
_make_header(vcprojfile, vsinfo, target)
-- make platforms
_make_platforms(vcprojfile, vsinfo, target)
-- make toolfiles
_make_toolfiles(vcprojfile, vsinfo, target)
-- make configurations
_make_configurations(vcprojfile, vsinfo, target, vcprojdir)
-- make references
_make_references(vcprojfile, vsinfo, target)
-- make files
_make_files(vcprojfile, vsinfo, target, vcprojdir)
-- make globals
_make_globals(vcprojfile, vsinfo, target)
-- make tailer
_make_tailer(vcprojfile, vsinfo, target)
-- exit solution file
vcprojfile:close()
end
|