Delphi in VS Code

The Delphi Extension Pack is an excellent extension for Visual Studio Code. It installs a bunch of useful components to help make the Delphi developer right at home.

It’s really neat, you even get helpful error information if compilation fails:

Before you install it, however, see the instructions to install Global here.

Once Global is installed return to VS Code and install the Delphi Extension Pack, reload the IDE, and then open the root folder of your project – the folder containing your .dpr file.

Add Configuration

Next we need to add a configuration (Run -> Add Configuration) so we can compile our project.

The only way I could get the task runner to build the Delphi project was to use a batch file. In the same root folder, I added these two files:

  • tasks.json – the new VS Code configuration file for my task, based on other.
  • build.bat – a batch file to build my project with dcc32.exe

The Build File

This is what my build file looks like:

"c:\program files (x86)\embarcadero\studio\21.0\bin\dcc32.exe" -$O- -$W+ --no-config -M -Q -TX.exe -AGenerics.Collections=System.Generics.Collections;Generics.Defaults=System.Generics.Defaults;WinTypes=Winapi.Windows;WinProcs=Winapi.Windows;DbiTypes=BDE;DbiProcs=BDE;DbiErrs=BDE -DDEBUG -E.\Win32\Debug -I"c:\program files (x86)\embarcadero\studio\21.0\lib\Win32\debug";C:\Components\HtmlComponents\source;C:\Components\woll2woll\firepower\13.0\lib\21.0\win32;"c:\program files (x86)\embarcadero\studio\21.0\lib\Win32\release";C:\Users\david\Documents\Embarcadero\Studio\21.0\Imports;"c:\program files (x86)\embarcadero\studio\21.0\Imports";C:\Users\Public\Documents\Embarcadero\Studio\21.0\Dcp;"c:\program files (x86)\embarcadero\studio\21.0\include";C:\Components\HtmlComponents\source;C:\Components\HtmlComponents\source\editor;C:\Components\HtmlComponents\source\localize;C:\Components\PentireFMX -LEC:\Users\Public\Documents\Embarcadero\Studio\21.0\Bpl -LNC:\Users\Public\Documents\Embarcadero\Studio\21.0\Dcp -NU.\Win32\Debug -NSWinapi;System.Win;Data.Win;Datasnap.Win;Web.Win;Soap.Win;Xml.Win;Bde;System;Xml;Data;Datasnap;Web;Soap; -OC:\Components\woll2woll\firepower\13.0\lib\21.0\win32;"c:\program files (x86)\embarcadero\studio\21.0\lib\Win32\release";C:\Users\david\Documents\Embarcadero\Studio\21.0\Imports;"c:\program files (x86)\embarcadero\studio\21.0\Imports";C:\Users\Public\Documents\Embarcadero\Studio\21.0\Dcp;"c:\program files (x86)\embarcadero\studio\21.0\include";C:\Components\HtmlComponents\source;C:\Components\HtmlComponents\source\editor;C:\Components\HtmlComponents\source\localize;C:\Components\PentireFMX -RC:\Components\woll2woll\firepower\13.0\lib\21.0\win32;"c:\program files (x86)\embarcadero\studio\21.0\lib\Win32\release";C:\Users\david\Documents\Embarcadero\Studio\21.0\Imports;"c:\program files (x86)\embarcadero\studio\21.0\Imports";C:\Users\Public\Documents\Embarcadero\Studio\21.0\Dcp;"c:\program files (x86)\embarcadero\studio\21.0\include";C:\Components\HtmlComponents\source;C:\Components\HtmlComponents\source\editor;C:\Components\HtmlComponents\source\localize;C:\Components\PentireFMX -U"c:\program files (x86)\embarcadero\studio\21.0\lib\Win32\debug";C:\Components\HtmlComponents\source;C:\Components\woll2woll\firepower\13.0\lib\21.0\win32;"c:\program files (x86)\embarcadero\studio\21.0\lib\Win32\release";C:\Users\david\Documents\Embarcadero\Studio\21.0\Imports;"c:\program files (x86)\embarcadero\studio\21.0\Imports";C:\Users\Public\Documents\Embarcadero\Studio\21.0\Dcp;"c:\program files (x86)\embarcadero\studio\21.0\include";C:\Components\HtmlComponents\source;C:\Components\HtmlComponents\source\editor;C:\Components\HtmlComponents\source\localize;C:\Components\PentireFMX -V -VN -NBC:\Users\Public\Documents\Embarcadero\Studio\21.0\Dcp -NHC:\Users\Public\Documents\Embarcadero\Studio\21.0\hpp\Win32 -NO.\Win32\Debug Temperaments.dpr

It is actually all on one line:

"c:\program files (x86)\embarcadero\studio\21.0\bin\dcc32.exe" -$O- -$W+ --no-con...

You can copy this command from Delphi’s Messages Window after you perform a compile in the Delphi IDE. Apart from needing to quote the dcc32.exe command, everything else is pretty much the same. I did trim some third party libraries I don’t need, and removed carriage returns and extra whitespace. Before continuing, test that it works from a normal Command Prompt:

The Tasks File

Next configure the tasks.json file, set the command and args properties:

{    
    "version": "2.0.0",    
    "tasks": [
       {
          "label": "Pascal",
          "type": "shell",
          "windows": {
             "command": "cmd.exe"
          },
          "presentation": {
             "reveal": "always",
             "panel": "new"
          },
          "args": [
             "/c",
             "C:\\source\\Temperaments\\buid.bat"
          ],
          "problemMatcher": {
             "owner": "external",
             "pattern": {
                "regexp": "^(.*.(pas|dpr|dpk))\\((\\d+)\\)\\s(Fatal|Error|Warning|Hint):(.*)",
                "file": 1,
                "location": 3,
                "message": 5
             }
          },
          "group": {
             "kind": "build",
             "isDefault": true
          }
       }
    ]    
}

Now when you press CTRL+F9 your project should build for you:

I hope this tip helps.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s