summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstar9029 <[email protected]>2024-12-02 17:33:34 +0800
committerstar9029 <[email protected]>2024-12-02 17:33:34 +0800
commit42934990cefe25d78a98e4314a56f9c8b9b6f281 (patch)
tree099699221f2bf52886fc5816c1f7b677dc76bac1
parent71a5624d37cdfaaba039a874e67341ee354357b7 (diff)
Use ccache when debug cmake package
-rw-r--r--xmake/core/project/policy.lua2
-rw-r--r--xmake/modules/package/tools/cmake.lua7
-rw-r--r--xmake/modules/private/action/require/impl/actions/install.lua27
3 files changed, 36 insertions, 0 deletions
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})