kunlun.lua 2.91 KB
Newer Older
1
2
add_defines("ENABLE_KUNLUN_API")
local KUNLUN_HOME = os.getenv("KUNLUN_HOME")
zhangyue's avatar
zhangyue committed
3
4
5
local XRE_DIR = path.join(KUNLUN_HOME, "xre")
local XTDK_DIR = path.join(KUNLUN_HOME, "xtdk")
local XDNN_DIR = path.join(KUNLUN_HOME, "xhpc", "xdnn")
6
7

-- Add include dirs
8
9
10
11
12
add_includedirs(path.join(XRE_DIR, "include"), {public = true})
add_includedirs(path.join(XDNN_DIR, "include"), {public = true})
add_includedirs(path.join(XTDK_DIR, "include"), {public = true})

-- Add link dirs
zhangyue's avatar
zhangyue committed
13
14
15
add_linkdirs(path.join(XRE_DIR, "so"))
add_linkdirs(path.join(XDNN_DIR, "so"))
add_links("xpurt", "xpuapi")
16

17
18
19
20
21
rule("xpu")
    set_extensions(".xpu")
    
    on_build_file(function (target, sourcefile)

zhangyue's avatar
zhangyue committed
22
23
24
        local sourcefile_config = target:fileconfig(sourcefile) or {}
        local includedirs = sourcefile_config.includedirs or {}

25
26
27
28
        local objectfile = target:objectfile(sourcefile)
        os.mkdir(path.directory(objectfile))
        local cc = path.join(XTDK_DIR, "bin/clang++")
        local includedirs = table.concat(target:get("includedirs"), " ")
29
30
31
32
33
        local arch_map = {
            ["x86_64"] = "x86_64-linux-gnu",
            ["arm64"] = "aarch64-linux-gnu"
        }

34
35
        local args = {
            "--sysroot=/",
36
            "--target=" .. arch_map[os.arch()],
37
            "-fPIC",
zhangyue's avatar
zhangyue committed
38
39
            "--xpu-arch=xpu3",
            "-std=c++17",
40
41
42
            "-O2",
            "-fno-builtin",
            "-c", sourcefile,
zhangyue's avatar
zhangyue committed
43
            "-o", objectfile
44
45
46
47
48
49
50
        }
        
        for _, includedir in ipairs(target:get("includedirs")) do
            table.insert(args, "-I" .. includedir)
        end

        -- print(args)
zhangyue's avatar
zhangyue committed
51
52
53
        local ok, code = os.execv(cc, args)
        assert(ok == 0, "Compile failed: " .. sourcefile)

54
55
56
57
58
59
60
        table.insert(target:objectfiles(), objectfile)
        print(target:objectfiles())
    end)
rule_end()

local src_dir = path.join(os.projectdir(), "src", "infiniop")

61
62
target("infiniop-kunlun")
    set_kind("static")
PanZezhong's avatar
PanZezhong committed
63
    add_deps("infini-utils")
64
65
    on_install(function (target) end)

zhangyue's avatar
zhangyue committed
66
    add_cxflags("-lstdc++ -fPIC -Wno-error=unused-function")
67
68
69
70
    set_warnings("all", "error")

    set_languages("cxx17")
    add_files("$(projectdir)/src/infiniop/devices/kunlun/*.cc", "$(projectdir)/src/infiniop/ops/*/kunlun/*.cc")
71
72
73
    -- compile handwriting kernel
    local xpu_files = os.files(src_dir .. "/ops/*/kunlun/*.xpu")
    if #xpu_files > 0 then
zhangyue's avatar
zhangyue committed
74
75
76
        add_files(xpu_files, {
            rule = "xpu",
            includedirs = {
77
                path.join(os.projectdir(), "include"),
zhangyue's avatar
zhangyue committed
78
79
80
81
82
                path.join(XRE_DIR, "include"),
                path.join(XDNN_DIR, "include"),
                path.join(XTDK_DIR, "include")
            }
        })
83
    end
84
target_end()
zhangyue's avatar
zhangyue committed
85
86
87
88
89
90
91

target("infinirt-kunlun")
    set_kind("static")
    add_deps("infini-utils")
    set_languages("cxx17")
    on_install(function (target) end)
    -- Add include dirs
zhangyue's avatar
zhangyue committed
92
    add_files("$(projectdir)/src/infinirt/kunlun/*.cc")
zhangyue's avatar
zhangyue committed
93
94
95
    add_cxflags("-lstdc++ -Wall -Werror -fPIC")

target_end()