cache build cache

This commit is contained in:
ruki
2025-01-15 00:58:15 +08:00
Unverified
parent 993d6256e9
commit 5ac639794f
9 changed files with 87748 additions and 35 deletions
+5 -8
View File
@@ -22,16 +22,13 @@ jobs:
with:
xmake-version: ${{ matrix.version }}
actions-cache-folder: '.xmake-cache'
build-cache-folder: '.xmake-build-cache'
package-cache-folder: '.xmake-package-cache'
build-cache: true
build-cache-path: 'tests/sample/build/.build_cache'
package-cache: true
- name: Run tests
run: |
xmake --version
xmake create test
cd test
echo 'add_packages("libpng")' >> xmake.lua
echo 'target_end()' >> xmake.lua
echo 'add_requires("libpng", {system = false})' >> xmake.lua
xmake -vD -y
cd tests/sample
xmake -y -vD
xmake run
+8 -1
View File
@@ -74,6 +74,14 @@ with:
### Cache build
By default, xmake disables build cache when building on ci, so we need to enable it first.
```bash
$ xmake f --policies=build.ccache:y
```
And xmake v2.9.8 will enable it by default if action/build-cache is enabled.
```yml
uses: xmake-io/github-action-setup-xmake@v1
with:
@@ -84,7 +92,6 @@ with:
Cache build with the specific build path.
```yml
uses: xmake-io/github-action-setup-xmake@v1
with:
+87666
View File
File diff suppressed because one or more lines are too long
+20 -21
View File
@@ -1,19 +1,22 @@
import * as core from '@actions/core';
import { exec } from '@actions/exec';
import * as io from '@actions/io';
import * as toolCache from '@actions/tool-cache';
import * as cache from '@actions/cache';
import * as os from 'os';
import * as fs from 'fs';
import * as path from 'path';
import * as fsutils from './fsutils';
export async function loadBuildCache(): Promise<void> {
let buildCache = core.getInput('build-cache');
const buildCache = core.getBooleanInput('build-cache');
if (!buildCache) {
return;
}
const buildCacheFolder = ".xmake-build-cache";
// export $XMAKE_ACTION_BUILD_CACHE, xmake will check it and enable build cache by default on ci.
core.exportVariable('XMAKE_ACTION_BUILD_CACHE', 'true');
const buildCacheFolder = '.xmake-build-cache';
let buildCacheKey = core.getInput('build-cache-key');
if (!buildCacheKey) {
@@ -25,25 +28,21 @@ export async function loadBuildCache(): Promise<void> {
let buildCachePath = core.getInput('build-cache-path');
if (!buildCachePath) {
buildCachePath = "build/.build_cache";
buildCachePath = 'build/.build_cache';
}
if (buildCacheFolder && process.env.GITHUB_WORKSPACE) {
const fullCachePath = path.join(process.env.GITHUB_WORKSPACE, buildCacheFolder);
try {
try {
fs.accessSync(path.join(fullCachePath, 'build_cache_saved.txt'), fs.constants.X_OK);
} catch {
const filepath = path.join(fullCachePath, 'build_cache_saved.txt');
if (!fsutils.isFile(filepath)) {
core.info(`Restore build cache path: ${fullCachePath} to ${buildCachePath}, key: ${cacheKey}`);
await cache.restoreCache([buildCacheFolder], cacheKey);
}
fs.accessSync(path.join(fullCachePath, 'build_cache_saved.txt'), fs.constants.X_OK);
core.info(`restore build cache path: ${fullCachePath} to ${buildCachePath}, key: ${cacheKey}`);
// restore build cache
if (fsutils.isFile(filepath)) {
await io.cp(fullCachePath, buildCachePath, {
recursive: true,
});
} catch {
} else {
core.warning(`No cached files found at path "${fullCachePath}".`);
await io.rmRF(fullCachePath);
}
@@ -51,12 +50,12 @@ export async function loadBuildCache(): Promise<void> {
}
export async function saveBuildCache(): Promise<void> {
let buildCache = core.getInput('build-cache');
const buildCache = core.getBooleanInput('build-cache');
if (!buildCache) {
return;
}
const buildCacheFolder = ".xmake-build-cache";
const buildCacheFolder = '.xmake-build-cache';
let buildCacheKey = core.getInput('build-cache-key');
if (!buildCacheKey) {
@@ -68,16 +67,16 @@ export async function saveBuildCache(): Promise<void> {
let buildCachePath = core.getInput('build-cache-path');
if (!buildCachePath) {
buildCachePath = "build/.build_cache";
buildCachePath = 'build/.build_cache';
}
if (buildCacheFolder && process.env.GITHUB_WORKSPACE) {
cacheDir = path.join(process.env.GITHUB_WORKSPACE, buildCacheFolder);
core.info(`save build cache path: ${buildCachePath} to ${cacheDir}, key: ${cacheKey}`);
await io.cp(buildCachePath, cacheDir, {
if (buildCacheFolder && process.env.GITHUB_WORKSPACE && fsutils.isDir(buildCachePath)) {
const fullCachePath = path.join(process.env.GITHUB_WORKSPACE, buildCacheFolder);
core.info(`Save build cache path: ${buildCachePath} to ${fullCachePath}, key: ${cacheKey}`);
await io.cp(buildCachePath, fullCachePath, {
recursive: true,
});
await exec("xmake", ["l", "io.touch", path.join(cacheDir, "build_cache_saved.txt")]);
await exec('xmake', ['l', 'os.touch', path.join(fullCachePath, 'build_cache_saved.txt')]);
await cache.saveCache([buildCacheFolder], cacheKey);
}
}
+22
View File
@@ -0,0 +1,22 @@
import * as fs from 'fs';
export function isFile(filepath: string): boolean {
try {
fs.accessSync(filepath, fs.constants.F_OK);
return true;
} catch {
return false;
}
}
export function isDir(filepath: string): boolean {
try {
const stats = fs.statSync(filepath);
if (stats.isDirectory()) {
return true;
}
return false;
} catch {
return false;
}
}
-1
View File
@@ -1,7 +1,6 @@
import * as core from '@actions/core';
import { exec } from '@actions/exec';
import * as io from '@actions/io';
import * as toolCache from '@actions/tool-cache';
import * as cache from '@actions/cache';
import * as os from 'os';
import * as fs from 'fs';
+8
View File
@@ -0,0 +1,8 @@
# Xmake cache
.xmake/
build/
# MacOS Cache
.DS_Store
+6
View File
@@ -0,0 +1,6 @@
#include <iostream>
int main(int argc, char** argv) {
std::cout << "hello world!" << std::endl;
return 0;
}
+9
View File
@@ -0,0 +1,9 @@
add_rules("mode.debug", "mode.release")
add_requires("libpng", {system = false})
target("sample")
set_kind("binary")
add_files("src/*.cpp")
add_packages("libpng")