Repository navigation
Can an extension run custom scripts before the build process? #5068
|
The main problem for me is setting up the environment. I’m not a fan of changing the global state of the operating system, and I’d like to configure the environment for a specific terminal session via scripts, like MSVC does, but I didn’t find any documentation in this extension that mentions the possibility of installing custom scripts. Question: is such a possibility even available, or not? If not, then I’ll have to suffer and drag my CMakeUserPresets.json file from project to project, which sounds extremely discouraging. |
Replies: 1 comment 4 replies
|
For environment initialization, use a user-local CMake Tools kit with Run CMake: Edit User-Local CMake Kits from the Command Palette and add a kit such as: [
{
"name": "My SDK environment",
"environmentSetupScript": "C:/SDK/setup-env.bat",
"keep": true
}
]The Windows script can set variables normally: @echo off
set "SDK_ROOT=C:\SDK"
set "PATH=%SDK_ROOT%\bin;%PATH%"On Linux, point the property at a shell script instead: #!/usr/bin/env bash
export SDK_ROOT=/opt/sdk
export PATH="$SDK_ROOT/bin:$PATH"Select that kit for the project. CMake Tools calls the batch file on Windows or sources the shell script in bash, then captures the resulting environment for configure, build, and test. This hook is intended for environment setup. If the script generates files or performs an actual build step, put that work in a CMake custom command/target or a VS Code task instead. The supported format is documented under |
Thanks, that explains why the command was missing. My previous answer missed an important detail: CMake Tools uses either Presets mode or Kits mode for a folder. With
cmake.useCMakePresetsset toauto, the presence ofCMakePresets.jsonenables Presets mode, and the kit commands are intentionally unavailable. Therefore,environmentSetupScriptis only an option if you disable preset integration with"cmake.useCMakePresets": "never".You are also right about the limitation. The CMake Presets schema supports an
environmentmap, but it has no standard field for sourcing an arbitrary setup script. So there is currently no preset equivalent ofenvironmentSetupScript.You do not need to duplicate…