From 42934990cefe25d78a98e4314a56f9c8b9b6f281 Mon Sep 17 00:00:00 2001 From: star9029 Date: Mon, 2 Dec 2024 17:33:34 +0800 Subject: Use ccache when debug cmake package --- xmake/core/project/policy.lua | 2 ++ xmake/modules/package/tools/cmake.lua | 7 ++++++ .../action/require/impl/actions/install.lua | 27 ++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/xmake/core/project/policy.lua b/xmake/core/project/policy.lua index 1eb28130f..3f7915b84 100644 --- a/xmake/core/project/policy.lua +++ b/xmake/core/project/policy.lua @@ -143,6 +143,8 @@ function policy.policies() ["package.cmake_generator.ninja"] = {description = "Set cmake package use ninja for build", type = "boolean"}, -- Enable msbuild MultiToolTask ["package.msbuild.multi_tool_task"] = {description = "Enable msbuild MultiToolTask.", default = false, type = "boolean"}, + -- C/C++ build cache + ["package.build.ccache"] = {description = "Enable C/C++ package build cache.", default = false, type = "boolean"}, -- Stop to test on the first failure ["test.stop_on_first_failure"] = {description = "Stop to test on the first failure", default = false, type = "boolean"}, -- Return zero as exit code on failure diff --git a/xmake/modules/package/tools/cmake.lua b/xmake/modules/package/tools/cmake.lua index b0bc9f34d..6d7f15dce 100644 --- a/xmake/modules/package/tools/cmake.lua +++ b/xmake/modules/package/tools/cmake.lua @@ -952,6 +952,13 @@ function _get_configs(package, configs, opt) end end _insert_configs_from_envs(configs, envs or {}, opt) + + local ccache = package:data("ccache") + if ccache then + table.insert(configs, "-DCMAKE_C_COMPILER_LAUNCHER=" .. ccache) + table.insert(configs, "-DCMAKE_CXX_COMPILER_LAUNCHER=" .. ccache) + end + return configs end diff --git a/xmake/modules/private/action/require/impl/actions/install.lua b/xmake/modules/private/action/require/impl/actions/install.lua index 04ecd0036..0a6e9b0be 100644 --- a/xmake/modules/private/action/require/impl/actions/install.lua +++ b/xmake/modules/private/action/require/impl/actions/install.lua @@ -23,8 +23,10 @@ import("core.base.option") import("core.base.tty") import("core.package.package", {alias = "core_package"}) import("core.project.target") +import("core.project.project") import("core.platform.platform") import("lib.detect.find_file") +import("lib.detect.find_tool") import("private.action.require.impl.actions.test") import("private.action.require.impl.actions.patch_sources") import("private.action.require.impl.actions.download_resources") @@ -343,6 +345,28 @@ function _enter_package_testenvs(package) package:envs_enter() end +function _enable_ccache(package) + if package:is_local() then + return + end + + if not project.policy("package.build.ccache") then + return + end + + local ccache = find_tool("ccache") + if not ccache then + ccache = find_tool("sccache") + end + + if ccache then + local name = path.basename(ccache.program) + package:data_set("ccache", name) + local ccache_dir = path.join(path.directory(package:cachedir()), name) + os.setenv(name:upper() .. "_DIR", ccache_dir) + end +end + function main(package) @@ -387,6 +411,9 @@ function main(package) -- enter the environments of all package dependencies _enter_package_installenvs(package) + -- set package ccache dir + _enable_ccache(package) + -- do install if script ~= nil then filter.call(script, package, {oldenvs = oldenvs}) -- cgit v1.3.1 From d9bc6250c4cab08285cbeab73293886527098f14 Mon Sep 17 00:00:00 2001 From: star9029 Date: Mon, 2 Dec 2024 22:53:20 +0800 Subject: Improve ccache tool --- xmake/modules/private/action/require/impl/actions/install.lua | 8 ++------ xmake/modules/private/tools/ccache.lua | 10 ++++++---- xmake/rules/platform/linux/driver/driver_modules.lua | 1 - 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/xmake/modules/private/action/require/impl/actions/install.lua b/xmake/modules/private/action/require/impl/actions/install.lua index 0a6e9b0be..1e64be14e 100644 --- a/xmake/modules/private/action/require/impl/actions/install.lua +++ b/xmake/modules/private/action/require/impl/actions/install.lua @@ -26,7 +26,7 @@ import("core.project.target") import("core.project.project") import("core.platform.platform") import("lib.detect.find_file") -import("lib.detect.find_tool") +import("private.tools.ccache") import("private.action.require.impl.actions.test") import("private.action.require.impl.actions.patch_sources") import("private.action.require.impl.actions.download_resources") @@ -354,11 +354,7 @@ function _enable_ccache(package) return end - local ccache = find_tool("ccache") - if not ccache then - ccache = find_tool("sccache") - end - + local ccache = ccache.get() if ccache then local name = path.basename(ccache.program) package:data_set("ccache", name) diff --git a/xmake/modules/private/tools/ccache.lua b/xmake/modules/private/tools/ccache.lua index 4a877d2b1..12bb0cdd0 100644 --- a/xmake/modules/private/tools/ccache.lua +++ b/xmake/modules/private/tools/ccache.lua @@ -23,18 +23,20 @@ import("core.project.config") import("lib.detect.find_tool") -- get ccache tool -function _ccache() +function get() local ccache = _g.ccache if ccache == nil and config.get("ccache") then ccache = find_tool("ccache") + if not ccache then + ccache = find_tool("sccache") + end _g.ccache = ccache or false end return ccache or nil end --- exists ccache? function exists() - return _ccache() ~= nil + return get() ~= nil end -- uses ccache to wrap the program and arguments @@ -44,7 +46,7 @@ end function cmdargv(program, argv) -- uses ccache? - local ccache = _ccache() + local ccache = get() if ccache then -- parse the filename and arguments, e.g. "xcrun -sdk macosx clang" diff --git a/xmake/rules/platform/linux/driver/driver_modules.lua b/xmake/rules/platform/linux/driver/driver_modules.lua index a89a28938..8218a972c 100644 --- a/xmake/rules/platform/linux/driver/driver_modules.lua +++ b/xmake/rules/platform/linux/driver/driver_modules.lua @@ -24,7 +24,6 @@ import("core.project.depend") import("core.cache.memcache") import("lib.detect.find_tool") import("utils.progress") -import("private.tools.ccache") -- get linux-headers sdk function _get_linux_headers_sdk(target) -- cgit v1.3.1