release 0.1.13
[liba.git] / python / xmake.lua
blob99a20706b7f7a0100ea8669492834c3c474048af
1 ---@diagnostic disable
2 -- option: liba-python
3 option("liba-python")
4 set_showmenu(true)
5 set_category("liba")
6 set_values("python3", "python2")
7 set_description("Enable/Disable Python")
8 option_end()
10 rule("cython")
11 set_extensions(".pyx")
12 on_load(function(target)
13 import("lib.detect.find_tool")
14 local python = get_config("liba-python"):split("::")
15 local python = assert(find_tool(python[#python]), "python not found!")
16 local cython = assert(find_tool("cython3") or find_tool("cython"), "cython not found!")
17 local suffix = os.iorunv(python.program, {
18 "-c",
19 'import sysconfig;print(sysconfig.get_config_var("EXT_SUFFIX") or sysconfig.get_config_var("SO"))',
21 local prefix = os.iorunv(python.program, { "-c", 'import sysconfig;print(sysconfig.get_path("data"))' })
22 local platlib = os.iorunv(python.program, { "-c", 'import sysconfig;print(sysconfig.get_path("platlib"))' })
23 target:set("cython", cython.program)
24 target:set("kind", "shared")
25 target:set("prefixname", "")
26 target:set("extension", suffix:trim())
27 target:set("installdir", prefix:trim())
28 target:add("runenvs", "PYTHONPATH", target:targetdir())
29 target:set("platlib", path.relative(platlib:trim(), prefix:trim()))
30 local python = target:pkg(get_config("liba-python"))
31 local links = python:get("links")
32 if type(links) ~= "table" then
33 links = {}
34 end
35 for _, link in ipairs(links) do
36 if link:startswith("py") then
37 python:set("links", link)
38 break
39 end
40 end
41 end)
42 on_buildcmd_file(function(target, batchcmds, sourcefile, opt)
43 local sourcefile_c = sourcefile:replace(".pyx", ".c")
44 local objectfile = target:objectfile(sourcefile_c)
45 table.insert(target:objectfiles(), objectfile)
47 batchcmds:show_progress(opt.progress, "${color.build.object}compiling.cython %s", sourcefile)
48 batchcmds:vrunv(target:get("cython"), { "--fast-fail", sourcefile })
50 batchcmds:show_progress(opt.progress, "${color.build.object}compiling.$(mode) %s", sourcefile_c)
51 batchcmds:compile(sourcefile_c, objectfile)
53 batchcmds:add_depfiles(sourcefile)
54 batchcmds:set_depmtime(os.mtime(objectfile))
55 batchcmds:set_depcache(target:dependfile(objectfile))
56 end)
57 rule_end()
59 if has_config("liba-python") then
60 add_requires(get_config("liba-python"), { system = true })
61 target("apy")
62 add_rules("cython")
63 add_defines("A_EXPORTS")
64 add_files("src/liba.pyx")
65 on_install(function(target)
66 local installdir = target:installdir()
67 if installdir then
68 print("installing %s to %s ..", target:name(), installdir)
69 installdir = path.join(installdir, target:get("platlib"))
70 os.mkdir(installdir)
71 os.vcp(target:targetfile(), installdir)
72 for _, installfile in ipairs(target:installfiles()) do
73 os.vcp(installfile, installdir)
74 end
75 end
76 end)
77 add_packages(get_config("liba-python"))
78 add_installfiles("liba.pyi")
79 set_basename("liba")
80 set_warnings("all")
81 set_targetdir(".")
82 add_deps("a")
83 target_end()
84 end