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 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 xmake/modules/cli/amalgamate.lua (limited to 'xmake/modules/cli/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 -- 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(-) (limited to 'xmake/modules/cli/amalgamate.lua') 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 From 6cc23f1211ea6f635393e9d6a02ef93857bca9ab Mon Sep 17 00:00:00 2001 From: ruki Date: Wed, 20 Oct 2021 00:43:14 +0800 Subject: fix cmake importfiles for headeronly --- xmake/modules/cli/amalgamate.lua | 4 ++-- xmake/modules/target/action/install/cmake_importfiles.lua | 14 +++++++------- .../scripts/cmake_importfiles/xxxTargets-headeronly.cmake | 9 --------- 3 files changed, 9 insertions(+), 18 deletions(-) delete mode 100644 xmake/scripts/cmake_importfiles/xxxTargets-headeronly.cmake (limited to 'xmake/modules/cli/amalgamate.lua') diff --git a/xmake/modules/cli/amalgamate.lua b/xmake/modules/cli/amalgamate.lua index e495ae70b..a9835eda6 100644 --- a/xmake/modules/cli/amalgamate.lua +++ b/xmake/modules/cli/amalgamate.lua @@ -35,8 +35,8 @@ local options = -- generate code function _generate_amalgamate_code(target, opt) - -- only for library - if not target:is_library() then + -- only for library/binary + if not target:is_library() and not target:is_binary() then return end diff --git a/xmake/modules/target/action/install/cmake_importfiles.lua b/xmake/modules/target/action/install/cmake_importfiles.lua index ea53ab519..c566b4471 100644 --- a/xmake/modules/target/action/install/cmake_importfiles.lua +++ b/xmake/modules/target/action/install/cmake_importfiles.lua @@ -41,7 +41,7 @@ function _get_builtinvars(target, installdir) return {TARGETNAME = target:name(), PROJECTNAME = project.name() or target:name(), TARGETFILENAME = target:targetfile() and _get_libfile(target, installdir), - TARGETKIND = target:is_shared() and "SHARED" or "STATIC", + TARGETKIND = target:is_headeronly() and "interface" or (target:is_shared() and "SHARED" or "STATIC"), PACKAGE_VERSION = target:get("version") or "1.0.0", TARGET_PTRBYTES = target:is_arch("x86", "i386") and "4" or "8"} end @@ -151,12 +151,12 @@ function main(target, opt) _append_cmake_configfile(target, installdir, "xxxConfig.cmake", opt) _install_cmake_configfile(target, installdir, "xxxConfigVersion.cmake", opt) _install_cmake_targetfile(target, installdir, "xxxTargets.cmake", opt) - if target:is_headeronly() then - _install_cmake_targetfile(target, installdir, "xxxTargets-headeronly.cmake", opt) - elseif is_mode("debug") then - _install_cmake_targetfile(target, installdir, "xxxTargets-debug.cmake", opt) - else - _install_cmake_targetfile(target, installdir, "xxxTargets-release.cmake", opt) + if not target:is_headeronly() then + if is_mode("debug") then + _install_cmake_targetfile(target, installdir, "xxxTargets-debug.cmake", opt) + else + _install_cmake_targetfile(target, installdir, "xxxTargets-release.cmake", opt) + end end end diff --git a/xmake/scripts/cmake_importfiles/xxxTargets-headeronly.cmake b/xmake/scripts/cmake_importfiles/xxxTargets-headeronly.cmake deleted file mode 100644 index c35a310a4..000000000 --- a/xmake/scripts/cmake_importfiles/xxxTargets-headeronly.cmake +++ /dev/null @@ -1,9 +0,0 @@ -#---------------------------------------------------------------- -# Generated CMake target import file for configuration "Headeronly". -#---------------------------------------------------------------- - -# Commands may need to know the format version. -set(CMAKE_IMPORT_FILE_VERSION 1) - -# Commands beyond this point should not need to know the version. -set(CMAKE_IMPORT_FILE_VERSION) -- cgit v1.3.1