From 87e3ab369eb97e7402aed596d2ec90c2741a605b Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 19 Oct 2021 00:52:04 +0800 Subject: add utils.amalgamate --- xmake/modules/utils/amalgamate.lua | 44 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 xmake/modules/utils/amalgamate.lua diff --git a/xmake/modules/utils/amalgamate.lua b/xmake/modules/utils/amalgamate.lua new file mode 100644 index 000000000..9d2f38209 --- /dev/null +++ b/xmake/modules/utils/amalgamate.lua @@ -0,0 +1,44 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file amalgamate.lua +-- + +-- imports +import("core.base.option") +import("core.project.config") +import("core.project.project") + +-- the options +local options = +{ + {'o', "outputdir", "kv", nil, "Set the output directory."}, + {nil, "target", "v", nil, "The target name." } +} + +-- generate amalgamate code +-- +-- https://github.com/xmake-io/xmake/issues/1438 +-- +function main(...) + + -- parse arguments + local argv = table.pack(...) + local args = option.parse(argv, options, "Generate amalgamate code." + , "" + , "Usage: xmake l utils.amalgamate [options]") +end -- cgit v1.3.1 From 632b064c26ed87080d891d193cf959ab68f09651 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 19 Oct 2021 00:57:06 +0800 Subject: impl cli.amalgamate --- xmake/modules/cli/amalgamate.lua | 103 +++++++++++++++++++++++++++++++++++++ xmake/modules/utils/amalgamate.lua | 44 ---------------- 2 files changed, 103 insertions(+), 44 deletions(-) create mode 100644 xmake/modules/cli/amalgamate.lua delete mode 100644 xmake/modules/utils/amalgamate.lua diff --git a/xmake/modules/cli/amalgamate.lua b/xmake/modules/cli/amalgamate.lua new file mode 100644 index 000000000..1657fbadf --- /dev/null +++ b/xmake/modules/cli/amalgamate.lua @@ -0,0 +1,103 @@ +--!A cross-platform build utility based on Lua +-- +-- Licensed under the Apache License, Version 2.0 (the "License"); +-- you may not use this file except in compliance with the License. +-- You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- +-- Copyright (C) 2015-present, TBOOX Open Source Group. +-- +-- @author ruki +-- @file amalgamate.lua +-- + +-- imports +import("core.base.option") +import("core.project.config") +import("core.project.task") +import("core.project.project") + +-- the options +local options = +{ + {'u', "uniqueid", "kv", nil, "Set the unique id." }, + {'o', "outputdir", "kv", nil, "Set the output directory."}, + {nil, "target", "v", nil, "The target name." } +} + +-- generate code +function _generate_amalgamate_code(target, opt) + + -- generate source code + local outputdir = opt.outputdir + local uniqueid = opt.uniqueid + for _, sourcebatch in pairs(target:sourcebatches()) do + 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) + end + end + + -- generate header file + local srcheaders = target:headerfiles(includedir) + if srcheaders 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) + end +end + +-- generate amalgamate code +-- +-- https://github.com/xmake-io/xmake/issues/1438 +-- +function main(...) + + -- parse arguments + local argv = table.pack(...) + local args = option.parse(argv, options, "Generate amalgamate code.", + "", + "Usage: xmake l cli.amalgamate [options]") + + -- config first + task.run("config") + + -- generate amalgamate code + args.outputdir = args.outputdir or config.buildir() + if args.target then + _generate_amalgamate_code(args.target, args) + else + for _, target in ipairs(project.ordertargets()) do + _generate_amalgamate_code(target, args) + end + end +end diff --git a/xmake/modules/utils/amalgamate.lua b/xmake/modules/utils/amalgamate.lua deleted file mode 100644 index 9d2f38209..000000000 --- a/xmake/modules/utils/amalgamate.lua +++ /dev/null @@ -1,44 +0,0 @@ ---!A cross-platform build utility based on Lua --- --- Licensed under the Apache License, Version 2.0 (the "License"); --- you may not use this file except in compliance with the License. --- You may obtain a copy of the License at --- --- http://www.apache.org/licenses/LICENSE-2.0 --- --- Unless required by applicable law or agreed to in writing, software --- distributed under the License is distributed on an "AS IS" BASIS, --- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. --- See the License for the specific language governing permissions and --- limitations under the License. --- --- Copyright (C) 2015-present, TBOOX Open Source Group. --- --- @author ruki --- @file amalgamate.lua --- - --- imports -import("core.base.option") -import("core.project.config") -import("core.project.project") - --- the options -local options = -{ - {'o', "outputdir", "kv", nil, "Set the output directory."}, - {nil, "target", "v", nil, "The target name." } -} - --- generate amalgamate code --- --- https://github.com/xmake-io/xmake/issues/1438 --- -function main(...) - - -- parse arguments - local argv = table.pack(...) - local args = option.parse(argv, options, "Generate amalgamate code." - , "" - , "Usage: xmake l utils.amalgamate [options]") -end -- cgit v1.3.1 From 63a7afefde2caf98b3b4a9114b6050f9d4baecd4 Mon Sep 17 00:00:00 2001 From: ruki Date: Tue, 19 Oct 2021 22:34:39 +0800 Subject: improve amalgamate --- xmake/modules/cli/amalgamate.lua | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/xmake/modules/cli/amalgamate.lua b/xmake/modules/cli/amalgamate.lua index 1657fbadf..e495ae70b 100644 --- a/xmake/modules/cli/amalgamate.lua +++ b/xmake/modules/cli/amalgamate.lua @@ -35,6 +35,11 @@ local options = -- generate code function _generate_amalgamate_code(target, opt) + -- only for library + if not target:is_library() then + return + end + -- generate source code local outputdir = opt.outputdir local uniqueid = opt.uniqueid @@ -59,7 +64,7 @@ function _generate_amalgamate_code(target, opt) -- generate header file local srcheaders = target:headerfiles(includedir) - if srcheaders then + 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 -- cgit v1.3.1