-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.ts
279 lines (241 loc) · 8.52 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
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';
async function addSymlinksToPath() {
const windowsCompileEnvironment = Core.getInput("windows_compile_environment");
if ((Process.platform === 'win32') && (windowsCompileEnvironment === 'msvc'))
{
// `choco` installer already put it on %PATH%
return;
}
await Core.group("Prepend ccache symlinks path to $PATH", async () => {
switch (Process.platform) {
case 'darwin':
case 'linux': {
const symlinks = await Utils.getCcacheSymlinksPath();
Core.info(`ccache symlinks path: "${symlinks}"`);
Core.addPath(symlinks);
Core.info(`PATH=${Process.env.PATH}`);
break;
}
case 'win32':
switch (windowsCompileEnvironment) {
case 'msys2': {
const symlinks = await Utils.getCcacheSymlinksPath();
Core.info(`ccache symlinks path (msys): "${symlinks}"`);
// adjust system PATH
Core.addPath((await Utils.getMsysInstallationPath()) + symlinks);
// adjust PATH within msys
const execOptions = {
"silent": true
};
await Exec.exec(Utils.platformExecWrap(`echo "export PATH=${symlinks}:\\$PATH" >> ~/.bash_profile`), undefined, execOptions);
const pathOutput = await Exec.getExecOutput(Utils.platformExecWrap("echo PATH=$PATH"), undefined, execOptions);
Core.info(`(msys) ${pathOutput.stdout.trim()}`);
break;
}
}
break;
default:
break;
}
});
}
async function checkCcacheAvailability() {
const ccachePath = await Utils.getCcacheBinaryPath();
if (ccachePath.length <= 0)
throw Error("Cannot find ccache on PATH");
Core.info(`Found ccache at: "${ccachePath}"`);
await Exec.exec(Utils.platformExecWrap(`${ccachePath} --version`));
}
async function configureCcache() {
// need to regenerate the config file for each run
await Utils.removeCcacheConfig();
const ccachePath = await Utils.getCcacheBinaryPath();
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 {
switch (Process.platform) {
case 'darwin': {
const execOptions = {
"env": {
"HOME": OS.homedir(), // https://github.com/Homebrew/brew/commit/4184546067cb33e8d4de3c0ce9b5e6f1ebf7daca
"HOMEBREW_NO_INSTALL_CLEANUP": "1",
"HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK": "1"
},
"ignoreReturnCode": true
};
const exitCode = await Exec.exec("brew install ccache", undefined, execOptions);
if ((exitCode !== 0) && (exitCode !== 1)) // `brew` returns `1` even when installation succeeded
throw Error(`Error occurred at \`brew install\`. Exit code: "${exitCode}"`);
}
break;
case 'linux':
await Exec.exec(Utils.sudoCommandWrap("apt install -y ccache"));
break;
case 'win32':
switch (Core.getInput("windows_compile_environment")) {
case 'msvc':
await Exec.exec("choco install ccache -y");
break;
case 'msys2':
await Exec.exec(Utils.platformExecWrap(`pacman --sync --noconfirm ${Utils.msysPackagePrefix()}ccache`));
break;
}
break;
default:
break;
}
}
catch (error) {
if (error instanceof Error)
Core.warning(error.message);
else
throw error;
}
});
}
async function restoreCache(): Promise<boolean> {
return await Core.group("Restore cache", async (): Promise<boolean> => {
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();
Core.info(`Retrieving cache with \`primaryKey\`: "${primaryKey}", \`restoreKeys\`: "${restoreKeys}", \`paths\`: "${paths}"`);
try {
const cacheKey = await Cache.restoreCache(paths, primaryKey, restoreKeys);
if (cacheKey) {
Core.info(`Cache found at: "${cacheKey}"`);
Core.exportVariable(Utils.foundCacheKey, cacheKey);
}
else {
Core.info("Cache not found...");
}
return cacheKey ? true : false;
}
catch (error) {
if (error instanceof Error)
Core.warning(`Error occurred in \`Cache.restoreCache()\`. Error message: "${error.message}"`);
else
throw error;
}
return false;
});
}
async function setOutputVariables() {
const envVars = new Map([
["ccache_symlinks_path", await Utils.getCcacheSymlinksPath()]
]);
for (const [key, value] of envVars) {
Core.exportVariable(key, value);
Core.info(`\${{ env.${key} }} = ${value}`);
}
}
async function updatePackgerIndex() {
const windowsCompileEnvironment = Core.getInput("windows_compile_environment");
if ((Process.platform === 'win32') && (windowsCompileEnvironment === 'msvc'))
{
// `choco` installer doesn't need this
return;
}
await Core.group("Update packager index", async () => {
switch (Process.platform) {
case 'darwin':
await Exec.exec("brew update");
break;
case 'linux':
await Exec.exec(Utils.sudoCommandWrap("apt update"));
break;
case 'win32':
switch (windowsCompileEnvironment) {
case 'msys2':
await Exec.exec(Utils.platformExecWrap(`pacman --sync --refresh`));
break;
}
break;
default:
break;
}
});
}
export default async function main(): Promise<void> {
// hide annoying nodejs deprecation warnings
Process.removeAllListeners('warning');
try {
if (!Utils.isSupportedPlatform()) {
if (Process.platform === "win32") {
const env = Core.getInput("windows_compile_environment");
Core.warning(`"windows_compile_environment=${env}" is not supported. No operation...`);
}
else {
Core.warning(`setup-ccache-action only support the following platforms: ["macos", "ubuntu", "windows"]. No operation...`);
}
return;
}
if (Core.getBooleanInput("update_packager_index"))
await updatePackgerIndex();
else
Core.info("Skip update packager index...");
if (Core.getBooleanInput("install_ccache"))
await installCcache();
else
Core.info("Skip install ccache...");
await Core.group("Check ccache availability", async () => {
await checkCcacheAvailability();
});
let cacheHit = false;
if (Core.getBooleanInput("restore_cache"))
cacheHit = await restoreCache();
else
Core.info("Skip restore cache...");
await Core.group(`Set output variable: cache_hit="${cacheHit}"`, async () => {
Core.setOutput("cache_hit", cacheHit.toString());
});
await Core.group("Configure ccache", async () => {
await configureCcache();
});
await Core.group("Clear ccache statistics", async () => {
await Exec.exec(Utils.platformExecWrap("ccache --zero-stats"));
});
if (Core.getBooleanInput("prepend_symlinks_to_path"))
await addSymlinksToPath();
else
Core.info("Skip prepend ccache symlinks path to $PATH...");
await Core.group("Create environment variables", async () => {
await setOutputVariables();
});
}
catch (error) {
if (error instanceof Error)
Core.warning(error.message);
}
}
main();