summaryrefslogtreecommitdiff
path: root/tests/plugins/project/test.lua
blob: 40edd6a3ae2ce3a043ab72230bb3ed8023b15dab (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
import("core.project.config")
import("core.platform.platform")
import("core.tool.toolchain")
import("lib.detect.find_tool")
import("detect.sdks.find_dotnet")

function test_vsxmake(t)

    if not is_subhost("windows") then
        return t:skip("wrong host platform")
    end

    local projname = "testproj"
    local tempdir = os.tmpfile()
    os.mkdir(tempdir)
    os.cd(tempdir)

    -- create project
    os.vrunv("xmake", {"create", projname})
    os.cd(projname)

    -- set config
    local arch = os.getenv("platform") or "x86"
    config.set("arch", arch, {readonly = true, force = true})
    platform.load(config.plat(), arch):check()

    -- create sln & vcxproj
    local vs = config.get("vs")
    local vstype = "vsxmake" .. vs
    os.execv("xmake", {"project", "-k", vstype, "-a", arch})
    os.cd(vstype)

    -- run msbuild
    try
    {
        function ()
            local runenvs = toolchain.load("msvc"):runenvs()
            local msbuild = find_tool("msbuild", {envs = runenvs})
            os.execv(msbuild.program, {"/P:XmakeDiagnosis=true", "/P:XmakeVerbose=true"}, {envs = runenvs})
        end,
        catch
        {
            function ()
                print("--- sln file ---")
                io.cat(projname .. ".sln")
                print("--- vcx file ---")
                io.cat(projname .. "/" .. projname .. ".vcxproj")
                print("--- filter file ---")
                io.cat(projname .. "/" .. projname .. ".vcxproj.filters")
                raise("msbuild failed")
            end
        }
    }

    -- clean up
    os.cd(os.scriptdir())
    os.tryrm(tempdir)
end

function test_vsxmake_csharp(t)

    if not is_subhost("windows") then
        return t:skip("wrong host platform")
    end

    local dotnet = find_dotnet()
    if not dotnet or not dotnet.sdkver then
        return t:skip("dotnet sdk not found")
    end

    local projname = "testcsproj"
    local tempdir = os.tmpfile()
    os.mkdir(tempdir)
    os.cd(tempdir)

    -- create a C# console project using xmake create
    os.vrunv("xmake", {"create", "-l", "csharp", "-t", "console", projname})
    os.cd(projname)

    -- create sln & csproj
    local arch = os.getenv("platform") or "x64"
    local vs = config.get("vs")
    local vstype = "vsxmake" .. vs
    os.execv("xmake", {"project", "-k", vstype, "-a", arch})
    os.cd(vstype)

    -- verify a .csproj was generated (not .vcxproj)
    local csproj_path = path.join(projname, projname .. ".csproj")
    assert(os.isfile(csproj_path), "expected .csproj to be generated at " .. csproj_path)
    assert(not os.isfile(path.join(projname, projname .. ".vcxproj")),
        "unexpected .vcxproj generated for C# target")

    -- verify .sln references the .csproj with the C# project type GUID
    local sln_text = io.readfile(projname .. ".sln")
    assert(sln_text:find("FAE04EC0-301F-11D3-BF4B-00C04F79EFBC", 1, true),
        "C# project type GUID missing from .sln")
    assert(sln_text:find(projname .. ".csproj", 1, true),
        ".csproj reference missing from .sln")

    -- verify the .csproj imports Xmake.CSharp.targets and has a Compile item
    local csproj_text = io.readfile(csproj_path)
    assert(csproj_text:find("Xmake.CSharp.targets", 1, true),
        "Xmake.CSharp.targets import missing from .csproj")
    assert(csproj_text:find("<Compile Include=", 1, true),
        "<Compile> item missing from .csproj")

    -- clean up
    os.cd(os.scriptdir())
    os.tryrm(tempdir)
end

function test_compile_commands(t)
    local projname = "testproj"
    local tempdir = os.tmpfile()
    os.mkdir(tempdir)
    os.cd(tempdir)

    -- create project
    os.vrunv("xmake", {"create", projname})
    os.cd(projname)

    -- generate compile_commands
    os.vrunv("xmake", {"project", "-k", "compile_commands"})

    -- test autoupdate
    io.insert("xmake.lua", 1, 'add_rules("plugin.compile_commands.autoupdate", {outputdir = ".vscode", lsp = "clangd"})')
    os.vrun("xmake")

    -- clean up
    os.cd(os.scriptdir())
    os.tryrm(tempdir)
end

function test_cmake(t)
    local cmake = find_tool("cmake")
    if not cmake then
        return t:skip("cmake not found")
    end
    local projname = "testproj"
    local tempdir = os.tmpfile()
    os.mkdir(tempdir)
    os.cd(tempdir)

    -- create project
    os.vrunv("xmake", {"create", projname})
    os.cd(projname)

    -- generate compile_commands
    os.vrunv("xmake", {"project", "-k", "cmake"})

    -- test build
    os.mkdir("build")
    os.cd("build")
    os.vrunv(cmake.program, {".."})
    os.vrunv(cmake.program, {"--build", "."})

    -- clean up
    os.cd(os.scriptdir())
    os.tryrm(tempdir)
end