summaryrefslogtreecommitdiff
path: root/xmake/plugins/project/vstudio/impl/vs200x_vcproj.lua
blob: b1bade847ada7ae62eb8b6bd0fe9d59bb574980f (plain)
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
--!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, Xmake Open Source Community.
--
-- @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
        local flag = flag

        -- replace -Idir or /Idir
        flag = flag:gsub("[%-|/]I(.*)", function (dir)
                        dir = path.translate(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 = path.translate(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: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
        local flag = flag

        -- replace -libpath:dir or /libpath:dir
        flag = flag:gsub("[%-|/]libpath:(.*)", function (dir)
                        dir = path.translate(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 = path.translate(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()

    -- make header
    vcprojfile:print("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
    vcprojfile:enter("<VisualStudioProject")
        vcprojfile:print("ProjectType=\"Visual C++\"")
        vcprojfile:print("Version=\"%s0\"", assert(vsinfo.project_version))
        vcprojfile:print("Name=\"%s\"", targetname)
        vcprojfile:print("ProjectGUID=\"{%s}\"", hash.uuid4(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="&quot;$(SolutionDir)\..\..\src&quot;;&quot;"
--      PreprocessorDefinitions=""
--      MinimalRebuild="true"
--      BasicRuntimeChecks="3"
--      RuntimeLibrary="3"
--      UsePrecompiledHeader="0"
--      WarningLevel="3"
--      DebugInformationFormat="4"
-- />
function _make_VCCLCompilerTool(vcprojfile, vsinfo, target, compflags)
    vcprojfile:enter("<Tool")
        vcprojfile:print("Name=\"VCCLCompilerTool\"")
        vcprojfile:print("ProgramDataBaseFileName=\"\"") -- disable pdb file default
        -- MT:0, MTd:1, MD:2, MDd:3, ML:4, MLd:5
        local runtime = 0
        for _,flag in pairs(compflags) do
            if flag:find("[%-|/]MD") then
                runtime = 2
                break
            elseif flag:find("[%-|/]MT") then
                runtime = 0
                break
            end
        end
        vcprojfile:print("RuntimeLibrary=\"%d\"", is_mode("debug") and runtime + 1 or runtime)
    vcprojfile:leave("/>")
end

-- make VCLinkerTool
--
-- e.g.
-- <Tool
--      Name="VCLinkerTool"
--      AdditionalDependencies="xxx.lib"
--      LinkIncremental="2"
--      AdditionalLibraryDirectories="&quot;$(SolutionDir)\..\..\lib&quot;"
--      GenerateDebugInformation="true"
--      SubSystem="1"
--      TargetMachine="1"
-- />
function _make_VCLinkerTool(vcprojfile, vsinfo, target, vcprojdir)

    -- need not linker?
    local kind = target: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

    -- subsystem, console: 1, windows: 2
    local subsystem = 1
    local flags = _make_linkflags(target, vcprojdir)
    if flags:lower():find("[%-/]subsystem:windows") then
        subsystem = 2
    end

    -- make it
    vcprojfile:enter("<Tool")
        vcprojfile:print("Name=\"VCLinkerTool\"")
        vcprojfile:print("AdditionalOptions=\"%s\"", flags)
		vcprojfile:print("AdditionalDependencies=\"\"")
		vcprojfile:print("AdditionalLibraryDirectories=\"\"")
        vcprojfile:print("LinkIncremental=\"2\"") -- enable: 2, disable: 1
        vcprojfile:print("GenerateDebugInformation=\"%s\"", tostring(debug))
        vcprojfile:print("SubSystem=\"%d\"", subsystem) -- console: 1, windows: 2
        vcprojfile:print("TargetMachine=\"%d\"", is_arch("x64") and 17 or 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
    }

    -- save compiler flags
    local compflags=nil
    for _, sourcebatch in pairs(target:sourcebatches()) do
        local sourcekind = sourcebatch.sourcekind
        if sourcekind then
            for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
                local flags = compiler.compflags(sourcefile, {target = target})
                if sourcekind == "cc" or sourcekind == "cxx" then
                    compflags = flags
                    break
                end
            end
        end
        if compflags then
            break
        end
    end

    -- use mfc? not used: 0, static: 1, shared: 2
    local usemfc = 0
    if target:rule("win.sdk.mfc.shared_app") or target:rule("win.sdk.mfc.shared") then
        usemfc = 2
    elseif target:rule("win.sdk.mfc.static_app") or target:rule("win.sdk.mfc.static") then
        usemfc = 1
    end

    -- set unicode
    local unicode = false
    for _, flag in ipairs(compflags) do
        if flag:find("[%-|/]DUNICODE") then
            unicode = true
            break
        end
    end

    -- 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(target:targetdir()), vcprojdir))
			vcprojfile:print("IntermediateDirectory=\"%s\"", path.relative(path.absolute(target:objectdir()), vcprojdir))
			vcprojfile:print("ConfigurationType=\"%d\"", assert(configuration_types[target:kind()]))
            vcprojfile:print("CharacterSet=\"%d\"", unicode and 1 or 2) -- mbc: 2, wcs: 1
            vcprojfile:print("UseOfMFC=\"%d\"", usemfc)
            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, compflags)

            -- 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 cxfile
--
-- e.g.
--  <File
--      RelativePath="..\..\..\src\file3.c"
--      >
--      <FileConfiguration
--          Name="Debug|Win32"
--          >
--          <Tool
--              Name="VCCLCompilerTool"
--              AdditionalOptions="-Dtest"
--          />
--      </FileConfiguration>
--  </File>
function _make_cxfile(vcprojfile, vsinfo, target, sourcefile, objectfile, vcprojdir)

    -- get the target key
    local key = target:cachekey()

    -- 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))

                -- compile 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 rcfile
--
-- e.g.
--  <File
--      RelativePath="..\..\..\src\resource.rc"
--      >
--      <FileConfiguration
--          Name="Debug|Win32"
--          >
--          <Tool
--              Name="VCResourceCompilerTool"
--              ResourceOutputFileName="..\..\..\build\src\resource.res"
--          />
--      </FileConfiguration>
--  </File>
function _make_rcfile(vcprojfile, vsinfo, target, sourcefile, objectfile, vcprojdir)

    -- 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=\"VCResourceCompilerTool\"")
                -- FIXME: multi rc files support
                -- vcprojfile:print("ResourceOutputFileName=\"%s\"", path.relative(path.absolute(objectfile), vcprojdir))
            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>")
        local sourcebatches = target:sourcebatches()
        -- c/cxx files
        vcprojfile:enter("<Filter Name=\"Source Files\">")
            for _, sourcebatch in pairs(sourcebatches) do
                local sourcekind = sourcebatch.sourcekind
                if sourcekind ~= "mrc" then
                    local objectfiles = sourcebatch.objectfiles
                    for idx, sourcefile in ipairs(sourcebatch.sourcefiles) do
                        _make_cxfile(vcprojfile, vsinfo, target, sourcefile, objectfiles[idx], vcprojdir)
                    end
                end
            end

        -- leave c/cxx files
        vcprojfile:leave("</Filter>")

        -- *.rc files
        vcprojfile:enter("<Filter Name=\"Resource Files\">")
            for _, sourcebatch in pairs(sourcebatches) do
                local sourcekind = sourcebatch.sourcekind
                if sourcekind == "mrc" then
                    local objectfiles = sourcebatch.objectfiles
                    for idx, sourcefile in ipairs(sourcebatch.sourcefiles) do
                        _make_rcfile(vcprojfile, vsinfo, target, sourcefile, objectfiles[idx], vcprojdir)
                    end
                end
            end

        -- leave *.rc 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