Skip to content

Commit

Permalink
Add support for aarch64 Linux
Browse files Browse the repository at this point in the history
Before this change, runners on non-macOS would always succeed by downloading
the x86_64 binary for their respective platform, which is obviously problematic.

To solve this, this change introduces both support for aarch64 Linux, and
processor architecture validation.
  • Loading branch information
winterqt committed Sep 6, 2024
1 parent 3aed395 commit 561549f
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 10 deletions.
8 changes: 8 additions & 0 deletions __tests__/os.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import * as os from "../src/os";
import getArch from "../src/arch";

jest.mock("getos");

jest.mock("../src/arch", () => {
return {
__esModule: true,
default: jest.fn(() => "x64"),
};
});

const setSystem = require("getos").__setSystem;

describe("os resolver", () => {
Expand Down
23 changes: 20 additions & 3 deletions __tests__/swift-versions.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
import { OS, System } from "../src/os";
import * as versions from "../src/swift-versions";

const macOS: System = { os: OS.MacOS, version: "latest", name: "macOS" };
const ubuntu: System = { os: OS.Ubuntu, version: "latest", name: "Ubuntu" };
const windows: System = { os: OS.Windows, version: "latest", name: "Windows" };
const macOS: System = {
os: OS.MacOS,
version: "latest",
name: "macOS",
arch: "arm64",
};

const ubuntu: System = {
os: OS.Ubuntu,
version: "latest",
name: "Ubuntu",
arch: "x64",
};

const windows: System = {
os: OS.Windows,
version: "latest",
name: "Windows",
arch: "x64",
};

describe("swift version resolver", () => {
it("identifies X.X.X versions", async () => {
Expand Down
7 changes: 6 additions & 1 deletion __tests__/visual-studio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ jest.mock("fs", () => {
};
});

const windows: System = { os: OS.Windows, version: "latest", name: "Windows" };
const windows: System = {
os: OS.Windows,
version: "latest",
name: "Windows",
arch: "x64",
};

describe("visual studio resolver", () => {
const env = process.env;
Expand Down
43 changes: 40 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
/******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ({

/***/ 209:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
function getArch() {
return process.arch;
}
exports["default"] = getArch;


/***/ }),

/***/ 951:
/***/ ((__unused_webpack_module, exports, __nccwpck_require__) => {

Expand Down Expand Up @@ -372,6 +386,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.getSystem = exports.OS = void 0;
const getos_1 = __importDefault(__nccwpck_require__(6068));
const arch_1 = __importDefault(__nccwpck_require__(209));
var OS;
(function (OS) {
OS[OS["MacOS"] = 0] = "MacOS";
Expand All @@ -395,23 +410,41 @@ async function getSystem() {
os ? resolve(os) : reject(error || "No OS detected");
});
});
const arch = (0, arch_1.default)();
let system;
switch (detectedSystem.os) {
case "darwin":
system = { os: OS.MacOS, version: "latest", name: "macOS" };
system = {
os: OS.MacOS,
version: "latest",
name: "macOS",
arch,
};
break;
case "linux":
if (detectedSystem.dist !== "Ubuntu") {
throw new Error(`"${detectedSystem.dist}" is not a supported linux distribution`);
throw new Error(`"${detectedSystem.dist}" is not a supported Linux distribution`);
}
if (arch !== "x64" && arch !== "arm64") {
throw new Error(`${arch} is not a supported architecture for Linux`);
}
system = {
os: OS.Ubuntu,
version: detectedSystem.release,
name: "Ubuntu",
arch,
};
break;
case "win32":
system = { os: OS.Windows, version: "latest", name: "Windows" };
if (arch !== "x64") {
throw new Error(`${arch} is not a supported architecture for Windows`);
}
system = {
os: OS.Windows,
version: "latest",
name: "Windows",
arch,
};
break;
default:
throw new Error(`"${detectedSystem.os}" is not a supported platform`);
Expand Down Expand Up @@ -542,7 +575,11 @@ function swiftPackage(version, system) {
break;
case os_1.OS.Ubuntu:
platform = `ubuntu${system.version.replace(/\D/g, "")}`;
if (system.arch === "arm64")
platform += "-aarch64";
archiveName = `swift-${version}-RELEASE-ubuntu${system.version}`;
if (system.arch === "arm64")
archiveName += "-aarch64";
archiveFile = `${archiveName}.tar.gz`;
break;
case os_1.OS.Windows:
Expand Down
3 changes: 3 additions & 0 deletions src/arch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function getArch(): string {
return process.arch;
}
26 changes: 23 additions & 3 deletions src/os.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import getos from "getos";
import getArch from "./arch";

export enum OS {
MacOS,
Expand All @@ -22,6 +23,7 @@ export interface System {
os: OS;
version: string;
name: string;
arch: string;
}

export async function getSystem(): Promise<System> {
Expand All @@ -30,27 +32,45 @@ export async function getSystem(): Promise<System> {
os ? resolve(os) : reject(error || "No OS detected");
});
});
const arch = getArch();

let system: System;

switch (detectedSystem.os) {
case "darwin":
system = { os: OS.MacOS, version: "latest", name: "macOS" };
system = {
os: OS.MacOS,
version: "latest",
name: "macOS",
arch,
};
break;
case "linux":
if (detectedSystem.dist !== "Ubuntu") {
throw new Error(
`"${detectedSystem.dist}" is not a supported linux distribution`
`"${detectedSystem.dist}" is not a supported Linux distribution`
);
}
if (arch !== "x64" && arch !== "arm64") {
throw new Error(`${arch} is not a supported architecture for Linux`);
}
system = {
os: OS.Ubuntu,
version: detectedSystem.release,
name: "Ubuntu",
arch,
};
break;
case "win32":
system = { os: OS.Windows, version: "latest", name: "Windows" };
if (arch !== "x64") {
throw new Error(`${arch} is not a supported architecture for Windows`);
}
system = {
os: OS.Windows,
version: "latest",
name: "Windows",
arch,
};
break;
default:
throw new Error(`"${detectedSystem.os}" is not a supported platform`);
Expand Down
2 changes: 2 additions & 0 deletions src/swift-versions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ export function swiftPackage(version: string, system: System): Package {
break;
case OS.Ubuntu:
platform = `ubuntu${system.version.replace(/\D/g, "")}`;
if (system.arch === "arm64") platform += "-aarch64";
archiveName = `swift-${version}-RELEASE-ubuntu${system.version}`;
if (system.arch === "arm64") archiveName += "-aarch64";
archiveFile = `${archiveName}.tar.gz`;
break;
case OS.Windows:
Expand Down

0 comments on commit 561549f

Please sign in to comment.