fix: support adding environment variables on non GitHub Action

This commit is contained in:
Amin Yahyaabadi 2022-01-17 18:30:02 -08:00
parent 67d09d6064
commit c6a5745cec
9 changed files with 82 additions and 32 deletions

2
dist/setup_cpp.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
import { exportVariable, info } from "@actions/core"
import { info } from "@actions/core"
import { addPath } from "../utils/path/addPath"
import { existsSync } from "fs"
import { setupAptPack } from "../utils/setup/setupAptPack"
@ -7,6 +7,7 @@ import { setupChocoPack } from "../utils/setup/setupChocoPack"
import semverMajor from "semver/functions/major"
import semverCoerce from "semver/functions/coerce"
import { setupMacOSSDK } from "../macos-sdk/macos-sdk"
import { addEnv } from "../utils/env/addEnv"
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function setupGcc(version: string, _setupDir: string, arch: string) {
@ -92,22 +93,22 @@ async function activateGcc(version: string, binDir: string) {
// const ld = process.env.LD_LIBRARY_PATH ?? ""
// const dyld = process.env.DYLD_LIBRARY_PATH ?? ""
// // Setup gcc as the compiler
// exportVariable("LD_LIBRARY_PATH", `${installDir}/lib${path.delimiter}${ld}`)
// exportVariable("DYLD_LIBRARY_PATH", `${installDir}/lib${path.delimiter}${dyld}`)
// exportVariable("CPATH", `${installDir}/lib/gcc/${majorVersion}/include`)
// exportVariable("LDFLAGS", `-L${installDir}/lib`)
// exportVariable("CPPFLAGS", `-I${installDir}/include`)
// addEnv("LD_LIBRARY_PATH", `${installDir}/lib${path.delimiter}${ld}`)
// addEnv("DYLD_LIBRARY_PATH", `${installDir}/lib${path.delimiter}${dyld}`)
// addEnv("CPATH", `${installDir}/lib/gcc/${majorVersion}/include`)
// addEnv("LDFLAGS", `-L${installDir}/lib`)
// addEnv("CPPFLAGS", `-I${installDir}/include`)
if (process.platform === "win32") {
exportVariable("CC", `${binDir}/gcc`)
exportVariable("CXX", `${binDir}/g++`)
addEnv("CC", `${binDir}/gcc`)
addEnv("CXX", `${binDir}/g++`)
} else {
const majorVersion = semverMajor(semverCoerce(version) ?? version)
if (majorVersion >= 5) {
exportVariable("CC", `${binDir}/gcc-${majorVersion}`)
exportVariable("CXX", `${binDir}/g++-${majorVersion}`)
addEnv("CC", `${binDir}/gcc-${majorVersion}`)
addEnv("CXX", `${binDir}/g++-${majorVersion}`)
} else {
exportVariable("CC", `${binDir}/gcc-${version}`)
exportVariable("CXX", `${binDir}/g++-${version}`)
addEnv("CC", `${binDir}/gcc-${version}`)
addEnv("CXX", `${binDir}/g++-${version}`)
}
}

View File

@ -1,4 +1,3 @@
import * as core from "@actions/core"
import * as path from "path"
import semverLte from "semver/functions/lte"
import semverMajor from "semver/functions/major"
@ -8,6 +7,8 @@ import { extractExe, extractTarByExe } from "../utils/setup/extract"
import { getSpecificVersionAndUrl, getVersions, semverCoerceIfInvalid } from "../utils/setup/version"
import { setupMacOSSDK } from "../macos-sdk/macos-sdk"
import { addBinExtension } from "../utils/extension/extension"
import { addEnv } from "../utils/env/addEnv"
import { setOutput } from "@actions/core"
//================================================
// Version
@ -223,7 +224,7 @@ export function getUrl(platform: string, version: string): string | null | Promi
async function getLLVMPackageInfo(version: string, platform: NodeJS.Platform): Promise<PackageInfo> {
const [specificVersion, url] = await getSpecificVersionAndUrl(VERSIONS, platform, version, getUrl)
core.setOutput("version", specificVersion)
setOutput("version", specificVersion)
return {
url,
extractedFolderName: "",
@ -252,26 +253,26 @@ export async function activateLLVM(directory: string, versionGiven: string) {
const ld = process.env.LD_LIBRARY_PATH ?? ""
const dyld = process.env.DYLD_LIBRARY_PATH ?? ""
core.exportVariable("LLVM_PATH", directory) // the output of this action
addEnv("LLVM_PATH", directory) // the output of this action
const llvmMajor = semverMajor(version)
// Setup LLVM as the compiler
core.exportVariable("LD_LIBRARY_PATH", `${lib}${path.delimiter}${ld}`)
core.exportVariable("DYLD_LIBRARY_PATH", `${lib}${path.delimiter}${dyld}`)
addEnv("LD_LIBRARY_PATH", `${lib}${path.delimiter}${ld}`)
addEnv("DYLD_LIBRARY_PATH", `${lib}${path.delimiter}${dyld}`)
if (process.platform !== "win32") {
// https://github.com/aminya/setup-cpp/issues/6
core.exportVariable("CPATH", `${directory}/lib/clang/${llvmMajor}/include`)
addEnv("CPATH", `${directory}/lib/clang/${llvmMajor}/include`)
}
core.exportVariable("LDFLAGS", `-L${directory}/lib`)
core.exportVariable("CPPFLAGS", `-I${directory}/include`)
addEnv("LDFLAGS", `-L${directory}/lib`)
addEnv("CPPFLAGS", `-I${directory}/include`)
core.exportVariable("CC", `${directory}/bin/clang`)
core.exportVariable("CXX", `${directory}/bin/clang++`)
addEnv("CC", `${directory}/bin/clang`)
addEnv("CXX", `${directory}/bin/clang++`)
core.exportVariable("LIBRARY_PATH", `${directory}/lib`)
addEnv("LIBRARY_PATH", `${directory}/lib`)
await setupMacOSSDK()
}

View File

@ -1,5 +1,6 @@
import { getExecOutput } from "@actions/exec"
import * as core from "@actions/core"
import { addEnv } from "../utils/env/addEnv"
export async function setupMacOSSDK() {
if (process.platform === "darwin") {
@ -7,7 +8,7 @@ export async function setupMacOSSDK() {
const xcrun = await getExecOutput("xcrun --sdk macosx --show-sdk-path")
const sdkroot = xcrun.stdout || xcrun.stderr
if (sdkroot) {
core.exportVariable("SDKROOT", sdkroot.trim())
addEnv("SDKROOT", sdkroot.trim())
} else {
core.error(`SDKROOT not set`)
}

View File

@ -26,6 +26,7 @@ import { setupVcpkg } from "./vcpkg/vcpkg"
import { join } from "path"
import { setupVCVarsall } from "./vcvarsall/vcvarsall"
import { setupKcov } from "./kcov/kcov"
import { addEnv } from "./utils/env/addEnv"
/** The setup functions */
const setups = {
@ -176,8 +177,8 @@ export async function main(args: string[]): Promise<number> {
case "appleclang":
case "applellvm": {
core.info("Assuming apple-clang is already installed")
core.exportVariable("CC", "clang")
core.exportVariable("CXX", "clang++")
addEnv("CC", "clang")
addEnv("CXX", "clang++")
break
}
default: {

46
src/utils/env/addEnv.ts vendored Normal file
View File

@ -0,0 +1,46 @@
import { exportVariable } from "@actions/core"
import * as core from "@actions/core"
import execa from "execa"
import { isGitHubCI } from "./isci"
import untildify from "untildify"
import { appendFileSync } from "fs"
/** An add path function that works locally or inside GitHub Actions */
export function addEnv(name: string, val: string | undefined) {
try {
if (isGitHubCI()) {
exportVariable(name, val)
} else {
addEnvSystem(name, val)
}
} catch (err) {
try {
core.error(err as Error)
return addEnvSystem(name, val)
} catch (err2) {
core.error(err2 as Error)
}
core.error(`Failed to export environment variable ${name}=${val}. You should add it manually.`)
}
}
function addEnvSystem(name: string, val: string | undefined) {
switch (process.platform) {
case "win32": {
execa.sync(`setx "${name}"="${val}"`)
core.info(`${name}="${val} was set in the environment."`)
return
}
case "linux":
case "darwin": {
const profile_path = untildify("~/.profile")
appendFileSync(profile_path, `\nexport ${name}="${val}\n`)
core.info(`${name}="${val} was added to "${profile_path}"`)
return
}
default: {
// fall through shell path modification
}
}
process.env[name] = val
}

View File

@ -5,7 +5,6 @@ import execa from "execa"
import { isGitHubCI } from "../env/isci"
import untildify from "untildify"
import { appendFileSync } from "fs"
// import { spawnSync } from "child_process"
/** An add path function that works locally or inside GitHub Actions */
export function addPath(path: string) {
@ -30,13 +29,13 @@ function addPathSystem(path: string) {
switch (process.platform) {
case "win32": {
execa.sync(`setx PATH=${path};%PATH%`)
core.info(`${path} was added to the PATH.`)
return
}
case "linux":
case "darwin": {
const profile_path = untildify("~/.profile")
appendFileSync(profile_path, `\nexport PATH=${path}:$PATH\n`)
// spawnSync(`source "${profile_path}"`, { shell: true })
core.info(`${path} was added to "${profile_path}"`)
return
}

View File

@ -1,8 +1,9 @@
import { exportVariable, info } from "@actions/core"
import { info } from "@actions/core"
import { existsSync } from "fs"
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { setupMSVCDevCmd } from "msvc-dev-cmd/lib.js"
import { addEnv } from "../utils/env/addEnv"
function getArch(arch: string): string {
switch (arch) {
@ -31,7 +32,7 @@ export function setupVCVarsall(
) {
if (VCTargetsPath !== undefined && existsSync(VCTargetsPath)) {
info(`Adding ${VCTargetsPath} to PATH`)
exportVariable("VCTargetsPath", VCTargetsPath)
addEnv("VCTargetsPath", VCTargetsPath)
}
setupMSVCDevCmd(getArch(arch), sdk, toolset, uwp, spectre, vsversion)