Skip to content
This repository was archived by the owner on Oct 1, 2024. It is now read-only.

Commit 95f5a18

Browse files
elektronikworkshopadiazulay
authored andcommitted
Documentation for pre-/post-build commands and their environment variables
1 parent f85a783 commit 95f5a18

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

README.md

+35-4
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ The following settings are as per sketch settings of the Arduino extension. You
101101
"board": "adafruit:samd:adafruit_feather_m0",
102102
"output": "../build",
103103
"debugger": "jlink",
104-
"prebuild": "bash prebuild.sh",
105-
"postbuild": "bash postbuild.sh",
104+
"prebuild": "./prebuild.sh",
105+
"postbuild": "./postbuild.sh",
106106
"intelliSenseGen": "global"
107107
}
108108
```
@@ -111,13 +111,44 @@ The following settings are as per sketch settings of the Arduino extension. You
111111
- `board` - Currently selected Arduino board alias. Can be set by the `Arduino: Change Board Type` command. Also, you can find the board list there.
112112
- `output` - Arduino build output path. If not set, Arduino will create a new temporary output folder each time, which means it cannot reuse the intermediate result of the previous build leading to long verify/upload time, so it is recommended to set the field. Arduino requires that the output path should not be the workspace itself or in a subfolder of the workspace, otherwise, it may not work correctly. By default, this option is not set. It's worth noting that the contents of this file could be deleted during the build process, so pick (or create) a directory that will not store files you want to keep.
113113
- `debugger` - The short name of the debugger that will be used when the board itself does not have a debugger and there is more than one debugger available. You can find the list of debuggers [here](https://github.com/Microsoft/vscode-arduino/blob/master/misc/debuggerUsbMapping.json). By default, this option is not set.
114-
- `prebuild` - External command which will be invoked before any sketch build (verify, upload). You must only set one `prebuild` command. `command1 && command2` does not work. If you need to run multiple commands before the build, then create a script.
115-
- `postbuild` - External command to be run after the sketch has been built successfully. Same rules as for `prebuild` apply. <!-- TODO: note about cwd would be a good thing -->
114+
- `prebuild` - External command which will be invoked before any sketch build (verify, upload, ...). For details see the [Pre- and Post-Build Commands](#Pre--and-Post-Build-Commands) section.
115+
- `postbuild` - External command to be run after the sketch has been built successfully. See the afore mentioned section for more details.
116116
- `intelliSenseGen` - Override the global setting for auto-generation of the IntelliSense configuration (i.e. `.vscode/c_cpp_properties.json`). Three options are available:
117117
- `"global"`: Use the global settings (default)
118118
- `"disable"`: Disable the auto-generation even if globally enabled
119119
- `"enable"`: Enable the auto-generation even if globally disabled
120120

121+
## Pre- and Post-Build Commands
122+
On Windows the commands run within a `cmd`-, on Linux and OSX within a `bash`-instance. Therefore your command can be anything what you can run within those shells. Instead of running a command you can invoke a script. This makes writing more complex pre-/post-build mechanisms much easier and opens up the possibility to run python or other scripting languages.
123+
The commands run within the workspace root directory and vscode-arduino sets the following environment variables:
124+
**`VSCA_BUILD_MODE`** The current build mode, one of `Verifying`, `Uploading`, `Uploading (programmer)` or `Analyzing`. This allows you to run your script on certain build modes only.
125+
**`VSCA_SKETCH`** The sketch file relative to your workspace root directory.
126+
**`VSCA_BOARD`** Your board and configuration, e.g. `arduino:avr:nano:cpu=atmega328`.
127+
**`VSCA_WORKSPACE_DIR`** The absolute path of your workspace root directory.
128+
**`VSCA_LOG_LEVEL`** The current log level. This allows you to control the verbosity of your scripts.
129+
**`VSCA_SERIAL`** The serial port used for uploading. Not set if you haven't set one in your `arduino.json`.
130+
**`VSCA_BUILD_DIR`** The build directory. Not set if you haven't set one in your `arduino.json`.
131+
132+
For example under Windows the following `arduino.json` setup
133+
```json
134+
{
135+
"board": "arduino:avr:nano",
136+
"sketch": "test.ino",
137+
"configuration": "cpu=atmega328",
138+
"prebuild": "IF \"%VSCA_BUILD_MODE%\"==\"Verifying\" (echo VSCA_BUILD_MODE=%VSCA_BUILD_MODE% && echo VSCA_BOARD=%VSCA_BOARD%)"
139+
}
140+
```
141+
will produce
142+
```
143+
[Starting] Verifying sketch 'test.ino'
144+
Running pre-build command: "IF "%VSCA_BUILD_MODE%"=="Verifying" (echo VSCA_BUILD_MODE=%VSCA_BUILD_MODE% && echo VSCA_BOARD=%VSCA_BOARD%)"
145+
VSCA_BUILD_MODE=Verifying
146+
VSCA_BOARD=arduino:avr:nano:cpu=atmega328
147+
Loading configuration...
148+
<...>
149+
```
150+
when verifying.
151+
121152
## IntelliSense
122153
vscode-arduino auto-configures IntelliSense by default. vscode-arduino analyzes Arduino's compiler output by running a separate build and generates the corresponding configuration file at `.vscode/c_cpp_properties.json`. vscode-arduino tries as hard as possible to keep things up to date, e.g. it runs the analysis when switching the board or the sketch.
123154

0 commit comments

Comments
 (0)