Merge pull request #12 from aminya/exportVariable-System [skip ci]

This commit is contained in:
Amin Yahyaabadi 2022-01-17 19:05:26 -08:00 committed by GitHub
commit dff4fe8d04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 106 additions and 32 deletions

View File

@ -51,6 +51,8 @@ An example that installs llvm, cmake, ninja, ccache, and vcpkg:
# windows example (open shell as admin)
curl -LJO "https://github.com/aminya/setup-cpp/releases/download/v0.5.3/setup_cpp_windows.exe"
./setup_cpp_windows --compiler llvm --cmake true --ninja true --ccache true --vcpkg true
RefreshEnv.cmd # reload the environment
```
```ps1
@ -58,6 +60,8 @@ curl -LJO "https://github.com/aminya/setup-cpp/releases/download/v0.5.3/setup_cp
wget "https://github.com/aminya/setup-cpp/releases/download/v0.5.3/setup_cpp_linux"
chmod +x setup_cpp_linux
sudo ./setup_cpp_linux --compiler llvm --cmake true --ninja true --ccache true --vcpkg true
source ~/.profile # reload the environment
```
```ps1
@ -65,11 +69,15 @@ sudo ./setup_cpp_linux --compiler llvm --cmake true --ninja true --ccache true -
wget "https://github.com/aminya/setup-cpp/releases/download/v0.5.3/setup_cpp_mac"
chmod +x setup_cpp_mac
sudo ./setup_cpp_mac --compiler llvm --cmake true --ninja true --ccache true --vcpkg true
source ~/.profile # reload the environment
```
NOTE: In the `compiler` entry, you can specify the version after `-` like `llvm-11.0.0`.
For the tools, instead of `true` that chooses the default version, you can pass a specific version.
NOTE: you will not need `sudo` if you are already a root user (e.g., in a GitLab runner).
### With Nodejs
Download the `setup_cpp.js` file form [here](https://github.com/aminya/setup-cpp/releases/download/v0.5.3/setup_cpp.js), and run it with the available options.
@ -82,6 +90,8 @@ Open the shell as admin, download via `curl`, then install
# open shell as admin
curl -LJO "https://github.com/aminya/setup-cpp/releases/download/v0.5.3/setup_cpp.js"
node ./setup_cpp.js --compiler llvm --cmake true --ninja true --ccache true --vcpkg true
RefreshEnv.cmd # reload the environment
```
On Linux or Mac:
@ -89,6 +99,8 @@ On Linux or Mac:
```ps1
wget "https://github.com/aminya/setup-cpp/releases/download/v0.5.3/setup_cpp.js"
sudo node ./setup_cpp.js --compiler llvm --cmake true --ninja true --ccache true --vcpkg true
source ~/.profile # reload the environment
```
# Inside GitHub Actions
@ -168,6 +180,9 @@ RUN chmod +x ./setup_cpp_linux
# install llvm, cmake, ninja, and ccache
RUN ./setup_cpp_linux --compiler llvm --cmake true --ninja true --ccache true
# reload the environment
RUN source ~/.profile
ENTRYPOINT [ "/bin/sh" ]
```

View File

@ -12,4 +12,7 @@ RUN chmod +x ./setup_cpp_linux
# install llvm, cmake, ninja, and ccache
RUN ./setup_cpp_linux --compiler llvm --cmake true --ninja true --ccache true
# reload the environment
CMD source ~/.profile
ENTRYPOINT [ "/bin/sh" ]

View File

@ -8,4 +8,7 @@ WORKDIR "/"
# run installation
RUN node ./setup_cpp.js --compiler llvm --cmake true --ninja true --ccache true
# reload the environment
CMD source ~/.profile
ENTRYPOINT [ "/bin/sh" ]

View File

@ -12,4 +12,7 @@ RUN apt-get install -y --no-install-recommends unzip
# run installation
RUN node ./setup_cpp.js --compiler llvm --cmake true --ninja true --ccache true --conan true
# reload the environment
CMD source ~/.profile
ENTRYPOINT [ "/bin/sh" ]

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)