From 46cc30329b0fbef3bbfa93550259ac39231183b5 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Tue, 14 Sep 2021 07:34:14 -0500 Subject: [PATCH] fix: support llvm 8 and 7 for windows --- src/llvm/__tests__/llvm.test.ts | 42 +++++++++++++++++++-------------- src/llvm/llvm.ts | 30 ++++++++++++++++------- src/utils/http/validate_url.ts | 13 ++++++++++ 3 files changed, 58 insertions(+), 27 deletions(-) create mode 100644 src/utils/http/validate_url.ts diff --git a/src/llvm/__tests__/llvm.test.ts b/src/llvm/__tests__/llvm.test.ts index bc18dddf..c506b73e 100644 --- a/src/llvm/__tests__/llvm.test.ts +++ b/src/llvm/__tests__/llvm.test.ts @@ -1,31 +1,37 @@ import { getSpecificVersionAndUrl } from "../llvm" -import * as https from "https" +import { isValidUrl } from "../../utils/http/validate_url" jest.setTimeout(100000) -function testUrl(version: string) { - const [specificVersion, url] = getSpecificVersionAndUrl(process.platform, version) +async function testUrl(version: string) { + const [specificVersion, url] = await getSpecificVersionAndUrl(process.platform, version) - const input = `Version: ${version} => ${specificVersion} \n URL: ${url}` - - return new Promise((resolve, reject) => { - https.get(url, (res) => { - const report = `${input}\nStatus: ${res.statusCode}\nContent-Length: ${res.headers["content-length"]}` - if (res.statusCode !== undefined && res.statusCode >= 200 && res.statusCode <= 399) { - resolve(report) - } else { - reject(new Error(`Failed to download LLVM and Clang binaries.\n${input}\n${report}`)) - } - }) - }) + if (!(await isValidUrl(url))) { + throw new Error(`Failed to install Version: ${version} => ${specificVersion} \n URL: ${url}`) + } } describe("setup-llvm", () => { it("Finds valid LLVM URLs", async () => { await Promise.all( - ["12.0.0", "12", "11", "11.0.0", "10", "10.0.0", "9.0.0", "8.0.0", "7.0.0", "6", "6.0.0", "5", "5.0.0", "4"].map( - (version) => testUrl(version) - ) + [ + "12.0.0", + "12", + "11", + "11.0.0", + "10", + "10.0.0", + "9.0.0", + "8", + "8.0.0", + "7.0.0", + "7", + "6", + "6.0.0", + "5", + "5.0.0", + "4", + ].map((version) => testUrl(version)) ) }) }) diff --git a/src/llvm/llvm.ts b/src/llvm/llvm.ts index c77f6dd8..70203e1b 100644 --- a/src/llvm/llvm.ts +++ b/src/llvm/llvm.ts @@ -5,6 +5,7 @@ import * as tc from "@actions/tool-cache" import * as path from "path" import semverLte from "semver/functions/lte" import semverLt from "semver/functions/lt" +import { isValidUrl } from "../utils/http/validate_url" //================================================ // Version @@ -200,22 +201,32 @@ function getLinuxUrl(versionGiven: string): string { const WIN32_MISSING: Set = new Set(["10.0.1"]) /** Gets an LLVM download URL for the Windows platform. */ -function getWin32Url(version: string): string | null { +async function getWin32Url(version: string): Promise { if (WIN32_MISSING.has(version)) { return null } const prefix = "LLVM-" const suffix = semverLte(version, "3.7.0") ? "-win32.exe" : "-win64.exe" - if (semverLte(version, "9.0.1")) { - return getReleaseUrl(version, prefix, suffix) - } else { - return getGitHubUrl(version, prefix, suffix) + + const olderThan9_1 = semverLte(version, "9.0.1") + let url: string + let fallback = false + if (olderThan9_1) { + url = getReleaseUrl(version, prefix, suffix) + if (!(await isValidUrl(url))) { + fallback = true // fallback to github + } } + if (fallback || !olderThan9_1) { + url = getGitHubUrl(version, prefix, suffix) + } + + return url! } /** Gets an LLVM download URL. */ -function getUrl(platform: string, version: string): string | null { +function getUrl(platform: string, version: string): string | null | Promise { switch (platform) { case "darwin": return getDarwinUrl(version) @@ -229,13 +240,14 @@ function getUrl(platform: string, version: string): string | null { } /** Gets the most recent specific LLVM version for which there is a valid download URL. */ -export function getSpecificVersionAndUrl(platform: string, version: string): [string, string] { +export async function getSpecificVersionAndUrl(platform: string, version: string): Promise<[string, string]> { if (!VERSIONS.has(version)) { throw new Error(`Unsupported target! (platform='${platform}', version='${version}')`) } for (const specificVersion of getSpecificVersions(version)) { - const url = getUrl(platform, specificVersion) + // eslint-disable-next-line no-await-in-loop + const url = await getUrl(platform, specificVersion) if (url !== null) { return [specificVersion, url] } @@ -253,7 +265,7 @@ const DEFAULT_WIN32_DIRECTORY = "C:/Program Files/LLVM" async function install(version: string, directory: string): Promise { const platform = process.platform - const [specificVersion, url] = getSpecificVersionAndUrl(platform, version) + const [specificVersion, url] = await getSpecificVersionAndUrl(platform, version) core.setOutput("version", specificVersion) core.info(`Installing LLVM and Clang ${version} (${specificVersion})...`) diff --git a/src/utils/http/validate_url.ts b/src/utils/http/validate_url.ts new file mode 100644 index 00000000..4848c3cb --- /dev/null +++ b/src/utils/http/validate_url.ts @@ -0,0 +1,13 @@ +import * as https from "https" + +export function isValidUrl(url: string) { + return new Promise((resolve) => { + https.get(url, (res) => { + if (res.statusCode !== undefined && res.statusCode >= 200 && res.statusCode <= 399) { + resolve(true) + } else { + resolve(false) + } + }) + }) +}