Skip to content

Commit 35be2d8

Browse files
committed
Allow using msys2 instead of cygwin
1 parent af1c9cf commit 35be2d8

File tree

7 files changed

+197
-20
lines changed

7 files changed

+197
-20
lines changed

action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ inputs:
3737
description: The prefix of the cache keys.
3838
required: false
3939
default: v1
40+
windows-environment:
41+
description: Whether to use `cygwin` (default) or `msys2` for windows installs.
42+
required: false
4043
github-token:
4144
description: DO NOT SET THIS.
4245
required: false

dist/index.js

Lines changed: 86 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/post/index.js

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/setup-ocaml/src/constants.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ export const ALLOW_PRERELEASE_OPAM = core.getBooleanInput(
8686
},
8787
);
8888

89+
export const WINDOWS_ENVIRONMENT: "cygwin" | "msys2" = (() => {
90+
const input = core.getInput("windows-environment");
91+
92+
if (!(PLATFORM === "windows" || input === "")) {
93+
core.error("windows-environment is only supported on windows");
94+
}
95+
96+
if (input === "msys2" || input === "cygwin") {
97+
return input;
98+
}
99+
100+
if (input !== "") {
101+
core.error("unrecognized value for windows-environment");
102+
}
103+
return "cygwin";
104+
})();
105+
89106
export const CACHE_PREFIX = core.getInput("cache-prefix", {
90107
required: false,
91108
trimWhitespace: true,

packages/setup-ocaml/src/installer.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
saveOpamCache,
1111
} from "./cache.js";
1212
import {
13+
CYGWIN_ROOT,
1314
CYGWIN_ROOT_BIN,
1415
DUNE_CACHE,
1516
DUNE_CACHE_ROOT,
@@ -18,6 +19,7 @@ import {
1819
OPAM_ROOT,
1920
PLATFORM,
2021
RESOLVED_COMPILER,
22+
WINDOWS_ENVIRONMENT,
2123
} from "./constants.js";
2224
import { installDune } from "./dune.js";
2325
import {
@@ -29,7 +31,7 @@ import {
2931
update,
3032
} from "./opam.js";
3133
import { retrieveOpamLocalPackages } from "./packages.js";
32-
import { setupCygwin } from "./windows.js";
34+
import { prepareMsys2, setupCygwin } from "./windows.js";
3335

3436
export async function installer() {
3537
if (core.isDebug()) {
@@ -64,13 +66,24 @@ export async function installer() {
6466
}
6567
const { opamCacheHit, cygwinCacheHit } = await restoreOpamCaches();
6668
if (PLATFORM === "windows") {
67-
await setupCygwin();
68-
if (!cygwinCacheHit) {
69-
await saveCygwinCache();
69+
switch (WINDOWS_ENVIRONMENT) {
70+
case "msys2": {
71+
const msys2Root = await prepareMsys2();
72+
await setupOpam(msys2Root);
73+
break;
74+
}
75+
case "cygwin":
76+
await setupCygwin();
77+
if (!cygwinCacheHit) {
78+
await saveCygwinCache();
79+
}
80+
core.addPath(CYGWIN_ROOT_BIN);
81+
await setupOpam(CYGWIN_ROOT);
82+
break;
7083
}
71-
core.addPath(CYGWIN_ROOT_BIN);
84+
} else {
85+
await setupOpam();
7286
}
73-
await setupOpam();
7487
if (!opamCacheHit) {
7588
await repositoryRemoveAll();
7689
await repositoryAddAll(OPAM_REPOSITORIES);

packages/setup-ocaml/src/opam.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import * as semver from "semver";
88
import {
99
ALLOW_PRERELEASE_OPAM,
1010
ARCHITECTURE,
11-
CYGWIN_ROOT,
1211
GITHUB_TOKEN,
1312
OPAM_DISABLE_SANDBOXING,
1413
PLATFORM,
@@ -86,7 +85,7 @@ async function acquireOpam() {
8685
});
8786
}
8887

89-
async function initializeOpam() {
88+
async function initializeOpam(prefix?: string) {
9089
await core.group("Initialise opam state", async () => {
9190
if (PLATFORM !== "windows") {
9291
try {
@@ -104,7 +103,7 @@ async function initializeOpam() {
104103
const extraOptions = [];
105104
if (PLATFORM === "windows") {
106105
extraOptions.push("--cygwin-local-install");
107-
extraOptions.push(`--cygwin-location=${CYGWIN_ROOT}`);
106+
extraOptions.push(`--cygwin-location=${prefix}`);
108107
}
109108
if (OPAM_DISABLE_SANDBOXING) {
110109
extraOptions.push("--disable-sandboxing");
@@ -119,9 +118,9 @@ async function initializeOpam() {
119118
});
120119
}
121120

122-
export async function setupOpam() {
121+
export async function setupOpam(prefix?: string) {
123122
await acquireOpam();
124-
await initializeOpam();
123+
await initializeOpam(prefix);
125124
}
126125

127126
export async function installOcaml(ocamlCompiler: string) {

packages/setup-ocaml/src/windows.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,58 @@ export async function setupCygwin() {
115115
await io.cp(setup, CYGWIN_ROOT);
116116
});
117117
}
118+
119+
function getPacmanPath(msys2Root: string): string {
120+
return path.join(msys2Root, "usr", "bin", "pacman");
121+
}
122+
123+
async function testMsys2Installation(path: string): Promise<void> {
124+
try {
125+
await fs.access(path);
126+
} catch {
127+
throw new Error(`No msys2 installation found at: ${path}.`);
128+
}
129+
}
130+
131+
async function getMsys2Install(): Promise<[root: string, pacmanPath: string]> {
132+
const msys2Root = process.env.MSYS2_ROOT;
133+
134+
// MSYS2_ROOT takes priority
135+
if (msys2Root) {
136+
await testMsys2Installation(msys2Root);
137+
return [msys2Root, getPacmanPath(msys2Root)];
138+
}
139+
try {
140+
// check for pacman from PATH
141+
const pacmanPath = await io.which("pacman", true);
142+
return [path.dirname(path.dirname(path.dirname(pacmanPath))), pacmanPath];
143+
} catch {
144+
// finally check the default msys directory
145+
const defaultRoot = "C:\\msys64";
146+
testMsys2Installation(defaultRoot);
147+
return [defaultRoot, getPacmanPath(defaultRoot)];
148+
}
149+
}
150+
151+
export async function prepareMsys2(): Promise<string> {
152+
return await core.group("Install needed Msys2 packages", async () => {
153+
const [root, pacmanPath] = await getMsys2Install();
154+
// core update
155+
await exec(pacmanPath, ["-Syu", "--noconfirm"]);
156+
// packages needed for opam
157+
const packages = [
158+
"curl",
159+
"diffutils",
160+
"m4",
161+
"make",
162+
"mingw-w64-i686-gcc",
163+
"mingw-w64-x86_64-gcc",
164+
"patch",
165+
"perl",
166+
"rsync",
167+
"unzip",
168+
];
169+
await exec(pacmanPath, ["-Syu", "--noconfirm", ...packages]);
170+
return root;
171+
});
172+
}

0 commit comments

Comments
 (0)