Skip to content

Commit

Permalink
Add support for cache_dir option
Browse files Browse the repository at this point in the history
  • Loading branch information
Chocobo1 committed Dec 30, 2023
1 parent 07de393 commit 64fc5fd
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 10 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,44 @@ jobs:
./hello_world
ubuntu_cache_dir:
name: Ubuntu test cache_dir
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup ccache
id: setup_ccache
uses: ./
with:
ccache_options: |
cache_dir=${{ github.workspace }}/../.ccache
- name: Print environment
run: |
whoami
echo ~
env
pwd
df -h
echo "$PATH"
which ccache
which gcc
ccache --version
ccache --show-config
ccache --show-stats
ls -la "/usr/lib/ccache"
echo "${{ env.ccache_symlinks_path }}"
echo "${{ steps.setup_ccache.outputs.cache_hit }}"
- name: Build test program
run: |
g++ ".github/workflows/hello_world.cpp" -o "hello_world"
./hello_world
ubuntu_20_04:
name: Ubuntu-20.04
runs-on: ubuntu-20.04
Expand Down
34 changes: 25 additions & 9 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Cache from '@actions/cache';
import * as Core from '@actions/core';
import * as Exec from '@actions/exec';
import * as OS from 'os';
import * as Path from 'path';
import * as Process from 'process';
import * as Utils from './utils.js';

Expand Down Expand Up @@ -66,19 +67,30 @@ async function configureCcache() {
await Utils.removeCcacheConfig();

const ccachePath = await Utils.getCcacheBinaryPath();
const settings = Core.getMultilineInput("ccache_options");
for (const setting of settings) {
const keyValue = setting.split("=", 2);
if (keyValue.length == 2) {
const [key, value] = keyValue;
await Exec.exec(Utils.platformExecWrap(`${ccachePath} --set-config "${key.trim()}=${value.trim()}"`));
}
}
const options = getUserCcacheOptions();
for (const [key, value] of options)
await Exec.exec(Utils.platformExecWrap(`${ccachePath} --set-config "${key}=${value}"`));

// `--show-config` is not available on older ccache versions: ubuntu-18.04 have ccache 3.4.1
await Exec.exec(Utils.platformExecWrap(`${ccachePath} -p`));
}

let g_userCcacheOptions: Map<string, string>;
function getUserCcacheOptions(): Map<string, string> {
if (g_userCcacheOptions === undefined) {
g_userCcacheOptions = new Map();
const settings = Core.getMultilineInput("ccache_options");
for (const setting of settings) {
const keyValue = setting.split("=", 2);
if (keyValue.length == 2) {
const [key, value] = keyValue;
g_userCcacheOptions.set(key.trim(), value.trim());
}
}
}
return g_userCcacheOptions;
}

async function installCcache() {
await Core.group("Install ccache", async () => {
try {
Expand Down Expand Up @@ -128,7 +140,11 @@ async function installCcache() {

async function restoreCache(): Promise<boolean> {
return await Core.group("Restore cache", async (): Promise<boolean> => {
const paths = [await Utils.getCachePath()];
const paths = await (async () => {
const options = getUserCcacheOptions();
const userDefinedDir = options.get("cache_dir");
return [Path.normalize(userDefinedDir ?? await Utils.getCachePath())];
})();
const primaryKey = Utils.getOverrideCacheKey().value;
const restoreKeys = Utils.getOverrideCacheKeyFallback();

Expand Down
3 changes: 2 additions & 1 deletion src/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as Cache from '@actions/cache';
import * as Core from '@actions/core';
import * as Exec from '@actions/exec';
import * as Github from '@actions/github';
import * as Path from 'path';
import * as Process from 'process';
import * as Utils from './utils.js';

Expand Down Expand Up @@ -81,7 +82,7 @@ async function saveCache(): Promise<boolean> {
return await Core.group("Store cache", async () => {
Utils.removeCcacheConfig();

const paths = [await Utils.getCachePath()];
const paths = [Path.normalize(await Utils.getCachePath())];

// the cache is immutable by design:
// https://docs.microsoft.com/en-us/azure/devops/pipelines/release/caching?view=azure-devops#use-cache-task
Expand Down

0 comments on commit 64fc5fd

Please sign in to comment.