summaryrefslogtreecommitdiff
path: root/xmake/rules/xcode/metal/xmake.lua
blob: a876f946ba9003b748d5e3ffca5eba09d71db1fd (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
--!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      ruki
-- @file        xmake.lua
--

-- build metal files
--
-- @see https://developer.apple.com/documentation/metal/libraries/building_a_library_with_metal_s_command-line_tools
--
rule("xcode.metal")

    -- support add_files("*.metal")
    set_extensions(".metal")

    on_load(function (target)
        local cross
        if target:is_plat("macosx") then
            cross = "xcrun -sdk macosx "
        elseif target:is_plat("iphoneos") then
            cross = target:is_arch("i386", "x86_64") and "xcrun -sdk iphonesimulator " or "xcrun -sdk iphoneos "
        elseif target:is_plat("watchos") then
            cross = target:is_arch("i386") and "xcrun -sdk watchsimulator " or "xcrun -sdk watchos "
        elseif target:is_plat("appletvos") then
            cross = target:is_arch("i386", "x86_64") and "xcrun -sdk appletvsimulator " or "xcrun -sdk appletvos "
        else
            raise("unknown platform for xcode!")
        end
        target:data_set("xcode.metal.cross", cross)
    end)

    -- build *.metal to *.air
    on_buildcmd_file(function (target, batchcmds, sourcefile, opt)

        -- get metal
        import("core.tool.toolchain")
        import("lib.detect.find_tool")
        local cross = target:data("xcode.metal.cross")
        local metal = assert(find_tool("metal", {program = cross .. " metal"}), "metal command not found!")

        -- get xcode toolchain
        local xcode = toolchain.load("xcode", {plat = target:plat(), arch = target:arch()})
        local target_minver = xcode:config("target_minver")
        local xcode_sysroot = xcode:config("xcode_sysroot")

        -- init metal arguments
        local objectfile = target:objectfile(sourcefile) .. ".air"
        local argv = {"-c", "-ffast-math", "-gline-tables-only"}
        if target_minver then
            table.insert(argv, "-target")
            local airarch = target:is_arch("x86_64", "arm64") and "air64" or "air32"
            if target:is_plat("macosx") then
                table.insert(argv, airarch .. "-apple-macos" .. target_minver)
            elseif target:is_plat("iphoneos") then
                local airtarget = airarch .. "-apple-ios" .. target_minver
                if target:is_arch("x86_64", "i386") then
                    airtarget = airtarget .. "-simulator"
                end
                table.insert(argv, airtarget)
            elseif target:is_plat("watchos") then
                local airtarget = airarch .. "-apple-watchos" .. target_minver
                if target:is_arch("x86_64", "i386") then
                    airtarget = airtarget .. "-simulator"
                end
                table.insert(argv, airtarget)
            elseif target:is_plat("appletvos") then
                local airtarget = airarch .. "-apple-tvos" .. target_minver
                if target:is_arch("x86_64", "i386") then
                    airtarget = airtarget .. "-simulator"
                end
                table.insert(argv, airtarget)
            end
        end
        if xcode_sysroot then
            table.insert(argv, "-isysroot")
            table.insert(argv, xcode_sysroot)
        end
        table.insert(argv, "-o")
        table.insert(argv, objectfile)
        table.insert(argv, sourcefile)

        -- add commands
        batchcmds:show_progress(opt.progress, "${color.build.object}compiling.metal %s", sourcefile)
        batchcmds:mkdir(path.directory(objectfile))
        batchcmds:vrunv(metal.program, argv)

        -- add deps
        batchcmds:add_depfiles(sourcefile)
        batchcmds:set_depmtime(os.mtime(objectfile))
        batchcmds:set_depcache(target:dependfile(objectfile))
    end)

    -- link *.air to *.metallib
    before_linkcmd(function (target, batchcmds, opt)

        -- get objectfiles
        local objectfiles = {}
        for rulename, sourcebatch in pairs(target:sourcebatches()) do
            if rulename == "xcode.metal" then
                for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
                    table.insert(objectfiles, target:objectfile(sourcefile) .. ".air")
                end
                break
            end
        end
        if #objectfiles == 0 then
            return
        end

        -- get metallib
        import("core.tool.toolchain")
        import("lib.detect.find_tool")
        local cross = target:data("xcode.metal.cross")
        local metallib = assert(find_tool("metallib", {program = cross .. " metallib"}), "metallib command not found!")

        -- get xcode toolchain
        local xcode = toolchain.load("xcode", {plat = target:plat(), arch = target:arch()})
        local xcode_sysroot = xcode:config("xcode_sysroot")

        -- add commands
        local resourcesdir = path.absolute(target:data("xcode.bundle.resourcesdir"))
        local libraryfile = resourcesdir and path.join(resourcesdir, "default.metallib") or (target:targetfile() .. ".metallib")
        batchcmds:show_progress(opt.progress, "${color.build.target}linking.metal %s", path.filename(libraryfile))
        batchcmds:mkdir(path.directory(libraryfile))
        batchcmds:vrunv(metallib.program, table.join({"-o", libraryfile}, objectfiles), {envs = {SDKROOT = xcode_sysroot}})

        -- add deps
        batchcmds:add_depfiles(objectfiles)
        batchcmds:set_depmtime(os.mtime(libraryfile))
        batchcmds:set_depcache(target:dependfile(libraryfile))
    end)