Skip to content

Commit

Permalink
Fixes environment expanding and document testEnvironment,
Browse files Browse the repository at this point in the history
They share a single computeExpandedEnvironment for expanding.

TODO: fixes debug launch and run program the same as CTest,
they should share the same environment variables.
  • Loading branch information
lygstate committed Feb 15, 2021
1 parent 5fe1a96 commit f76e220
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 38 deletions.
3 changes: 2 additions & 1 deletion docs/cmake-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Options that support substitution, in the table below, allow variable references
| `cmake.configureSettings` | An object containing `key:value` pairs, which will be passed to CMake when configuring. The same as passing `-DVAR_NAME=ON` via `cmake.configureArgs`. | `null` (no values) | yes |
| `cmake.copyCompileCommands`| If not `null`, copies the `compile_commands.json` file generated by CMake to the path specified by this setting whenever CMake successfully configures. | `null` (do not copy the file) | yes |
| `cmake.defaultVariants` | Override the default set of variants that will be supplied when no variants file is present. See [CMake variants](variants.md). | | no |
| `cmake.environment` | An object containing `key:value` pairs of environment variables, which will be passed onto CMake when configuring and to the compiler. | `null` (no environment variables) | yes |
| `cmake.environment` | An object containing `key:value` pairs of environment variables, which will be passed onto CMake when configuring, build to the compiler, run/debug program and CTest. | `null` (no environment variables) | yes |
| `cmake.generator` | Set to a string to override CMake Tools preferred generator logic. If set, CMake will unconditionally use it as the `-G` CMake generator command line argument. ||no|
| `cmake.installPrefix` | If specified, sets a value for `CMAKE_INSTALL_PREFIX` when running CMake configure. If not set, no value will be passed.</br>If `CMAKE_INSTALL_PREFIX` is set via `cmake.configureArgs` or `cmake.configureSettings`, `cmake.installPrefix` will be ignored.| `null` (no value specified) | yes |
| `cmake.loggingLevel` | A string setting that specifies how much output CMake Tools produces in its output channel. Set to one of `"trace"`, `"debug"`, `"info"`, `"note"`, `"warning"`, `"error"`, or `"fatal"`. `"trace"` is the most verbose.</br></br>Regardless of the logging level, CMake Tools writes all levels of logging to the CMake Tools log file. This file is useful if you need to [troubleshoot CMake Tools](troubleshoot.md) | `"info"` | no |
Expand All @@ -31,6 +31,7 @@ Options that support substitution, in the table below, allow variable references
| `cmake.preferredGenerators` | A list of strings of generator names to try, in order, when configuring a CMake project for the first time. | | no |
| `cmake.saveBeforeBuild` | If `true` (the default), saves open text documents when build or configure is invoked before running CMake. | `true` | no |
| `cmake.sourceDirectory` | Directory where the root `CMakeLists.txt` is stored. | `${workspaceFolder}` | yes |
| `cmake.testEnvironment` | An object containing `key:value` pairs of environment variables, which will be passed onto CTest and running/debugging program only. | `null` (no environment variables) | yes |

## Variable substitution

Expand Down
3 changes: 1 addition & 2 deletions src/ctest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,7 @@ export class CTestDriver implements vscode.Disposable {
[`-j${this.ws.config.numCTestJobs}`, '-C', configuration, '-T', 'test', '--output-on-failure'].concat(
this.ws.config.ctestArgs),
new CTestOutputLogger(),
{environment: this.ws.config.testEnvironment, cwd: driver.binaryDir});

{environment: await driver.getCTestCommandEnvironment(), cwd: driver.binaryDir});
const res = await child.result;
await this.reloadTests(driver);
if (res.retc === null) {
Expand Down
83 changes: 48 additions & 35 deletions src/drivers/driver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,54 @@ export abstract class CMakeDriver implements vscode.Disposable {
return util.reduce(this._kitEnvironmentVariables.entries(), {}, (acc, [key, value]) => ({...acc, [key]: value}));
}

/**
* Compute the environment variables that apply with substitutions by expansionOptions
*/
async computeExpandedEnvironment(in_env: proc.EnvironmentVariables, expanded_env:proc.EnvironmentVariables): Promise<proc.EnvironmentVariables>
{
const env = {} as {[key: string]: string};
const opts = this.expansionOptions;

await Promise.resolve(
util.objectPairs(in_env)
.forEach(async ([key, value]) => env[key] = await expand.expandString(value, {...opts, envOverride: expanded_env}))
);
return env;
}

/**
* Get the environment variables that should be set at CMake-configure time.
*/
async getConfigureEnvironment(): Promise<proc.EnvironmentVariables> {
return util.mergeEnvironment(this.getKitEnvironmentVariablesObject(),
await this.getExpandedEnvironment(),
await this.getBaseConfigureEnvironment(),
this._variantEnv);
let envs = this.getKitEnvironmentVariablesObject();
/* NOTE: By mergeEnvironment one by one to enable expanding self containd variable such as PATH properly */
/* If configureEnvironment and environment both configured different PATH, doing this will preserve them all */
envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this.config.environment, envs));
envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this.config.configureEnvironment, envs));
envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this._variantEnv, envs));
return envs;
}

/**
* Get the environment variables that should be set at CMake-build time.
*/
async getCMakeBuildCommandEnvironment(in_env: proc.EnvironmentVariables): Promise<proc.EnvironmentVariables> {
let envs = util.mergeEnvironment(in_env, this.getKitEnvironmentVariablesObject());
envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this.config.environment, envs));
envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this.config.buildEnvironment, envs));
envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this._variantEnv, envs));
return envs;
}

/**
* Get the environment variables that should be set at CTest and running program time.
*/
async getCTestCommandEnvironment(): Promise<proc.EnvironmentVariables> {
let envs = this.getKitEnvironmentVariablesObject();
envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this.config.environment, envs));
envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this.config.testEnvironment, envs));
envs = util.mergeEnvironment(envs, await this.computeExpandedEnvironment(this._variantEnv, envs));
return envs;
}

get onProgress(): vscode.Event<ProgressMessage> {
Expand All @@ -172,31 +212,6 @@ export abstract class CMakeDriver implements vscode.Disposable {
*/
private _kit: Kit|null = null;

/**
* Get the environment and apply any needed
* substitutions before returning it.
*/
async getExpandedEnvironment(): Promise<{[key: string]: string}> {
const env = {} as {[key: string]: string};
const opts = this.expansionOptions;
await Promise.resolve(util.objectPairs(this.config.environment)
.forEach(async ([key, value]) => env[key] = await expand.expandString(value, opts)));
return env;
}

/**
* Get the configure environment and apply any needed
* substitutions before returning it.
*/
async getBaseConfigureEnvironment(): Promise<{[key: string]: string}> {
const config_env = {} as {[key: string]: string};
const opts = this.expansionOptions;
await Promise.resolve(
util.objectPairs(this.config.configureEnvironment)
.forEach(async ([key, value]) => config_env[key] = await expand.expandString(value, opts)));
return config_env;
}

/**
* Get the vscode root workspace folder.
*
Expand Down Expand Up @@ -1212,15 +1227,13 @@ export abstract class CMakeDriver implements vscode.Disposable {
return [];
})();

const build_env = {} as {[key: string]: string};
build_env['NINJA_STATUS'] = '[%s/%t %p :: %e] ';
const opts = this.expansionOptions;
await Promise.resolve(
util.objectPairs(util.mergeEnvironment(this.config.buildEnvironment, await this.getExpandedEnvironment()))
.forEach(async ([key, value]) => build_env[key] = await expand.expandString(value, opts)));
const ninja_env = {} as {[key: string]: string};
ninja_env['NINJA_STATUS'] = '[%s/%t %p :: %e] ';
const build_env = await this.getCMakeBuildCommandEnvironment(ninja_env);

const args = ['--build', this.binaryDir, '--config', this.currentBuildType, '--target', target]
.concat(this.config.buildArgs, ['--'], generator_args, this.config.buildToolArgs);
const opts = this.expansionOptions;
const expanded_args_promises
= args.map(async (value: string) => expand.expandString(value, {...opts, envOverride: build_env}));
const expanded_args = await Promise.all(expanded_args_promises) as string[];
Expand Down

0 comments on commit f76e220

Please sign in to comment.