Forráskód Böngészése

improve linux tools (#324)

* improve linux-tools

* improve linux-tools
ruki 4 éve
szülő
commit
fd300238ea

+ 40 - 0
packages/l/linux-tools/modules/bpftool.lua

@@ -0,0 +1,40 @@
+function load(package)
+    package:add("deps", "libcap", "libelf", "zlib")
+    package:addenv("PATH", "sbin")
+end
+
+function install(package)
+
+    assert(package:is_plat("linux"), "bpftool: only support for linux!")
+
+    local cflags = {}
+    local ldflags = {}
+    for _, dep in ipairs(package:orderdeps()) do
+        local fetchinfo = dep:fetch()
+        if fetchinfo then
+            for _, includedir in ipairs(fetchinfo.includedirs or fetchinfo.sysincludedirs) do
+                table.insert(cflags, "-isystem " .. includedir)
+            end
+            for _, linkdir in ipairs(fetchinfo.linkdirs) do
+                table.insert(ldflags, "-L" .. linkdir)
+            end
+            for _, link in ipairs(fetchinfo.links) do
+                table.insert(ldflags, "-l" .. link)
+            end
+        end
+    end
+
+    local configs = {}
+    table.insert(configs, "EXTRA_CFLAGS=" .. table.concat(cflags, " "))
+    table.insert(configs, "LDFLAGS=" .. table.concat(ldflags, " "))
+
+    os.cd("tools/bpf/bpftool")
+    io.replace("Makefile", "prefix ?= /usr/local", "prefix ?= " .. package:installdir(), {plain = true})
+    io.replace("Makefile", "bash_compdir ?= /usr/share", "bash_compdir ?= " .. package:installdir("share"), {plain = true})
+    import("package.tools.make").build(package, configs)
+    os.vrunv("make", table.join("install", configs))
+end
+
+function test(package)
+    os.vrun("bpftool --version")
+end

+ 31 - 0
packages/l/linux-tools/modules/libbpf.lua

@@ -0,0 +1,31 @@
+function load(package)
+    package:add("deps", "libelf", "zlib")
+end
+
+function install(package)
+    os.cd("tools/lib/bpf")
+    io.writefile("xmake.lua", [[
+        add_rules("mode.debug", "mode.release")
+        add_requires("libelf", "zlib")
+        target("bpf")
+            set_kind("$(kind)")
+            add_files("*.c")
+            add_sysincludedirs("../../include", "../../include/uapi")
+            add_packages("libelf", "zlib")
+            add_headerfiles("*.h", {prefixdir = "bpf"})
+            if is_plat("android") then
+                add_defines("__user=", "__force=", "__poll_t=uint32_t")
+            end
+    ]])
+    local configs = {buildir = "xmakebuild"}
+    if package:config("shared") then
+        configs.kind = "shared"
+    elseif package:config("pic") ~= false then
+        configs.cxflags = "-fPIC"
+    end
+    import("package.tools.xmake").install(package, configs)
+end
+
+function test(package)
+    assert(package:has_cfuncs("bpf_object__open", {includes = "bpf/libbpf.h"}))
+end

+ 16 - 50
packages/l/linux-tools/xmake.lua

@@ -13,67 +13,33 @@ package("linux-tools")
     add_versions("5.0.8",  "11908044e8cce1e093141f8da594708d45d05d0381676ae9aa3d8aeaf7c85435")
     add_versions("5.9.16", "b0d7abae88e5f91893627c645e680a95c818defd1b4fcaf3e2afb4b2b6b4ab86")
 
-    add_configs("bpf",     { description = "Enable bpf tools and libraries.", default = true, type = "boolean"})
+    add_configs("bpftool",     { description = "Enable bpftool.", default = true, type = "boolean"})
+    add_configs("libbpf",      { description = "Enable libbpf library.", default = false, type = "boolean"})
+
+    local modules = {"bpftool", "libbpf"}
 
     on_load(function (package)
-        if package:config("bpf") then
-            package:add("deps", "libcap", "libelf", "zlib")
-            package:addenv("PATH", "sbin")
-            if package:is_plat("linux") and package:is_arch("x86_64") then
-                package:add("links", "bpf")
-                package:add("linkdirs", "lib64")
-                if package:config("shared") then
-                    package:addenv("LD_LIBRARY_PATH", "lib64")
-                end
+        for _, name in ipairs(modules) do
+            if package:config(name) then
+                import("modules." .. name).load(package)
             end
         end
     end)
 
     on_install("linux", function (package)
-        import("package.tools.make")
-
-        local cflags = {}
-        local ldflags = {}
-        for _, dep in ipairs(package:orderdeps()) do
-            local fetchinfo = dep:fetch()
-            if fetchinfo then
-                for _, includedir in ipairs(fetchinfo.includedirs or fetchinfo.sysincludedirs) do
-                    table.insert(cflags, "-isystem " .. includedir)
-                end
-                for _, linkdir in ipairs(fetchinfo.linkdirs) do
-                    table.insert(ldflags, "-L" .. linkdir)
-                end
-                for _, link in ipairs(fetchinfo.links) do
-                    table.insert(ldflags, "-l" .. link)
-                end
+        for _, name in ipairs(modules) do
+            if package:config(name) then
+                local oldir = os.curdir()
+                import("modules." .. name).install(package)
+                os.cd(oldir)
             end
         end
-
-        local configs = {}
-        table.insert(configs, "EXTRA_CFLAGS=" .. table.concat(cflags, " "))
-        table.insert(configs, "LDFLAGS=" .. table.concat(ldflags, " "))
-
-        if package:config("bpf") then
-            local oldir = os.cd("tools/bpf/bpftool")
-            io.replace("Makefile", "prefix ?= /usr/local", "prefix ?= " .. package:installdir(), {plain = true})
-            io.replace("Makefile", "bash_compdir ?= /usr/share", "bash_compdir ?= " .. package:installdir("share"), {plain = true})
-            make.build(package, configs)
-            os.vrunv("make", table.join("install", configs))
-            os.cd("../../lib/bpf")
-            io.replace("Makefile", "prefix ?= /usr/local", "prefix ?= " .. package:installdir(), {plain = true})
-            if not package:config("shared") then
-                io.replace("Makefile", "LIB_TARGET%s-=.-\n", "LIB_TARGET = libbpf.a\n")
-                io.replace("Makefile", "LIB_FILE%s-=.-\n", "LIB_FILE = libbpf.a\n")
-                io.replace("Makefile", "all_cmd: $(CMD_TARGETS) check", "all_cmd: $(CMD_TARGETS)", {plain = true})
-            end
-            os.vrunv("make", table.join("install", configs))
-            os.cd(oldir)
-        end
     end)
 
     on_test(function (package)
-        if package:config("bpf") then
-            assert(package:has_cfuncs("bpf_object__open", {includes = "bpf/libbpf.h"}))
-            os.vrun("bpftool --version")
+        for _, name in ipairs(modules) do
+            if package:config(name) then
+                import("modules." .. name).test(package)
+            end
         end
     end)