kunlun.lua 3.08 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")
zhangyue's avatar
zhangyue committed
6
local XBLAS_DIR = path.join(KUNLUN_HOME, "xhpc", "xblas")
7
8

-- Add include dirs
9
10
11
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})
zhangyue's avatar
zhangyue committed
12
add_includedirs(path.join(XBLAS_DIR, "include"), {public = true})
13
14

-- Add link dirs
zhangyue's avatar
zhangyue committed
15
16
add_linkdirs(path.join(XRE_DIR, "so"))
add_linkdirs(path.join(XDNN_DIR, "so"))
zhangyue's avatar
zhangyue committed
17
18
add_linkdirs(path.join(XBLAS_DIR, "so"))
add_links("xpurt", "xpuapi", "xpu_blas")
19

20
21
22
23
24
rule("xpu")
    set_extensions(".xpu")
    
    on_build_file(function (target, sourcefile)

zhangyue's avatar
zhangyue committed
25
26
27
        local sourcefile_config = target:fileconfig(sourcefile) or {}
        local includedirs = sourcefile_config.includedirs or {}

28
29
30
31
        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"), " ")
32
33
34
35
36
        local arch_map = {
            ["x86_64"] = "x86_64-linux-gnu",
            ["arm64"] = "aarch64-linux-gnu"
        }

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

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

57
58
59
60
61
62
63
        table.insert(target:objectfiles(), objectfile)
        print(target:objectfiles())
    end)
rule_end()

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

64
65
target("infiniop-kunlun")
    set_kind("static")
PanZezhong's avatar
PanZezhong committed
66
    add_deps("infini-utils")
67
68
    on_install(function (target) end)

zhangyue's avatar
zhangyue committed
69
    add_cxflags("-lstdc++ -fPIC -Wno-error=unused-function")
70
71
72
73
    set_warnings("all", "error")

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

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
95
    add_files("$(projectdir)/src/infinirt/kunlun/*.cc")
zhangyue's avatar
zhangyue committed
96
97
98
    add_cxflags("-lstdc++ -Wall -Werror -fPIC")

target_end()