summaryrefslogtreecommitdiff
path: root/xmake/modules/cli/amalgamate.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2023-09-30 21:25:21 +0800
committerruki <[email protected]>2023-09-30 21:25:21 +0800
commite65e3c9f80a79b0931295c2d37f2cec23a43b070 (patch)
tree7b9a743f66de603f0bfa8937444d555f5babc484 /xmake/modules/cli/amalgamate.lua
parent9c0a7245d583e161e9ce4081c1cabf06a6092ac5 (diff)
improve amalgamate
Diffstat (limited to 'xmake/modules/cli/amalgamate.lua')
-rw-r--r--xmake/modules/cli/amalgamate.lua43
1 files changed, 19 insertions, 24 deletions
diff --git a/xmake/modules/cli/amalgamate.lua b/xmake/modules/cli/amalgamate.lua
index e6494914f..b09758691 100644
--- a/xmake/modules/cli/amalgamate.lua
+++ b/xmake/modules/cli/amalgamate.lua
@@ -20,6 +20,7 @@
-- imports
import("core.base.option")
+import("core.base.graph")
import("core.project.config")
import("core.project.task")
import("core.project.project")
@@ -32,6 +33,22 @@ local options =
{nil, "target", "v", nil, "The target name." }
}
+-- generate file
+function _generate_file(inputpaths, outputpath, uniqueid)
+ local outputfile = io.open(outputpath, "w")
+ for _, inputpath in ipairs(inputpaths) do
+ if uniqueid then
+ outputfile:print("#define %s %s", uniqueid, "unity_" .. hash.uuid():split("-", {plain = true})[1])
+ end
+ outputfile:write(io.readfile(inputpath))
+ if uniqueid then
+ outputfile:print("#undef %s", uniqueid)
+ end
+ end
+ outputfile:close()
+ cprint("${bright}%s generated!", outputpath)
+end
+
-- generate code
function _generate_amalgamate_code(target, opt)
@@ -47,18 +64,7 @@ function _generate_amalgamate_code(target, opt)
local sourcekind = sourcebatch.sourcekind
if sourcekind == "cc" or sourcekind == "cxx" then
local outputpath = path.join(outputdir, target:name() .. (sourcekind == "cxx" and ".cpp" or ".c"))
- local outputfile = io.open(outputpath, "w")
- for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
- if uniqueid then
- outputfile:print("#define %s %s", uniqueid, "unity_" .. hash.uuid():split("-", {plain = true})[1])
- end
- outputfile:write(io.readfile(sourcefile))
- if uniqueid then
- outputfile:print("#undef %s", uniqueid)
- end
- end
- outputfile:close()
- cprint("${bright}%s generated!", outputpath)
+ _generate_file(sourcebatch.sourcefiles, outputpath, uniqueid)
end
end
@@ -66,18 +72,7 @@ function _generate_amalgamate_code(target, opt)
local srcheaders = target:headerfiles(includedir)
if srcheaders and #srcheaders > 0 then
local outputpath = path.join(outputdir, target:name() .. ".h")
- local outputfile = io.open(outputpath, "w")
- for _, srcheader in ipairs(srcheaders) do
- if uniqueid then
- outputfile:print("#define %s %s", uniqueid, "unity_" .. hash.uuid():split("-", {plain = true})[1])
- end
- outputfile:write(io.readfile(srcheader))
- if uniqueid then
- outputfile:print("#undef %s", uniqueid)
- end
- end
- outputfile:close()
- cprint("${bright}%s generated!", outputpath)
+ _generate_file(srcheaders, outputpath, uniqueid)
end
end