From b302dc3e2547cead5809c84aae69b0e422100c4c Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Wed, 15 Sep 2021 05:25:02 -0500 Subject: [PATCH] feat: add msvc installation --- .eslintrc.json | 2 +- .gitmodules | 4 +++ .prettierignore | 3 +- README.md | 2 +- action.yml | 3 ++ src/main.ts | 7 +++++ src/msvc/__tests__/msvc.test.ts | 17 ++++++++++ src/msvc/msvc-dev-cmd | 1 + src/msvc/msvc.ts | 52 +++++++++++++++++++++++++++++++ src/utils/setup/setupChocoPack.ts | 6 ++-- tsconfig.json | 2 +- 11 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 src/msvc/__tests__/msvc.test.ts create mode 160000 src/msvc/msvc-dev-cmd create mode 100644 src/msvc/msvc.ts diff --git a/.eslintrc.json b/.eslintrc.json index c8933df5..08c17b41 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -1,4 +1,4 @@ { "extends": "eslint-config-atomic", - "ignorePatterns": ["dist/", "node_modules/", "src/python/setup-python/"] + "ignorePatterns": ["dist/", "node_modules/", "src/python/setup-python/", "src/msvc/msvc-dev-cmd/"] } diff --git a/.gitmodules b/.gitmodules index 15d5a049..de1ec0f8 100644 --- a/.gitmodules +++ b/.gitmodules @@ -2,3 +2,7 @@ path = src/python/setup-python url = https://github.com/actions/setup-python branch = main +[submodule "src/msvc/msvc-dev-cmd"] + path = src/msvc/msvc-dev-cmd + url = https://github.com/aminya/msvc-dev-cmd + branch = lib diff --git a/.prettierignore b/.prettierignore index 2ddc2e55..833fbbde 100644 --- a/.prettierignore +++ b/.prettierignore @@ -4,4 +4,5 @@ package-lock.json CHANGELOG.md dist stats.html -src/python/setup-python/ \ No newline at end of file +src/python/setup-python/ +src/msvc/msvc-dev-cmd/ diff --git a/README.md b/README.md index 5a7ce361..2ac8b83a 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ The package will be usable from any environment (locally, GitHub Actions, etc). - [x] setup meson - [x] setup gcovr - [x] setup python -- [ ] setup msvc +- [x] setup msvc - [ ] setup gcc/mingw - [ ] setup OpenCppCoverage - [ ] setup cppcheck diff --git a/action.yml b/action.yml index 9e3f5efb..8faf67b1 100644 --- a/action.yml +++ b/action.yml @@ -9,6 +9,9 @@ inputs: llvm: description: "The llvm version to install" required: false + msvc: + description: "The msvc version to install" + required: false cmake: description: "The cmake version to install." default: "3.20.2" diff --git a/src/main.ts b/src/main.ts index 94ec47cf..f482848b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,7 @@ import { setupConan } from "./conan/conan" import { setupGcovr } from "./gcovr/gcovr" import { setupLLVM } from "./llvm/llvm" import { setupMeson } from "./meson/meson" +import { setupMSVC } from "./msvc/msvc" import { setupNinja } from "./ninja/ninja" import { setupPython } from "./python/python" @@ -67,6 +68,12 @@ export async function main(): Promise { if (chocoVersion !== undefined) { await setupChocolatey() } + + // setup msvc + const msvcVersion = maybeGetInput("msvc") + if (msvcVersion !== undefined) { + await setupMSVC(msvcVersion) + } } catch (err) { core.error(err as string | Error) core.setFailed("install-cpp failed") diff --git a/src/msvc/__tests__/msvc.test.ts b/src/msvc/__tests__/msvc.test.ts new file mode 100644 index 00000000..d16b9a0f --- /dev/null +++ b/src/msvc/__tests__/msvc.test.ts @@ -0,0 +1,17 @@ +import { setupMSVC } from "../msvc" +import { spawnSync as spawn } from "child_process" + +jest.setTimeout(200000) +describe("setup-msvc", () => { + it("should setup msvc", async () => { + if (process.platform !== "win32") { + return + } + await setupMSVC("2017") + + const { status } = spawn("cl", { + encoding: "utf8", + }) + expect(status).toBe(0) + }) +}) diff --git a/src/msvc/msvc-dev-cmd b/src/msvc/msvc-dev-cmd new file mode 160000 index 00000000..082928e0 --- /dev/null +++ b/src/msvc/msvc-dev-cmd @@ -0,0 +1 @@ +Subproject commit 082928e04f67114ec851d165f1ab48fce0b5d752 diff --git a/src/msvc/msvc.ts b/src/msvc/msvc.ts new file mode 100644 index 00000000..c1fd44ca --- /dev/null +++ b/src/msvc/msvc.ts @@ -0,0 +1,52 @@ +import { setupMSVCDevCmd } from "./msvc-dev-cmd/index" +import { setupChocoPack } from "../utils/setup/setupChocoPack" +import { exportVariable } from "@actions/core" +import { existsSync } from "fs" +import { arch as osArch } from "os" + +type MSVCVersion = "2015" | "2017" | "2019" | string + +function getArch(arch: string): string { + switch (arch) { + case "x32": + case "32": + case "ia32": { + return "x86" + } + case "64": { + return "x64" + } + default: { + return arch + } + } +} + +export async function setupMSVC( + version: MSVCVersion, + sdk?: string, + uwp?: boolean, + spectre?: boolean, + arch = osArch() +): Promise { + let toolset: string | undefined + if (version === "2015") { + toolset = "14.0.25420.1" + await setupChocoPack("visualcpp-build-tools", toolset, ["--ignore-dependencies", "--params", "'/IncludeRequired'"]) + + const VCTargetsPath = "C:\\Program Files (x86)\\MSBuild\\Microsoft.Cpp\\v4.0\\v140" + if (existsSync(VCTargetsPath)) { + exportVariable("VCTargetsPath", VCTargetsPath) + } + } else if (version === "2017") { + toolset = "15.0.26228.20170424" + await setupChocoPack("visualcpp-build-tools", toolset, ["--ignore-dependencies", "--params", "'/IncludeRequired'"]) + } else if (version === "2019") { + toolset = "16.11.2.0" + await setupChocoPack("visualstudio2019buildtools", toolset, [ + "--package-parameters", + "add Microsoft.VisualStudio.Workload.NativeDesktop --includeRecommended --passive", + ]) + } + setupMSVCDevCmd(getArch(arch), sdk, toolset, uwp, spectre) +} diff --git a/src/utils/setup/setupChocoPack.ts b/src/utils/setup/setupChocoPack.ts index 81998d21..98e4fa7b 100644 --- a/src/utils/setup/setupChocoPack.ts +++ b/src/utils/setup/setupChocoPack.ts @@ -6,7 +6,7 @@ import { setupChocolatey } from "../../chocolatey/chocolatey" let hasChoco = false /** A function that installs a package using choco */ -export async function setupChocoPack(name: string, version?: string) { +export async function setupChocoPack(name: string, version?: string, args: string[] = []) { if (!hasChoco || which.sync("choco", { nothrow: true }) === null) { await setupChocolatey() hasChoco = true @@ -14,9 +14,9 @@ export async function setupChocoPack(name: string, version?: string) { let exit if (version === undefined) { - exit = await exec("choco", ["install", name]) + exit = await exec("choco", ["install", name, ...args]) } else { - exit = await exec("choco", ["install", name, `--version=${version}`]) + exit = await exec("choco", ["install", name, `--version=${version}`, ...args]) } if (exit !== 0) { diff --git a/tsconfig.json b/tsconfig.json index d948841a..d5a667b6 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -28,5 +28,5 @@ }, "compileOnSave": false, "include": ["./src"], - "exclude": ["./src/python/setup-python"] + "exclude": ["./src/python/setup-python", "src/msvc/msvc-dev-cmd/"] }