Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 29 additions & 57 deletions buildtools/xmake/main.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import("core.base.json")
import("core.base.option")

local function xmake_program()
return os.programfile() or "xmake"
end
import("core.project.project")
import("core.project.config")

local function get_d2x_lang()
local d2x_json_file = path.join(os.projectdir(), ".d2x.json")
Expand All @@ -14,61 +12,32 @@ local function get_d2x_lang()
return nil
end

local function apply_project_config()
function list()

config.load()
local lang = get_d2x_lang()
if lang and #lang > 0 then
os.execv(xmake_program(), {"f", "--lang=" .. lang})
config.set("lang", lang, {force = true})
end
end

local function print_target_files(name)
local target_output = try {function ()
return os.iorunv(xmake_program(), {"show", "-t", name, "--json"})
end}
if not target_output or #target_output == 0 then
return
end
local target_info = try {function ()
return json.decode(target_output)
end}
local flag = true
local targets = project.targets()
for name, _ in pairs(targets) do
local files = targets[name]:sourcefiles()
local flag = true

printf(name)
local files = (target_info and target_info.files) or {}
for _, fileinfo in ipairs(files) do
local file = fileinfo.path or fileinfo
file = path.absolute(file)
if flag then
printf("@ " .. file)
flag = false
else
printf(", " .. file)
-- cprintf / print xxx
printf(name) -- target name
for _, file in ipairs(files) do
file = path.absolute(file)
if flag then
--printf(": " .. file) -- avoid C:\ issue on Windows
printf("@ " .. file)
flag = false
else
printf(", " .. file)
end
end
end
printf("\n")
end

function list(target)
if target and #target > 0 then
print_target_files(target)
return
end

-- if target is nil, list all targets
local target_names_output = try {function ()
return os.iorunv(xmake_program(), {"show", "-l", "targets", "--json"})
end}
if not target_names_output or #target_names_output == 0 then
return
end
local target_names = try {function ()
return json.decode(target_names_output)
end}
if not target_names or #target_names == 0 then
return
end
for _, name in ipairs(target_names) do
print_target_files(name)
printf("\n")
end
end

Expand All @@ -81,13 +50,16 @@ function main()
--print("project file: " .. project.rootfile())

if command == "init" then
apply_project_config()
local lang = get_d2x_lang()
if lang and #lang > 0 then
os.exec("xmake f --lang=" .. lang)
end
elseif command == "list" then
list(target)
list()
elseif command == "build" then
os.execv(xmake_program(), {"build", target or ""})
os.exec("xmake build " .. (target or ""))
elseif command == "run" then
os.execv(xmake_program(), {"run", target or ""})
os.exec("xmake run " .. (target or ""))
else
print("Unknown command: " .. tostring(command))
end
Expand Down