From 726de2822472f8c18a325d37d98253313805304d Mon Sep 17 00:00:00 2001 From: Saikari Date: Tue, 27 Jan 2026 02:28:45 +0300 Subject: add dependency file generation for Nim source files --- xmake/rules/nim/build/target.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/xmake/rules/nim/build/target.lua b/xmake/rules/nim/build/target.lua index 7db19a559..f8abfef1f 100644 --- a/xmake/rules/nim/build/target.lua +++ b/xmake/rules/nim/build/target.lua @@ -66,6 +66,41 @@ function build_sourcefiles(target, sourcebatch, opt) dependinfo.files = {} assert(compinst:build(sourcefiles, targetfile, {target = target, dependinfo = dependinfo, compflags = compflags})) + -- generate dependency file (.deps) + local flags = {} + local nimcache + for _, flag in ipairs(compflags) do + table.insert(flags, flag) + if flag:startswith("--nimcache:") then + nimcache = flag:sub(12) + end + end + table.insert(flags, "--genScript") + + -- run nim --genScript to generate .deps file + local program = compinst:program() + local argv = table.join("c", flags, sourcefiles) + os.runv(program, argv, {envs = compinst:_tool():runenvs()}) + + -- parse .deps file + if nimcache then + for _, sourcefile in ipairs(sourcefiles) do + local filename = path.basename(sourcefile) + local depsfile = path.join(nimcache, filename .. ".deps") + if os.isfile(depsfile) then + local depsdata = io.readfile(depsfile) + if depsdata then + for _, line in ipairs(depsdata:split("\n")) do + line = line:trim() + if #line > 0 then + table.insert(dependinfo.files, line) + end + end + end + end + end + end + -- update files and values to the dependent file dependinfo.values = depvalues table.join2(dependinfo.files, sourcefiles) -- cgit v1.3.1 From 23257c45322717546109effca33421ed6a20a028 Mon Sep 17 00:00:00 2001 From: Saikari Date: Tue, 27 Jan 2026 06:06:24 +0300 Subject: fix: update environment variable retrieval in build_sourcefiles function --- xmake/rules/nim/build/target.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xmake/rules/nim/build/target.lua b/xmake/rules/nim/build/target.lua index f8abfef1f..a24b3c7c2 100644 --- a/xmake/rules/nim/build/target.lua +++ b/xmake/rules/nim/build/target.lua @@ -80,7 +80,7 @@ function build_sourcefiles(target, sourcebatch, opt) -- run nim --genScript to generate .deps file local program = compinst:program() local argv = table.join("c", flags, sourcefiles) - os.runv(program, argv, {envs = compinst:_tool():runenvs()}) + os.runv(program, argv, {envs = compinst:runenvs()}) -- parse .deps file if nimcache then -- cgit v1.3.1 From 17344133cb2b52d04c0ad26ca9b8d61b5280aae9 Mon Sep 17 00:00:00 2001 From: Saikari Date: Tue, 27 Jan 2026 06:07:56 +0300 Subject: refactor: extract dependency generation logic into a separate function --- xmake/rules/nim/build/target.lua | 73 ++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/xmake/rules/nim/build/target.lua b/xmake/rules/nim/build/target.lua index a24b3c7c2..52599b74d 100644 --- a/xmake/rules/nim/build/target.lua +++ b/xmake/rules/nim/build/target.lua @@ -25,6 +25,45 @@ import("core.tool.compiler") import("core.project.depend") import("utils.progress") +-- generate dependency info +function _generate_dependinfo(compinst, compflags, sourcefiles, dependinfo) + + -- generate dependency file (.deps) + local flags = {} + local nimcache + for _, flag in ipairs(compflags) do + table.insert(flags, flag) + if flag:startswith("--nimcache:") then + nimcache = flag:sub(12) + end + end + table.insert(flags, "--genScript") + + -- run nim --genScript to generate .deps file + local program = compinst:program() + local argv = table.join("c", flags, sourcefiles) + os.runv(program, argv, {envs = compinst:runenvs()}) + + -- parse .deps file + if nimcache then + for _, sourcefile in ipairs(sourcefiles) do + local filename = path.basename(sourcefile) + local depsfile = path.join(nimcache, filename .. ".deps") + if os.isfile(depsfile) then + local depsdata = io.readfile(depsfile) + if depsdata then + for _, line in ipairs(depsdata:split("\n")) do + line = line:trim() + if #line > 0 then + table.insert(dependinfo.files, line) + end + end + end + end + end + end +end + -- build the source files function build_sourcefiles(target, sourcebatch, opt) @@ -67,39 +106,7 @@ function build_sourcefiles(target, sourcebatch, opt) assert(compinst:build(sourcefiles, targetfile, {target = target, dependinfo = dependinfo, compflags = compflags})) -- generate dependency file (.deps) - local flags = {} - local nimcache - for _, flag in ipairs(compflags) do - table.insert(flags, flag) - if flag:startswith("--nimcache:") then - nimcache = flag:sub(12) - end - end - table.insert(flags, "--genScript") - - -- run nim --genScript to generate .deps file - local program = compinst:program() - local argv = table.join("c", flags, sourcefiles) - os.runv(program, argv, {envs = compinst:runenvs()}) - - -- parse .deps file - if nimcache then - for _, sourcefile in ipairs(sourcefiles) do - local filename = path.basename(sourcefile) - local depsfile = path.join(nimcache, filename .. ".deps") - if os.isfile(depsfile) then - local depsdata = io.readfile(depsfile) - if depsdata then - for _, line in ipairs(depsdata:split("\n")) do - line = line:trim() - if #line > 0 then - table.insert(dependinfo.files, line) - end - end - end - end - end - end + _generate_dependinfo(compinst, compflags, sourcefiles, dependinfo) -- update files and values to the dependent file dependinfo.values = depvalues -- cgit v1.3.1